CodingBison

Blocking Queue interface is a popularly used Collection interfaces. Blocking Queue interface as shown in the below figure extends Queue interface, and hence it inherits all the operations from Queue interface. Before we go into the details of the Blocking Queue, let us try to understand the basic data structure underneath the blocking Queue, which is Queue. Queue, in general is a very popular data structure and is used in many scenarios. More details about Queue is available in our data structures modules understanding data structure Queue.



Figure: Blocking Queue Interface

Queue, typically follows the notions, FIFO (first in first out). The elements that inserted first into the queue are deleted first. This is opposite to an other very popular data structure "Stack", which follows the notion LIFO (last in first out), more details about Stack is available here understanding data structure Stack. Elements into the Queue are always inserted at the "tail" of the Queue and removed from the "head" of the Queue. Both "head" and "tail" points to the same position when the Queue is empty.

In general, Queue interface does not define blocking queue methods, which are very commonly used in concurrent programming. Blocking queue methods, while adding an element in the queue, "wait" for the space to be available if the Queue is FULL. At the same time, "waits" for the Queue to be non-empty, when tries to remove an element from the Queue. Generic Queue interface does not offer this kind of behavior. The other important property of the Blocking Queue interface, is that it does not allow insertion of "null" elements in to the Blocking Queue.

Things To Remember
Insertion of "null" elements is not allowed into the Blocking Queue.

Methods defined in Blocking Queue interface

The Blocking Queue interface inherits all the methods from the Queue interface, but as we know Blocking queue has its own constraints/properties that are added on top of the Queue. Therefore, it defines additional methods, on top of the ones inherited from Queue interface.

Blocking queue offers four sets of methods for the same/similar functionality. Two out of Four are inherited from Queue, where the first set of methods throws an "exception" on failure while the second set does not throw an exception, but instead lets the caller know by returning an appropriate value. The third set of methods that comes with Blocking queue interface, blocks the current thread indefinitely until the operation can succeed. This means, calling any of the third set methods guarantees success, but remember it comes at a cost, where the current thread is indefinitely blocked. The fourth set of methods, takes "time" as an argument and blocks the current thread for a period of time specified in that argument. The method gives us once the specified time elapses. We will take a look at the third and fourth set of methods here, you can imagine this table to be an extension of the table Queue Interface Methods List.


Table: Blocking Queue Methods
MethodDescription
void put(E e)Inserts the specified element into this queue, waiting if necessary for space to become available. Throws "InterruptedException" if interrupted while waiting for space.
E take()Retrieves and removes the head of this queue, waiting if necessary until an element becomes available. Returns the "removed element" on success. Throws "InterruptedException" if interrupted while waiting.
boolean offer(E e, long timeout, TimeUnit unit)Inserts the specified element into this queue, waiting up to the specified wait time if necessary for space to become available. Returns "true" if successful, or false if the specified waiting time elapses before space is available. Throws "InterruptedException" if interrupted while waiting.
E poll(long timeout,TimeUnit unit)Retrieves and removes the head of this queue, waiting up to the specified wait time if necessary for an element to become available. Returns the "retrieved element" on success or "null" if the Queue is empty. Throws "InterruptedException" if interrupted while waiting.




comments powered by Disqus