|
Queues in Java 5: the Queue interface
Java 5 introduces several queue
implementations to the Collections framework. Queue implementations firstly share a new
Queue interface, which has several methods for accessing the head and tail
of the queue. Recall that items are always placed on the end or "tail" of the list, and
always read from the beginning or "head" of the list.
| Operation | Throws exception if not possible | Returns value if not possible |
| Add item to tail | add() | offer() |
| Remove item from head | remove() | poll() |
| "Peek" item at head | element() | peek() |
Methods specified by the Java Queue interface
Types of Queues
Java provides Queue implementations depending on a few key criteria:
- thread-safety: if you don't require the queue to be accessed
concurrently from multiple threads, then a plain LinkedList can be used
as a Queue; the advantage of the other implementations is that they
offer efficient thread-safety;
- blocking or non-blocking: various blocking
implementations add extra methods to put and remove items from the queue,
blocking until the operation is possible, with an optional time limit;
- bound or non-bound: sometimes it is useful to put an upper
limit on the number of items that can fit in the queue, e.g. to prevent a
thread pool from queueing up too many jobs when the machine is busy;
- other special operations: Java provides an implementation that
orders by priority, and another that applies a delay
to queued items.
As of Java 6, the various queue classes are as follows:
Queue implementations as of Java 6
| Blocking? | Other criteria | Bound | Non-bound |
| Blocking | None | ArrayBlockingQueue | LinkedBlockingQueue |
| Priority-based | | PriorityBlockingQueue |
| Delayed | | DelayQueue |
| Non-blocking | Thread-safe | | ConcurrentLinkedQueue |
| Non thread-safe | | LinkedList |
| Non thread-safe, priority-based | | PriorityQueue |
One further type of queue not included above is the SynchronousQueue,
which is effectively a zero-length queue (so that a thread adding an item to the
queue will block until another thread removes the item).
Blocking queues
In general, the most interesting queue implementations are the various
blocking queues, which allow efficient concurrent access
and are useful for coordinating objects between threads, particularly in
the so-called producer-consumer
pattern. In Java, all blocking queue implementations implement the
BlockingQueue interface, which we look at next.
Unless otherwise stated, the Java programming articles and tutorials on this site are written by Neil Coffey.
Suggestions are always welcome if you wish to suggest topics for Java tutorials or programming articles,
or if you simply have a programming question that you would like to see answered on this site. Most topics
will be considered. But in particular, the site aims to provide tutorials and information on topics that
aren't well covered elsewhere, or on Java performance information that is poorly described or understood.
Suggestions may be made via the Javamex blog (see the site's front page for details).
Copyright © Javamex UK 2009. All rights reserved.
|