title: notify() instead of notifyAll() for blocking queue
body: <p>I am trying to find out whether it is possible to have a multiple producer / multiple consumer queue where I can use <code>notify()</code> instead of <code>notifyAll()</code>. For example, in the implementation below (source: <a href="http://tutorials.jenkov.com/java-concurrency/blocking-queues.html" rel="nofollow">here</a>) you cannot just simply switch the <code>notifyAll()</code> for <code>notify()</code>. 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.</p>
  
  <p>So the code below is broken:</p>
  
  <pre><code>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);
    }
   }
  }
  </code></pre>