[concurrency-interest] Asynchronous-nature of ConcurrentLinkedQueue

David Holmes davidcholmes at aapt.net.au
Tue May 18 18:35:34 EDT 2010


Gregg Wonderly writes:
> 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?

Yes but at a cost to every queue operation to maintain that counter. This
penalises the main queue operations to support another operation (size())
that is for most intents and purposes completely useless.

The present code only penalises the misguided client that thinks they need
to know the size.


> Gregg Wonderly
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest

More information about the Concurrency-interest mailing list