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

David Holmes dcholmes at optusnet.com.au
Thu Apr 19 18:59:56 EDT 2007

Szabolcs Ferenczi writes:
> LBQ is meant to be a shared data structure used by concurrent threads.
> It has a method: isEmpty() with the following documentation:
> "Returns true if this collection contains no elements."
> Is it thread safe?

In the sense that concurrent calls to this method with other methods on the
LBQ won't lead to broken invariants - yes it is "thread-safe".

But like any query method on a potentially concurrently modified data
structure, the answer could have changed the instant after the question has
been asked. So the programmer has to know when asking this question makes
sense. You can't use it as part of a check-then-act sequence:

  if (q.isEmpty()) q...

when concurrent modification is possible.

David Holmes

More information about the Concurrency-interest mailing list