取号器不是new了三个号码代理类吗?应该线程互不相干,不会产出线程安全问题吧?
public class NumberMachine {
private NumberManager commonManager=new NumberManager();
private NumberManager expressManager=new NumberManager();
private NumberManager vipManager=new NumberManager();
}
public class NumberManager {
private int lastNumber=1;
private List<Integer> queueNumber=new ArrayList<Integer>();
//客户需要取号排队(前端用)并显示当前都有那第几号客户在排队等待!
public synchronized Integer generateNewManager(){
queueNumber.add(lastNumber);
return lastNumber++;
}
//业务员需要给排队的人办理业务(服务端用)
public synchronized Integer fetchServiceNumber(){
Integer number=null;
if(queueNumber.size()>0){
number=queueNumber.remove(0);
}
return number;
}
}
|
|