[concurrency-interest] Asynchronous-nature of ConcurrentLinkedQueue

Gregg Wonderly gergg at cox.net
Tue May 18 18:28:17 EDT 2010


Doug Lea wrote:
> 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.

And I guess I am always curious why there is no "counter" associated with the 
queue length.  It would provide the same "rough" estimate as the "traversal" 
without the repeated overhead would it not?

Gregg Wonderly


More information about the Concurrency-interest mailing list