Is BlockingQueue completely thread safe in Java

前端 未结 5 1797
不思量自难忘°
不思量自难忘° 2020-12-04 22:32

I know that the documentation says that the object is thread safe but does that mean that all access to it from all methods are thread safe? So if I call put()

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-04 22:35

    I think @Chris K has missed some points. "When the queue has elements within it, then the push and the pop points are not at the same region of memory and contention can be avoided. ", notice that when the queue has one element, head.next and tail points to the same node and put() and take() can both get locks and execute.

    I think empty and full condition can be solved by synchronized put() and take(). However when it comes to one element, the lb queue has a null dummy head node, which may has something to do with the thread safety.

提交回复
热议问题