前言:看完视频感觉这个比交通灯简单多了 主要是我东南西北部分
首先银行业务调度系统 就是模拟一下 银行办理业务 取排队的票 然后到对应窗口办理业务。所以 应该有 服务窗口 取票机器 顾客
顾客:用枚举解决 分普通 快速 VIP
取票机器:根据窗口性质的不同读取 不同的窗口对象 对象中有记录号码的函数
服务窗口:根据要求服务窗口 分 普通 快速 VIP 比例5:1:1
顾客:public enum customer {
common, speed, vip;//定义 普通 快速 vip
public String toString() {//重写toString方法
String name = null;
switch (this) {//判断枚举?(不太专业不知道用什么词 囧)返回对应中文
case common:
name = "普通";
break;
case speed:
name = "快速";
break;
case vip:
name = "vip";
break;
}
return name;
}
}
取票机器:
public class numberrobot {//将取票机设成单例
private numberrobot(){}//私有化构造函数
private static numberrobot nr=new numberrobot();//创建静态本类对象
public static numberrobot returnnumberrobot()//返回本类对象
{return nr;}
private robotmain common=new robotmain();//
private robotmain speed=new robotmain();
private robotmain vip=new robotmain();
public robotmain returncommon()
{return common;}
public robotmain returnspeed()
{return speed;}
public robotmain returnvip()
{return vip;}
}
服务窗口 :public class window {
public class robotmain {
private int count=1;//计数
private List<Integer> l=new ArrayList<Integer>();//用来存放产生的服务号
public synchronized int generateNewNumber(){ //用来产生服务号 即客户拿了好
l.add(++count);
return count;
}
public synchronized Integer fetchNumber(){//去号 即客户办理完了
if(l.size()>0){
return (Integer)l.remove(0);
}else{
return (Integer) null;
}
}
}
服务窗口 :public class window {
private int num=1;//窗口号
private int maxtime=10;//最大办理业务时间
private int mintime=1;//最小办理业务时间
private customer c=customer.common;//获取枚举对象
public void setNum(int num) {//重设窗口号
this.num = num;
}
public customer getC() {//获取枚举
return c;
}
public void setC(customer c) {//重设枚举
this.c = c;
}
public void on()
{Executors.newSingleThreadExecutor().execute(new Runnable(){
@Override
public void run() {//建立一个线程 判断窗口类型
// TODO Auto-generated method stub
while(true){
switch(c){//根据枚举的不同调用不同的窗口
case common:
commonService();
break;
case speed:
speedService();
break;
case vip:
vipService();
break;
}
}
}
});
}
private void commonService()
{String window="第"+num+"号"+c+"窗口";
System.out.println(window+"开始获取任务......");
Integer servicenum=numberrobot.returnnumberrobot().returncommon().fetchNumber();//获取取票机中的服务号
if(servicenum!=null){System.out.println("开始为第"+num+"号普通客户服务");//为客户服务
int serviceTime = new Random().nextInt(maxtime-mintime)+1 ;//随机产生服务时间
try {
Thread.sleep(serviceTime*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(window + "完成为第" + servicenum + "号普通客户服务,总共耗时" + serviceTime + "秒");
}else{
System.out.println(window + "没有取到普通任务,正在空闲一秒");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void speedService()
{String window="第"+num+"号"+c+"窗口";
System.out.println(window+"开始获取任务......");
Integer servicenum=numberrobot.returnnumberrobot().returnspeed().fetchNumber();
if(servicenum!=null){System.out.println("开始为第"+num+"号快速客户服务");
int serviceTime = new Random().nextInt(maxtime-mintime)+1 ;
try {
Thread.sleep(serviceTime*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(window + "完成为第" + servicenum + "号快速客户服务,总共耗时" + serviceTime + "秒");
}else{
System.out.println(window + "没有取到快速任务");
commonService();
}}
private void vipService()
{String window="第"+num+"号"+c+"窗口";
System.out.println(window+"开始获取任务......");
Integer servicenum=numberrobot.returnnumberrobot().returnvip().fetchNumber();
if(servicenum!=null){System.out.println("开始为第"+num+"号vip客户服务");
int serviceTime = new Random().nextInt(maxtime-mintime)+1 ;
try {
Thread.sleep(serviceTime*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(window + "完成为第" + servicenum + "号vip客户服务,总共耗时" + serviceTime + "秒");
}else{
System.out.println(window + "没有取到vip任务");
commonService();
}}
}
主函数:public class main {
主函数:public class main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=1;i<=5;i++)//建立普通窗口
{window w=new window();
w.setNum(i);
w.on();}
window wspeed=new window();//建立快速窗口
wspeed.setC(customer.speed);
wspeed.on();
window wvip=new window();//建立VIP窗口
wvip.setC(customer.vip);
wvip.on();
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable(){//模拟普通客户取号
@Override
public void run() {
// TODO Auto-generated method stub
int num=numberrobot.returnnumberrobot().returncommon().generateNewNumber();
System.out.println("第"+num+"普通客户正在等待服务");
}
}
, 0, 1, TimeUnit.SECONDS);
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable(){//模拟快速客户取号
@Override
public void run() {
// TODO Auto-generated method stub
int num=numberrobot.returnnumberrobot().returnspeed().generateNewNumber();
System.out.println("第"+num+"快速客户正在等待服务");
}
}
, 0, 2, TimeUnit.SECONDS);
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable(){//模拟VIP客户取号
@Override
public void run() {
// TODO Auto-generated method stub
int num=numberrobot.returnnumberrobot().returnvip().generateNewNumber();
System.out.println("第"+num+"vip客户正在等待服务");
}
}
, 0, 6, TimeUnit.SECONDS);
}
}
学习感悟:窗口只要写好一个 后面2个明显轻松很多 针对executors类 有必要去研究一下 嘿嘿 技术博客又可以多一条了。