iwekesi iwekesi - 6 months ago 9
Java Question

Is there a max blocking time on BlockingQueue

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

LinkedBlockingQueue
as the shared buffer.

Producer thread puts an element on the queue using the
put()
method and the Consumer thread consumes the element on the queue using the
take()
method.

My question is: say if the queue is empty and the consumer thread calls the
take()
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
put()
call?

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

Answer

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.

Comments