[concurrency-interest] Asynchronous-nature of ConcurrentLinkedQueue

Doug Lea dl at cs.oswego.edu
Tue May 18 08:17:46 EDT 2010


On 05/18/10 08:03, Kai Meder wrote:
> Hello
>
> reading the Java-Docs of ConcurrentLinkedQueue I wonder what the
> "asynchronous nature" mentioned in the size()-doc is?
>
> "Beware that, unlike in most collections, this method is NOT a
> constant-time operation. Because of the asynchronous nature of these
> queues, determining the current number of elements requires an O(n)
> traversal. "
>

Because insertion and removal operations can occur concurrently
(even while you are asking about the size), you generally
don't want to ask about the size (although isEmpty is usually
still useful). But if you do ask, the queue
will provide an answer by counting up the elements. The
answer it returns might not have much bearing to the actual
number of elements upon return of the method.

-Doug


More information about the Concurrency-interest mailing list