iwekesi iwekesi - 1 year ago 63
Java Question

Is there a max blocking time on BlockingQueue

In the producer-consumer problem, I would like to use the

as the shared buffer.

Producer thread puts an element on the queue using the
method and the Consumer thread consumes the element on the queue using the

My question is: say if the queue is empty and the consumer thread calls the
method, I am aware that the thread is blocked until the producer puts an element on the queue. But is there something like a maximum block duration? Can I safely assume that the consumer thread will be blocked until the

I have heard people saying that there is nothing like block for ever. Is it true?

Answer Source

No there isn't a timeout for the wait in the take() method. And the documentation does not mention any timeout:

Retrieves and removes the head of this queue, waiting if necessary until an element becomes available.

The poll method, on the other hand, can take a timeout as an argument.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download