Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LinkedBlockingQueue - java - queue full

I have a linked blocking queue around which I coordinate
the work of 20 consumer threads and 20 producer threads.
The capacity is 10 of the queue (I am just testing currently).

But instead of blocking, when I put into a full queue,
I get this error below. Why so? The JavaDoc says
the queue should be blocking in this case.
Am I misunderstanding something?

java.lang.IllegalStateException: Queue full
    at java.util.AbstractQueue.add(AbstractQueue.java:98)
    at java.util.AbstractQueue.addAll(AbstractQueue.java:187)
like image 989
peter.petrov Avatar asked Sep 24 '15 08:09

peter.petrov


People also ask

What happens if blocking queue is full?

Here we have a blockingQueue that has a capacity equal to 10. It means that when a producer tries to add an element to an already full queue, depending on a method that was used to add it (offer(), add() or put()), it will block until space for inserting object becomes available. Otherwise, the operations will fail.

What is LinkedBlockingQueue in Java?

The LinkedBlockingQueue is an optionally-bounded blocking queue based on linked nodes. It means that the LinkedBlockingQueue can be bounded, if its capacity is given, else the LinkedBlockingQueue will be unbounded. The capacity can be given as a parameter to the constructor of LinkedBlockingQueue.

Is LinkedBlockingQueue thread-safe?

BlockingQueue implementations are thread-safe. All queuing methods achieve their effects atomically using internal locks or other forms of concurrency control.

What is difference between ArrayBlockingQueue and LinkedBlockingQueue in Java concurrency?

ArrayBlockingQueue is bounded which means the size will never change after its creation. LinkedBlockingQueue is optionally bounded which means it can optionally have an upper bound if desired.


1 Answers

You need to use put on the LinkedBlockingQueue. put will wait as long as necessary until space is available. offer will return a boolean indicating success or failure.

like image 164
Tim Bender Avatar answered Oct 02 '22 12:10

Tim Bender