Concurrent Collections – Concurrency: Part II

Concurrent Collections

Concurrent sets, queues, and deques are implemented by the classes Concurrent-SkipListSet, ConcurrentLinkedQueue, and ConcurrentLinkedDeque, respectively. These thread-safe classes implement the corresponding NavigableSet, Queue, and Deque interfaces in the java.util package, as shown in Figure 23.4 and summarized in Table 23.5. Their characteristics are summarized in Table 23.6. These concurrent collections are unbounded and non-blocking. They do not allow null values and have a weakly consistent iterator. As they are unbounded, their insert operation will always succeed. Also worth noting is that their bulk operations (addAll(), removeIf(), forEach()) are not guaranteed to perform atomically.

Figure 23.4 Concurrent Collections in the java.util.concurrent Package

Table 23.5 Concurrent Collections in the java.util.concurrent Package

Concurrent collectionsDescription
ConcurrentSkipListSet<E> implements NavigableSet<E>An unbounded, non-blocking, concurrent NavigableSet implementation that internally uses a ConcurrentSkipListMap. Elements are sorted according to the natural ordering or by a comparator that is provided in the constructor.
ConcurrentLinkedQueue<E> implements Queue<E>An unbounded, non-blocking, concurrent queue based on linked nodes.
ConcurrentLinkedDeque<E> implements Deque<E>An unbounded, non-blocking, concurrent deque based on linked nodes.

Table 23.6 Characteristics of Concurrent Collections

Concurrent collectionsnull valueDuplicatesOrderingKind of iterator
ConcurrentSkipListSet<E> implements NavigableSet<E>NoNoSort orderWeakly consistent
ConcurrentLinkedQueue<E> implements Queue<E>NoYesFIFO orderWeakly consistent
ConcurrentLinkedDeque<E> implements Deque<E>NoYesLIFO/FIFO orderWeakly consistent

The ConcurrentSkipListSet<E> Class

The ConcurrentSkipListSet class implements the NavigableSet interface in the java.util package (§15.5, p. 811). The class implements a scalable, concurrent, sorted set. It provides efficient insertion, removal, and access operations that can be executed safely and concurrently by multiple threads. Its salient properties are summarized in Table 23.6. Selected methods provided by the ConcurrentSkipListSet class are listed below.

// First-last elements
E pollFirst()
E pollLast()

Remove the first and the last elements currently in this concurrent set, respectively.

Click here to view code image

// Range-view operations
NavigableSet<E> headSet(E toElement,   boolean inclusive)
NavigableSet<E> tailSet(E fromElement, boolean inclusive)
NavigableSet<E> subSet(E fromElement, boolean fromInclusive,
                       E toElement,   boolean toInclusive)

Return different views of the underlying concurrent set, depending on the bound elements.
// Closest-matches
E ceiling(E e)
E floor(E e)
E higher(E e)
E lower(E e)

Determine closest-match elements according to various criteria.

Click here to view code image

// Reverse order
Iterator<E> descendingIterator()
NavigableSet<E> descendingSet()

The first method returns a reverse-order iterator for this concurrent set. The second method returns a reverse-order view of the elements in the set.

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>