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

David Holmes dcholmes at optusnet.com.au
Fri Apr 20 17:02:18 EDT 2007


If you are going to raise this issue every time you come across a query
method on a concurrent collection then it is going to get very tedious for
everyone on this list. As the discussions are going to be the same.

The concurrent collections were defined as extensions of the existing
non-concurrent ones - that is a fact and nothing will change that. Was it
the right choice? You obviously believe not. Fine but that isn't going to
change anything. If you feel the need to argue this then write a blog and
share your views. Other's (such as the expert group that defined these
collections, and the broader Java community that supported their definition)
know that the benefits from maintaining commonality between the concurrent
and non-concurrent collections vastly outweighs the fact that some of the
inherited methods have semantics that are not ideal for a concurrent
environment and have to be used with care and understanding.

You also seem to be under the mistaken impression that a concurrent
collection is always used concurrently 100% of the time it exists. That is
far from true and has been pointed out by a number of people a number of
times, but you seem to ignore it. There are often phases where a concurrent
data structure is not used concurrently and during those times simple query
methods are often used to check application invariants - such as checking
whether a collection is or is not empty.

> > Should a concurrent class'
> > isEmpty() throw MightBeStaleStateException instead?
> If you think it should throw any exception, it is even worse. I am
> afraid, it is a very amateur idea in a concurrent environment.

You have misunderstood what Holger meant here. You complained that a method
like isEmpty serves no purpose in a concurrent environment. So Holger was
asking if you thought that all such methods should just throw an exception
(given that we have inherited them and so they already exist) - the
exception basically meaning "this method is not useful in a concurrent
environment so you shouldn't have called it". That would have been one way
to deal with inherited methods that "made no sense".

> The isEmpty() query is useless, as I have explained earlier. The result of
> the query does not tell you anything whatsoever in a concurrent
> environment.

Of course it tells you something. The issue is what inference you draw from
the answer based on the time you ask the question.

David Holmes

More information about the Concurrency-interest mailing list