ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166e/ConcurrentHashMapV8.java
Revision: 1.12
Committed: Tue Aug 30 18:31:54 2011 UTC (12 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.11: +1 -1 lines
Log Message:
whitespace

File Contents

# Content
1 /*
2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/publicdomain/zero/1.0/
5 */
6
7 package jsr166e;
8 import jsr166e.LongAdder;
9 import java.util.Map;
10 import java.util.Set;
11 import java.util.Collection;
12 import java.util.AbstractMap;
13 import java.util.AbstractSet;
14 import java.util.AbstractCollection;
15 import java.util.Hashtable;
16 import java.util.HashMap;
17 import java.util.Iterator;
18 import java.util.Enumeration;
19 import java.util.ConcurrentModificationException;
20 import java.util.NoSuchElementException;
21 import java.util.concurrent.ConcurrentMap;
22 import java.io.Serializable;
23
24 /**
25 * A hash table supporting full concurrency of retrievals and
26 * high expected concurrency for updates. This class obeys the
27 * same functional specification as {@link java.util.Hashtable}, and
28 * includes versions of methods corresponding to each method of
29 * {@code Hashtable}. However, even though all operations are
30 * thread-safe, retrieval operations do <em>not</em> entail locking,
31 * and there is <em>not</em> any support for locking the entire table
32 * in a way that prevents all access. This class is fully
33 * interoperable with {@code Hashtable} in programs that rely on its
34 * thread safety but not on its synchronization details.
35 *
36 * <p> Retrieval operations (including {@code get}) generally do not
37 * block, so may overlap with update operations (including {@code put}
38 * and {@code remove}). Retrievals reflect the results of the most
39 * recently <em>completed</em> update operations holding upon their
40 * onset. For aggregate operations such as {@code putAll} and {@code
41 * clear}, concurrent retrievals may reflect insertion or removal of
42 * only some entries. Similarly, Iterators and Enumerations return
43 * elements reflecting the state of the hash table at some point at or
44 * since the creation of the iterator/enumeration. They do
45 * <em>not</em> throw {@link ConcurrentModificationException}.
46 * However, iterators are designed to be used by only one thread at a
47 * time. Bear in mind that the results of aggregate status methods
48 * including {@code size}, {@code isEmpty}, and {@code containsValue}
49 * are typically useful only when a map is not undergoing concurrent
50 * updates in other threads. Otherwise the results of these methods
51 * reflect transient states that may be adequate for monitoring
52 * purposes, but not for program control.
53 *
54 * <p> Resizing this or any other kind of hash table is a relatively
55 * slow operation, so, when possible, it is a good idea to provide
56 * estimates of expected table sizes in constructors. Also, for
57 * compatibility with previous versions of this class, constructors
58 * may optionally specify an expected {@code concurrencyLevel} as an
59 * additional hint for internal sizing.
60 *
61 * <p>This class and its views and iterators implement all of the
62 * <em>optional</em> methods of the {@link Map} and {@link Iterator}
63 * interfaces.
64 *
65 * <p> Like {@link Hashtable} but unlike {@link HashMap}, this class
66 * does <em>not</em> allow {@code null} to be used as a key or value.
67 *
68 * <p>This class is a member of the
69 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
70 * Java Collections Framework</a>.
71 *
72 * <p><em>jsr166e note: This class is a candidate replacement for
73 * java.util.concurrent.ConcurrentHashMap.<em>
74 *
75 * @since 1.5
76 * @author Doug Lea
77 * @param <K> the type of keys maintained by this map
78 * @param <V> the type of mapped values
79 */
80 public class ConcurrentHashMapV8<K, V>
81 implements ConcurrentMap<K, V>, Serializable {
82 private static final long serialVersionUID = 7249069246763182397L;
83
84 /**
85 * A function computing a mapping from the given key to a value,
86 * or {@code null} if there is no mapping. This is a place-holder
87 * for an upcoming JDK8 interface.
88 */
89 public static interface MappingFunction<K, V> {
90 /**
91 * Returns a value for the given key, or null if there is no
92 * mapping. If this function throws an (unchecked) exception,
93 * the exception is rethrown to its caller, and no mapping is
94 * recorded. Because this function is invoked within
95 * atomicity control, the computation should be short and
96 * simple. The most common usage is to construct a new object
97 * serving as an initial mapped value.
98 *
99 * @param key the (non-null) key
100 * @return a value, or null if none
101 */
102 V map(K key);
103 }
104
105 /*
106 * Overview:
107 *
108 * The primary design goal of this hash table is to maintain
109 * concurrent readability (typically method get(), but also
110 * iterators and related methods) while minimizing update
111 * contention.
112 *
113 * Each key-value mapping is held in a Node. Because Node fields
114 * can contain special values, they are defined using plain Object
115 * types. Similarly in turn, all internal methods that use them
116 * work off Object types. All public generic-typed methods relay
117 * in/out of these internal methods, supplying casts as needed.
118 *
119 * The table is lazily initialized to a power-of-two size upon the
120 * first insertion. Each bin in the table contains a (typically
121 * short) list of Nodes. Table accesses require volatile/atomic
122 * reads, writes, and CASes. Because there is no other way to
123 * arrange this without adding further indirections, we use
124 * intrinsics (sun.misc.Unsafe) operations. The lists of nodes
125 * within bins are always accurately traversable under volatile
126 * reads, so long as lookups check hash code and non-nullness of
127 * key and value before checking key equality. (All valid hash
128 * codes are nonnegative. Negative values are reserved for special
129 * forwarding nodes; see below.)
130 *
131 * A bin may be locked during update (insert, delete, and replace)
132 * operations. We do not want to waste the space required to
133 * associate a distinct lock object with each bin, so instead use
134 * the first node of a bin list itself as a lock, using builtin
135 * "synchronized" locks. These save space and we can live with
136 * only plain block-structured lock/unlock operations. Using the
137 * first node of a list as a lock does not by itself suffice
138 * though: When a node is locked, any update must first validate
139 * that it is still the first node, and retry if not. (Because new
140 * nodes are always appended to lists, once a node is first in a
141 * bin, it remains first until deleted or the bin becomes
142 * invalidated.) However, update operations can and sometimes do
143 * still traverse the bin until the point of update, which helps
144 * reduce cache misses on retries. This is a converse of sorts to
145 * the lazy locking technique described by Herlihy & Shavit. If
146 * there is no existing node during a put operation, then one can
147 * be CAS'ed in (without need for lock except in computeIfAbsent);
148 * the CAS serves as validation. This is on average the most
149 * common case for put operations -- under random hash codes, the
150 * distribution of nodes in bins follows a Poisson distribution
151 * (see http://en.wikipedia.org/wiki/Poisson_distribution) with a
152 * parameter of 0.5 on average under the default loadFactor of
153 * 0.75. The expected number of locks covering different elements
154 * (i.e., bins with 2 or more nodes) is approximately 10% at
155 * steady state under default settings. Lock contention
156 * probability for two threads accessing arbitrary distinct
157 * elements is, roughly, 1 / (8 * #elements).
158 *
159 * The table is resized when occupancy exceeds a threshold. Only
160 * a single thread performs the resize (using field "resizing", to
161 * arrange exclusion), but the table otherwise remains usable for
162 * both reads and updates. Resizing proceeds by transferring bins,
163 * one by one, from the table to the next table. Upon transfer,
164 * the old table bin contains only a special forwarding node (with
165 * negative hash code ("MOVED")) that contains the next table as
166 * its key. On encountering a forwarding node, access and update
167 * operations restart, using the new table. To ensure concurrent
168 * readability of traversals, transfers must proceed from the last
169 * bin (table.length - 1) up towards the first. Any traversal
170 * starting from the first bin can then arrange to move to the new
171 * table for the rest of the traversal without revisiting nodes.
172 * This constrains bin transfers to a particular order, and so can
173 * block indefinitely waiting for the next lock, and other threads
174 * cannot help with the transfer. However, expected stalls are
175 * infrequent enough to not warrant the additional overhead and
176 * complexity of access and iteration schemes that could admit
177 * out-of-order or concurrent bin transfers.
178 *
179 * A similar traversal scheme (not yet implemented) can apply to
180 * partial traversals during partitioned aggregate operations.
181 * Also, read-only operations give up if ever forwarded to a null
182 * table, which provides support for shutdown-style clearing,
183 * which is also not currently implemented.
184 *
185 * The element count is maintained using a LongAdder, which avoids
186 * contention on updates but can encounter cache thrashing if read
187 * too frequently during concurrent updates. To avoid reading so
188 * often, resizing is normally attempted only upon adding to a bin
189 * already holding two or more nodes. Under the default threshold
190 * (0.75), and uniform hash distributions, the probability of this
191 * occurring at threshold is around 13%, meaning that only about 1
192 * in 8 puts check threshold (and after resizing, many fewer do
193 * so). But this approximation has high variance for small table
194 * sizes, so we check on any collision for sizes <= 64. Further,
195 * to increase the probability that a resize occurs soon enough, we
196 * offset the threshold (see THRESHOLD_OFFSET) by the expected
197 * number of puts between checks. This is currently set to 8, in
198 * accord with the default load factor. In practice, this is
199 * rarely overridden, and in any case is close enough to other
200 * plausible values not to waste dynamic probability computation
201 * for more precision.
202 */
203
204 /* ---------------- Constants -------------- */
205
206 /**
207 * The smallest allowed table capacity. Must be a power of 2, at
208 * least 2.
209 */
210 static final int MINIMUM_CAPACITY = 2;
211
212 /**
213 * The largest allowed table capacity. Must be a power of 2, at
214 * most 1<<30.
215 */
216 static final int MAXIMUM_CAPACITY = 1 << 30;
217
218 /**
219 * The default initial table capacity. Must be a power of 2, at
220 * least MINIMUM_CAPACITY and at most MAXIMUM_CAPACITY.
221 */
222 static final int DEFAULT_CAPACITY = 16;
223
224 /**
225 * The default load factor for this table, used when not otherwise
226 * specified in a constructor.
227 */
228 static final float DEFAULT_LOAD_FACTOR = 0.75f;
229
230 /**
231 * The default concurrency level for this table. Unused, but
232 * defined for compatibility with previous versions of this class.
233 */
234 static final int DEFAULT_CONCURRENCY_LEVEL = 16;
235
236 /**
237 * The count value to offset thresholds to compensate for checking
238 * for resizing only when inserting into bins with two or more
239 * elements. See above for explanation.
240 */
241 static final int THRESHOLD_OFFSET = 8;
242
243 /**
244 * Special node hash value indicating to use table in node.key
245 * Must be negative.
246 */
247 static final int MOVED = -1;
248
249 /* ---------------- Fields -------------- */
250
251 /**
252 * The array of bins. Lazily initialized upon first insertion.
253 * Size is always a power of two. Accessed directly by inner
254 * classes.
255 */
256 transient volatile Node[] table;
257
258 /** The counter maintaining number of elements. */
259 private transient final LongAdder counter;
260 /** Nonzero when table is being initialized or resized. Updated via CAS. */
261 private transient volatile int resizing;
262 /** The target load factor for the table. */
263 private transient float loadFactor;
264 /** The next element count value upon which to resize the table. */
265 private transient int threshold;
266 /** The initial capacity of the table. */
267 private transient int initCap;
268
269 // views
270 transient Set<K> keySet;
271 transient Set<Map.Entry<K,V>> entrySet;
272 transient Collection<V> values;
273
274 /** For serialization compatibility. Null unless serialized; see below */
275 Segment<K,V>[] segments;
276
277 /**
278 * Applies a supplemental hash function to a given hashCode, which
279 * defends against poor quality hash functions. The result must
280 * be non-negative, and for reasonable performance must have good
281 * avalanche properties; i.e., that each bit of the argument
282 * affects each bit (except sign bit) of the result.
283 */
284 private static final int spread(int h) {
285 // Apply base step of MurmurHash; see http://code.google.com/p/smhasher/
286 h ^= h >>> 16;
287 h *= 0x85ebca6b;
288 h ^= h >>> 13;
289 h *= 0xc2b2ae35;
290 return (h >>> 16) ^ (h & 0x7fffffff); // mask out sign bit
291 }
292
293 /**
294 * Key-value entry. Note that this is never exported out as a
295 * user-visible Map.Entry.
296 */
297 static final class Node {
298 final int hash;
299 final Object key;
300 volatile Object val;
301 volatile Node next;
302
303 Node(int hash, Object key, Object val, Node next) {
304 this.hash = hash;
305 this.key = key;
306 this.val = val;
307 this.next = next;
308 }
309 }
310
311 /*
312 * Volatile access methods are used for table elements as well as
313 * elements of in-progress next table while resizing. Uses in
314 * access and update methods are null checked by callers, and
315 * implicitly bounds-checked, relying on the invariants that tab
316 * arrays have non-zero size, and all indices are masked with
317 * (tab.length - 1) which is never negative and always less than
318 * length. The "relaxed" non-volatile forms are used only during
319 * table initialization. The only other usage is in
320 * HashIterator.advance, which performs explicit checks.
321 */
322
323 static final Node tabAt(Node[] tab, int i) { // used in HashIterator
324 return (Node)UNSAFE.getObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE);
325 }
326
327 private static final boolean casTabAt(Node[] tab, int i, Node c, Node v) {
328 return UNSAFE.compareAndSwapObject(tab, ((long)i<<ASHIFT)+ABASE, c, v);
329 }
330
331 private static final void setTabAt(Node[] tab, int i, Node v) {
332 UNSAFE.putObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE, v);
333 }
334
335 private static final Node relaxedTabAt(Node[] tab, int i) {
336 return (Node)UNSAFE.getObject(tab, ((long)i<<ASHIFT)+ABASE);
337 }
338
339 private static final void relaxedSetTabAt(Node[] tab, int i, Node v) {
340 UNSAFE.putObject(tab, ((long)i<<ASHIFT)+ABASE, v);
341 }
342
343 /* ---------------- Access and update operations -------------- */
344
345 /** Implementation for get and containsKey */
346 private final Object internalGet(Object k) {
347 int h = spread(k.hashCode());
348 Node[] tab = table;
349 retry: while (tab != null) {
350 Node e = tabAt(tab, (tab.length - 1) & h);
351 while (e != null) {
352 int eh = e.hash;
353 if (eh == h) {
354 Object ek = e.key, ev = e.val;
355 if (ev != null && ek != null && (k == ek || k.equals(ek)))
356 return ev;
357 }
358 else if (eh < 0) { // bin was moved during resize
359 tab = (Node[])e.key;
360 continue retry;
361 }
362 e = e.next;
363 }
364 break;
365 }
366 return null;
367 }
368
369
370 /** Implementation for put and putIfAbsent */
371 private final Object internalPut(Object k, Object v, boolean replace) {
372 int h = spread(k.hashCode());
373 Object oldVal = null; // the previous value or null if none
374 Node[] tab = table;
375 for (;;) {
376 Node e; int i;
377 if (tab == null)
378 tab = grow(0);
379 else if ((e = tabAt(tab, i = (tab.length - 1) & h)) == null) {
380 if (casTabAt(tab, i, null, new Node(h, k, v, null)))
381 break;
382 }
383 else if (e.hash < 0)
384 tab = (Node[])e.key;
385 else {
386 boolean validated = false;
387 boolean checkSize = false;
388 synchronized (e) {
389 if (tabAt(tab, i) == e) {
390 validated = true;
391 for (Node first = e;;) {
392 Object ek, ev;
393 if (e.hash == h &&
394 (ek = e.key) != null &&
395 (ev = e.val) != null &&
396 (k == ek || k.equals(ek))) {
397 oldVal = ev;
398 if (replace)
399 e.val = v;
400 break;
401 }
402 Node last = e;
403 if ((e = e.next) == null) {
404 last.next = new Node(h, k, v, null);
405 if (last != first || tab.length <= 64)
406 checkSize = true;
407 break;
408 }
409 }
410 }
411 }
412 if (validated) {
413 if (checkSize && tab.length < MAXIMUM_CAPACITY &&
414 resizing == 0 && counter.sum() >= threshold)
415 grow(0);
416 break;
417 }
418 }
419 }
420 if (oldVal == null)
421 counter.increment();
422 return oldVal;
423 }
424
425 /**
426 * Implementation for the four public remove/replace methods:
427 * Replaces node value with v, conditional upon match of cv if
428 * non-null. If resulting value is null, delete.
429 */
430 private final Object internalReplace(Object k, Object v, Object cv) {
431 int h = spread(k.hashCode());
432 Object oldVal = null;
433 Node e; int i;
434 Node[] tab = table;
435 while (tab != null &&
436 (e = tabAt(tab, i = (tab.length - 1) & h)) != null) {
437 if (e.hash < 0)
438 tab = (Node[])e.key;
439 else {
440 boolean validated = false;
441 boolean deleted = false;
442 synchronized (e) {
443 if (tabAt(tab, i) == e) {
444 validated = true;
445 Node pred = null;
446 do {
447 Object ek, ev;
448 if (e.hash == h &&
449 (ek = e.key) != null &&
450 (ev = e.val) != null &&
451 (k == ek || k.equals(ek))) {
452 if (cv == null || cv == ev || cv.equals(ev)) {
453 oldVal = ev;
454 if ((e.val = v) == null) {
455 deleted = true;
456 Node en = e.next;
457 if (pred != null)
458 pred.next = en;
459 else
460 setTabAt(tab, i, en);
461 }
462 }
463 break;
464 }
465 pred = e;
466 } while ((e = e.next) != null);
467 }
468 }
469 if (validated) {
470 if (deleted)
471 counter.decrement();
472 break;
473 }
474 }
475 }
476 return oldVal;
477 }
478
479 /** Implementation for computeIfAbsent and compute */
480 @SuppressWarnings("unchecked")
481 private final V internalCompute(K k,
482 MappingFunction<? super K, ? extends V> f,
483 boolean replace) {
484 int h = spread(k.hashCode());
485 V val = null;
486 boolean added = false;
487 Node[] tab = table;
488 for (;;) {
489 Node e; int i;
490 if (tab == null)
491 tab = grow(0);
492 else if ((e = tabAt(tab, i = (tab.length - 1) & h)) == null) {
493 Node node = new Node(h, k, null, null);
494 boolean validated = false;
495 synchronized (node) {
496 if (casTabAt(tab, i, null, node)) {
497 validated = true;
498 try {
499 val = f.map(k);
500 if (val != null) {
501 node.val = val;
502 added = true;
503 }
504 } finally {
505 if (!added)
506 setTabAt(tab, i, null);
507 }
508 }
509 }
510 if (validated)
511 break;
512 }
513 else if (e.hash < 0)
514 tab = (Node[])e.key;
515 else if (Thread.holdsLock(e))
516 throw new IllegalStateException("Recursive map computation");
517 else {
518 boolean validated = false;
519 boolean checkSize = false;
520 synchronized (e) {
521 if (tabAt(tab, i) == e) {
522 validated = true;
523 for (Node first = e;;) {
524 Object ek, ev, fv;
525 if (e.hash == h &&
526 (ek = e.key) != null &&
527 (ev = e.val) != null &&
528 (k == ek || k.equals(ek))) {
529 if (replace && (fv = f.map(k)) != null)
530 ev = e.val = fv;
531 val = (V)ev;
532 break;
533 }
534 Node last = e;
535 if ((e = e.next) == null) {
536 if ((val = f.map(k)) != null) {
537 last.next = new Node(h, k, val, null);
538 added = true;
539 if (last != first || tab.length <= 64)
540 checkSize = true;
541 }
542 break;
543 }
544 }
545 }
546 }
547 if (validated) {
548 if (checkSize && tab.length < MAXIMUM_CAPACITY &&
549 resizing == 0 && counter.sum() >= threshold)
550 grow(0);
551 break;
552 }
553 }
554 }
555 if (added)
556 counter.increment();
557 return val;
558 }
559
560 /*
561 * Reclassifies nodes in each bin to new table. Because we are
562 * using power-of-two expansion, the elements from each bin must
563 * either stay at same index, or move with a power of two
564 * offset. We eliminate unnecessary node creation by catching
565 * cases where old nodes can be reused because their next fields
566 * won't change. Statistically, at the default threshold, only
567 * about one-sixth of them need cloning when a table doubles. The
568 * nodes they replace will be garbage collectable as soon as they
569 * are no longer referenced by any reader thread that may be in
570 * the midst of concurrently traversing table.
571 *
572 * Transfers are done from the bottom up to preserve iterator
573 * traversability. On each step, the old bin is locked,
574 * moved/copied, and then replaced with a forwarding node.
575 */
576 private static final void transfer(Node[] tab, Node[] nextTab) {
577 int n = tab.length;
578 int mask = nextTab.length - 1;
579 Node fwd = new Node(MOVED, nextTab, null, null);
580 for (int i = n - 1; i >= 0; --i) {
581 for (Node e;;) {
582 if ((e = tabAt(tab, i)) == null) {
583 if (casTabAt(tab, i, e, fwd))
584 break;
585 }
586 else {
587 int idx = e.hash & mask;
588 boolean validated = false;
589 synchronized (e) {
590 if (tabAt(tab, i) == e) {
591 validated = true;
592 Node lastRun = e;
593 for (Node p = e.next; p != null; p = p.next) {
594 int j = p.hash & mask;
595 if (j != idx) {
596 idx = j;
597 lastRun = p;
598 }
599 }
600 relaxedSetTabAt(nextTab, idx, lastRun);
601 for (Node p = e; p != lastRun; p = p.next) {
602 int h = p.hash;
603 int j = h & mask;
604 Node r = relaxedTabAt(nextTab, j);
605 relaxedSetTabAt(nextTab, j,
606 new Node(h, p.key, p.val, r));
607 }
608 setTabAt(tab, i, fwd);
609 }
610 }
611 if (validated)
612 break;
613 }
614 }
615 }
616 }
617
618 /**
619 * If not already resizing, initializes or creates next table and
620 * transfers bins. Rechecks occupancy after a transfer to see if
621 * another resize is already needed because resizings are lagging
622 * additions.
623 *
624 * @param sizeHint overridden capacity target (nonzero only from putAll)
625 * @return current table
626 */
627 private final Node[] grow(int sizeHint) {
628 if (resizing == 0 &&
629 UNSAFE.compareAndSwapInt(this, resizingOffset, 0, 1)) {
630 try {
631 for (;;) {
632 int cap, n;
633 Node[] tab = table;
634 if (tab == null) {
635 int c = initCap;
636 if (c < sizeHint)
637 c = sizeHint;
638 if (c == DEFAULT_CAPACITY)
639 cap = c;
640 else if (c >= MAXIMUM_CAPACITY)
641 cap = MAXIMUM_CAPACITY;
642 else {
643 cap = MINIMUM_CAPACITY;
644 while (cap < c)
645 cap <<= 1;
646 }
647 }
648 else if ((n = tab.length) < MAXIMUM_CAPACITY &&
649 (sizeHint <= 0 || n < sizeHint))
650 cap = n << 1;
651 else
652 break;
653 threshold = (int)(cap * loadFactor) - THRESHOLD_OFFSET;
654 Node[] nextTab = new Node[cap];
655 if (tab != null)
656 transfer(tab, nextTab);
657 table = nextTab;
658 if (tab == null || cap >= MAXIMUM_CAPACITY ||
659 ((sizeHint > 0) ? cap >= sizeHint :
660 counter.sum() < threshold))
661 break;
662 }
663 } finally {
664 resizing = 0;
665 }
666 }
667 else if (table == null)
668 Thread.yield(); // lost initialization race; just spin
669 return table;
670 }
671
672 /**
673 * Implementation for putAll and constructor with Map
674 * argument. Tries to first override initial capacity or grow
675 * based on map size to pre-allocate table space.
676 */
677 private final void internalPutAll(Map<? extends K, ? extends V> m) {
678 int s = m.size();
679 grow((s >= (MAXIMUM_CAPACITY >>> 1)) ? s : s + (s >>> 1));
680 for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) {
681 Object k = e.getKey();
682 Object v = e.getValue();
683 if (k == null || v == null)
684 throw new NullPointerException();
685 internalPut(k, v, true);
686 }
687 }
688
689 /**
690 * Implementation for clear. Steps through each bin, removing all nodes.
691 */
692 private final void internalClear() {
693 long deletions = 0L;
694 int i = 0;
695 Node[] tab = table;
696 while (tab != null && i < tab.length) {
697 Node e = tabAt(tab, i);
698 if (e == null)
699 ++i;
700 else if (e.hash < 0)
701 tab = (Node[])e.key;
702 else {
703 boolean validated = false;
704 synchronized (e) {
705 if (tabAt(tab, i) == e) {
706 validated = true;
707 do {
708 if (e.val != null) {
709 e.val = null;
710 ++deletions;
711 }
712 } while ((e = e.next) != null);
713 setTabAt(tab, i, null);
714 }
715 }
716 if (validated) {
717 ++i;
718 if (deletions > THRESHOLD_OFFSET) { // bound lag in counts
719 counter.add(-deletions);
720 deletions = 0L;
721 }
722 }
723 }
724 }
725 if (deletions != 0L)
726 counter.add(-deletions);
727 }
728
729 /**
730 * Base class for key, value, and entry iterators, plus internal
731 * implementations of public traversal-based methods, to avoid
732 * duplicating traversal code.
733 */
734 class HashIterator {
735 private Node next; // the next entry to return
736 private Node[] tab; // current table; updated if resized
737 private Node lastReturned; // the last entry returned, for remove
738 private Object nextVal; // cached value of next
739 private int index; // index of bin to use next
740 private int baseIndex; // current index of initial table
741 private final int baseSize; // initial table size
742
743 HashIterator() {
744 Node[] t = tab = table;
745 if (t == null)
746 baseSize = 0;
747 else {
748 baseSize = t.length;
749 advance(null);
750 }
751 }
752
753 public final boolean hasNext() { return next != null; }
754 public final boolean hasMoreElements() { return next != null; }
755
756 /**
757 * Advances next. Normally, iteration proceeds bin-by-bin
758 * traversing lists. However, if the table has been resized,
759 * then all future steps must traverse both the bin at the
760 * current index as well as at (index + baseSize); and so on
761 * for further resizings. To paranoically cope with potential
762 * (improper) sharing of iterators across threads, table reads
763 * are bounds-checked.
764 */
765 final void advance(Node e) {
766 for (;;) {
767 Node[] t; int i; // for bounds checks
768 if (e != null) {
769 Object ek = e.key, ev = e.val;
770 if (ev != null && ek != null) {
771 nextVal = ev;
772 next = e;
773 break;
774 }
775 e = e.next;
776 }
777 else if (baseIndex < baseSize && (t = tab) != null &&
778 t.length > (i = index) && i >= 0) {
779 if ((e = tabAt(t, i)) != null && e.hash < 0) {
780 tab = (Node[])e.key;
781 e = null;
782 }
783 else if (i + baseSize < t.length)
784 index += baseSize; // visit forwarded upper slots
785 else
786 index = ++baseIndex;
787 }
788 else {
789 next = null;
790 break;
791 }
792 }
793 }
794
795 final Object nextKey() {
796 Node e = next;
797 if (e == null)
798 throw new NoSuchElementException();
799 Object k = e.key;
800 advance((lastReturned = e).next);
801 return k;
802 }
803
804 final Object nextValue() {
805 Node e = next;
806 if (e == null)
807 throw new NoSuchElementException();
808 Object v = nextVal;
809 advance((lastReturned = e).next);
810 return v;
811 }
812
813 final WriteThroughEntry nextEntry() {
814 Node e = next;
815 if (e == null)
816 throw new NoSuchElementException();
817 WriteThroughEntry entry =
818 new WriteThroughEntry(e.key, nextVal);
819 advance((lastReturned = e).next);
820 return entry;
821 }
822
823 public final void remove() {
824 if (lastReturned == null)
825 throw new IllegalStateException();
826 ConcurrentHashMapV8.this.remove(lastReturned.key);
827 lastReturned = null;
828 }
829
830 /** Helper for serialization */
831 final void writeEntries(java.io.ObjectOutputStream s)
832 throws java.io.IOException {
833 Node e;
834 while ((e = next) != null) {
835 s.writeObject(e.key);
836 s.writeObject(nextVal);
837 advance(e.next);
838 }
839 }
840
841 /** Helper for containsValue */
842 final boolean containsVal(Object value) {
843 if (value != null) {
844 Node e;
845 while ((e = next) != null) {
846 Object v = nextVal;
847 if (value == v || value.equals(v))
848 return true;
849 advance(e.next);
850 }
851 }
852 return false;
853 }
854
855 /** Helper for Map.hashCode */
856 final int mapHashCode() {
857 int h = 0;
858 Node e;
859 while ((e = next) != null) {
860 h += e.key.hashCode() ^ nextVal.hashCode();
861 advance(e.next);
862 }
863 return h;
864 }
865
866 /** Helper for Map.toString */
867 final String mapToString() {
868 Node e = next;
869 if (e == null)
870 return "{}";
871 StringBuilder sb = new StringBuilder();
872 sb.append('{');
873 for (;;) {
874 sb.append(e.key == this ? "(this Map)" : e.key);
875 sb.append('=');
876 sb.append(nextVal == this ? "(this Map)" : nextVal);
877 advance(e.next);
878 if ((e = next) != null)
879 sb.append(',').append(' ');
880 else
881 return sb.append('}').toString();
882 }
883 }
884 }
885
886 /* ---------------- Public operations -------------- */
887
888 /**
889 * Creates a new, empty map with the specified initial
890 * capacity, load factor and concurrency level.
891 *
892 * @param initialCapacity the initial capacity. The implementation
893 * performs internal sizing to accommodate this many elements.
894 * @param loadFactor the load factor threshold, used to control resizing.
895 * Resizing may be performed when the average number of elements per
896 * bin exceeds this threshold.
897 * @param concurrencyLevel the estimated number of concurrently
898 * updating threads. The implementation may use this value as
899 * a sizing hint.
900 * @throws IllegalArgumentException if the initial capacity is
901 * negative or the load factor or concurrencyLevel are
902 * nonpositive.
903 */
904 public ConcurrentHashMapV8(int initialCapacity,
905 float loadFactor, int concurrencyLevel) {
906 if (!(loadFactor > 0) || initialCapacity < 0 || concurrencyLevel <= 0)
907 throw new IllegalArgumentException();
908 this.initCap = initialCapacity;
909 this.loadFactor = loadFactor;
910 this.counter = new LongAdder();
911 }
912
913 /**
914 * Creates a new, empty map with the specified initial capacity
915 * and load factor and with the default concurrencyLevel (16).
916 *
917 * @param initialCapacity The implementation performs internal
918 * sizing to accommodate this many elements.
919 * @param loadFactor the load factor threshold, used to control resizing.
920 * Resizing may be performed when the average number of elements per
921 * bin exceeds this threshold.
922 * @throws IllegalArgumentException if the initial capacity of
923 * elements is negative or the load factor is nonpositive
924 *
925 * @since 1.6
926 */
927 public ConcurrentHashMapV8(int initialCapacity, float loadFactor) {
928 this(initialCapacity, loadFactor, DEFAULT_CONCURRENCY_LEVEL);
929 }
930
931 /**
932 * Creates a new, empty map with the specified initial capacity,
933 * and with default load factor (0.75) and concurrencyLevel (16).
934 *
935 * @param initialCapacity the initial capacity. The implementation
936 * performs internal sizing to accommodate this many elements.
937 * @throws IllegalArgumentException if the initial capacity of
938 * elements is negative.
939 */
940 public ConcurrentHashMapV8(int initialCapacity) {
941 this(initialCapacity, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
942 }
943
944 /**
945 * Creates a new, empty map with a default initial capacity (16),
946 * load factor (0.75) and concurrencyLevel (16).
947 */
948 public ConcurrentHashMapV8() {
949 this(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
950 }
951
952 /**
953 * Creates a new map with the same mappings as the given map.
954 * The map is created with a capacity of 1.5 times the number
955 * of mappings in the given map or 16 (whichever is greater),
956 * and a default load factor (0.75) and concurrencyLevel (16).
957 *
958 * @param m the map
959 */
960 public ConcurrentHashMapV8(Map<? extends K, ? extends V> m) {
961 this(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
962 if (m == null)
963 throw new NullPointerException();
964 internalPutAll(m);
965 }
966
967 /**
968 * Returns {@code true} if this map contains no key-value mappings.
969 *
970 * @return {@code true} if this map contains no key-value mappings
971 */
972 public boolean isEmpty() {
973 return counter.sum() <= 0L; // ignore transient negative values
974 }
975
976 /**
977 * Returns the number of key-value mappings in this map. If the
978 * map contains more than {@code Integer.MAX_VALUE} elements, returns
979 * {@code Integer.MAX_VALUE}.
980 *
981 * @return the number of key-value mappings in this map
982 */
983 public int size() {
984 long n = counter.sum();
985 return ((n >>> 31) == 0) ? (int)n : (n < 0L) ? 0 : Integer.MAX_VALUE;
986 }
987
988 /**
989 * Returns the value to which the specified key is mapped,
990 * or {@code null} if this map contains no mapping for the key.
991 *
992 * <p>More formally, if this map contains a mapping from a key
993 * {@code k} to a value {@code v} such that {@code key.equals(k)},
994 * then this method returns {@code v}; otherwise it returns
995 * {@code null}. (There can be at most one such mapping.)
996 *
997 * @throws NullPointerException if the specified key is null
998 */
999 @SuppressWarnings("unchecked")
1000 public V get(Object key) {
1001 if (key == null)
1002 throw new NullPointerException();
1003 return (V)internalGet(key);
1004 }
1005
1006 /**
1007 * Tests if the specified object is a key in this table.
1008 *
1009 * @param key possible key
1010 * @return {@code true} if and only if the specified object
1011 * is a key in this table, as determined by the
1012 * {@code equals} method; {@code false} otherwise.
1013 * @throws NullPointerException if the specified key is null
1014 */
1015 public boolean containsKey(Object key) {
1016 if (key == null)
1017 throw new NullPointerException();
1018 return internalGet(key) != null;
1019 }
1020
1021 /**
1022 * Returns {@code true} if this map maps one or more keys to the
1023 * specified value. Note: This method requires a full internal
1024 * traversal of the hash table, and so is much slower than
1025 * method {@code containsKey}.
1026 *
1027 * @param value value whose presence in this map is to be tested
1028 * @return {@code true} if this map maps one or more keys to the
1029 * specified value
1030 * @throws NullPointerException if the specified value is null
1031 */
1032 public boolean containsValue(Object value) {
1033 if (value == null)
1034 throw new NullPointerException();
1035 return new HashIterator().containsVal(value);
1036 }
1037
1038 /**
1039 * Legacy method testing if some key maps into the specified value
1040 * in this table. This method is identical in functionality to
1041 * {@link #containsValue}, and exists solely to ensure
1042 * full compatibility with class {@link java.util.Hashtable},
1043 * which supported this method prior to introduction of the
1044 * Java Collections framework.
1045 *
1046 * @param value a value to search for
1047 * @return {@code true} if and only if some key maps to the
1048 * {@code value} argument in this table as
1049 * determined by the {@code equals} method;
1050 * {@code false} otherwise
1051 * @throws NullPointerException if the specified value is null
1052 */
1053 public boolean contains(Object value) {
1054 return containsValue(value);
1055 }
1056
1057 /**
1058 * Maps the specified key to the specified value in this table.
1059 * Neither the key nor the value can be null.
1060 *
1061 * <p> The value can be retrieved by calling the {@code get} method
1062 * with a key that is equal to the original key.
1063 *
1064 * @param key key with which the specified value is to be associated
1065 * @param value value to be associated with the specified key
1066 * @return the previous value associated with {@code key}, or
1067 * {@code null} if there was no mapping for {@code key}
1068 * @throws NullPointerException if the specified key or value is null
1069 */
1070 @SuppressWarnings("unchecked")
1071 public V put(K key, V value) {
1072 if (key == null || value == null)
1073 throw new NullPointerException();
1074 return (V)internalPut(key, value, true);
1075 }
1076
1077 /**
1078 * {@inheritDoc}
1079 *
1080 * @return the previous value associated with the specified key,
1081 * or {@code null} if there was no mapping for the key
1082 * @throws NullPointerException if the specified key or value is null
1083 */
1084 @SuppressWarnings("unchecked")
1085 public V putIfAbsent(K key, V value) {
1086 if (key == null || value == null)
1087 throw new NullPointerException();
1088 return (V)internalPut(key, value, false);
1089 }
1090
1091 /**
1092 * Copies all of the mappings from the specified map to this one.
1093 * These mappings replace any mappings that this map had for any of the
1094 * keys currently in the specified map.
1095 *
1096 * @param m mappings to be stored in this map
1097 */
1098 public void putAll(Map<? extends K, ? extends V> m) {
1099 if (m == null)
1100 throw new NullPointerException();
1101 internalPutAll(m);
1102 }
1103
1104 /**
1105 * If the specified key is not already associated with a value,
1106 * computes its value using the given mappingFunction, and if
1107 * non-null, enters it into the map. This is equivalent to
1108 *
1109 * <pre>
1110 * if (map.containsKey(key))
1111 * return map.get(key);
1112 * value = mappingFunction.map(key);
1113 * if (value != null)
1114 * map.put(key, value);
1115 * return value;
1116 * </pre>
1117 *
1118 * except that the action is performed atomically. Some attempted
1119 * update operations on this map by other threads may be blocked
1120 * while computation is in progress, so the computation should be
1121 * short and simple, and must not attempt to update any other
1122 * mappings of this Map. The most appropriate usage is to
1123 * construct a new object serving as an initial mapped value, or
1124 * memoized result, as in:
1125 * <pre>{@code
1126 * map.computeIfAbsent(key, new MappingFunction<K, V>() {
1127 * public V map(K k) { return new Value(f(k)); }};
1128 * }</pre>
1129 *
1130 * @param key key with which the specified value is to be associated
1131 * @param mappingFunction the function to compute a value
1132 * @return the current (existing or computed) value associated with
1133 * the specified key, or {@code null} if the computation
1134 * returned {@code null}.
1135 * @throws NullPointerException if the specified key or mappingFunction
1136 * is null,
1137 * @throws IllegalStateException if the computation detectably
1138 * attempts a recursive update to this map that would
1139 * otherwise never complete.
1140 * @throws RuntimeException or Error if the mappingFunction does so,
1141 * in which case the mapping is left unestablished.
1142 */
1143 public V computeIfAbsent(K key, MappingFunction<? super K, ? extends V> mappingFunction) {
1144 if (key == null || mappingFunction == null)
1145 throw new NullPointerException();
1146 return internalCompute(key, mappingFunction, false);
1147 }
1148
1149 /**
1150 * Computes the value associated with the given key using the given
1151 * mappingFunction, and if non-null, enters it into the map. This
1152 * is equivalent to
1153 *
1154 * <pre>
1155 * value = mappingFunction.map(key);
1156 * if (value != null)
1157 * map.put(key, value);
1158 * else
1159 * value = map.get(key);
1160 * return value;
1161 * </pre>
1162 *
1163 * except that the action is performed atomically. Some attempted
1164 * update operations on this map by other threads may be blocked
1165 * while computation is in progress, so the computation should be
1166 * short and simple, and must not attempt to update any other
1167 * mappings of this Map.
1168 *
1169 * @param key key with which the specified value is to be associated
1170 * @param mappingFunction the function to compute a value
1171 * @return the current value associated with
1172 * the specified key, or {@code null} if the computation
1173 * returned {@code null} and the value was not otherwise present.
1174 * @throws NullPointerException if the specified key or mappingFunction
1175 * is null,
1176 * @throws IllegalStateException if the computation detectably
1177 * attempts a recursive update to this map that would
1178 * otherwise never complete.
1179 * @throws RuntimeException or Error if the mappingFunction does so,
1180 * in which case the mapping is unchanged.
1181 */
1182 public V compute(K key, MappingFunction<? super K, ? extends V> mappingFunction) {
1183 if (key == null || mappingFunction == null)
1184 throw new NullPointerException();
1185 return internalCompute(key, mappingFunction, true);
1186 }
1187
1188 /**
1189 * Removes the key (and its corresponding value) from this map.
1190 * This method does nothing if the key is not in the map.
1191 *
1192 * @param key the key that needs to be removed
1193 * @return the previous value associated with {@code key}, or
1194 * {@code null} if there was no mapping for {@code key}
1195 * @throws NullPointerException if the specified key is null
1196 */
1197 @SuppressWarnings("unchecked")
1198 public V remove(Object key) {
1199 if (key == null)
1200 throw new NullPointerException();
1201 return (V)internalReplace(key, null, null);
1202 }
1203
1204 /**
1205 * {@inheritDoc}
1206 *
1207 * @throws NullPointerException if the specified key is null
1208 */
1209 public boolean remove(Object key, Object value) {
1210 if (key == null)
1211 throw new NullPointerException();
1212 if (value == null)
1213 return false;
1214 return internalReplace(key, null, value) != null;
1215 }
1216
1217 /**
1218 * {@inheritDoc}
1219 *
1220 * @throws NullPointerException if any of the arguments are null
1221 */
1222 public boolean replace(K key, V oldValue, V newValue) {
1223 if (key == null || oldValue == null || newValue == null)
1224 throw new NullPointerException();
1225 return internalReplace(key, newValue, oldValue) != null;
1226 }
1227
1228 /**
1229 * {@inheritDoc}
1230 *
1231 * @return the previous value associated with the specified key,
1232 * or {@code null} if there was no mapping for the key
1233 * @throws NullPointerException if the specified key or value is null
1234 */
1235 @SuppressWarnings("unchecked")
1236 public V replace(K key, V value) {
1237 if (key == null || value == null)
1238 throw new NullPointerException();
1239 return (V)internalReplace(key, value, null);
1240 }
1241
1242 /**
1243 * Removes all of the mappings from this map.
1244 */
1245 public void clear() {
1246 internalClear();
1247 }
1248
1249 /**
1250 * Returns a {@link Set} view of the keys contained in this map.
1251 * The set is backed by the map, so changes to the map are
1252 * reflected in the set, and vice-versa. The set supports element
1253 * removal, which removes the corresponding mapping from this map,
1254 * via the {@code Iterator.remove}, {@code Set.remove},
1255 * {@code removeAll}, {@code retainAll}, and {@code clear}
1256 * operations. It does not support the {@code add} or
1257 * {@code addAll} operations.
1258 *
1259 * <p>The view's {@code iterator} is a "weakly consistent" iterator
1260 * that will never throw {@link ConcurrentModificationException},
1261 * and guarantees to traverse elements as they existed upon
1262 * construction of the iterator, and may (but is not guaranteed to)
1263 * reflect any modifications subsequent to construction.
1264 */
1265 public Set<K> keySet() {
1266 Set<K> ks = keySet;
1267 return (ks != null) ? ks : (keySet = new KeySet());
1268 }
1269
1270 /**
1271 * Returns a {@link Collection} view of the values contained in this map.
1272 * The collection is backed by the map, so changes to the map are
1273 * reflected in the collection, and vice-versa. The collection
1274 * supports element removal, which removes the corresponding
1275 * mapping from this map, via the {@code Iterator.remove},
1276 * {@code Collection.remove}, {@code removeAll},
1277 * {@code retainAll}, and {@code clear} operations. It does not
1278 * support the {@code add} or {@code addAll} operations.
1279 *
1280 * <p>The view's {@code iterator} is a "weakly consistent" iterator
1281 * that will never throw {@link ConcurrentModificationException},
1282 * and guarantees to traverse elements as they existed upon
1283 * construction of the iterator, and may (but is not guaranteed to)
1284 * reflect any modifications subsequent to construction.
1285 */
1286 public Collection<V> values() {
1287 Collection<V> vs = values;
1288 return (vs != null) ? vs : (values = new Values());
1289 }
1290
1291 /**
1292 * Returns a {@link Set} view of the mappings contained in this map.
1293 * The set is backed by the map, so changes to the map are
1294 * reflected in the set, and vice-versa. The set supports element
1295 * removal, which removes the corresponding mapping from the map,
1296 * via the {@code Iterator.remove}, {@code Set.remove},
1297 * {@code removeAll}, {@code retainAll}, and {@code clear}
1298 * operations. It does not support the {@code add} or
1299 * {@code addAll} operations.
1300 *
1301 * <p>The view's {@code iterator} is a "weakly consistent" iterator
1302 * that will never throw {@link ConcurrentModificationException},
1303 * and guarantees to traverse elements as they existed upon
1304 * construction of the iterator, and may (but is not guaranteed to)
1305 * reflect any modifications subsequent to construction.
1306 */
1307 public Set<Map.Entry<K,V>> entrySet() {
1308 Set<Map.Entry<K,V>> es = entrySet;
1309 return (es != null) ? es : (entrySet = new EntrySet());
1310 }
1311
1312 /**
1313 * Returns an enumeration of the keys in this table.
1314 *
1315 * @return an enumeration of the keys in this table
1316 * @see #keySet()
1317 */
1318 public Enumeration<K> keys() {
1319 return new KeyIterator();
1320 }
1321
1322 /**
1323 * Returns an enumeration of the values in this table.
1324 *
1325 * @return an enumeration of the values in this table
1326 * @see #values()
1327 */
1328 public Enumeration<V> elements() {
1329 return new ValueIterator();
1330 }
1331
1332 /**
1333 * Returns the hash code value for this {@link Map}, i.e.,
1334 * the sum of, for each key-value pair in the map,
1335 * {@code key.hashCode() ^ value.hashCode()}.
1336 *
1337 * @return the hash code value for this map
1338 */
1339 public int hashCode() {
1340 return new HashIterator().mapHashCode();
1341 }
1342
1343 /**
1344 * Returns a string representation of this map. The string
1345 * representation consists of a list of key-value mappings (in no
1346 * particular order) enclosed in braces ("{@code {}}"). Adjacent
1347 * mappings are separated by the characters {@code ", "} (comma
1348 * and space). Each key-value mapping is rendered as the key
1349 * followed by an equals sign ("{@code =}") followed by the
1350 * associated value.
1351 *
1352 * @return a string representation of this map
1353 */
1354 public String toString() {
1355 return new HashIterator().mapToString();
1356 }
1357
1358 /**
1359 * Compares the specified object with this map for equality.
1360 * Returns {@code true} if the given object is a map with the same
1361 * mappings as this map. This operation may return misleading
1362 * results if either map is concurrently modified during execution
1363 * of this method.
1364 *
1365 * @param o object to be compared for equality with this map
1366 * @return {@code true} if the specified object is equal to this map
1367 */
1368 public boolean equals(Object o) {
1369 if (o == this)
1370 return true;
1371 if (!(o instanceof Map))
1372 return false;
1373 Map<?,?> m = (Map<?,?>) o;
1374 try {
1375 for (Map.Entry<K,V> e : this.entrySet())
1376 if (! e.getValue().equals(m.get(e.getKey())))
1377 return false;
1378 for (Map.Entry<?,?> e : m.entrySet()) {
1379 Object k = e.getKey();
1380 Object v = e.getValue();
1381 if (k == null || v == null || !v.equals(get(k)))
1382 return false;
1383 }
1384 return true;
1385 } catch (ClassCastException unused) {
1386 return false;
1387 } catch (NullPointerException unused) {
1388 return false;
1389 }
1390 }
1391
1392 /**
1393 * Custom Entry class used by EntryIterator.next(), that relays
1394 * setValue changes to the underlying map.
1395 */
1396 final class WriteThroughEntry extends AbstractMap.SimpleEntry<K,V> {
1397 @SuppressWarnings("unchecked")
1398 WriteThroughEntry(Object k, Object v) {
1399 super((K)k, (V)v);
1400 }
1401
1402 /**
1403 * Sets our entry's value and writes through to the map. The
1404 * value to return is somewhat arbitrary here. Since a
1405 * WriteThroughEntry does not necessarily track asynchronous
1406 * changes, the most recent "previous" value could be
1407 * different from what we return (or could even have been
1408 * removed in which case the put will re-establish). We do not
1409 * and cannot guarantee more.
1410 */
1411 public V setValue(V value) {
1412 if (value == null) throw new NullPointerException();
1413 V v = super.setValue(value);
1414 ConcurrentHashMapV8.this.put(getKey(), value);
1415 return v;
1416 }
1417 }
1418
1419 final class KeyIterator extends HashIterator
1420 implements Iterator<K>, Enumeration<K> {
1421 @SuppressWarnings("unchecked")
1422 public final K next() { return (K)super.nextKey(); }
1423 @SuppressWarnings("unchecked")
1424 public final K nextElement() { return (K)super.nextKey(); }
1425 }
1426
1427 final class ValueIterator extends HashIterator
1428 implements Iterator<V>, Enumeration<V> {
1429 @SuppressWarnings("unchecked")
1430 public final V next() { return (V)super.nextValue(); }
1431 @SuppressWarnings("unchecked")
1432 public final V nextElement() { return (V)super.nextValue(); }
1433 }
1434
1435 final class EntryIterator extends HashIterator
1436 implements Iterator<Entry<K,V>> {
1437 public final Map.Entry<K,V> next() { return super.nextEntry(); }
1438 }
1439
1440 final class KeySet extends AbstractSet<K> {
1441 public int size() {
1442 return ConcurrentHashMapV8.this.size();
1443 }
1444 public boolean isEmpty() {
1445 return ConcurrentHashMapV8.this.isEmpty();
1446 }
1447 public void clear() {
1448 ConcurrentHashMapV8.this.clear();
1449 }
1450 public Iterator<K> iterator() {
1451 return new KeyIterator();
1452 }
1453 public boolean contains(Object o) {
1454 return ConcurrentHashMapV8.this.containsKey(o);
1455 }
1456 public boolean remove(Object o) {
1457 return ConcurrentHashMapV8.this.remove(o) != null;
1458 }
1459 }
1460
1461 final class Values extends AbstractCollection<V> {
1462 public int size() {
1463 return ConcurrentHashMapV8.this.size();
1464 }
1465 public boolean isEmpty() {
1466 return ConcurrentHashMapV8.this.isEmpty();
1467 }
1468 public void clear() {
1469 ConcurrentHashMapV8.this.clear();
1470 }
1471 public Iterator<V> iterator() {
1472 return new ValueIterator();
1473 }
1474 public boolean contains(Object o) {
1475 return ConcurrentHashMapV8.this.containsValue(o);
1476 }
1477 }
1478
1479 final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
1480 public int size() {
1481 return ConcurrentHashMapV8.this.size();
1482 }
1483 public boolean isEmpty() {
1484 return ConcurrentHashMapV8.this.isEmpty();
1485 }
1486 public void clear() {
1487 ConcurrentHashMapV8.this.clear();
1488 }
1489 public Iterator<Map.Entry<K,V>> iterator() {
1490 return new EntryIterator();
1491 }
1492 public boolean contains(Object o) {
1493 if (!(o instanceof Map.Entry))
1494 return false;
1495 Map.Entry<?,?> e = (Map.Entry<?,?>)o;
1496 V v = ConcurrentHashMapV8.this.get(e.getKey());
1497 return v != null && v.equals(e.getValue());
1498 }
1499 public boolean remove(Object o) {
1500 if (!(o instanceof Map.Entry))
1501 return false;
1502 Map.Entry<?,?> e = (Map.Entry<?,?>)o;
1503 return ConcurrentHashMapV8.this.remove(e.getKey(), e.getValue());
1504 }
1505 }
1506
1507 /* ---------------- Serialization Support -------------- */
1508
1509 /**
1510 * Helper class used in previous version, declared for the sake of
1511 * serialization compatibility
1512 */
1513 static class Segment<K,V> extends java.util.concurrent.locks.ReentrantLock
1514 implements Serializable {
1515 private static final long serialVersionUID = 2249069246763182397L;
1516 final float loadFactor;
1517 Segment(float lf) { this.loadFactor = lf; }
1518 }
1519
1520 /**
1521 * Saves the state of the {@code ConcurrentHashMapV8} instance to a
1522 * stream (i.e., serializes it).
1523 * @param s the stream
1524 * @serialData
1525 * the key (Object) and value (Object)
1526 * for each key-value mapping, followed by a null pair.
1527 * The key-value mappings are emitted in no particular order.
1528 */
1529 @SuppressWarnings("unchecked")
1530 private void writeObject(java.io.ObjectOutputStream s)
1531 throws java.io.IOException {
1532 if (segments == null) { // for serialization compatibility
1533 segments = (Segment<K,V>[])
1534 new Segment<?,?>[DEFAULT_CONCURRENCY_LEVEL];
1535 for (int i = 0; i < segments.length; ++i)
1536 segments[i] = new Segment<K,V>(loadFactor);
1537 }
1538 s.defaultWriteObject();
1539 new HashIterator().writeEntries(s);
1540 s.writeObject(null);
1541 s.writeObject(null);
1542 segments = null; // throw away
1543 }
1544
1545 /**
1546 * Reconstitutes the instance from a stream (that is, deserializes it).
1547 * @param s the stream
1548 */
1549 @SuppressWarnings("unchecked")
1550 private void readObject(java.io.ObjectInputStream s)
1551 throws java.io.IOException, ClassNotFoundException {
1552 s.defaultReadObject();
1553 // find load factor in a segment, if one exists
1554 if (segments != null && segments.length != 0)
1555 this.loadFactor = segments[0].loadFactor;
1556 else
1557 this.loadFactor = DEFAULT_LOAD_FACTOR;
1558 this.initCap = DEFAULT_CAPACITY;
1559 LongAdder ct = new LongAdder(); // force final field write
1560 UNSAFE.putObjectVolatile(this, counterOffset, ct);
1561 this.segments = null; // unneeded
1562
1563 // Read the keys and values, and put the mappings in the table
1564 for (;;) {
1565 K key = (K) s.readObject();
1566 V value = (V) s.readObject();
1567 if (key == null)
1568 break;
1569 put(key, value);
1570 }
1571 }
1572
1573 // Unsafe mechanics
1574 private static final sun.misc.Unsafe UNSAFE;
1575 private static final long counterOffset;
1576 private static final long resizingOffset;
1577 private static final long ABASE;
1578 private static final int ASHIFT;
1579
1580 static {
1581 int ss;
1582 try {
1583 UNSAFE = getUnsafe();
1584 Class<?> k = ConcurrentHashMapV8.class;
1585 counterOffset = UNSAFE.objectFieldOffset
1586 (k.getDeclaredField("counter"));
1587 resizingOffset = UNSAFE.objectFieldOffset
1588 (k.getDeclaredField("resizing"));
1589 Class<?> sc = Node[].class;
1590 ABASE = UNSAFE.arrayBaseOffset(sc);
1591 ss = UNSAFE.arrayIndexScale(sc);
1592 } catch (Exception e) {
1593 throw new Error(e);
1594 }
1595 if ((ss & (ss-1)) != 0)
1596 throw new Error("data type scale not a power of two");
1597 ASHIFT = 31 - Integer.numberOfLeadingZeros(ss);
1598 }
1599
1600 /**
1601 * Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package.
1602 * Replace with a simple call to Unsafe.getUnsafe when integrating
1603 * into a jdk.
1604 *
1605 * @return a sun.misc.Unsafe
1606 */
1607 private static sun.misc.Unsafe getUnsafe() {
1608 try {
1609 return sun.misc.Unsafe.getUnsafe();
1610 } catch (SecurityException se) {
1611 try {
1612 return java.security.AccessController.doPrivileged
1613 (new java.security
1614 .PrivilegedExceptionAction<sun.misc.Unsafe>() {
1615 public sun.misc.Unsafe run() throws Exception {
1616 java.lang.reflect.Field f = sun.misc
1617 .Unsafe.class.getDeclaredField("theUnsafe");
1618 f.setAccessible(true);
1619 return (sun.misc.Unsafe) f.get(null);
1620 }});
1621 } catch (java.security.PrivilegedActionException e) {
1622 throw new RuntimeException("Could not initialize intrinsics",
1623 e.getCause());
1624 }
1625 }
1626 }
1627
1628 }