[concurrency-interest] An article example

Alexandru Popescu ☀ the.mindstorm.mailinglist at gmail.com
Sun Apr 29 19:08:58 EDT 2007

Hi all!

I was reading the PrintQueue example in this article:
Later in the article there is the following comment:

[snip...] and suddenly you'll get a NoSuchElementException thrown by
LinkedList.removeFirst() in line 24. The bug lurks in the following

   1. Two consumer threads are started, find the queue to be empty,
and do wait().
   2. A producer enqueues a task and notifies both consumers.
   3. One consumer gets the lock, works the task, and leaves the queue
empty. It then releases the lock.
   4. The second consumer gets the lock (it can proceed because it was
notified) and tries to work a task, but now the queue is empty.

Now this comment kind of confuses me because as far as I can say by
the end of step3, the queue is indeed empty and the 2nd consumer will
see this in the condition queue.isEmpty() so it will wait.

Can you please explain what am I missing?

many thanks in advance,

.w( the_mindstorm )p.

More information about the Concurrency-interest mailing list