java BlockingQueue does not have a blocking peek?

前端 未结 8 1645
一向
一向 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:37

    Could you also just add an event listener queue to your blocking queue, then when something is added to the (blocking)queue, send an event off to your listeners? You could have your thread block until it's actionPerformed method was called.

    0 讨论(0)
  • 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?

    0 讨论(0)
提交回复
热议问题