java.util.concurrent package includes a number of additions to the Java Collections
Framework. These are most easily categorized by the collection interfaces provided:
BlockingQueue defines a first-in-first-out data structure that blocks or
times out when you attempt to add to a full queue, or retrieve from an empty queue.
ConcurrentMap is a subinterface of
java.util.Map that defines
useful atomic operations. These operations remove or replace a key-value pair only if the
key is present, or add a key-value pair only if the key is absent. Making these operations
atomic helps avoid synchronization. The standard general-purpose implementation of
ConcurrentHashMap, which is a concurrent
ConcurrentNavigableMap is a subinterface of
that supports approximate matches. The standard general-purpose implementation of
ConcurrentSkipListMap, which is
a concurrent analog of
Set implementations are provided for use in situations where read operations
vastly outnumber write operations and iteration cannot or should not be synchronized:
CopyOnWriteArrayList - a List implementation backed by an array.
This is a thread-safe variant of
ArrayList in which all mutative operations (
so on) are implemented by making a fresh copy of the underlying array.
This is ordinarily too costly, but may be more efficient than alternatives when traversal operations vastly outnumber mutations,
and is useful when you cannot or don't want to synchronize traversals, yet need to preclude interference among concurrent threads.
The "snapshot" style iterator method uses a reference to the state of the array at the point that the iterator was created. This
array never changes during the lifetime of the iterator, so interference is impossible and the iterator is guaranteed not to
ConcurrentModificationException. The iterator will NOT reflect additions, removals, or changes to the list
since the iterator was created. Element-changing operations on iterators themselves (
add) are NOT supported. These methods throw
CopyOnWriteArraySet - a
Set implementation backed by a copy-on-write array. This implementation is similar
in nature to
CopyOnWriteArrayList. Unlike most
Set implementations, the
contains methods require time proportional to the size of the set. This implementation is well-suited to maintaining
event-handler lists that must prevent duplicates.
All of these collections help avoid Memory Consistency Errors by defining a happens-before relationship between an operation that adds an object to the collection with subsequent operations that access or remove that object.