In an application which uses a BlockingQueue
, I am facing a new requirement that can only be implemented by iterating over the elements present in the queue (to provide info about the current status of the elements in there).
According to the API Javadoc
only the queueing methods of a BlockingQueue implementation are required to be thread-safe. Other API methods
(eg. those inherited from the Collection interface
) may not be used concurrently, though I am not sure whether this also applies to mere read access...
Can I safely use iterator() WITHOUT altering the producer/consumer threads
which may normally interact with the queue at any time? I have no need for a 100% consistent iteration
(it does not matter whether I see elements added/removed while iterating the queue), but I don't want to end up with nasty ConcurrentModificationExceptions
.
Note that the application is currently using a LinkedBlockingQueue
, but I am free to choose any other (unbounded) BlockingQueue implementation
(including free open-source third-party implementations
). Also, I don't want to rely on things that may break in the future, so I want a solution that is OK according to the API
and does not just merely happen to work with the current JRE
.