Concurrent Maps – Concurrency: Part II

Concurrent Maps

Figure 23.5 shows the inheritance hierarchy of concurrent maps in the java.util .concurrent package. Note the interfaces ConcurrentMap<K,V> and ConcurrentNavigableMap<K,V> that extend the thread-unsafe map interfaces in the java.util package. Their implementations, ConcurrentHashMap<K,V> and ConcurrentSkipListMap<K,V>, provide efficient unsorted and sorted concurrent maps, respectively. The concrete map implementations are summarized in Table 23.9, together with their characteristics in Table 23.10.

Figure 23.5 Concurrent Maps in the java.util.concurrent Package

Table 23.9 Concurrent Maps in the java.util.concurrent Package

Concurrent mapsDescription
interface ConcurrentMap<K,V> extends Map<K,V> ConcurrentHashMap<K,V> implements ConcurrentMap<K,V>A Map providing thread-safety and atomicity guarantees A hash table implementation supporting full concurrency of retrievals and high expected concurrency for updates
interface ConcurrentNavigableMap<K,V> extends ConcurrentMap<K,V>, NavigableMap<K,V> ConcurrentSkipListMap<K,V> implements
ConcurrentNavigableMap<K,V>
A ConcurrentMap supporting NavigableMap operations, and recursively so for its navigable sub-maps A scalable concurrent ConcurrentNavigableMap implementation that is highly efficient for traversal

Table 23.10 Characteristics of Concurrent Maps

Concurrent mapsnull valueDuplicatesOrderingKind of iterator
ConcurrentHashMap<K,V> implements ConcurrentMap<K,V>Not allowed as key or valuesUnique keysNo orderWeakly consistent
ConcurrentSkipListMap<K,V> implements ConcurrentNavigableMap<K,V>Not allowed as key or valuesUnique keysKey sort orderWeakly consistent

The ConcurrentMap<K,V> Interface

In order to maintain the guarantees of thread-safety and atomicity of operations, the ConcurrentMap<K,V> overrides the methods shown below from the Map<K,V> interface, and stipulates that concurrent implementations will honor these guarantees—as exemplified by the ConcurrentHashMap<K,V> class, affording efficient concurrent insertion and lookup. Details on these overridden methods can be found in the Map<K,V> interface (§15.8, p. 831).

The implementation of the ConcurrentHashMap<K,V> class conceptually divides the map into segments (also called sub-maps) that can be independently locked by a thread, thus allowing several threads to perform operations on the map concurrently.

Click here to view code image

interface ConcurrentMap<K,V> extends Map<K, V>  // §15.8, p. 831.
default V compute(K key,
                  BiFunction<? super K,? super V,? extends V> remapFunction)
default V computeIfAbsent(K key,
                          Function<? super K,? extends V> mappingFunction)
default V computeIfPresent(K key,
               BiFunction<? super K,? super V,? extends V> remapFunction)
default void forEach(BiConsumer<? super K,? super V> action)
default V getOrDefault(Object key, V defaultValue)
default V merge(K key, V value,
                BiFunction<? super V,? super V,? extends V> remapFunction)
V putIfAbsent(K key, V value)
boolean remove(Object key, Object value)
V       replace(K key, V value)
boolean replace(K key, V oldValue, V newValue)
default void replaceAll(BiFunction<? super K,? super V,? extends V> func)

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>