[concurrency-interest] LinkedBlockingQueue and iterator question

Doug Lea dl at cs.oswego.edu
Wed Oct 26 07:54:35 EDT 2005

ray wrote:
> When a thread is using an iterator which point to an object in a 
> LinkedBlockingQueue, can other threads remove this object from the 
> queue? 

Yes, they can. This is a property of "weakly consistent iterators"
which says in part:

    Most concurrent Collection implementations (including most Queues)
    also differ from the usual java.util conventions in that their
    Iterators provide weakly consistent rather than fast-fail traversal.
    A   weakly consistent iterator is thread-safe, but does not
    necessarily freeze the collection while iterating, so it may (or may
    not) reflect any updates since the iterator was created.

This means that an iterator need not reflect any concurrent updates
performed since the iterator was constructed. For example, one
legal implementation, that isn't actually used in LinkedBlockingQueue,
is to perform toArray() on the queue upon iterator construction, and
just iterate over that array. (The actual implementation is one
in which you may see some additions and removals since iterator

This is the best policy we know.


More information about the Concurrency-interest mailing list