I am trying to find out whether it is possible to have a multiple producer / multiple consumer queue where I can use notify()
instead of notifyAll()
. For example, in the implementation below (source: here) you cannot just simply switch the notifyAll()
for notify()
. It is not totally obvious why you cannot switch so I will leave it as an teaser to whoever wants to help me out understanding this problem.
So the code below is broken:
public class BlockingQueue {
private Object lock = new Object();
private List queue = new LinkedList();
private int limit = 10;
public BlockingQueue(int limit){
this.limit = limit;
}
public void enqueue(Object item)
throws InterruptedException {
synchronized(lock) {
while(this.queue.size() == this.limit) {
lock.wait();
}
if(this.queue.size() == 0) {
lock.notify();
}
this.queue.add(item);
}
}
public Object dequeue()
throws InterruptedException{
synchronized(lock) {
while(this.queue.size() == 0){
lock.wait();
}
if(this.queue.size() == this.limit){
lock.notify();
}
return this.queue.remove(0);
}
}
}