[concurrency-interest] Is LinkedBlockingQueue.isEmpty() thread safe?

Brian Goetz brian at quiotix.com
Sun Apr 22 14:28:44 EDT 2007

> I doubt if this query could be used in a concurrent program at all. I
> wonder if anyone can show a meaningful application of it. It is a mere
> YAGNI. I am afraid, the presence of the isEmpty() call and some other
> query method calls on a shared data structure indicate some
> incompetence in concurrent programming. It is a bad smell in a
> concurrent program, as they would call it in the refactoring arena.

To use your words...I think this statement merely reflects your 
incompetence in your analysis of all the ways a concurrent class might 
be intelligently used!

In that light, I offer a credible example: a single-producer, 
multiple-consumer situation.  Since the single producer is the only one 
calling put(), having the producer call isEmpty() and receive "true" 
would allow it to conclude "everything that's been put on the queue has 
been processed, so it is safe to shut down now."

Why might you have only a single-producer?  Perhaps that producer might 
be the GUI event thread, queuing background tasks that would take too 
long to execute in the GUI thread.

Please think a little more before you start using loaded words like 
"incompetence".  It is most unattractive, and, more importantly, makes 
people unreceptive to your ideas -- even when they are sensible.

More information about the Concurrency-interest mailing list