---------------------- ASP.Net+Android+IO开发S、.Net培训、期待与您交流! ----------------------
package com.itheima.bankqueue;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class Main {//与张孝祥老师的相比,该类提取了一个customerThread 方法。
public static void main(String[] args) {
for(int i=1;i<5;i++){
new ServiceWindow(i,CustomerType.COMMON).start();
}
new ServiceWindow(1,CustomerType.EXPRESS).start();
new ServiceWindow(1,CustomerType.VIP).start();
customerThread(CustomerType.COMMON, 1);
customerThread(CustomerType.EXPRESS, 2);
customerThread(CustomerType.VIP, 6);
}
public static void customerThread(final CustomerType type, int time){
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
NumberMachine.newInstance().getManager(type).generateQueue(type);
}
}, 0, time, TimeUnit.SECONDS);
}
}
package com.itheima.bankqueue;
import java.util.Random;
import java.util.concurrent.Executors;
public class ServiceWindow {//与张孝祥老师的相比该类以CustomerType的参数类确定具体的窗口服务,减少了2段代码。
private CustomerType type = CustomerType.COMMON;
private int num = 1;
private String winName = "第"+num+"号 "+type+"窗口";
public ServiceWindow(){}
public ServiceWindow(int num,CustomerType type){
this.num = num;
this.type = type;
winName = "第"+num+"号 "+type+"窗口";
}
public void setType(CustomerType type) {
this.type = type;
}
public void setNum(int num) {
this.num = num;
}
public void start(){//启动一个线程来运行一个窗口的服务
Executors.newSingleThreadExecutor().execute(new Runnable() {
@Override
public void run() {
while(true){
serve(type);
}
}
});
}
public void serve(CustomerType type){//一个窗口的服务以该 窗口的类型来确定。
System.out.println(winName+"开始获取"+type+"任务。");
Integer customerNum = NumberMachine.newInstance().getManager(type).fetchNum();
if(customerNum!=null){
System.out.println(winName+"开始为第"+customerNum+"号"+type+"客户服务。");
int serviceTime = serveTime(type);
try {
Thread.sleep(serviceTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(winName+"完成为第"+customerNum+
"号"+type+"客户服务,总共耗时"+serviceTime/1000+"秒");
}else{
nextServe(type);
}
}
public void nextServe(CustomerType type){//没有等待服务的客户时,不同的窗口 以不同的方式结束一次服务。
if(type != CustomerType.COMMON){
System.out.println(winName+"没有取到"+type+"任务。");
serve(CustomerType.COMMON);
}
else{
System.out.println(winName+"没有取到普通任务,休息一秒");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public int serveTime(CustomerType type){//服务时间
if(type == CustomerType.EXPRESS)
return Constants.MIN_SERVE_TIME;
return new Random().nextInt(Constants.MAX_SERVE_TIME-Constants.MIN_SERVE_TIME)+Constants.MIN_SERVE_TIME;
}
}
package com.itheima.bankqueue;
public class NumberMachine {
private static NumberMachine numMachine = new NumberMachine();
public static NumberMachine newInstance(){
return numMachine;
}
private NumberManager commonManager = new NumberManager();
private NumberManager expressManager = new NumberManager();
private NumberManager VIPManager = new NumberManager();
public NumberManager getCommonManager() {
return commonManager;
}
public NumberManager getExpressManager() {
return expressManager;
}
public NumberManager getVIPManager() {
return VIPManager;
}
public NumberManager getManager(CustomerType type){
switch(type){
case COMMON:
return commonManager;
case EXPRESS:
return expressManager;
}
return VIPManager;
}
}
package com.itheima.bankqueue;
import java.util.ArrayList;
import java.util.List;
public class NumberManager {
private List<Integer> queueNum = new ArrayList<Integer>();
private int lastNum = 0;
public synchronized void generateQueue(CustomerType type){
queueNum.add(++lastNum);
System.out.println("第"+lastNum+type+"号客户正在等待服务。");
}
public synchronized Integer fetchNum(){
if(queueNum.isEmpty())
return null;
return queueNum.remove(0);
}
}
package com.itheima.bankqueue;
public enum CustomerType {
COMMON,EXPRESS,VIP;
@Override
public String toString() {
// TODO Auto-generated method stub
switch(this){
case COMMON:
return "普通";
case EXPRESS:
return "快速";
}
return super.toString();
}
}
package com.itheima.bankqueue;
public class Constants {
public static final int MAX_SERVE_TIME = 10000;
public static final int MIN_SERVE_TIME = 1000;
}
---------------------- ASP.Net+Android+IO开发S、.Net培训、期待与您交流! ----------------------