Java thread wait and notify

后端 未结 2 2035
暖寄归人
暖寄归人 2021-02-14 07:28

I have two threads. Thread A is pulling some elements from queue and thread B is adding some elements to the queue.

I want thread A to go to sleep when the queue is emp

2条回答
  •  死守一世寂寞
    2021-02-14 08:22

    Use a BlockingQueue, which is:

    A Queue that additionally supports operations that wait for the queue to become non-empty when retrieving an element, and wait for space to become available in the queue when storing an element.

    Seriously, don't try to reinvent the wheel here.




    (If you must use wait/notify, read this tutorial. But spare yourself the pain, please!)

提交回复
热议问题