Maximum BlockingQueue Blocking Time

In the problem of producer-consumer, I would like to use LinkedBlockingQueueas a shared buffer.

The player queues the item using the method put(), and the Consumer thread consumes the item in the queue using the method take().

My question is: let's say if the queue is empty and the consumer thread calls the method take(), I know that the thread is blocked until the producer puts the item in the queue. But is there something like maximum block length? Can we safely assume that the consumer flow will be blocked before the call put()?

I heard people say that forever there is nothing like a block. It's true?

+4
source share
2 answers

There is no waiting expectation in the method take(). And the documentation doesn’t mention the timeout:

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

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

+5
source

If you look at the implementation of the LinkedBlockingQueue take () method, it expects a notEmpty condition in the loop, which will exit only if count becomes nonzero.

So yes, he will always wait until the condition is satisfied, which means that someone puts the element there.

+1
source

All Articles