如果发现BUG了就说下哈 呵呵- package cn.itcast.bank;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- import java.util.concurrent.Executors;
- import java.util.concurrent.TimeUnit;
- public class MainClass {
- public static void main(String[] args) {
- int windowNumber;// 窗口编号
- /* 创建4个普通窗口 */
- for (windowNumber = 1; windowNumber <= 4; windowNumber++) {
- new ServiceWindow(CustomerType.GENERAL, windowNumber);
- }
- new ServiceWindow(CustomerType.FAST, windowNumber++);// 创建1个快速窗口
- new ServiceWindow(CustomerType.VIP, windowNumber++);// 创建1个VIP窗口
- NumberMachine.getInstance().open();// 打开号码管理器的开关
- }
- }
- /* 服务窗口类 */
- class ServiceWindow {
- private static NumberMachine numberMachine = NumberMachine.getInstance();// 所有服务窗口共享一个号码管理器
- private CustomerType type;// 窗口类型
- private int number;// 窗口编号
- /**
- * @param type
- * 窗口类型
- * @param number
- * 窗口编号
- */
- public ServiceWindow(CustomerType type, int number) {
- this.number = number;
- this.type = type;
- Executors.newScheduledThreadPool(1).schedule(new Runnable() {
- @Override
- public void run() {
- Random random = new Random();
- do {
- /* 向号码管理器索取下一个顾客,如果没有索取到则返回null */
- Customer customer = numberMachine.getCustomer(ServiceWindow.this);
- if (null == customer) {
- System.out.println(ServiceWindow.this.getNumber()
- + "号窗口空闭中");
- try {
- Thread.sleep(2000);// 窗口休息2秒再进行下一次索取
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- } else {
- System.out.println(customer.toString() + "到"
- + ServiceWindow.this.getNumber() + "号窗口办理业务");
- try {
- Thread.sleep((random.nextInt(19000) + 1000));// 如果索取到了顾客,随机生成为该顾客服务的时间
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println(ServiceWindow.this.getNumber()
- + "号窗口为" + customer.toString() + "办理完毕");
- }
- } while (true);
- }
- }, 0, TimeUnit.SECONDS);
- }
- public int getNumber() {
- return number;// 返回窗口的的编号
- }
- public CustomerType getType() {
- return type;// 返回窗口的类型
- }
- }
- /* 号码管理器类 */
- class NumberMachine {
- private int customers = 0;// 客户编号,每来一个顾客,这个编号的值就会增加1
- private List<Customer> generalList = new ArrayList<Customer>();// 普通用户队列
- private List<Customer> fastList = new ArrayList<Customer>();// 快速用户队列
- private List<Customer> VIPList = new ArrayList<Customer>();// VIP用户队列
- private static NumberMachine numberMachine = new NumberMachine();// 单例
- private boolean onOff = false;// 号码管理器的开关初始状态为关
- private NumberMachine() {
- }
- public static NumberMachine getInstance() {
- return numberMachine;
- }
- public void open() {// 打开开关,让号码管理器开始工作
- onOff = true;
- Executors.newScheduledThreadPool(1).schedule(new Runnable() {
- @Override
- public void run() {
- Random random = new Random();
- while (onOff) {
- try {
- Thread.sleep((random.nextInt(3) + 1) * 1000);// 随机生成顾客到来的时间间隔
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- switch (random.nextInt(10)) {// VIP客户:快速客户:普通客户=1:3:6
- case 0:
- /* VIP客户加入到VIP用户队列 */
- VIPList.add(new Customer(CustomerType.VIP, ++customers));
- System.out.println(VIPList.get(VIPList.size() - 1)
- .toString() + "进入VIP用户队列");
- break;
- case 1:
- case 2:
- case 3:
- /* 快速用户加入到快速用户队列 */
- fastList.add(new Customer(CustomerType.FAST,
- ++customers));
- System.out.println(fastList.get(fastList.size() - 1)
- .toString() + "进入快速用户队列");
- break;
- default:
- /* 普通用户加入到普通用户队列 */
- generalList.add(new Customer(CustomerType.GENERAL,
- ++customers));
- System.out.println(generalList.get(
- generalList.size() - 1).toString()
- + "进入普通用户队列");
- }
- }
- }
- }, 0, TimeUnit.SECONDS);
- }
- /* 多个线程都在向号码管理器索取客户,所以要同步,如果不同步可能会出现两个窗口取到同一个客户 */
- public synchronized Customer getCustomer(ServiceWindow serviceWindow) {
- Customer customer = null;// 取到的客户,初始值为null
- switch (serviceWindow.getType()) {
- case GENERAL:// 普通窗口在索取
- if (0 < generalList.size()) {
- customer = generalList.get(0);
- generalList.remove(0);
- } else if (0 < VIPList.size()) {
- customer = VIPList.get(0);
- VIPList.remove(0);
- } else if (0 < fastList.size()) {
- customer = fastList.get(0);
- fastList.remove(0);
- }
- break;
- case FAST:// 快速窗口在索取
- if (0 < fastList.size()) {
- customer = fastList.get(0);
- fastList.remove(0);
- } else if (0 < VIPList.size()) {
- customer = VIPList.get(0);
- VIPList.remove(0);
- } else if (0 < generalList.size()) {
- customer = generalList.get(0);
- generalList.remove(0);
- }
- break;
- case VIP:// VIP窗口在索取
- if (0 < VIPList.size()) {
- customer = VIPList.get(0);
- VIPList.remove(0);
- } else if (0 < generalList.size()) {
- customer = generalList.get(0);
- generalList.remove(0);
- } else if (0 < fastList.size()) {
- customer = fastList.get(0);
- fastList.remove(0);
- }
- }
- return customer;// 返回取到的客户
- }
- public void close() {// 关闭号码管理器
- onOff = false;
- }
- }
- class Customer {
- private CustomerType type;// 客户类型
- private int number;// 客户编号
- public Customer(CustomerType type, int number) {
- this.type = type;
- this.number = number;
- }
- public String toString() {
- switch (type) {
- case GENERAL:
- return "第" + number + "号普通用户";
- case FAST:
- return "第" + number + "号快速用户";
- case VIP:
- return "第" + number + "号VIP用户";
- }
- return null;
- }
- public CustomerType getType() {
- return type;
- }
- public void setType(CustomerType type) {
- this.type = type;
- }
- public int getNumber() {
- return number;
- }
- public void setNumber(int number) {
- this.number = number;
- }
- }
- /* 3种不同的服务类型 */
- enum CustomerType {
- GENERAL, FAST, VIP
- };
复制代码 |