Pergunta

I read about PriorityBlockingQueue, and seems I've got some delusions about it. You can see four statements below. I hope if you disagree with them you'll correct me.

  1. Blocking collections block a thread if the thread can't execute operation on collection. (Block till condition when the thread can continue). Main purpose - producer-consumer scheme.
  2. Non-blocking collections doesn't block a thread and the thread proceeds execution. Main purpose - schemes where reader shouldn't wait for writer. I mean schemes where the delay read-oparation delay after write is not significant.
  3. As it follows from the name of PriorityBlockingQueue - it's a blocking collection.
  4. PriorityBlockingQueue offer such non-blocking methods as poll, remove, put etc; and seems only poll(long timeout, TimeUnit unit) and take methods are blocking.

Conclusion: From the fourth statement I conclude that PriorityBlockingQueue can be used both as blocking and non-blocking queue.

Question: Am I right in my conclusion? Can I say the same about all other blocking collections?

Foi útil?

Solução

You're correct. Sometimes it can be useful to not block for some operations, even if you're using it regularly with blocking purposes.

From the javadoc for the BlockingQueue interface:

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.

So the blocking property is an addition to normal queue functionality. Soo a blocking queue is a queue, but a queue is not necessarily a blocking one.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top