java BlockingQueue does not have a blocking peek?

前端 未结 8 1672
一向
一向 2020-12-15 15:37

I have a blocking queue of objects.

I want to write a thread that blocks till there is a object on the queue. Similar to the functionality provided by BlockingQueue.

8条回答
  •  余生分开走
    2020-12-15 16:38

    However, since I do not know if I will be able to process the object successfully, I want to just peek() and not remove the object. I want to remove the object only if I am able to process it successfully.

    In general, it is not thread-safe. What if, after you peek() and determine that the object can be processed successfully, but before you take() it to remove and process, another thread takes that object?

提交回复
热议问题