//=====================================================================
//@TITLE:
// 银行调度系统
//@AUTHOR:
// Benwolf
//@DATE:
// Tuesday 26-April-2013
//=====================================================================
银行业务调度系统
熟悉银行的业务。
银行有一个取号机,vip窗口,普通窗口,快速窗口。取号机取出的号码相当于客户,窗口需要叫号。可以把这个逻辑封装成一个对象NumberManager。
创建号管理类NumberManager。
package cn.itheima.interview.bank;
import java.util.ArrayList;
import java.util.List;
public class NumberManager {
private int lastNumber = 1;
// 在创建对象的时候尽量使用父类或者接口指向子类对象,不仅灵活而且看起来比较专业。
private List<Integer> queue = new ArrayList<Integer>();
// 下面这两个方法,在程序运行的时候,是由两个不同的线程调用。它们操作了同一个变量queue.
// 所以用到synchronized关键字进行同步。
public synchronized int generateNumber() {
// 调用一次生成一个号码,把它加入队列中。然后再返回该值,让外界知道新客户来了。
// lastNumber自增,如果再有下个用户来了,直接加入队列中,继续上述操作。
queue.add(lastNumber);
return lastNumber++;
}
public synchronized int fetchServiceNumber() {
// 记住这种设计思想,从一个容器中取走(得到该值,并且容器删除该值)一个值。
Integer number = null;
if (queue.size() > 0) {
number = queue.remove(0);
}
return number;
}
}
创建取号码的机器。
由于一个银行只有一个取号机器,采用单例设计模式。
package cn.itheima.interview.bank;
public class NumberMachine {
private NumberManager commonManager;
private NumberManager expressManager;
private NumberManager vipManager;
private NumberMachine() {
}
//单例设计模式。
private static NumberMachine instance = new NumberMachine();
public static NumberMachine getInstance() {
return instance;
}
public NumberManager getCommonManager() {
return commonManager;
}
public NumberManager getExpressManager() {
return expressManager;
}
public NumberManager getVipManager() {
return vipManager;
}
}
4.创建服务窗口。
package cn.itheima.interview.bank;
import java.util.Random;
import java.util.concurrent.Executors;
public class ServiceWindow {
private CustomerType type = CustomerType.COMMON;
private int number = 1;
public void setType(CustomerType type) {
this.type = type;
}
public void setWindowId(int number) {
this.number = number;
}
public void start() {
Executors.newSingleThreadExecutor().execute(new Runnable() {
@Override
public void run() {
while (true) {
switch (type) {
case COMMON:
commonService();
break;
case EXPRESS:
expressService();
break;
case VIP:
vipService();
break;
}
}
}
private void vipService() {
String windowName = "第" + number + "号" + type + "窗口";
System.out.println(windowName + "开始准备获取任务!");
Integer serviceNum = NumberMachine.getInstance()
.getCommonManager().fetchServiceNumber();
if (serviceNum != null) {
System.out.println(windowName + "开始为第" + serviceNum
+ "号vip客户服务");
long startTime = System.currentTimeMillis();
int randomTime = Constants.MAX_SERVICE_TIME
- Constants.MIN_SERVICE_TIME;
long serviceTime = new Random().nextInt(randomTime) + 1
+ Constants.MIN_SERVICE_TIME;
try {
Thread.sleep(serviceTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
long costTime = System.currentTimeMillis() - startTime;
System.out.println(windowName + "窗口完成第" + number
+ "号快速vip的服务,共耗时" + costTime / 1000 + "秒");
} else {
System.out.println(windowName + "没有取得vip任务");
commonService();
}
}
private void expressService() {
String windowName = "第" + number + "号" + type + "窗口";
System.out.println(windowName + "开始准备获取任务!");
Integer serviceNum = NumberMachine.getInstance()
.getCommonManager().fetchServiceNumber();
if (serviceNum != null) {
System.out.println(windowName + "开始为第" + serviceNum
+ "号快速客户服务");
long startTime = System.currentTimeMillis();
int randomTime = Constants.MAX_SERVICE_TIME
- Constants.MIN_SERVICE_TIME;
long serviceTime = new Random().nextInt(randomTime) + 1
+ Constants.MIN_SERVICE_TIME;
try {
Thread.sleep(serviceTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
long costTime = System.currentTimeMillis() - startTime;
System.out.println(windowName + "窗口完成第" + number
+ "号快速用户的服务,共耗时" + costTime / 1000 + "秒");
} else {
System.out.println(windowName + "没有取得快速任务");
commonService();
}
}
private void commonService() {
String windowName = "第" + number + "号" + type + "窗口";
System.out.println(windowName + "开始准备获取任务!");
Integer serviceNum = NumberMachine.getInstance()
.getCommonManager().fetchServiceNumber();
if (serviceNum != null) {
System.out.println(windowName + "开始为第" + serviceNum
+ "号普通客户服务");
long startTime = System.currentTimeMillis();
int randomTime = Constants.MAX_SERVICE_TIME
- Constants.MIN_SERVICE_TIME;
long serviceTime = new Random().nextInt(randomTime) + 1
+ Constants.MIN_SERVICE_TIME;
try {
Thread.sleep(serviceTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
long costTime = System.currentTimeMillis() - startTime;
System.out.println(windowName + "窗口完成第" + number
+ "号普通用户的服务,共耗时" + costTime / 1000 + "秒");
} else {
System.out.println(windowName + "没有取得普通任务,空闲1秒");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
}
}
创建测试类包含用户的取号。
package cn.itheima.interview.bank;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class MainClass {
public static void main(String[] args) {
// 产生4个普通用户
for (int i = 1; i < 5; i++) {
ServiceWindow commonWindow = new ServiceWindow();
commonWindow.setType(CustomerType.COMMON);
commonWindow.setWindowId(i);
commonWindow.start();
}
// 产生1个快速用户
ServiceWindow expressWindow = new ServiceWindow();
expressWindow.setType(CustomerType.EXPRESS);
expressWindow.setWindowId(1);
expressWindow.start();
// 产生1个vip用户
ServiceWindow vipWindow = new ServiceWindow();
vipWindow.setType(CustomerType.EXPRESS);
vipWindow.setWindowId(1);
vipWindow.start();
// 普通客户拿号
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
public void run() {
Integer serviceNumber = NumberMachine.getInstance()
.getCommonManager().generateNumber();
/**
* 采用logger方式,无法看到直观的运行效果,因为logger.log方法内部并不是直接把内容打印出出来,
* 而是交给内部的一个线程去处理,所以,打印出来的结果在时间顺序上看起来很混乱。
*/
// logger.info("第" + serviceNumber + "号普通客户正在等待服务!");
System.out.println("第" + serviceNumber + "号普通客户正在等待服务!");
}
}, 0, Constants.COMMON_CUSTOMER_INTERVAL_TIME, TimeUnit.SECONDS);
// 快速客户拿号
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
public void run() {
Integer serviceNumber = NumberMachine.getInstance()
.getExpressManager().generateNumber();
System.out.println("第" + serviceNumber + "号快速客户正在等待服务!");
}
}, 0, Constants.COMMON_CUSTOMER_INTERVAL_TIME * 2, TimeUnit.SECONDS);
// VIP客户拿号
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
public void run() {
Integer serviceNumber = NumberMachine.getInstance()
.getVipManager().generateNumber();
System.out.println("第" + serviceNumber + "号VIP客户正在等待服务!");
}
}, 0, Constants.COMMON_CUSTOMER_INTERVAL_TIME * 6, TimeUnit.SECONDS);
}
}