本帖最后由 李铁 于 2012-10-28 11:25 编辑
代码如下:
public class NumberManager {
private int lastNumber=1;
//保存正在排队的号码
private List<Integer> queueNumber=new ArrayList<Integer>();
// 以下两个方法同时访问一个资源,所以要让它们互斥;加上锁。
public synchronized Integer generateManager(){
queueNumber.add(lastNumber);
return lastNumber++;
}
public synchronized Integer fctchServiceNumber(){
Integer number=null;
if(queueNumber.size()>0){
return queueNumber.remove(0);
}
return number;
}
}
public class ServiceWindow {
private CustomerType type = CustomerType.COMMON;
private int windowId = 1;
public CustomerType getType() {
return type;
}
public void setType(CustomerType type) {
this.type = type;
}
public int getWindowId() {
return windowId;
}
// 只需要一个线程
public void setWindowId(int windowId) {
this.windowId = windowId;
}
public void start() {
Executors.newSingleThreadExecutor().execute(new Runnable() {
@Override
public void run() {
// 不停地取号
while (true) {
switch (type) {
case COMMON:
comnonService();
break;
case EXPRESS:
expressService();
break;
case VIP:
vipService();
break;
}
}
}
});
}
private void comnonService() {
String windowName = "第" + windowId + "号" + type + "窗口";
Integer number = NumberMachine.getInstance().getCommonManager()
.fctchServiceNumber();
System.out.println(windowName + "正在获取任务");
if (number != null) {
System.out
.println(windowName + "为第" + number + "个"+"普通客户服务");
long beginTime = System.currentTimeMillis();
int maxRand = Constans.MAX_SERVICE_TIME - Constans.MIX_SERVICE_TIME;
long serverTime = new Random().nextInt(maxRand) + 1
+ Constans.MIX_SERVICE_TIME;
try {
Thread.sleep(serverTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
long codeTime = System.currentTimeMillis() - beginTime;
System.out.println(windowName + "为第" + number + "个"
+ "普通客户服务完成服务,耗时" + codeTime / 1000 + "秒");
} else {
System.out.println(windowName + "没有获取任务,先休息一下秒嘛!");
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void expressService() {
String windowName = "第" + windowId + "号" + type + "窗口";
Integer number = NumberMachine.getInstance().getExpressManager()
.fctchServiceNumber();
System.out.println(windowName + "正在获取快速任务");
if (number != null) {
System.out.println(windowName + "为第" + number + "个" + type + "客户服务");
long beginTime = System.currentTimeMillis();
// int maxRand=Constans.MAX_SERVICE_TIME-Constans.MIX_SERVICE_TIME;
// long serverTime=new
// Random().nextInt(maxRand)+1+Constans.MIX_SERVICE_TIME;
try {
Thread.sleep(Constans.MIX_SERVICE_TIME);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long codeTime = System.currentTimeMillis() - beginTime;
System.out.println(windowName + "为第" + number + "个" + type
+ "客户服务完成服务,耗时" + codeTime / 1000 + "秒");
} else {
System.out.println(windowName + "正在获取任务!");
comnonService();
}
}
private void vipService() {
String windowName = "第" + windowId + "号" + type + "窗口";
Integer number = NumberMachine.getInstance().getVipManager()
.fctchServiceNumber();
System.out.println(windowName + "正在获取任务");
if (number != null) {
System.out
.println(windowName + "为第" + number + "个" + type + "客户服务");
long beginTime = System.currentTimeMillis();
int maxRand = Constans.MAX_SERVICE_TIME - Constans.MIX_SERVICE_TIME;
long serverTime = new Random().nextInt(maxRand) + 1
+ Constans.MIX_SERVICE_TIME;
try {
Thread.sleep(serverTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
long codeTime = System.currentTimeMillis() - beginTime;
System.out.println(windowName + "为第" + number + "个" + type
+ "客户服务完成服务,耗时" + codeTime / 1000 + "秒");
} else {
System.out.println(windowName + "没有取到任务");
comnonService();
}
}
public class Constans {
public static int MAX_SERVICE_TIME=10000;
public static int MIX_SERVICE_TIME=1000;
public static int COMON_CUSTOMER_INTERVAL_TIME=1;
}
public enum CustomerType {
COMMON,EXPRESS,VIP;
public String toString(){
switch (this) {
case COMMON:
return "普通";
case EXPRESS:
return "快速";
case VIP:
return name();
}
return null;
}
}
public class MainClass {
public static void main(String args[]){
for(int i=1;i<5;i++){
ServiceWindow commWindow=new ServiceWindow();
commWindow.setWindowId(i);
commWindow.start();
}
ServiceWindow expressWindow=new ServiceWindow();
expressWindow.setType(CustomerType.EXPRESS);
expressWindow.start();
ServiceWindow vipWindow=new ServiceWindow();
vipWindow.setType(CustomerType.VIP);
vipWindow.start();
//模拟客户来取号,调度系统
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
new Runnable() {
public void run() {
Integer number=NumberMachine.getInstance().getCommonManager().generateManager();
System.out.println(number+"号普通客户等待服务");
}
},
0,
Constans.COMON_CUSTOMER_INTERVAL_TIME,
TimeUnit.SECONDS
);
//
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
new Runnable() {
public void run() {
Integer number=NumberMachine.getInstance().getExpressManager().generateManager();
System.out.println(number+"号快速 客户等待服务");
}
},
0,
Constans.COMON_CUSTOMER_INTERVAL_TIME*6,
TimeUnit.SECONDS
);
//
|
|