The API says that the implementation of BlockingQueue are thread safe, but about the drainTo method, it says :

"This operation may be more efficient than repeatedly polling this queue. A failure encountered while attempting to add elements to collection c may result in elements being in neither, either or both collections when the associated exception is thrown. Attempts to drain a queue to itself result in IllegalArgumentException. Further, the behavior of this operation is undefined if the specified collection is modified while the operation is in progress."

I take it that the collection can be modified by another thread while the operation is in progress, so how is it thread-safe.

6 Years
Discussion Span
Last Post by daudiam

The API says:-

BlockingQueue implementations are thread-safe. All queuing methods achieve their effects atomically using internal locks or other forms of concurrency control. However, the bulk Collection operations addAll, containsAll, retainAll and removeAll are not necessarily performed atomically unless specified otherwise in an implementation

It says specifically "bulk Collection operations .... are not thread safe unless specified otherwise in an implementation", and if you think about it "drainTo()" does fall in the said category.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.