发信人: AlphaCode (Alpha), 信区: JobHunting
标 题: 这个Java blocking queue实现是不是有问题?
发信站: BBS 未名空间站 (Sat Jun 13 23:26:59 2015, 美东)
假设空队列时,先有dequeue执行,发现队列为空,等待。
这时来一个enqueue,调用notifyAll()之后,插入新对象之前,dequeue已经唤醒,试
图去删除,这时队列仍然可能是空啊。
public class BlockingQueue {
private List queue = new LinkedList();
private int limit = 10;
public BlockingQueue(int limit){
this.limit = limit;
}
public synchronized void enqueue(Object item)
throws InterruptedException {
while(this.queue.size() == this.limit) {
wait();
}
if(this.queue.size() == 0) {
notifyAll();
}
this.queue.add(item);
}
public synchronized Object dequeue()
throws InterruptedException{
while(this.queue.size() == 0){
wait();
}
if(this.queue.size() == this.limit){
notifyAll();
}
return this.queue.remove(0);
}
}
Saturday, June 13, 2015
这个Java blocking queue实现是不是有问题?
http://www.mitbbs.com/article_t/JobHunting/32988719.html
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment