The Java programming language's Java Collections Framework version 1.5 and later defines and implements the original regular single-threaded maps, and also new thread-safe maps implementing the interface among other concurrent interfaces. In Java 1.6, the interface was added, extending , and the interface was added as a subinterface combination.
Java Map Interfaces
The version 1.8 java.util.Map<K, V> interface diagram has the shape below. Sets can be considered sub-cases of corresponding maps in which the values are always a particular constant which can be ignored, although the java.util.Set<E> API uses corresponding but differently named methods. At the bottom is the java.util.concurrent.ConcurrentNavigableMap<K, V>, which is a multiple-inheritance.
Implementations ConcurrentHashMap
For unordered access as defined in the java.util.Map<K, V> interface, the java.util.concurrent.ConcurrentHashMap<K, V> implements java.util.concurrent.ConcurrentMap<K, V>. The mechanism is a hash access to a hash table with lists of entries, each entry holding a key, a value, the hash, and a next reference. Previous to Java 8, there were multiple locks each serializing access to a 'segment' of the table. In Java 8, native synchronization is used on the heads of the lists themselves, and the lists can mutate into small trees when they threaten to grow too large due to unfortunate hash collisions. Also, Java 8 uses the compare-and-set primitive optimistically to place the initial heads in the table, which is very fast. Performance is , but there are delays occasionally when rehashing is necessary. After the hash table expands, it never shrinks, possibly leading to a memory 'leak' after entries are removed.
ConcurrentSkipListMap
For ordered access as defined by the java.util.NavigableMap<K, V> interface, java.util.concurrent.ConcurrentSkipListMap<K, V> was added in Java 1.6, and implements java.util.concurrent.ConcurrentMap<K, V> and also java.util.concurrent.ConcurrentNavigableMap<K, V>. It is a skip list which uses lock-free techniques to make a tree. Performance is .
Concurrent modification problem
One problem solved by the Java 1.5 java.util.concurrent<K, V> package is that of concurrent modification. The collection classes it provides may be reliably used by multiple java.lang.Threads.
All thread-shared non-concurrent maps and other collections need to use some form of explicit locking such as native synchronization in order to prevent concurrent modification, or else there must be a way to prove from the program logic that concurrent modification cannot occur. Concurrent modification of a java.lang.Map<K, V> by multiple threads will sometimes destroy the internal consistency of the data structures inside the java.lang.Map<K, V>, leading to bugs which manifest rarely or unpredictably, and which are difficult to detect and fix. Also, concurrent modification by one thread with read access by another thread or threads will sometimes give unpredictable results to the reader, although the map's internal consistency will not be destroyed. Using external program logic to prevent concurrent modification increases code complexity and creates an unpredictable risk of errors in existing and future code, although it enables non-concurrent Collections to be used. However, either locks or program logic cannot coordinate external threads which may come in contact with the java.util.Collection<E>.
Modification counters
In order to help with the concurrent modification problem, the non-concurrent java.lang.Map<K, V> implementations and other java.util.Collection<E>s use internal modification counters which are consulted before and after a read to watch for changes: the writers increment the modification counters. A concurrent modification is supposed to be detected by this mechanism, throwing a , but it is not guaranteed to occur in all cases and should not be relied on. The counter maintenance is also a performance reducer. For performance reasons, the counters are not volatile, so it is not guaranteed that changes to them will be propagated between Threads.
Collections.synchronizedMap()
One solution to the concurrent modification problem is using a particular wrapper class provided by a factory in : which wraps an existing non-thread-safe Map with methods that synchronize on an internal mutex. There are also wrappers for the other kinds of java.util.Collection<E>s. This is a partial solution, because it is still possible that the underlying java.util.Map<K, V> can be inadvertently accessed by java.lang.Threads which keep or obtain unwrapped references. Also, all java.util.Collection<E>s implement the but the synchronized-wrapped java.util.Map<K, V>s and other wrapped java.util.Collection<E>s do not provide synchronized iterators, so the synchronization is left to the client code, which is slow and error prone and not possible to expect to be duplicated by other consumers of the synchronized java.util.Map<K, V>. The entire duration of the iteration must be protected as well. Furthermore, a java.util.Map<K, V> which is wrapped twice in different places will have different internal mutex objects on which the synchronizations operate, allowing overlap. The delegation is a performance reducer, but modern just-in-time compilers often inline heavily, limiting the performance reduction. Here is how the wrapping works inside the wrapper - the mutex is just a final java.util.Object and m is the final wrapped java.util.Map<K, V>:
public V put(K key, V value) { synchronized (mutex) { return m.put(key, value);
The synchronization of the iteration is recommended as follows; however, this synchronizes on the wrapper rather than on the internal mutex, allowing overlap:
import java.util.Collections; import java.util.Map;
Map<String, String> wrappedMap = Collections.synchronizedMap(map);
synchronized (wrappedMap) { for (String s : wrappedMap.keySet()) { // some possibly long operation executed possibly // many times, delaying all other accesses
Native synchronization
Any java.util.Map<K, V> can be used safely in a multi-threaded system by ensuring that all accesses to it are handled by the Java synchronization mechanism:
import java.util.HashMap; import java.util.Map;
Map<String, String> map = new HashMap<>();
// Thread A // Use the map itself as the lock. Any agreed object can be used instead. synchronized (map) { map.put("key","value"); }
// Thread B synchronized (map) { String result = map.get("key"); // ... }
// Thread C synchronized (map) { for (Map.Entry<String, String> s : map.entrySet()) { /* * Some possibly slow operation, delaying all other supposedly fast operations. * Synchronization on individual iterations is not possible. */ // ...
ReentrantReadWriteLock
The code using a is similar to that for native synchronization. However, for safety, the locks should be used in a try/finally block so that early exit such as throwing or break/continue will be sure to pass through the unlock. This technique is better than using synchronization because reads can overlap each other, there is a new issue in deciding how to prioritize the writes with respect to the reads. For simplicity a can be used instead, which makes no read/write distinction. More operations on the locks are possible than with synchronization, such as tryLock() and tryLock(long timeout, TimeUnit unit). import java.util.Map; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock;
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); final ReadWriteLock readLock = lock.readLock(); final ReadWriteLock writeLock = lock.writeLock();
// Thread A try { writeLock.lock(); map.put("key","value"); } finally { writeLock.unlock(); }
// Thread B try { readLock.lock(); String s = map.get("key"); } finally { readLock.unlock(); }
// Thread C try { readLock.lock(); for (Map.Entry<String, String> s : map.entrySet()) { /* * Some possibly slow operation, delaying all other supposedly fast operations. * Synchronization on individual iterations is not possible. */ // ...
} finally { readLock.unlock(); }
Convoys
Mutual exclusion has a lock convoy problem, in which threads may pile up on a lock, causing the JVM to need to maintain expensive queues of waiters and to 'park' the waiting java.lang.Threads. It is expensive to park and unpark a java.lang.Threads, and a slow context switch may occur. Context switches require from microseconds to milliseconds, while the map's own basic operations normally take nanoseconds. Performance can drop to a small fraction of a single java.lang.Thread's throughput as contention increases. When there is no or little contention for the lock, there is little performance impact; however, except for the lock's contention test. Modern JVMs will inline most of the lock code, reducing it to only a few instructions, keeping the no-contention case very fast. Reentrant techniques like native synchronization or java.util.concurrent.locks.ReentrantReadWriteLock however have extra performance-reducing baggage in the maintenance of the reentrancy depth, affecting the no-contention case as well. The convoy problem seems to be easing with modern JVMs, but it can be hidden by slow context switching: in this case, latency will increase, but throughput will continue to be acceptable. With hundreds of java.lang.Threads, a context switch time of 10ms produces a latency in seconds.
Multiple cores
Mutual exclusion solutions fail to take advantage of all of the computing power of a multiple-core system, because only one java.lang.Thread is allowed inside the java.util.Map<K, V> code at a time. The implementations of the particular concurrent maps provided by the Java Collections Framework and others sometimes take advantage of multiple cores using lock free programming techniques. Lock-free techniques use operations like the compareAndSet() intrinsic method available on many of the Java classes such as to do conditional updates of some Map-internal structures atomically. The compareAndSet() primitive is augmented in the JCF classes by native code that can do compareAndSet on special internal parts of some objects for some algorithms (using 'unsafe' access). The techniques are complex, relying often on the rules of inter-thread communication provided by volatile variables, the happens-before relation, special kinds of lock-free 'retry loops' (which are not like spin locks in that they always produce progress). The compareAndSet() relies on special processor-specific instructions. It is possible for any Java code to use for other purposes the compareAndSet() method on various concurrent classes to achieve lock-free or even wait-free concurrency, which provides finite latency. Lock-free techniques are simple in many common cases and with some simple collections like stacks.
The diagram indicates how synchronizing using wrapping a regular HashMap (purple) may not scale as well as ConcurrentHashMap (red). The others are the ordered java.util.concurrent.ConcurrentNavigableMap<K, V>s java.util.concurrent.AirConcurrentMap<K, V> (blue) and java.util.concurrent.ConcurrentSkipListMap<K, V> (CSLM green). (The flat spots may be rehashes producing tables that are bigger than the nursery, and java.util.concurrent.ConcurrentHashMap<K, V> takes more space. Note y-axis should say 'puts K'. System is 8-core i7 2.5 GHz, with -Xms5000m to prevent GC). GC and JVM process expansion change the curves considerably, and some internal lock-Free techniques generate garbage on contention.
Predictable latency