This class is a threadsafe implementation of the java.util.Map interface, and of the atomic operations added by the ConcurrentMap interface. This class is intended as a drop-in replacement for java.util.Hashtable. It is more efficient than that class, however, because it provides threadsafety without using synchronized methods that lock the entire data structure. ConcurrentHashMap allows any number of concurrent read operations without locking. Locking is required for updates to a ConcurrentHashMap, but the internal data structure is segmented so that only the segment being updated is locked, and reads and writes can proceed concurrently in other segments. You can specify the number of internal segments with the concurrencyLevel argument to the constructor. The default is 16. Set this to the approximate number of updater threads you expect to access the data structure. Like Hashtable, ConcurrentHashMap does not allow null keys or values. (Note that this differs from the behavior of java.util.HashMap.)


Figure 16-75. java.util.concurrent.ConcurrentHashMap<K,V>

public class ConcurrentHashMap<K,V> extends java.util.AbstractMap<K,V>
 implements ConcurrentMap<K,V>, Serializable {
// Public Constructors
     public ConcurrentHashMap( );  
     public ConcurrentHashMap(java.util.Map<? extends K,? extends V> t);  
     public ConcurrentHashMap(int initialCapacity);  
     public ConcurrentHashMap ...

Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.