本帖最后由 谢毅 于 2013-1-17 16:40 编辑
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
public void run() {
Integer number = NumberMachine.getInstance().getCommonManager()
.generateNewManager();
System.out.println(number + "号普通客户等待服务");
}
}, 0, Constants.COMMON_CUSTOMER_INTERVAL_TIME, TimeUnit.SECONDS);
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
public void run() {
Integer number = NumberMachine.getInstance()
.getExpressManager().generateNewManager();
System.out.println(number + "号快速客户等待服务");
}
}, 0, Constants.COMMON_CUSTOMER_INTERVAL_TIME * 2, TimeUnit.SECONDS);
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
public void run() {
Integer number = NumberMachine.getInstance().getVipManager()
.generateNewManager();
System.out.println(number + "号VIP客户等待服务");
}
}, 0, Constants.COMMON_CUSTOMER_INTERVAL_TIME * 6, TimeUnit.SECONDS);
3个调度线程需要进行同步,老张也说过,不过说用两个锁去做很可能死锁,因为其中的generateNewManager()也是阻塞状态。
我想了下是不是可以用Lock和Condition去取代两个锁,不过还没实现,求各位大虾建议
|