JavaBlog.fr / Java.lu DEVELOPMENT,Java Java: We have said Queue/BlockingQueue?! (Part1)

Java: We have said Queue/BlockingQueue?! (Part1)

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
java.util.concurrent.BlockingQueue.

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.

BlockingQueue Methods
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
Insert add(elt) offer(elt) put(elt) offer(o, timeout, timeunit)
Remove remove(elt) poll(elt) take(elt) poll(timeout, timeunit)
Examine element(elt) peek(elt)    

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.

Queue/BlockingQueue Implementations
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.

To conclude
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.

Best regards,

Huseyin OZVEREN

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

Related Post

Documentum : Audit trail entries dm_audittrail / SessionConfig / application_codeDocumentum : Audit trail entries dm_audittrail / SessionConfig / application_code

Hi, After my previous posts concerning the Documentum audit trail entries (dm_audittrail) http://www.javablog.fr/documentum-creation-of-audit-trail-entries-dm_audittrail.html and http://www.javablog.fr/deleting-of-audit-trail-entries-dm_audittrail.html, here, I would like to expose a solution in order to force content server to