public class ServiceWindow {
private CustomerType type=CustomerType.COMMON;
private int windowId=1;
public void setType(CustomerType type) {
this.type = type;
}
public void setWindowId(int windowId) {
this.windowId = windowId;
}
public void start(){
Executors.newSingleThreadExecutor().execute(
new Runnable(){
public void run(){
while(true){
switch(type){
case COMMON:
commonServices();
break;
case EXPRESS:
expressServices();
break;
case VIP:
vipServices();
break;
}
}
}
});
}
private void commonServices() {
String windowName="第"+windowId+"號"+type+"窗口";
Integer number= NumberMachine.getInstance().getCommonManager().fetchServiceNumber();
System.out.println(windowName+"正在獲取任務");
if(number !=null){
System.out.println(windowName+"爲第"+number+"個普通客戶服務");
long beginTime=System.currentTimeMillis();
int maxRand=Constants.MAX_SERVICE_TIME-Constants.MIN_SERVICE_TIME;
long serveTime=new Random().nextInt(maxRand)+1+Constants.MIN_SERVICE_TIME;
try {
Thread.sleep(serveTime);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long costTime=System.currentTimeMillis()-beginTime;
System.out.println(windowName+"爲第"+number+"個"+"普通"+"客戶完成服務,耗時"+costTime/1000+"秒");
}else{
System.out.println(windowName+"沒有取到任務,先休息1秒鐘");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void expressServices() {
String windowName="第"+windowId+"號"+type+"窗口";
Integer number= NumberMachine.getInstance().getExpressManager().fetchServiceNumber();
System.out.println(windowName+"正在獲取任務");
if(number !=null){
System.out.println(windowName+"爲第"+number+"個"+type+"客戶服務");
long beginTime=System.currentTimeMillis();
//int maxRand=Constants.MAX_SERVICE_TIME-Constants.MIN_SERVICE_TIME;
//long serveTime=new Random().nextInt(maxRand)+1+Constants.MIN_SERVICE_TIME;
try {
Thread.sleep(Constants.MIN_SERVICE_TIME);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long costTime=System.currentTimeMillis()-beginTime;
System.out.println(windowName+"爲第"+number+"個"+type+"客戶完成服務,耗時"+costTime/1000+"秒");
}else{
System.out.println(windowName+"沒有取到任務!");
commonServices();
}
}
private void vipServices() {
String windowName="第"+windowId+"號"+type+"窗口";
Integer number= NumberMachine.getInstance().getVipManager().fetchServiceNumber();
System.out.println(windowName+"正在獲取任務");
if(number !=null){
System.out.println(windowName+"爲第"+number+"個"+type+"客戶服務");
long beginTime=System.currentTimeMillis();
//int maxRand=Constants.MAX_SERVICE_TIME-Constants.MIN_SERVICE_TIME;
//long serveTime=new Random().nextInt(maxRand)+1+Constants.MIN_SERVICE_TIME;
try {
Thread.sleep(Constants.MIN_SERVICE_TIME);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long costTime=System.currentTimeMillis()-beginTime;
System.out.println(windowName+"爲第"+number+"個"+type+"客戶完成服務,耗時"+costTime/1000+"秒");
}else{
System.out.println(windowName+"沒有取到任務!");
commonServices();
}
}
}
lConstants類
Ø定義三個常量:MAX_SERVICE_TIME、MIN_SERVICE_TIME、COMMON_CUSTOMER_INTERVAL_TIME
l
public class Constants {
public static int MAX_SERVICE_TIME=10000;
public static int MIN_SERVICE_TIME=1000;
public static int COMMON_CUSTOMER_INTERVAL_TIME=1;
}
lMainClass類
Ø用for循環創建出4個普通窗口,再創建出1個快速窗口和一個VIP窗口。
Ø接着再創建三個定時器,分別定時去創建新的普通客戶號碼、新的快速客戶號碼、新的VIP客戶號碼。
l
public class MainClass {
public static void main(String[] args) {
for(int i=1;i<5;i++){
ServiceWindow commonWindow = new ServiceWindow();
commonWindow.setWindowId(i);
commonWindow.start();
}
ServiceWindow expressWindow= new ServiceWindow();
expressWindow.setType(CustomerType.EXPRESS);
expressWindow.start();
ServiceWindow vipWindow = new ServiceWindow();
vipWindow.setType(CustomerType.VIP);
vipWindow.start();
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
new Runnable(){
public void run(){
Integer number=NumberMachine.getInstance().getCommonManager().generateNewManager();
System.out.println(number+"號普通客戶等待服務!");
}
},
0,
Constants.COMMON_CUSTOMER_INTERVAL_TIME,
TimeUnit.SECONDS);
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
new Runnable(){
public void run(){
Integer number=NumberMachine.getInstance().getExpressManager().generateNewManager();
System.out.println(number+"號快速客戶等待服務!");
}
},
0,
Constants.COMMON_CUSTOMER_INTERVAL_TIME*3,
TimeUnit.SECONDS);
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
new Runnable(){
public void run(){
Integer number=NumberMachine.getInstance().getVipManager().generateNewManager();
System.out.println(number+"號VIP客戶等待服務!");
}
},
0,
Constants.COMMON_CUSTOMER_INTERVAL_TIME*6,
TimeUnit.SECONDS);
}
}
一個模塊一個模塊的看,基本都能理解,總體邏輯關係和線程的啓動、鎖定還是不是很清晰,以後多看那幾篇,理解學習下設計思想,編程思路