这个是客户拿号的类也就是运行类
package heima7kbankqueue;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
//运行的类
public class MainClass {
//
private static Logger logger = Logger.getLogger("cn.itcast.bankqueue");
public static void main(String[] args) {
//产生4个普通窗口
for(int i=1;i<5;i++){
ServiceWindow window = new ServiceWindow();
window.setNumber(i);
window.start();
}
//产生1个快速窗口
ServiceWindow expressWindow = new ServiceWindow();
expressWindow.setType(CustomerType.EXPRESS);
expressWindow.start();
//产生1个VIP窗口
ServiceWindow vipWindow = new ServiceWindow();
vipWindow.setType(CustomerType.VIP);
vipWindow.start();
//普通客户拿号 创建线程
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
new Runnable(){
public void run(){
//拿到的号码
Integer serviceNumber = NumberMachine.getInstance().getCommonManager().generateNewNumber();
/**
* 采用logger方式,无法看到直观的运行效果,因为logger.log方法内部并不是直接把内容打印出出来,
* 而是交给内部的一个线程去处理,所以,打印出来的结果在时间顺序上看起来很混乱。
*/
//logger.info("第" + serviceNumber + "号普通客户正在等待服务!");
System.out.println("第" + serviceNumber +5555555+ "号普通客户正在等待服务!");
}
},
0,
Constants.COMMON_CUSTOMER_INTERVAL_TIME,
TimeUnit.SECONDS);
//间隔多长时间普通客户进门
//快速客户拿号
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
new Runnable(){
public void run(){
Integer serviceNumber = NumberMachine.getInstance().getExpressManager().generateNewNumber();
System.out.println("第" + serviceNumber +1111111+ "号快速客户正在等待服务!");
}
},
0,
//间隔多长时间快速客户出现
Constants.COMMON_CUSTOMER_INTERVAL_TIME * 2,
TimeUnit.SECONDS);
//VIP客户拿号
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
new Runnable(){
public void run(){
Integer serviceNumber = NumberMachine.getInstance().getVipManager().generateNewNumber();
System.out.println("第。。。。。。。。。。" + serviceNumber +100000+ "。。。。。。。号VIP客户正在等待服务!");
}
},
0,
//间隔多长时间vip客户进门
Constants.COMMON_CUSTOMER_INTERVAL_TIME * 6,
TimeUnit.SECONDS);
}
}
这个是窗口拿号的类、
package heima7kbankqueue;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.logging.Logger;
/**
* 没有把VIP窗口和快速窗口做成子类,是因为实际业务中的普通窗口可以随时被设置为VIP窗口和快速窗口。
* */
//服务窗口
public class ServiceWindow {
private static Logger logger = Logger.getLogger("cn.itcast.bankqueue");
//窗口的类型
private CustomerType type = CustomerType.COMMON;
//第几号窗口
private int number = 1;
//获取窗口
public CustomerType getType() {
return type;
}
//设置顾客类型
public void setType(CustomerType type) {
this.type = type;
}
//设置第几号窗口
public void setNumber(int number){
this.number = number;
}
//开始叫号
public void start(){
//执行器的方法执行一个线程
Executors.newSingleThreadExecutor().execute(
new Runnable(){
public void run(){
//无限叫号服务
while(true){
switch(type){
//这里可以省略前面的CustomerType
case COMMON:
commonService();
break;
case EXPRESS:
expressService();
break;
case VIP:
vipService();
break;
}
}
}
}
);
}
//普通客户服务窗口
private void commonService(){
//普通窗口获取任务
String windowName = "第" + number + "号" + type + "窗口";
System.out.println(windowName + "开始获取普通任务!");
//开始取号
Integer serviceNumber = NumberMachine.getInstance().getCommonManager().fetchNumber();
if(serviceNumber != null ){
//不为null开始服务
System.out.println(windowName + "开始为第" + serviceNumber + "号普通客户服务");
//服务时间为
int maxRandom = Constants.MAX_SERVICE_TIME - Constants.MIN_SERVICE_TIME;
//服务时间为
int serviceTime = 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();
}
}
}
//快速服务窗口
private void expressService(){
//窗口票号
Integer serviceNumber = NumberMachine.getInstance().getExpressManager().fetchNumber();
String windowName = "第" + number + "号" + type + "窗口";
//窗口叫号
System.out.println(windowName + "开始获取快速任务!");
//不为null 开始服务
if(serviceNumber !=null){
System.out.println(windowName + "开始为第" + serviceNumber + "号快速客户服务");
int serviceTime = 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();
}
}
//VIP窗口
private void vipService(){
//vip取号
Integer serviceNumber = NumberMachine.getInstance().getVipManager().fetchNumber();
String windowName = "第" + number + "号" + type + "窗口";
System.out.println(windowName + "开始获取VIP任务!");
if(serviceNumber !=null){
System.out.println(windowName + "开始为第" + serviceNumber + "号VIP客户服务");
//服务时间
int maxRandom = Constants.MAX_SERVICE_TIME - Constants.MIN_SERVICE_TIME;
int serviceTime = 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();
}
}
}
窗口的类与客户手中的号码是怎么联系到一起的呢? |
|