[concurrency-interest] JCiP Condition Queues

Taras Tielkes mailinglist.taras.tielkes at gmail.com
Sat Nov 4 06:40:33 EST 2006

Alexandru Popescu wrote:
> Hi!
> I have made my way through the Chapter 14 (the book is great, the time
> is limited :-)) . At the end of 14.2.4 subchapter I have found the
> following code:
> [code - page: 304]
> public synchronized void put(V v) throws InterruptedException {
>     while (isFull()) wait();
>      booleam wasEmpty = isEmpty();
>      doPut(v);
>      if(wasEmpty) {
>          notifyAll(); // <=== question here
>      }
> }
> [/code]
> Considering that the chapter was detailing the usage of single
> notification and conditional notification, and also that the example
> comes from the BoundedBuffer (which is one-in one-out),  I am
> wondering what is the benefit of having notifyAll instead of notify.
> I feel I am missing something, because indeed BoundedBuffer doesn't
> conform to the rules to use notify() instead of notifyAll(), but in
> this case when passing from empty to 1-element queue, only one thread
> will be able to take().

This example (as the subscript indicates) is about conditional 
notification, not single notification.
The idea is to notifyAll() only on empty->notEmpty and full->notFull 
transitions, and not on all the other state transitions.

The 'uniform waiters' condition does not hold for BoundedBuffer: having 
the number of producers/consumers exceed the buffer capacity makes it 
possible to have different threads wait for "not full" and "not empty" 
at the same time. (see pages 300-304)


More information about the Concurrency-interest mailing list