If I want to pop the head of a LinkedBlockingQueue, I call .poll()
. This returns null if the queue is empty. If I want it to block until the queue is nonempty, I can instead call .take()
, which will always return a value, and blocks as long as necessary.
If I want to view the head of a LinkedBlockingQueue (without mutating it), I call .peek()
. This returns null if the queue is empty. If I want it to block until the queue is nonempty, I......... do what?
I want to block until the queue is nonempty, but I don't want to mutate the queue. I just want to peek.
Currently I just have:
while (queue.isEmpty()) {
Thread.sleep(1);
}