When populating the queue from the contents of the file, depth does not seem to ever increase, as elements are not added in this implementation.
BlockingQueue<String> q = new SynchronousQueue<String>();
...
fstream = new FileInputStream("/path/to/file.txt");
...
while ((line = br.readLine()) != null) {
if (q.offer(line))
System.out.println("Depth: " + q.size()); //0
}
When replacing offer
with add
, exception if thrown
Exception in thread "main" java.lang.IllegalStateException: Queue full
...
What am i doing wrong please? Why is the queue full immediately, upon insertion of the first element?
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.
A blocking queue is a queue which provides insert and remove operations that block or keep waiting until they are performed. The blocking queues are usually used in Producer-Consumer frameworks. This interface extends Queue and exists since Java 5. Null elements are not allowed.
BlockingQueue is a java Queue that support operations that wait for the queue to become non-empty when retrieving and removing an element, and wait for space to become available in the queue when adding an element.
put( ): The method inserts an element to the BlockingQueue. If in case the queue is full, the put( ) method waits until the queue has some vacant space to insert an element. ii. take( ): The method removes and returns an element from the BlockingQueue.
Check the documentation for SynchronousQueue
:
A blocking queue in which each put must wait for a take, and vice versa. A synchronous queue does not have any internal capacity, not even a capacity of one. You cannot peek at a synchronous queue because an element is only present when you try to take it; you cannot add an element (using any method) unless another thread is trying to remove it; you cannot iterate as there is nothing to iterate. The head of the queue is the element that the first queued thread is trying to add to the queue; if there are no queued threads then no element is being added and the head is null. For purposes of other Collection methods (for example contains), a SynchronousQueue acts as an empty collection. This queue does not permit null elements.
You need to have consumers set up and waiting before you can try to add to the queue.
The offer
method doesn't do anything if there are no consumers:
Inserts the specified element into this queue, if another thread is waiting to receive it.
You can use ArrayBlockingQueue. This is a bounded blocking queue backed by an array. This queue orders elements FIFO (first-in-first-out). ArrayBlockingQueue is a classic "bounded buffer", in which a fixed-sized array holds elements inserted by producers and extracted by consumers. http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ArrayBlockingQueue.html (for those who also stepped on a rake)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With