public class AllTimeQueue {
private final static GameAllTimeQueue gatqInstance = new GameAllTimeQueue();
private BlockingQueue<Long> timeNow = new LinkedBlockingQueue<Long>();
private GameAllTimeQueue(){}
public synchronized GameAllTimeQueue getInstance(){
return gatqInstance;
}
public synchronized void set(BlockingQueue<Long> bq){
timeNow = bq;
}
public synchronized BlockingQueue<Long> get(){
return timeNow;
}
}
------解决方案-------------------- set要加synchronized ------解决方案-------------------- BlockingQueue线程安全,所以不需要顺序,就都不用加 ------解决方案-------------------- 1.set,get方法还需要不需要synchronized了?
这种set get 没有意义,怎么搞都不是线程安全的。
public void set(Long id){ 这样是安全的,不需要加sync
timeNow.add(id);
}