title: Blocking Queue - Why is there no notify()
body: <p>I was going through this bounding queue code online trying to understand it</p>
  
  <pre><code>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);
    }
  
  }
  </code></pre>
  
  <p>My understanding is this:</p>
  
  <ol>
  <li>if there are no more items in the queue, nothing can be dequeued and so we call a wait()</li>
  <li>if there are maximum number of items in the queue, nothing can be enqueued and so we call a wait()</li>
  <li>if there is some space (and some elements) in the queue we can call enqueue as well as dequeue</li>
  <li>also we notifyAll() so that all producers and consumers wake up</li>
  </ol>
  
  <p>But what happens to the requests that we call wait() on. Do they only get notified on the notifyAll() call? Why do they not get notified as soon as their is space in the queue?</p>