title: while loop in java BlockingQueue implementation
body: <p>I recently saw the following implementation of enqueue for a BlockingQueue
  (<a href="http://tutorials.jenkov.com/java-concurrency/blocking-queues.html" rel="nofollow">source</a>)</p>
  
  <pre><code>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);
  }
  </code></pre>
  
  <p>Why is the <code>while</code> loop necessary, and can the <code>while</code> be replaced by <code>if (this.queue.size() == this.limit)</code> </p>
  
  <p>It seems the method enqueue is synchronized so only 1 thread at a time can be executing in the method body and make the call to <code>wait()</code>.  Once the thread is notified, can't it just continue onward without checking the <code>this.queue.size() == this.limit</code> condition again?</p>