Thread-safety of BlockingQueue's drainTo() method

后端 未结 3 830
说谎
说谎 2021-01-01 15:50

Documentation of BlockingQueue says bulk operations are not thread-safe, though it doesn\'t explicitly mention the method drainTo().

BlockingQueue imp

3条回答
  •  粉色の甜心
    2021-01-01 16:19

    drainTo() is thread safe in the sense that any operation on the queue that happens at the same time will not change the result nor will it corrupt the state of the queue. Otherwise, the method would be pretty pointless.

    You could run into problems if the target collection (the one to which the results are added) does something "clever". But since you usually drain the queue to a collection to which only a single thread has access, it's more of a theoretical problem.

提交回复
热议问题