本帖最后由 刘馨琪 于 2012-8-14 16:17 编辑
我在做银行系统的时候出现图中标蓝的情况,我希望这里应该出现获取任务后来了客户应该就可以取到任务,我觉得可能是线程同步的问题,可又不知道怎么改,请问怎么办?谢谢
下面贴出部分代码,我认为可能需要做分析的部分。
代码如下:
主程序:- import java.util.concurrent.Executors;
- import java.util.concurrent.TimeUnit;
- public class MainClass {
- public static void main(String[] args){
- for (int i = 1; i < 5; i++) {
- ServiceWindow commonWindow = new ServiceWindow();
- commonWindow.setWindowID(i);
- commonWindow.start();
- }
-
- ServiceWindow expressWindow = new ServiceWindow();
- expressWindow.setType(CustomerType.EXPRESS);
- expressWindow.setWindowID(1);
- expressWindow.start();
-
- ServiceWindow vipWindow = new ServiceWindow();
- vipWindow.setType(CustomerType.VIP);
- vipWindow.setWindowID(1);
- vipWindow.start();
-
- Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
- new Runnable(){
- public void run() {
- Integer number = NumberMachine.getInsance().getCommonManger().generateNewNumber();
- System.out.println(number + "号普通客户在等待服务");
- }
-
- },
- 0,
- Constants.COMMON_CUSTOMER_INTERVAL_TIME,
- TimeUnit.SECONDS);
-
- Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
- new Runnable(){
- public void run() {
- Integer number = NumberMachine.getInsance().getExpressManger().generateNewNumber();
- System.out.println(number + "号快速客户在等待服务");
- }
-
- },
- 0,
- Constants.EXPRESS_CUSTOMER_INTERVAL_TIME,
- TimeUnit.SECONDS);
-
- Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
- new Runnable(){
- public void run() {
- Integer number = NumberMachine.getInsance().getVipManger().generateNewNumber();
- System.out.println(number + "号VIP客户在等待服务");
- }
-
- },
- 0,
- Constants.VIP_CUSTOMER_INTERVAL_TIME,
- TimeUnit.SECONDS);
- }
- }
复制代码 下面这段是叫号机管理系统:- import java.util.ArrayList;
- import java.util.List;
- public class NumberManger {
- private int lastNumber = 1;
- private List<Integer> queueNumber = new ArrayList<Integer>();
-
- public synchronized Integer generateNewNumber(){
- queueNumber.add(lastNumber);
- return lastNumber++;
- }
-
- public synchronized Integer fetchServiceNumber(){
- Integer number = null;
- if(queueNumber.size() > 0){
- number = queueNumber.remove(0);
- }
- return number;
- }
- }
复制代码 |