After my post concerning Multi tasks, Multi threading, Synchronization, Semaphore, Mutex, Barrier, I would continue with a post about the presentation of java.util.Queue and
First, what is a Queue?
In computer science, a queue is simply a collection that order elements in a FIFO (First-In-First-Out) manner, ie that the elements are always added to the end and taken from the beginning.
However, there are exceptions among queues:
- the priority queues which order elements according to a supplied comparator, or the elements’ natural ordering,
- LIFO queues (or stacks) which order the elements LIFO (Last-In-First-Out),
What is a BlockingQueue?
Blocking queues are queues that also expose functionality for blocking on requests to retrieve an element when no element is available with the additional option to limit the amount of time spent waiting. These queues are typically used by the ‘Producer-Consumer” pattern.
More, introduced in java 1.6, the Deque (pronounced “deck”) is a double-ended queue. This type of queue will not be studied in this current post.
The BlockingQueue interface has 4 different sets of methods for inserting, removing and examining the elements in a queue. Each set of methods behaves differently in case the requested operation cannot be carried out immediately. Here is a table of the methods:
|Throws Exception||Special Value||Blocks||Times Out|
The 4 different sets of behaviour means this:
- Throws Exception: If the attempted operation is not possible immediately, an exception is thrown.
- Special Value: If the attempted operation is not possible immediately, a special value is returned (often true / false).
- Blocks: If the attempted operation is not possible immedidately, the method call blocks until it is.
- Times Out: If the attempted operation is not possible immedidately, the method call blocks until it is, but waits no longer than the given timeout. Returns a special value telling whether the operation succeeded or not (typically true / false).
It is not possible to insert null into a BlockingQueue. If you try to insert null, the BlockingQueue will throw a NullPointerException.
In the future posts, we will study some implementations of the BlockingQueue interface. Each implementation will be illustrated by examples. Then, we will conclude this series with a last post concerning a concrete example of these implementations: the multi servers dispatching.
- BlockingQueue implementation LinkedBlockingQueue (Part2),
- BlockingQueue implementation ArrayBlockingQueue (Part3),
- Queue implementation ConcurrentLinkedQueue (Part4),
- BlockingQueue implementation SynchronousQueue (Part5),
- BlockingQueue implementation PriorityBlockingQueue (Part6),
- BlockingQueue implementation DelayQueue (Part7),
- A concrete example of Queue’s utilization: Multi Servers Dispatching (Part8),
I hope these mini-tutorials help you for the development of queue concurrent code, or at least, for the understanding of the concurrency blocking queues.
At last, the DelayQueue, ConcurrentLinkedQueue, PriorityBlockingQueue, and SynchronousQueue are to be used for special cases requiring extra functionalities. The ArrayBlockingQueue and LinkedBlockingQueue are the implementations the most often used. Respectively, the first is similair to an ArrayList and the second on a LinkedList. More, thee LinkedBlockingQueue is based on “linked nodes which are dynamically created upon each insertion”; this might tend to push you toward ArrayBlockingQueue.