Iterators and Enumerations (i.e., those returned by keySet().iterator(), entrySet().iterator(), values().iterator(), keys(), and elements()) return elements reflecting the state of the hash table at some point at or since the creation of the iterator/enumeration. They will return at most one instance of each element (via next()/nextElement()), but might or might not reflect puts and removes that have been processed since they were created. They do not throw ConcurrentModificationException. However, these iterators are designed to be used by only one thread at a time. Passing an iterator across multiple threads may lead to unpredictable results if the table is being concurrently modified.
There is NOT any support for locking the entire table to prevent updates. This makes it imposssible, for example, to add an element only if it is not already present, since another thread may be in the process of doing the same thing. If you need such capabilities, consider instead using the ConcurrentReaderHashMap class.
This class may be used as a direct replacement for java.util.Hashtable in any application that does not rely on the ability to lock the entire table to prevent updates. As of this writing, it performs much faster than Hashtable in typical multi-threaded applications with multiple readers and writers. Like Hashtable but unlike java.util.HashMap, this class does NOT allow null to be used as a key or value.
Implementation Note
A slightly faster implementation of this class will be possible once planned Java Memory Model revisions are in place.
Introduction to this package.
| Modifier and Type | Class and Description |
|---|---|
| protected static class | ConcurrentHashMap.Entry
ConcurrentHashMap collision list entry. |
| private class | |
| protected class | |
| protected class | |
| private class | |
| protected static class | ConcurrentHashMap.Segment
Bookkeeping for each concurrency control segment. |
| protected class | |
| private class |
| Modifier and Type | Field and Description |
|---|---|
| protected static final int | CONCURRENCY_LEVEL
The number of concurrency control segments. |
| public static int | DEFAULT_INITIAL_CAPACITY
The default initial number of table slots for this table (32). |
| public static final float | DEFAULT_LOAD_FACTOR
The default load factor for this table (0.75) Used when not otherwise specified in constructor. |
| protected transient Set | |
| protected transient Set | |
| protected final float | loadFactor
The load factor for the hash table. |
| private static final int | MAXIMUM_CAPACITY
The maximum capacity, used if a higher value is implicitly specified by either of the constructors with arguments. |
| private static final int | MINIMUM_CAPACITY
The minimum capacity, used if a lower value is implicitly specified by either of the constructors with arguments. |
| protected static final int | SEGMENT_MASK
Mask value for indexing into segments |
| protected final ConcurrentHashMap. | segments
The array of concurrency control segments. |
| protected transient ConcurrentHashMap. | table
The hash table data. |
| protected int | threshold
Per-segment resize threshold. |
| protected transient Collection | |
| protected transient volatile int | votesForResize
Number of segments voting for resize. |
| Access | Constructor and Description |
|---|---|
| public | ConcurrentHashMap(int
the initial capacity.
The actual initial capacity is rounded to the nearest power of two. initialCapacity, float the load factor threshold, used to control resizing.
This value is used in an approximate way: When at least
a quarter of the segments of the table reach per-segment threshold, or
one of the segments itself exceeds overall threshold,
the table is doubled.
This will on average cause resizing when the table-wide
load factor is slightly less than the threshold. If you'd like
to avoid resizing, you can set this to a ridiculously large
value. loadFactor)Constructs a new, empty map with the specified initial capacity and the specified load factor. |
| public | ConcurrentHashMap(int
the initial capacity of the
ConcurrentHashMap. initialCapacity)Constructs a new, empty map with the specified initial capacity and default load factor. |
| public | ConcurrentHashMap()
Constructs a new, empty map with a default initial capacity and default load factor. |
| public |
| Modifier and Type | Method and Description |
|---|---|
| protected static int | |
| public void | clear()
Overrides java. Implements java. |
| public Object | Returns: a shallow copy of this map.Overrides java. |
| public boolean | |
| public boolean | Returns: true if and only if the specified object
is a key in this table, as determined by the
equals method; false otherwise.possible key. key)Overrides java. Implements java. |
| public boolean | Returns: true if this map maps one or more keys to the specified value.value whose presence in this map is to be tested. value)Overrides java. Implements java. |
| public Enumeration | Returns: an enumeration of the values in this table.Returns an enumeration of the values in this table. |
| public Set | Returns: a collection view of the mappings contained in this map.Implements abstract java. Implements java. |
| protected boolean | eq(Object x, Object y)
Hides java. |
| public Object | Returns: the value to which the key is mapped in this table;null if the key is not mapped to any value in
this table.a key in the table. key)Overrides java. Implements java. |
| protected static int | |
| public boolean | Returns: true if this map contains no key-value mappings.Overrides java. Implements java. |
| public Enumeration | Returns: an enumeration of the keys in this table.Returns an enumeration of the keys in this table. |
| public Set | Returns: a set view of the keys contained in this map.Overrides java. Implements java. |
| protected ConcurrentHashMap. | |
| private int | p2capacity(int initialCapacity)
Returns the appropriate capacity (power of two) for the specified initial capacity argument. |
| public Object | Returns: the previous value of the specified key in this table, ornull if it did not have one.the table key. key, Object the value. value)Overrides java. Implements java. key to the specified
value in this table.
|
| public void | putAll(Map<K, V>
Mappings to be stored in this map. t)Overrides java. Implements java. |
| private void | readObject(ObjectInputStream s)
Reconstitute the ConcurrentHashMap instance from a stream (i.e., deserialize it). |
| protected void | |
| public Object | Returns: the value to which the key had been mapped in this table, ornull if the key did not have a mapping.the key that needs to be removed. key)Overrides java. Implements java. |
| protected Object | Returns: the value to which the key had been mapped in this table, ornull if the key did not have a mapping.the key that needs to be removed. key, Object the associated value. If the value is null,
it means "any value". value)Overrides default java. |
| protected void | resize(int
the current segment. initially call value must be 0 index, ConcurrentHashMap.the state of table on first call to resize. If
this changes on any call, the attempt is aborted because the
table has already been resized by another thread. assumedTabGather all locks in order to call rehash, by recursing within synch blocks for each segment index. |
| public int | Returns: the number of key-value mappings in this map.Overrides java. Implements java. |
| public Collection | Returns: a collection view of the values contained in this map.Overrides java. Implements java. |
| private void | writeObject(ObjectOutputStream s)
Save the state of the ConcurrentHashMap instance to a stream (i.e., serialize it). |