本帖最后由 meng 于 2013-8-30 06:48 编辑
这个项目中有三类服务窗口,普通窗口,快速窗口和VIP窗口,我存在的疑问是为什么VIP窗口如果空闲了要去为普通窗口服务,而不是先为快速窗口服务,然后再为普通窗口服务.部分代码如下(疑问在红色代码部分).
package cn.itcast.bankqueue;
import java.util.Random; import java.util.concurrent.Executors; import java.util.logging.Logger;
/** * 没有把VIP窗口和快速窗口做成子类,是因为实际业务中的普通窗口可以随时被设置为VIP窗口和快速窗口。 * */ public class ServiceWindow { privatestatic Logger logger = Logger.getLogger("cn.itcast.bankqueue"); privateCustomerType type = CustomerType.COMMON; privateint number = 1;
publicCustomerType getType() { returntype; }
publicvoid setType(CustomerType type) { this.type= type; }
publicvoid setNumber(int number){ this.number= number; }
publicvoid start(){ Executors.newSingleThreadExecutor().execute( newRunnable(){ publicvoid run(){ //下面这种写法的运行效率低,最好是把while放在case下面 while(true){ switch(type){ caseCOMMON: commonService(); break; caseEXPRESS: expressService(); break; caseVIP: vipService(); break; } } } } ); }
privatevoid commonService(){ StringwindowName = "第" + number + "号" + type + "窗口"; System.out.println(windowName+ "开始获取普通任务!"); IntegerserviceNumber = NumberMachine.getInstance().getCommonManager().fetchNumber(); if(serviceNumber!= null ){ System.out.println(windowName+ "开始为第" +serviceNumber + "号普通客户服务"); intmaxRandom = Constants.MAX_SERVICE_TIME - Constants.MIN_SERVICE_TIME; intserviceTime = new Random().nextInt(maxRandom)+1 + Constants.MIN_SERVICE_TIME;
try{ Thread.sleep(serviceTime); }catch (InterruptedException e) { e.printStackTrace(); } System.out.println(windowName+ "完成为第" +serviceNumber + "号普通客户服务,总共耗时" + serviceTime/1000 + "秒"); }else{ System.out.println(windowName+ "没有取到普通任务,正在空闲一秒"); try{ Thread.sleep(1000); }catch (InterruptedException e) { e.printStackTrace(); } } }
privatevoid expressService(){ IntegerserviceNumber = NumberMachine.getInstance().getExpressManager().fetchNumber(); StringwindowName = "第" + number + "号" + type + "窗口"; System.out.println(windowName+ "开始获取快速任务!"); if(serviceNumber!=null){ System.out.println(windowName+ "开始为第" +serviceNumber + "号快速客户服务"); intserviceTime = Constants.MIN_SERVICE_TIME; try{ Thread.sleep(serviceTime); }catch (InterruptedException e) { e.printStackTrace(); } System.out.println(windowName+ "完成为第" +serviceNumber + "号快速客户服务,总共耗时" + serviceTime/1000 + "秒"); }else{ System.out.println(windowName+ "没有取到快速任务!"); commonService(); } }
privatevoid vipService(){
IntegerserviceNumber = NumberMachine.getInstance().getVipManager().fetchNumber(); StringwindowName = "第" + number + "号" + type + "窗口"; System.out.println(windowName + "开始获取VIP任务!"); if(serviceNumber!=null){ System.out.println(windowName+ "开始为第" +serviceNumber + "号VIP客户服务"); intmaxRandom = Constants.MAX_SERVICE_TIME - Constants.MIN_SERVICE_TIME; intserviceTime = new Random().nextInt(maxRandom)+1 + Constants.MIN_SERVICE_TIME; try{ Thread.sleep(serviceTime); }catch (InterruptedException e) { e.printStackTrace(); } System.out.println(windowName+ "完成为第" +serviceNumber + "号VIP客户服务,总共耗时"+ serviceTime/1000 + "秒"); }else{ System.out.println(windowName+ "没有取到VIP任务!"); commonService();//此处是不是应该修改成expressService更好呢? } } }
|