Moving beyond Map, Collection, List, and Set: John Zukowski discusses the new library release in the Tiger release of the J2SE platform and what it provides: a set of utilities commonly needed in concurrent programs. If you are interested in optimizing multithreaded access to your collections, you've come to the right place. (This intermediate-level article was first published by IBM developerWorks, June 16, 2004, at http://www.ibm.com/developerWorks.)
There are two sets of Queue implementations in Tiger: those that implement the new BlockingQueue interface, and those that don't. First, I'll take a look at those that don't.
In the simplest case, the pre-existing java.util.LinkedList implementation has been retrofitted to implement not only the java.util.List interface, but also the java.util.Queue interface. You can treat the collection as either. Listing 1 shows one way a LinkedList as a Queue can be used:
Listing 1. Using a Queue implementation
Queue queue = new LinkedList(); queue.offer("One"); queue.offer("Two"); queue.offer("Three"); queue.offer("Four"); // Head of queue should be One System.out.println("Head of queue is: " + queue.poll());
Next up in complexity is the new java.util.AbstractQueue class. This class works similarly to the java.util.AbstractList and java.util.AbstractSet classes. Instead of implementing the entire interface yourself when creating a custom collection, you just subclass the abstract implementation and fill in the details. With AbstractQueue, the methods you must provide implementations for are offer(), poll(), and peek(). Methods like add() and addAll() are modified to use offer(), while clear() and remove() use poll(). Finally, element() uses peek(). You certainly can provide optimized implementations of these methods in your subclasses, but you don't have to. And, instead of creating your own subclasses, you can use one of the several built-in implementations, two of which aren't blocking queues: PriorityQueue and ConcurrentLinkedQueue.
The PriorityQueue and ConcurrentLinkedQueue classes add two more concrete collection implementations to the Collections Framework. The PriorityQueue class essentially maintains an ordered list. Elements added to a Queue are positioned in the PriorityQueue class based on either their natural ordering (through its java.util.Comparable implementation) or according to the java.util.Comparator implementation passed into the constructor. Changing LinkedList in Listing 2 to PriorityQueue will result in Four being printed out instead of One, as Four comes first alphabetically -- the natural ordering of strings. A ConcurrentLinkedQueue is a thread-safe queue based on linked nodes. Concurrent access does not need to be synchronized. Because it adds elements to the end of the queue and removes them from the head, ConcurrentLinkedQueue works well for shared access to a common collection, as long as you don't need to know the size of the queue. Collecting this information is slow, requiring queue traversal.
Visit developerWorks for thousands of developer articles, tutorials, and resources related to open standard technologies, IBM products, and more. See developerWorks.