[concurrency-interest] Is LinkedBlockingQueue.isEmpty() thread safe?
szabolcs.ferenczi at gmail.com
Sun Apr 22 14:55:38 EDT 2007
On 22/04/07, Brian Goetz <brian at quiotix.com> wrote:
> 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."
Hmm... Why should a producer concern about whether the items are
consumed or not? The producer,in fact, can safely shut down as soon as
it has finished its job, can't it. If you cannot shut down the
producer as soon as it has finished, it really indicates something
wrong with your program. You assign too many roles for your producer.
Well, thinking about concurrency needs quite a different attitude to
sequential programming, I must tell you. What seems to be natural to
you in sequential programming, becomes a mistake in a concurrent
environment. Just like what you have pointed out here. Usually queries
on shared data structure are of a bad smell.
More information about the Concurrency-interest