本帖最后由 osully 于 2014-4-2 13:58 编辑
如下,还请大牛帮看看......上次的交通灯也没个人帮忙看看....哎...
- package YinHangDemo;
- import java.util.LinkedList;
- public enum CustomerType {
- //三种客户
- VIP, General, Fast;
-
- private int lastNumber = 1;
-
- //增删Linked集合比较优秀啊
- LinkedList<Integer> list = new LinkedList<Integer>();
- //来一个人就产生一个号
- public synchronized int produceNumber() {
- list.addLast(lastNumber++);
- return lastNumber;
- }
- //有人就服务
- public synchronized Integer sellNumber() {
- if (list.size() != 0) {
- return list.removeFirst();
- }
- return null;
- }
-
- public String toString(){
- switch(this){
- case VIP:
- return "VIP客户";
- case General:
- return "普通客户";
- case Fast :
- return "快速客户";
- }
- return null;
- }
- }
复制代码- package YinHangDemo;
- public class ServiceWindow implements Runnable {
- //窗口号
- private int windowNumber = 1;
- //客户类型
- private CustomerType type;
- public ServiceWindow(CustomerType type,int windowNumber) {
- this.windowNumber = windowNumber;
- this.type = type;
- }
- public void run() {
- //记录窗口名称
- String windowName = type + "的" + windowNumber + "号窗口";
- //获取是否有人需要服务
- Integer num = type.sellNumber();
- //获取此类型服务需要的时间
- int time = TimeSet.getTime(type);
- while (true) {
- switch (type) {
- case VIP:
- specialWindow(windowName,num,time);
- break;
- case General:
- GeneralWindow(windowName);
- break;
- case Fast:
- specialWindow(windowName,num,time);
- break;
- }
- }
- }
- //因其他窗口调用普通窗口,所以只传入窗口名称
- private void GeneralWindow(String windowName) {
- //是否有普通的需要服务
- Integer num = CustomerType.General.sellNumber();
- //获取普通类型需要服务的时间
- int time = TimeSet.getTime(CustomerType.General);
- if (num != null) {
- System.out.println(windowName + "正在为第" + num + "个普通客户服务");
- try {
- Thread.sleep(time*1000);
- }
- catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println(windowName + "已经完成第" + num + "个普通客户服务,一共花费了"
- + time + "秒");
- }
- else {
- System.out.println(windowName + "没有客户需要服务");
- try {
- Thread.sleep(1000);
- }
- catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- private void specialWindow(String windowName,Integer num,int time) {
- //如果有特殊人员就为特殊人员服务,没有就调用普通窗口
- if (num != null) {
- getService(windowName, num, time);
- }
- else {
- GeneralWindow(windowName);
- }
- }
- //提供服务
- private void getService(String windowName, Integer num, int time) {
- System.out.println(windowName + "正在为第" + num + "个"+type+"客户服务");
- try {
- Thread.sleep(time*1000);
- }
- catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println(windowName + "已经完成第" + num + "个"+type+"客户服务,一共花费了"
- + time + "秒");
- }
- }
复制代码- package YinHangDemo;
- import java.util.Random;
- public class TimeSet {
- //服务需要的最大最小时间
- private static int max;
- private static int min;
-
- //客户出现的时间
- private static int time;
-
- public static int getTime() {
- return time;
- }
- //设置客户出现的时间
- public static void setTime(int time) {
- TimeSet.time = time;
- }
- public static int getMax() {
- return max;
- }
- //可以设置最大时间
- public static void setMax(int max) {
- TimeSet.max = max;
- }
- public static int getMin() {
- return min;
- }
- //可以设置最小时间
- public static void setMin(int min) {
- TimeSet.min = min;
- }
- //某个类型客户服务需要的时间
- public static int getTime(CustomerType type) {
- switch(type){
- case VIP:
- return max;
- case General:
- int num = new Random().nextInt(max)+min;
- return num;
- case Fast:
- return min;
- }
- return 0;
- }
- }
复制代码- package YinHangDemo;
- import java.util.concurrent.Executors;
- import java.util.concurrent.TimeUnit;
- public class MainClass {
- public static void main(String[] args) {
-
- //设定业务办理时间最大值和最小值
- TimeSet.setMax(10);
- TimeSet.setMin(1);
-
- //设定客户的时间基数
- TimeSet.setTime(1);
- //产生VIP客户
- Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
- public void run() {
- CustomerType.VIP.produceNumber();
- }
- }, 1, TimeSet.getTime()*6, TimeUnit.SECONDS);
-
- //产生普通客户
- Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
- public void run() {
- CustomerType.General.produceNumber();
- }
- }, 1, TimeSet.getTime()*1, TimeUnit.SECONDS);
-
- //产生快速客户
- Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
- public void run() {
- CustomerType.Fast.produceNumber();
- }
- }, 1, TimeSet.getTime()*2, TimeUnit.SECONDS);
-
- //产生4个普通窗口
- for (int i = 1; i <= 4; i++) {
- ServiceWindow general = new ServiceWindow(CustomerType.General, i);
- new Thread(general).start();
- }
- //一个VIP窗口
- ServiceWindow VIP = new ServiceWindow(CustomerType.VIP, 1);
- new Thread(VIP).start();
- //一个快速窗口
- ServiceWindow Fast = new ServiceWindow(CustomerType.Fast, 1);
- new Thread(Fast).start();
- }
- }
复制代码
|
|