银行业务调度系统
需求分析
1、银行有6个窗口,1-4个窗口为普通窗口,5号窗口为快速窗口。6号窗口为VIP窗口
2、有三种对应类型的客户:vip客户 ,普通客户,快速客户
3、异步随机生成各种类型的客户,生成各类型用户的概率比例为 vip:普通客户:快速客户:=1:6:3
客户办理业务所需时间有最大值和最小值,该范围内随机设定每个VIP客户以及普通客户办理业务所需时间,快速客户办理业务所需时间为最小值
4、各类型客户在其对应窗口按顺序依次办理业务
5、各类型客户在其对应窗口按顺序依次办理业务。
6、当VIP窗口和快速业务窗口没有等待业务,这两个窗口可以处理普通客户业务,而一旦有vip客户优先办理vip。
通过对以上的需求分析的解剖
我们做一个号码发生器
public class NumberManager {
private int lastNumber=1;
private List<Integer> queueNumber=new ArrayList<Integer>();
public synchronized int generateNewManager(){
queueNumber.add(lastNumber);
return lastNumber++;
}
public synchronized int fetchServiceNumber(){
return queueNumber.remove(0);
}
还要一个取号机器
public class NumberMachine {
private NumberManager commonManager=new NumberManager();
private NumberManager vipManager=new NumberManager();
private NumberManager expressManager=new NumberManager();
public NumberManager getCommonManager(){
return commonManager;
}
public NumberManager getVipManager(){
return vipManager;
}
public NumberManager getExpressManager(){
return expressManager;
}
public static NumberMachine getInstance(){
return instance;
}
private static NumberMachine instance=new NumberMachine();
}
张孝祥老师说 新技术就像 一种新药,能治疗不能治疗的疾病,更新自己的技术,努力的学习。在技术的最前端。
定义窗口并实现取号的设计。。
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:
commonService();
break;
case EXPRESS:
break;
case VIP:
break;
}
NumberMachine.getInstance().getCommonManager();
}
}
});
}
private void commonService(){
String windowName="第"+windowId+"号"+type+"窗口";
System.out.println("正在获取任务");
Integer number=NumberMachine.getInstance().getCommonManager().fetchServiceNumber();
if(number!=null){
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;
long costTime=System.currentTimeMillis()-beginTime;
try {
Thread.sleep(serveTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(windowName+"为第"+number+"个"+type+"客户完成,耗时"+costTime/1000+"秒");
}else{
System.out.println(windowName+"没有取到任务休息一秒钟");
try{
Thread.sleep(1000);
}catch(InterruptedException e){e.printStackTrace();}
}
}
private void expressService(){
String windowName="第"+windowId+"号"+type+"窗口";
System.out.println("正在获取任务");
Integer number=NumberMachine.getInstance().getExpressManager().fetchServiceNumber();
if(number!=null){
long beginTime=System.currentTimeMillis();
long costTime=System.currentTimeMillis()-beginTime;
try {
Thread.sleep(Constants.MIN_SERVICE_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(windowName+"为第"+number+"个"+type+"客户完成,耗时"+costTime/1000+"秒");
}else{
System.out.println(windowName+"没有取到任务休息一秒钟");
commonService();
}
}
private void vipService(){
String windowName="第"+windowId+"号"+type+"窗口";
System.out.println("正在获取任务");
Integer number=NumberMachine.getInstance().getVipManager().fetchServiceNumber();
if(number!=null){
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;
long costTime=System.currentTimeMillis()-beginTime;
try {
Thread.sleep(serveTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(windowName+"为第"+number+"个"+type+"客户完成,耗时"+costTime/1000+"秒");
}else{
System.out.println(windowName+"没有取到任务");
commonService();
}
}
}
通过主类来创建服务窗口并进行模拟取号服务等相关操作。
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).scheduleWithFixedDelay(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
);
Executors.newScheduledThreadPool(1).scheduleWithFixedDelay(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).scheduleWithFixedDelay(new Runnable(){
public void run(){
Integer number=NumberMachine.getInstance().getCommonManager().generateNewManager();
System.out.println(number+"号普通客户等待服务。");
}
},
0,
|
|