THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

there is no inherent limit Integer.MAX_VALUE is returned. If there is a capacity limit then
Collection.add will throw IllegalStateException if the element cannot be added.


The drainTo method takes a collection and attempts to transfer all the elements of this queue into the given
collection. The number of elements transferred is returned. An overloaded form of drainTo takes an
additional int parameter that limits the maximum number of elements to transfer. This operation is provided
as an alternative to repeated polling of the queue because some implementations will be able to implement it
very efficientlyeven atomically. However, the guarantees of this method are somewhat loose: It is possible
that if an error occurs when an element is added to the target collection, the element may end up in one,
neither, or both collections!


All BlockingQueue implementations must be thread-safe, but it is not required (unless explicitly
documented by an implementation) that the bulk collection operations (addAll, containsAll,
retainAll) are executed atomically.


The following blocking queue implementations are provided:


ArrayBlockingQueue A bounded blocking queue backed by an array. You construct one with a
capacity and an optional fairness setting. If the queue is fair then blocked threads are processed in
first-in-first-out order; otherwise, blocked threads can be unblocked in arbitrary order. The queue
itself is ordered first-in-first-out. It provides a weakly consistent iterator.


LinkedBlockingQueue An optionally bounded queue based on a linked node implementation.
The queue elements are ordered first-in-first-out. It provides a weakly consistent iterator.

A linked queue implementation will typically provide greater throughput than an array-based
implementation when used concurrently. This is due to the use of algorithms that allow the head and
tail to be locked independently. However, a linked queue implementation may require allocation for
each insertion and produce garbage on each removal, which can add significant overhead. When the
queue is used as a fixed size buffer that is constantly filled by one set of threads and then drained by
another, this overhead is at its worst, and an array-based implementation may perform better than a
bounded linked queue. Moreover, in the usage pattern described, the additional concurrency support
of the linked queue isn't used because each thread within a set operates on the same end of the queue.


PriorityBlockingQueue An unbounded queue ordered the same way as a PriorityQueue.
This implementation adds thread safety and the blocking retrieval operations. It provides an iterator
that is thread-safe but fail-fast.


SynchronousQueue A specialized blocking queue where each take must wait for a put, and
vice versa. A synchronous queue has no capacity, not even a capacity of one, so you cannot peek into
a synchronous queue, nor can you iterate through it. From the perspective of most of the collection
methods a synchronous queue acts like an empty collection.


DelayQueue A specialized unbounded blocking queue of Delayed elements. The Delayed
interface has a single method, getdelay, which takes a TimeUnit constant and returns the delay
in that time unit. A Delayed element cannot be removed from a DelayQueue until its delay has
expired. The head of a delay queue is that element whose delay expired furthest in the past. If no
elements have a delay that has passed then the head is null. It provides an iterator that is thread-safe
but fail-fast.


In addition to the thread-safe blocking queues, java.util.concurrent provides a number of other
concurrent collections.


ConcurrentHashMap provides a hash map implementation that allows for fully concurrent retrievals and
up to a set number of concurrent insertions. Although all operations are thread-safe, they do not involve
locking, and there is no way to externally synchronize the map to provide atomic sequences of operations.
Because of this, some extra support is needed to do things like inserting a value in the map only if it is not
present. The ConcurrentMap<K,V> interface, implemented by ConcurrentHashMap, defines a

Free download pdf