2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * This code is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 only, as
6 * published by the Free Software Foundation. Oracle designates this
7 * particular file as subject to the "Classpath" exception as provided
8 * by Oracle in the LICENSE file that accompanied this code.
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
26 * This file is available under and governed by the GNU General Public
27 * License version 2 only, as published by the Free Software Foundation.
28 * However, the following notice accompanied the original version of this
31 * Written by Doug Lea with assistance from members of JCP JSR-166
32 * Expert Group and released to the public domain, as explained at
33 * http://creativecommons.org/publicdomain/zero/1.0/
36 package java.util.concurrent;
37 import java.util.concurrent.locks.*;
39 import java.io.Serializable;
40 import java.io.IOException;
41 import java.io.ObjectInputStream;
42 import java.io.ObjectOutputStream;
45 * A hash table supporting full concurrency of retrievals and
46 * adjustable expected concurrency for updates. This class obeys the
47 * same functional specification as {@link java.util.Hashtable}, and
48 * includes versions of methods corresponding to each method of
49 * <tt>Hashtable</tt>. However, even though all operations are
50 * thread-safe, retrieval operations do <em>not</em> entail locking,
51 * and there is <em>not</em> any support for locking the entire table
52 * in a way that prevents all access. This class is fully
53 * interoperable with <tt>Hashtable</tt> in programs that rely on its
54 * thread safety but not on its synchronization details.
56 * <p> Retrieval operations (including <tt>get</tt>) generally do not
57 * block, so may overlap with update operations (including
58 * <tt>put</tt> and <tt>remove</tt>). Retrievals reflect the results
59 * of the most recently <em>completed</em> update operations holding
60 * upon their onset. For aggregate operations such as <tt>putAll</tt>
61 * and <tt>clear</tt>, concurrent retrievals may reflect insertion or
62 * removal of only some entries. Similarly, Iterators and
63 * Enumerations return elements reflecting the state of the hash table
64 * at some point at or since the creation of the iterator/enumeration.
65 * They do <em>not</em> throw {@link ConcurrentModificationException}.
66 * However, iterators are designed to be used by only one thread at a time.
68 * <p> The allowed concurrency among update operations is guided by
69 * the optional <tt>concurrencyLevel</tt> constructor argument
70 * (default <tt>16</tt>), which is used as a hint for internal sizing. The
71 * table is internally partitioned to try to permit the indicated
72 * number of concurrent updates without contention. Because placement
73 * in hash tables is essentially random, the actual concurrency will
74 * vary. Ideally, you should choose a value to accommodate as many
75 * threads as will ever concurrently modify the table. Using a
76 * significantly higher value than you need can waste space and time,
77 * and a significantly lower value can lead to thread contention. But
78 * overestimates and underestimates within an order of magnitude do
79 * not usually have much noticeable impact. A value of one is
80 * appropriate when it is known that only one thread will modify and
81 * all others will only read. Also, resizing this or any other kind of
82 * hash table is a relatively slow operation, so, when possible, it is
83 * a good idea to provide estimates of expected table sizes in
86 * <p>This class and its views and iterators implement all of the
87 * <em>optional</em> methods of the {@link Map} and {@link Iterator}
90 * <p> Like {@link Hashtable} but unlike {@link HashMap}, this class
91 * does <em>not</em> allow <tt>null</tt> to be used as a key or value.
93 * <p>This class is a member of the
94 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
95 * Java Collections Framework</a>.
99 * @param <K> the type of keys maintained by this map
100 * @param <V> the type of mapped values
102 public class ConcurrentHashMap<K, V> extends AbstractMap<K, V>
103 implements ConcurrentMap<K, V>, Serializable {
104 private static final long serialVersionUID = 7249069246763182397L;
107 * The basic strategy is to subdivide the table among Segments,
108 * each of which itself is a concurrently readable hash table. To
109 * reduce footprint, all but one segments are constructed only
110 * when first needed (see ensureSegment). To maintain visibility
111 * in the presence of lazy construction, accesses to segments as
112 * well as elements of segment's table must use volatile access,
113 * which is done via Unsafe within methods segmentAt etc
114 * below. These provide the functionality of AtomicReferenceArrays
115 * but reduce the levels of indirection. Additionally,
116 * volatile-writes of table elements and entry "next" fields
117 * within locked operations use the cheaper "lazySet" forms of
118 * writes (via putOrderedObject) because these writes are always
119 * followed by lock releases that maintain sequential consistency
122 * Historical note: The previous version of this class relied
123 * heavily on "final" fields, which avoided some volatile reads at
124 * the expense of a large initial footprint. Some remnants of
125 * that design (including forced construction of segment 0) exist
126 * to ensure serialization compatibility.
129 /* ---------------- Constants -------------- */
132 * The default initial capacity for this table,
133 * used when not otherwise specified in a constructor.
135 static final int DEFAULT_INITIAL_CAPACITY = 16;
138 * The default load factor for this table, used when not
139 * otherwise specified in a constructor.
141 static final float DEFAULT_LOAD_FACTOR = 0.75f;
144 * The default concurrency level for this table, used when not
145 * otherwise specified in a constructor.
147 static final int DEFAULT_CONCURRENCY_LEVEL = 16;
150 * The maximum capacity, used if a higher value is implicitly
151 * specified by either of the constructors with arguments. MUST
152 * be a power of two <= 1<<30 to ensure that entries are indexable
155 static final int MAXIMUM_CAPACITY = 1 << 30;
158 * The minimum capacity for per-segment tables. Must be a power
159 * of two, at least two to avoid immediate resizing on next use
160 * after lazy construction.
162 static final int MIN_SEGMENT_TABLE_CAPACITY = 2;
165 * The maximum number of segments to allow; used to bound
166 * constructor arguments. Must be power of two less than 1 << 24.
168 static final int MAX_SEGMENTS = 1 << 16; // slightly conservative
171 * Number of unsynchronized retries in size and containsValue
172 * methods before resorting to locking. This is used to avoid
173 * unbounded retries if tables undergo continuous modification
174 * which would make it impossible to obtain an accurate result.
176 static final int RETRIES_BEFORE_LOCK = 2;
178 /* ---------------- Fields -------------- */
181 * Mask value for indexing into segments. The upper bits of a
182 * key's hash code are used to choose the segment.
184 final int segmentMask;
187 * Shift value for indexing within segments.
189 final int segmentShift;
192 * The segments, each of which is a specialized hash table.
194 final Segment<K,V>[] segments;
196 transient Set<K> keySet;
197 transient Set<Map.Entry<K,V>> entrySet;
198 transient Collection<V> values;
201 * ConcurrentHashMap list entry. Note that this is never exported
202 * out as a user-visible Map.Entry.
204 static final class HashEntry<K,V> {
208 volatile HashEntry<K,V> next;
210 HashEntry(int hash, K key, V value, HashEntry<K,V> next) {
218 * Sets next field with volatile write semantics. (See above
219 * about use of putOrderedObject.)
221 final void setNext(HashEntry<K,V> n) {
222 UNSAFE.putOrderedObject(this, nextOffset, n);
226 static final sun.misc.Unsafe UNSAFE;
227 static final long nextOffset;
230 UNSAFE = sun.misc.Unsafe.getUnsafe();
231 Class k = HashEntry.class;
232 nextOffset = UNSAFE.objectFieldOffset
233 (k.getDeclaredField("next"));
234 } catch (Exception e) {
241 * Gets the ith element of given table (if nonnull) with volatile
242 * read semantics. Note: This is manually integrated into a few
243 * performance-sensitive methods to reduce call overhead.
245 @SuppressWarnings("unchecked")
246 static final <K,V> HashEntry<K,V> entryAt(HashEntry<K,V>[] tab, int i) {
247 return (tab == null) ? null :
248 (HashEntry<K,V>) UNSAFE.getObjectVolatile
249 (tab, ((long)i << TSHIFT) + TBASE);
253 * Sets the ith element of given table, with volatile write
254 * semantics. (See above about use of putOrderedObject.)
256 static final <K,V> void setEntryAt(HashEntry<K,V>[] tab, int i,
258 UNSAFE.putOrderedObject(tab, ((long)i << TSHIFT) + TBASE, e);
262 * Applies a supplemental hash function to a given hashCode, which
263 * defends against poor quality hash functions. This is critical
264 * because ConcurrentHashMap uses power-of-two length hash tables,
265 * that otherwise encounter collisions for hashCodes that do not
266 * differ in lower or upper bits.
268 private static int hash(int h) {
269 // Spread bits to regularize both segment and index locations,
270 // using variant of single-word Wang/Jenkins hash.
271 h += (h << 15) ^ 0xffffcd7d;
275 h += (h << 2) + (h << 14);
276 return h ^ (h >>> 16);
280 * Segments are specialized versions of hash tables. This
281 * subclasses from ReentrantLock opportunistically, just to
282 * simplify some locking and avoid separate construction.
284 static final class Segment<K,V> extends ReentrantLock implements Serializable {
286 * Segments maintain a table of entry lists that are always
287 * kept in a consistent state, so can be read (via volatile
288 * reads of segments and tables) without locking. This
289 * requires replicating nodes when necessary during table
290 * resizing, so the old lists can be traversed by readers
291 * still using old version of table.
293 * This class defines only mutative methods requiring locking.
294 * Except as noted, the methods of this class perform the
295 * per-segment versions of ConcurrentHashMap methods. (Other
296 * methods are integrated directly into ConcurrentHashMap
297 * methods.) These mutative methods use a form of controlled
298 * spinning on contention via methods scanAndLock and
299 * scanAndLockForPut. These intersperse tryLocks with
300 * traversals to locate nodes. The main benefit is to absorb
301 * cache misses (which are very common for hash tables) while
302 * obtaining locks so that traversal is faster once
303 * acquired. We do not actually use the found nodes since they
304 * must be re-acquired under lock anyway to ensure sequential
305 * consistency of updates (and in any case may be undetectably
306 * stale), but they will normally be much faster to re-locate.
307 * Also, scanAndLockForPut speculatively creates a fresh node
308 * to use in put if no node is found.
311 private static final long serialVersionUID = 2249069246763182397L;
314 * The maximum number of times to tryLock in a prescan before
315 * possibly blocking on acquire in preparation for a locked
316 * segment operation. On multiprocessors, using a bounded
317 * number of retries maintains cache acquired while locating
320 static final int MAX_SCAN_RETRIES =
321 Runtime.getRuntime().availableProcessors() > 1 ? 64 : 1;
324 * The per-segment table. Elements are accessed via
325 * entryAt/setEntryAt providing volatile semantics.
327 transient volatile HashEntry<K,V>[] table;
330 * The number of elements. Accessed only either within locks
331 * or among other volatile reads that maintain visibility.
336 * The total number of mutative operations in this segment.
337 * Even though this may overflows 32 bits, it provides
338 * sufficient accuracy for stability checks in CHM isEmpty()
339 * and size() methods. Accessed only either within locks or
340 * among other volatile reads that maintain visibility.
342 transient int modCount;
345 * The table is rehashed when its size exceeds this threshold.
346 * (The value of this field is always <tt>(int)(capacity *
349 transient int threshold;
352 * The load factor for the hash table. Even though this value
353 * is same for all segments, it is replicated to avoid needing
354 * links to outer object.
357 final float loadFactor;
359 Segment(float lf, int threshold, HashEntry<K,V>[] tab) {
360 this.loadFactor = lf;
361 this.threshold = threshold;
365 final V put(K key, int hash, V value, boolean onlyIfAbsent) {
366 HashEntry<K,V> node = tryLock() ? null :
367 scanAndLockForPut(key, hash, value);
370 HashEntry<K,V>[] tab = table;
371 int index = (tab.length - 1) & hash;
372 HashEntry<K,V> first = entryAt(tab, index);
373 for (HashEntry<K,V> e = first;;) {
376 if ((k = e.key) == key ||
377 (e.hash == hash && key.equals(k))) {
391 node = new HashEntry<K,V>(hash, key, value, first);
393 if (c > threshold && tab.length < MAXIMUM_CAPACITY)
396 setEntryAt(tab, index, node);
410 * Doubles size of table and repacks entries, also adding the
411 * given node to new table
413 @SuppressWarnings("unchecked")
414 private void rehash(HashEntry<K,V> node) {
416 * Reclassify nodes in each list to new table. Because we
417 * are using power-of-two expansion, the elements from
418 * each bin must either stay at same index, or move with a
419 * power of two offset. We eliminate unnecessary node
420 * creation by catching cases where old nodes can be
421 * reused because their next fields won't change.
422 * Statistically, at the default threshold, only about
423 * one-sixth of them need cloning when a table
424 * doubles. The nodes they replace will be garbage
425 * collectable as soon as they are no longer referenced by
426 * any reader thread that may be in the midst of
427 * concurrently traversing table. Entry accesses use plain
428 * array indexing because they are followed by volatile
431 HashEntry<K,V>[] oldTable = table;
432 int oldCapacity = oldTable.length;
433 int newCapacity = oldCapacity << 1;
434 threshold = (int)(newCapacity * loadFactor);
435 HashEntry<K,V>[] newTable =
436 (HashEntry<K,V>[]) new HashEntry[newCapacity];
437 int sizeMask = newCapacity - 1;
438 for (int i = 0; i < oldCapacity ; i++) {
439 HashEntry<K,V> e = oldTable[i];
441 HashEntry<K,V> next = e.next;
442 int idx = e.hash & sizeMask;
443 if (next == null) // Single node on list
445 else { // Reuse consecutive sequence at same slot
446 HashEntry<K,V> lastRun = e;
448 for (HashEntry<K,V> last = next;
451 int k = last.hash & sizeMask;
457 newTable[lastIdx] = lastRun;
458 // Clone remaining nodes
459 for (HashEntry<K,V> p = e; p != lastRun; p = p.next) {
462 int k = h & sizeMask;
463 HashEntry<K,V> n = newTable[k];
464 newTable[k] = new HashEntry<K,V>(h, p.key, v, n);
469 int nodeIndex = node.hash & sizeMask; // add the new node
470 node.setNext(newTable[nodeIndex]);
471 newTable[nodeIndex] = node;
476 * Scans for a node containing given key while trying to
477 * acquire lock, creating and returning one if not found. Upon
478 * return, guarantees that lock is held. UNlike in most
479 * methods, calls to method equals are not screened: Since
480 * traversal speed doesn't matter, we might as well help warm
481 * up the associated code and accesses as well.
483 * @return a new node if key not found, else null
485 private HashEntry<K,V> scanAndLockForPut(K key, int hash, V value) {
486 HashEntry<K,V> first = entryForHash(this, hash);
487 HashEntry<K,V> e = first;
488 HashEntry<K,V> node = null;
489 int retries = -1; // negative while locating node
491 HashEntry<K,V> f; // to recheck first below
494 if (node == null) // speculatively create node
495 node = new HashEntry<K,V>(hash, key, value, null);
498 else if (key.equals(e.key))
503 else if (++retries > MAX_SCAN_RETRIES) {
507 else if ((retries & 1) == 0 &&
508 (f = entryForHash(this, hash)) != first) {
509 e = first = f; // re-traverse if entry changed
517 * Scans for a node containing the given key while trying to
518 * acquire lock for a remove or replace operation. Upon
519 * return, guarantees that lock is held. Note that we must
520 * lock even if the key is not found, to ensure sequential
521 * consistency of updates.
523 private void scanAndLock(Object key, int hash) {
524 // similar to but simpler than scanAndLockForPut
525 HashEntry<K,V> first = entryForHash(this, hash);
526 HashEntry<K,V> e = first;
531 if (e == null || key.equals(e.key))
536 else if (++retries > MAX_SCAN_RETRIES) {
540 else if ((retries & 1) == 0 &&
541 (f = entryForHash(this, hash)) != first) {
549 * Remove; match on key only if value null, else match both.
551 final V remove(Object key, int hash, Object value) {
553 scanAndLock(key, hash);
556 HashEntry<K,V>[] tab = table;
557 int index = (tab.length - 1) & hash;
558 HashEntry<K,V> e = entryAt(tab, index);
559 HashEntry<K,V> pred = null;
562 HashEntry<K,V> next = e.next;
563 if ((k = e.key) == key ||
564 (e.hash == hash && key.equals(k))) {
566 if (value == null || value == v || value.equals(v)) {
568 setEntryAt(tab, index, next);
586 final boolean replace(K key, int hash, V oldValue, V newValue) {
588 scanAndLock(key, hash);
589 boolean replaced = false;
592 for (e = entryForHash(this, hash); e != null; e = e.next) {
594 if ((k = e.key) == key ||
595 (e.hash == hash && key.equals(k))) {
596 if (oldValue.equals(e.value)) {
610 final V replace(K key, int hash, V value) {
612 scanAndLock(key, hash);
616 for (e = entryForHash(this, hash); e != null; e = e.next) {
618 if ((k = e.key) == key ||
619 (e.hash == hash && key.equals(k))) {
635 HashEntry<K,V>[] tab = table;
636 for (int i = 0; i < tab.length ; i++)
637 setEntryAt(tab, i, null);
646 // Accessing segments
649 * Gets the jth element of given segment array (if nonnull) with
650 * volatile element access semantics via Unsafe. (The null check
651 * can trigger harmlessly only during deserialization.) Note:
652 * because each element of segments array is set only once (using
653 * fully ordered writes), some performance-sensitive methods rely
654 * on this method only as a recheck upon null reads.
656 @SuppressWarnings("unchecked")
657 static final <K,V> Segment<K,V> segmentAt(Segment<K,V>[] ss, int j) {
658 long u = (j << SSHIFT) + SBASE;
659 return ss == null ? null :
660 (Segment<K,V>) UNSAFE.getObjectVolatile(ss, u);
664 * Returns the segment for the given index, creating it and
665 * recording in segment table (via CAS) if not already present.
668 * @return the segment
670 @SuppressWarnings("unchecked")
671 private Segment<K,V> ensureSegment(int k) {
672 final Segment<K,V>[] ss = this.segments;
673 long u = (k << SSHIFT) + SBASE; // raw offset
675 if ((seg = (Segment<K,V>)UNSAFE.getObjectVolatile(ss, u)) == null) {
676 Segment<K,V> proto = ss[0]; // use segment 0 as prototype
677 int cap = proto.table.length;
678 float lf = proto.loadFactor;
679 int threshold = (int)(cap * lf);
680 HashEntry<K,V>[] tab = (HashEntry<K,V>[])new HashEntry[cap];
681 if ((seg = (Segment<K,V>)UNSAFE.getObjectVolatile(ss, u))
682 == null) { // recheck
683 Segment<K,V> s = new Segment<K,V>(lf, threshold, tab);
684 while ((seg = (Segment<K,V>)UNSAFE.getObjectVolatile(ss, u))
686 if (UNSAFE.compareAndSwapObject(ss, u, null, seg = s))
694 // Hash-based segment and entry accesses
697 * Get the segment for the given hash
699 @SuppressWarnings("unchecked")
700 private Segment<K,V> segmentForHash(int h) {
701 long u = (((h >>> segmentShift) & segmentMask) << SSHIFT) + SBASE;
702 return (Segment<K,V>) UNSAFE.getObjectVolatile(segments, u);
706 * Gets the table entry for the given segment and hash
708 @SuppressWarnings("unchecked")
709 static final <K,V> HashEntry<K,V> entryForHash(Segment<K,V> seg, int h) {
710 HashEntry<K,V>[] tab;
711 return (seg == null || (tab = seg.table) == null) ? null :
712 (HashEntry<K,V>) UNSAFE.getObjectVolatile
713 (tab, ((long)(((tab.length - 1) & h)) << TSHIFT) + TBASE);
716 /* ---------------- Public operations -------------- */
719 * Creates a new, empty map with the specified initial
720 * capacity, load factor and concurrency level.
722 * @param initialCapacity the initial capacity. The implementation
723 * performs internal sizing to accommodate this many elements.
724 * @param loadFactor the load factor threshold, used to control resizing.
725 * Resizing may be performed when the average number of elements per
726 * bin exceeds this threshold.
727 * @param concurrencyLevel the estimated number of concurrently
728 * updating threads. The implementation performs internal sizing
729 * to try to accommodate this many threads.
730 * @throws IllegalArgumentException if the initial capacity is
731 * negative or the load factor or concurrencyLevel are
734 @SuppressWarnings("unchecked")
735 public ConcurrentHashMap(int initialCapacity,
736 float loadFactor, int concurrencyLevel) {
737 if (!(loadFactor > 0) || initialCapacity < 0 || concurrencyLevel <= 0)
738 throw new IllegalArgumentException();
739 if (concurrencyLevel > MAX_SEGMENTS)
740 concurrencyLevel = MAX_SEGMENTS;
741 // Find power-of-two sizes best matching arguments
744 while (ssize < concurrencyLevel) {
748 this.segmentShift = 32 - sshift;
749 this.segmentMask = ssize - 1;
750 if (initialCapacity > MAXIMUM_CAPACITY)
751 initialCapacity = MAXIMUM_CAPACITY;
752 int c = initialCapacity / ssize;
753 if (c * ssize < initialCapacity)
755 int cap = MIN_SEGMENT_TABLE_CAPACITY;
758 // create segments and segments[0]
760 new Segment<K,V>(loadFactor, (int)(cap * loadFactor),
761 (HashEntry<K,V>[])new HashEntry[cap]);
762 Segment<K,V>[] ss = (Segment<K,V>[])new Segment[ssize];
763 UNSAFE.putOrderedObject(ss, SBASE, s0); // ordered write of segments[0]
768 * Creates a new, empty map with the specified initial capacity
769 * and load factor and with the default concurrencyLevel (16).
771 * @param initialCapacity The implementation performs internal
772 * sizing to accommodate this many elements.
773 * @param loadFactor the load factor threshold, used to control resizing.
774 * Resizing may be performed when the average number of elements per
775 * bin exceeds this threshold.
776 * @throws IllegalArgumentException if the initial capacity of
777 * elements is negative or the load factor is nonpositive
781 public ConcurrentHashMap(int initialCapacity, float loadFactor) {
782 this(initialCapacity, loadFactor, DEFAULT_CONCURRENCY_LEVEL);
786 * Creates a new, empty map with the specified initial capacity,
787 * and with default load factor (0.75) and concurrencyLevel (16).
789 * @param initialCapacity the initial capacity. The implementation
790 * performs internal sizing to accommodate this many elements.
791 * @throws IllegalArgumentException if the initial capacity of
792 * elements is negative.
794 public ConcurrentHashMap(int initialCapacity) {
795 this(initialCapacity, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
799 * Creates a new, empty map with a default initial capacity (16),
800 * load factor (0.75) and concurrencyLevel (16).
802 public ConcurrentHashMap() {
803 this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
807 * Creates a new map with the same mappings as the given map.
808 * The map is created with a capacity of 1.5 times the number
809 * of mappings in the given map or 16 (whichever is greater),
810 * and a default load factor (0.75) and concurrencyLevel (16).
814 public ConcurrentHashMap(Map<? extends K, ? extends V> m) {
815 this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
816 DEFAULT_INITIAL_CAPACITY),
817 DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
822 * Returns <tt>true</tt> if this map contains no key-value mappings.
824 * @return <tt>true</tt> if this map contains no key-value mappings
826 public boolean isEmpty() {
828 * Sum per-segment modCounts to avoid mis-reporting when
829 * elements are concurrently added and removed in one segment
830 * while checking another, in which case the table was never
831 * actually empty at any point. (The sum ensures accuracy up
832 * through at least 1<<31 per-segment modifications before
833 * recheck.) Methods size() and containsValue() use similar
834 * constructions for stability checks.
837 final Segment<K,V>[] segments = this.segments;
838 for (int j = 0; j < segments.length; ++j) {
839 Segment<K,V> seg = segmentAt(segments, j);
846 if (sum != 0L) { // recheck unless no modifications
847 for (int j = 0; j < segments.length; ++j) {
848 Segment<K,V> seg = segmentAt(segments, j);
862 * Returns the number of key-value mappings in this map. If the
863 * map contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
864 * <tt>Integer.MAX_VALUE</tt>.
866 * @return the number of key-value mappings in this map
869 // Try a few times to get accurate count. On failure due to
870 // continuous async changes in table, resort to locking.
871 final Segment<K,V>[] segments = this.segments;
873 boolean overflow; // true if size overflows 32 bits
874 long sum; // sum of modCounts
875 long last = 0L; // previous sum
876 int retries = -1; // first iteration isn't retry
879 if (retries++ == RETRIES_BEFORE_LOCK) {
880 for (int j = 0; j < segments.length; ++j)
881 ensureSegment(j).lock(); // force creation
886 for (int j = 0; j < segments.length; ++j) {
887 Segment<K,V> seg = segmentAt(segments, j);
891 if (c < 0 || (size += c) < 0)
900 if (retries > RETRIES_BEFORE_LOCK) {
901 for (int j = 0; j < segments.length; ++j)
902 segmentAt(segments, j).unlock();
905 return overflow ? Integer.MAX_VALUE : size;
909 * Returns the value to which the specified key is mapped,
910 * or {@code null} if this map contains no mapping for the key.
912 * <p>More formally, if this map contains a mapping from a key
913 * {@code k} to a value {@code v} such that {@code key.equals(k)},
914 * then this method returns {@code v}; otherwise it returns
915 * {@code null}. (There can be at most one such mapping.)
917 * @throws NullPointerException if the specified key is null
919 public V get(Object key) {
920 Segment<K,V> s; // manually integrate access methods to reduce overhead
921 HashEntry<K,V>[] tab;
922 int h = hash(key.hashCode());
923 long u = (((h >>> segmentShift) & segmentMask) << SSHIFT) + SBASE;
924 if ((s = (Segment<K,V>)UNSAFE.getObjectVolatile(segments, u)) != null &&
925 (tab = s.table) != null) {
926 for (HashEntry<K,V> e = (HashEntry<K,V>) UNSAFE.getObjectVolatile
927 (tab, ((long)(((tab.length - 1) & h)) << TSHIFT) + TBASE);
928 e != null; e = e.next) {
930 if ((k = e.key) == key || (e.hash == h && key.equals(k)))
938 * Tests if the specified object is a key in this table.
940 * @param key possible key
941 * @return <tt>true</tt> if and only if the specified object
942 * is a key in this table, as determined by the
943 * <tt>equals</tt> method; <tt>false</tt> otherwise.
944 * @throws NullPointerException if the specified key is null
946 @SuppressWarnings("unchecked")
947 public boolean containsKey(Object key) {
948 Segment<K,V> s; // same as get() except no need for volatile value read
949 HashEntry<K,V>[] tab;
950 int h = hash(key.hashCode());
951 long u = (((h >>> segmentShift) & segmentMask) << SSHIFT) + SBASE;
952 if ((s = (Segment<K,V>)UNSAFE.getObjectVolatile(segments, u)) != null &&
953 (tab = s.table) != null) {
954 for (HashEntry<K,V> e = (HashEntry<K,V>) UNSAFE.getObjectVolatile
955 (tab, ((long)(((tab.length - 1) & h)) << TSHIFT) + TBASE);
956 e != null; e = e.next) {
958 if ((k = e.key) == key || (e.hash == h && key.equals(k)))
966 * Returns <tt>true</tt> if this map maps one or more keys to the
967 * specified value. Note: This method requires a full internal
968 * traversal of the hash table, and so is much slower than
969 * method <tt>containsKey</tt>.
971 * @param value value whose presence in this map is to be tested
972 * @return <tt>true</tt> if this map maps one or more keys to the
974 * @throws NullPointerException if the specified value is null
976 public boolean containsValue(Object value) {
977 // Same idea as size()
979 throw new NullPointerException();
980 final Segment<K,V>[] segments = this.segments;
981 boolean found = false;
986 if (retries++ == RETRIES_BEFORE_LOCK) {
987 for (int j = 0; j < segments.length; ++j)
988 ensureSegment(j).lock(); // force creation
992 for (int j = 0; j < segments.length; ++j) {
993 HashEntry<K,V>[] tab;
994 Segment<K,V> seg = segmentAt(segments, j);
995 if (seg != null && (tab = seg.table) != null) {
996 for (int i = 0 ; i < tab.length; i++) {
998 for (e = entryAt(tab, i); e != null; e = e.next) {
1000 if (v != null && value.equals(v)) {
1006 sum += seg.modCount;
1009 if (retries > 0 && sum == last)
1014 if (retries > RETRIES_BEFORE_LOCK) {
1015 for (int j = 0; j < segments.length; ++j)
1016 segmentAt(segments, j).unlock();
1023 * Legacy method testing if some key maps into the specified value
1024 * in this table. This method is identical in functionality to
1025 * {@link #containsValue}, and exists solely to ensure
1026 * full compatibility with class {@link java.util.Hashtable},
1027 * which supported this method prior to introduction of the
1028 * Java Collections framework.
1030 * @param value a value to search for
1031 * @return <tt>true</tt> if and only if some key maps to the
1032 * <tt>value</tt> argument in this table as
1033 * determined by the <tt>equals</tt> method;
1034 * <tt>false</tt> otherwise
1035 * @throws NullPointerException if the specified value is null
1037 public boolean contains(Object value) {
1038 return containsValue(value);
1042 * Maps the specified key to the specified value in this table.
1043 * Neither the key nor the value can be null.
1045 * <p> The value can be retrieved by calling the <tt>get</tt> method
1046 * with a key that is equal to the original key.
1048 * @param key key with which the specified value is to be associated
1049 * @param value value to be associated with the specified key
1050 * @return the previous value associated with <tt>key</tt>, or
1051 * <tt>null</tt> if there was no mapping for <tt>key</tt>
1052 * @throws NullPointerException if the specified key or value is null
1054 @SuppressWarnings("unchecked")
1055 public V put(K key, V value) {
1058 throw new NullPointerException();
1059 int hash = hash(key.hashCode());
1060 int j = (hash >>> segmentShift) & segmentMask;
1061 if ((s = (Segment<K,V>)UNSAFE.getObject // nonvolatile; recheck
1062 (segments, (j << SSHIFT) + SBASE)) == null) // in ensureSegment
1063 s = ensureSegment(j);
1064 return s.put(key, hash, value, false);
1070 * @return the previous value associated with the specified key,
1071 * or <tt>null</tt> if there was no mapping for the key
1072 * @throws NullPointerException if the specified key or value is null
1074 @SuppressWarnings("unchecked")
1075 public V putIfAbsent(K key, V value) {
1078 throw new NullPointerException();
1079 int hash = hash(key.hashCode());
1080 int j = (hash >>> segmentShift) & segmentMask;
1081 if ((s = (Segment<K,V>)UNSAFE.getObject
1082 (segments, (j << SSHIFT) + SBASE)) == null)
1083 s = ensureSegment(j);
1084 return s.put(key, hash, value, true);
1088 * Copies all of the mappings from the specified map to this one.
1089 * These mappings replace any mappings that this map had for any of the
1090 * keys currently in the specified map.
1092 * @param m mappings to be stored in this map
1094 public void putAll(Map<? extends K, ? extends V> m) {
1095 for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
1096 put(e.getKey(), e.getValue());
1100 * Removes the key (and its corresponding value) from this map.
1101 * This method does nothing if the key is not in the map.
1103 * @param key the key that needs to be removed
1104 * @return the previous value associated with <tt>key</tt>, or
1105 * <tt>null</tt> if there was no mapping for <tt>key</tt>
1106 * @throws NullPointerException if the specified key is null
1108 public V remove(Object key) {
1109 int hash = hash(key.hashCode());
1110 Segment<K,V> s = segmentForHash(hash);
1111 return s == null ? null : s.remove(key, hash, null);
1117 * @throws NullPointerException if the specified key is null
1119 public boolean remove(Object key, Object value) {
1120 int hash = hash(key.hashCode());
1122 return value != null && (s = segmentForHash(hash)) != null &&
1123 s.remove(key, hash, value) != null;
1129 * @throws NullPointerException if any of the arguments are null
1131 public boolean replace(K key, V oldValue, V newValue) {
1132 int hash = hash(key.hashCode());
1133 if (oldValue == null || newValue == null)
1134 throw new NullPointerException();
1135 Segment<K,V> s = segmentForHash(hash);
1136 return s != null && s.replace(key, hash, oldValue, newValue);
1142 * @return the previous value associated with the specified key,
1143 * or <tt>null</tt> if there was no mapping for the key
1144 * @throws NullPointerException if the specified key or value is null
1146 public V replace(K key, V value) {
1147 int hash = hash(key.hashCode());
1149 throw new NullPointerException();
1150 Segment<K,V> s = segmentForHash(hash);
1151 return s == null ? null : s.replace(key, hash, value);
1155 * Removes all of the mappings from this map.
1157 public void clear() {
1158 final Segment<K,V>[] segments = this.segments;
1159 for (int j = 0; j < segments.length; ++j) {
1160 Segment<K,V> s = segmentAt(segments, j);
1167 * Returns a {@link Set} view of the keys contained in this map.
1168 * The set is backed by the map, so changes to the map are
1169 * reflected in the set, and vice-versa. The set supports element
1170 * removal, which removes the corresponding mapping from this map,
1171 * via the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
1172 * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
1173 * operations. It does not support the <tt>add</tt> or
1174 * <tt>addAll</tt> operations.
1176 * <p>The view's <tt>iterator</tt> is a "weakly consistent" iterator
1177 * that will never throw {@link ConcurrentModificationException},
1178 * and guarantees to traverse elements as they existed upon
1179 * construction of the iterator, and may (but is not guaranteed to)
1180 * reflect any modifications subsequent to construction.
1182 public Set<K> keySet() {
1184 return (ks != null) ? ks : (keySet = new KeySet());
1188 * Returns a {@link Collection} view of the values contained in this map.
1189 * The collection is backed by the map, so changes to the map are
1190 * reflected in the collection, and vice-versa. The collection
1191 * supports element removal, which removes the corresponding
1192 * mapping from this map, via the <tt>Iterator.remove</tt>,
1193 * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
1194 * <tt>retainAll</tt>, and <tt>clear</tt> operations. It does not
1195 * support the <tt>add</tt> or <tt>addAll</tt> operations.
1197 * <p>The view's <tt>iterator</tt> is a "weakly consistent" iterator
1198 * that will never throw {@link ConcurrentModificationException},
1199 * and guarantees to traverse elements as they existed upon
1200 * construction of the iterator, and may (but is not guaranteed to)
1201 * reflect any modifications subsequent to construction.
1203 public Collection<V> values() {
1204 Collection<V> vs = values;
1205 return (vs != null) ? vs : (values = new Values());
1209 * Returns a {@link Set} view of the mappings contained in this map.
1210 * The set is backed by the map, so changes to the map are
1211 * reflected in the set, and vice-versa. The set supports element
1212 * removal, which removes the corresponding mapping from the map,
1213 * via the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
1214 * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
1215 * operations. It does not support the <tt>add</tt> or
1216 * <tt>addAll</tt> operations.
1218 * <p>The view's <tt>iterator</tt> is a "weakly consistent" iterator
1219 * that will never throw {@link ConcurrentModificationException},
1220 * and guarantees to traverse elements as they existed upon
1221 * construction of the iterator, and may (but is not guaranteed to)
1222 * reflect any modifications subsequent to construction.
1224 public Set<Map.Entry<K,V>> entrySet() {
1225 Set<Map.Entry<K,V>> es = entrySet;
1226 return (es != null) ? es : (entrySet = new EntrySet());
1230 * Returns an enumeration of the keys in this table.
1232 * @return an enumeration of the keys in this table
1235 public Enumeration<K> keys() {
1236 return new KeyIterator();
1240 * Returns an enumeration of the values in this table.
1242 * @return an enumeration of the values in this table
1245 public Enumeration<V> elements() {
1246 return new ValueIterator();
1249 /* ---------------- Iterator Support -------------- */
1251 abstract class HashIterator {
1252 int nextSegmentIndex;
1254 HashEntry<K,V>[] currentTable;
1255 HashEntry<K, V> nextEntry;
1256 HashEntry<K, V> lastReturned;
1259 nextSegmentIndex = segments.length - 1;
1260 nextTableIndex = -1;
1265 * Set nextEntry to first node of next non-empty table
1266 * (in backwards order, to simplify checks).
1268 final void advance() {
1270 if (nextTableIndex >= 0) {
1271 if ((nextEntry = entryAt(currentTable,
1272 nextTableIndex--)) != null)
1275 else if (nextSegmentIndex >= 0) {
1276 Segment<K,V> seg = segmentAt(segments, nextSegmentIndex--);
1277 if (seg != null && (currentTable = seg.table) != null)
1278 nextTableIndex = currentTable.length - 1;
1285 final HashEntry<K,V> nextEntry() {
1286 HashEntry<K,V> e = nextEntry;
1288 throw new NoSuchElementException();
1289 lastReturned = e; // cannot assign until after null check
1290 if ((nextEntry = e.next) == null)
1295 public final boolean hasNext() { return nextEntry != null; }
1296 public final boolean hasMoreElements() { return nextEntry != null; }
1298 public final void remove() {
1299 if (lastReturned == null)
1300 throw new IllegalStateException();
1301 ConcurrentHashMap.this.remove(lastReturned.key);
1302 lastReturned = null;
1306 final class KeyIterator
1307 extends HashIterator
1308 implements Iterator<K>, Enumeration<K>
1310 public final K next() { return super.nextEntry().key; }
1311 public final K nextElement() { return super.nextEntry().key; }
1314 final class ValueIterator
1315 extends HashIterator
1316 implements Iterator<V>, Enumeration<V>
1318 public final V next() { return super.nextEntry().value; }
1319 public final V nextElement() { return super.nextEntry().value; }
1323 * Custom Entry class used by EntryIterator.next(), that relays
1324 * setValue changes to the underlying map.
1326 final class WriteThroughEntry
1327 extends AbstractMap.SimpleEntry<K,V>
1329 WriteThroughEntry(K k, V v) {
1334 * Set our entry's value and write through to the map. The
1335 * value to return is somewhat arbitrary here. Since a
1336 * WriteThroughEntry does not necessarily track asynchronous
1337 * changes, the most recent "previous" value could be
1338 * different from what we return (or could even have been
1339 * removed in which case the put will re-establish). We do not
1340 * and cannot guarantee more.
1342 public V setValue(V value) {
1343 if (value == null) throw new NullPointerException();
1344 V v = super.setValue(value);
1345 ConcurrentHashMap.this.put(getKey(), value);
1350 final class EntryIterator
1351 extends HashIterator
1352 implements Iterator<Entry<K,V>>
1354 public Map.Entry<K,V> next() {
1355 HashEntry<K,V> e = super.nextEntry();
1356 return new WriteThroughEntry(e.key, e.value);
1360 final class KeySet extends AbstractSet<K> {
1361 public Iterator<K> iterator() {
1362 return new KeyIterator();
1365 return ConcurrentHashMap.this.size();
1367 public boolean isEmpty() {
1368 return ConcurrentHashMap.this.isEmpty();
1370 public boolean contains(Object o) {
1371 return ConcurrentHashMap.this.containsKey(o);
1373 public boolean remove(Object o) {
1374 return ConcurrentHashMap.this.remove(o) != null;
1376 public void clear() {
1377 ConcurrentHashMap.this.clear();
1381 final class Values extends AbstractCollection<V> {
1382 public Iterator<V> iterator() {
1383 return new ValueIterator();
1386 return ConcurrentHashMap.this.size();
1388 public boolean isEmpty() {
1389 return ConcurrentHashMap.this.isEmpty();
1391 public boolean contains(Object o) {
1392 return ConcurrentHashMap.this.containsValue(o);
1394 public void clear() {
1395 ConcurrentHashMap.this.clear();
1399 final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
1400 public Iterator<Map.Entry<K,V>> iterator() {
1401 return new EntryIterator();
1403 public boolean contains(Object o) {
1404 if (!(o instanceof Map.Entry))
1406 Map.Entry<?,?> e = (Map.Entry<?,?>)o;
1407 V v = ConcurrentHashMap.this.get(e.getKey());
1408 return v != null && v.equals(e.getValue());
1410 public boolean remove(Object o) {
1411 if (!(o instanceof Map.Entry))
1413 Map.Entry<?,?> e = (Map.Entry<?,?>)o;
1414 return ConcurrentHashMap.this.remove(e.getKey(), e.getValue());
1417 return ConcurrentHashMap.this.size();
1419 public boolean isEmpty() {
1420 return ConcurrentHashMap.this.isEmpty();
1422 public void clear() {
1423 ConcurrentHashMap.this.clear();
1427 /* ---------------- Serialization Support -------------- */
1430 * Save the state of the <tt>ConcurrentHashMap</tt> instance to a
1431 * stream (i.e., serialize it).
1432 * @param s the stream
1434 * the key (Object) and value (Object)
1435 * for each key-value mapping, followed by a null pair.
1436 * The key-value mappings are emitted in no particular order.
1438 private void writeObject(java.io.ObjectOutputStream s) throws IOException {
1439 // force all segments for serialization compatibility
1440 for (int k = 0; k < segments.length; ++k)
1442 s.defaultWriteObject();
1444 final Segment<K,V>[] segments = this.segments;
1445 for (int k = 0; k < segments.length; ++k) {
1446 Segment<K,V> seg = segmentAt(segments, k);
1449 HashEntry<K,V>[] tab = seg.table;
1450 for (int i = 0; i < tab.length; ++i) {
1452 for (e = entryAt(tab, i); e != null; e = e.next) {
1453 s.writeObject(e.key);
1454 s.writeObject(e.value);
1461 s.writeObject(null);
1462 s.writeObject(null);
1466 * Reconstitute the <tt>ConcurrentHashMap</tt> instance from a
1467 * stream (i.e., deserialize it).
1468 * @param s the stream
1470 @SuppressWarnings("unchecked")
1471 private void readObject(java.io.ObjectInputStream s)
1472 throws IOException, ClassNotFoundException {
1473 s.defaultReadObject();
1475 // Re-initialize segments to be minimally sized, and let grow.
1476 int cap = MIN_SEGMENT_TABLE_CAPACITY;
1477 final Segment<K,V>[] segments = this.segments;
1478 for (int k = 0; k < segments.length; ++k) {
1479 Segment<K,V> seg = segments[k];
1481 seg.threshold = (int)(cap * seg.loadFactor);
1482 seg.table = (HashEntry<K,V>[]) new HashEntry[cap];
1486 // Read the keys and values, and put the mappings in the table
1488 K key = (K) s.readObject();
1489 V value = (V) s.readObject();
1497 private static final sun.misc.Unsafe UNSAFE;
1498 private static final long SBASE;
1499 private static final int SSHIFT;
1500 private static final long TBASE;
1501 private static final int TSHIFT;
1506 UNSAFE = sun.misc.Unsafe.getUnsafe();
1507 Class tc = HashEntry[].class;
1508 Class sc = Segment[].class;
1509 TBASE = UNSAFE.arrayBaseOffset(tc);
1510 SBASE = UNSAFE.arrayBaseOffset(sc);
1511 ts = UNSAFE.arrayIndexScale(tc);
1512 ss = UNSAFE.arrayIndexScale(sc);
1513 } catch (Exception e) {
1516 if ((ss & (ss-1)) != 0 || (ts & (ts-1)) != 0)
1517 throw new Error("data type scale not a power of two");
1518 SSHIFT = 31 - Integer.numberOfLeadingZeros(ss);
1519 TSHIFT = 31 - Integer.numberOfLeadingZeros(ts);