Concurrent Collections and Maps 2 – Concurrency: Part II

  • The null value

Some collections allow the null value, and others do not. All concurrent collections, blocking queues, and concurrent maps do not allow the null value as elements, whereas the copy-on-write collections do.

  • Duplicate elements

All collections that embody the concept of a set do not allow duplicates—for example, a CopyOnWriteArraySet. All maps (e.g., a ConcurrentHashMap) do not allow duplicate keys which would violate the concept of hashing values. However, all queues allow duplicates—for example, a LinkedBlockingDeque.

  • Ordering

Elements in some concurrent sets and maps do not have any ordering—for example, a CopyOnWriteArraySet or a ConcurrentHashMap.

Some sets and maps keep their elements and entries in sort order, according to either their natural ordering or a total ordering defined by a comparator on the elements or the keys, respectively. Examples of concurrent sorted collections are a ConcurrentSkipListSet and a ConcurrentSkipListMap.

Insertion ordering is the order in which the elements are inserted into the collection—as used by a CopyOnWriteArrayList for its elements.

FIFO (First-In-First-Out) order is maintained by some thread-safe queues, such as a LinkedBlockingQueue and a ConcurrentLinkedQueue. Other queues have a more specialized ordering, like a DelayQueue.

Not surprisingly, some thread-safe deques exhibit both FIFO and LIFO (Last-In-First-Out) order for their elements depending on how the deque is used—for example, a LinkedBlockingDeque.

  • Iterator behavior during serial access

Serial access is implemented differently for different categories of concurrent collections. It is important to understand the behavior of the iterator provided by these collections. An iterator has to address situations where modifications are made to the collection, outside the control of the iterator, that might render the state of the collection inconsistent—for example, removing or inserting elements during iteration. Iterators can be classified according to the response they provide in this situation, which is referred to as concurrent modification:

  • Weakly consistent iterator

Most of the concurrent collections, and especially queues, provide an iterator that is weakly consistent during serial access. Such an iterator is created on a clone of the collection, and will always traverse elements that existed at the time the iterator was constructed only once. It may not reflect all subsequent modifications made to the collection after the iterator is constructed. It will also not throw a ConcurrentModificationException, and can execute concurrently with other operations on the collection. Weakly consistent iterators also provide guarantees against repeated elements occurring, and guard against a variety of errors and from infinite loops occurring during traversal.

Weakly consistent iterators are also informally referred to as fail-safe iterators.

  • Snapshot-style iterator

A CopyOnWriteArrayList, which is based on a thread-safe version of an ArrayList, uses snapshot-style iterators for serial access. The name of the iterator reflects that it iterates over a snapshot of the underlying array taken at the time the iterator is created. Concurrent multiple read operations on this array snapshot are thread-safe as this snapshot of the array cannot be changed, but any write operation is done on a new copy of the array. An iterator is always created on the most current modified array. A snapshot iterator does not reflect any changes made in the copy of the array—in contrast to a weakly consistent iterator that may reflect such changes.

  • Fail-fast iterator

At the start of each iteration, a fail-fast iterator detects whether the collection has been modified. If that is the case, it throws a ConcurrentModification-Exception. In other words, it fails as soon as possible at the next iteration. It is perfectly safe during iteration to use the Iterator.remove() method to delete the current element, but not with the Collection.remove() method.

Thread-unsafe collections from the java.util package exhibit this behavior, as do the PriorityBlockingQueue and DelayDeque classes in the java.util.concurrent package.

Leave a Reply

Your email address will not be published. Required fields are marked *.

*
*
You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>