ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166e/ConcurrentHashMapV8.java
(Generate patch)

Comparing jsr166/src/jsr166e/ConcurrentHashMapV8.java (file contents):
Revision 1.26 by jsr166, Wed Sep 21 11:42:08 2011 UTC vs.
Revision 1.42 by jsr166, Wed Jul 4 20:10:00 2012 UTC

# Line 20 | Line 20 | import java.util.Enumeration;
20   import java.util.ConcurrentModificationException;
21   import java.util.NoSuchElementException;
22   import java.util.concurrent.ConcurrentMap;
23 + import java.util.concurrent.ThreadLocalRandom;
24   import java.util.concurrent.locks.LockSupport;
25 + import java.util.concurrent.locks.AbstractQueuedSynchronizer;
26   import java.io.Serializable;
27  
28   /**
# Line 71 | Line 73 | import java.io.Serializable;
73   * versions of this class, constructors may optionally specify an
74   * expected {@code concurrencyLevel} as an additional hint for
75   * internal sizing.  Note that using many keys with exactly the same
76 < * {@code hashCode{}} is a sure way to slow down performance of any
76 > * {@code hashCode()} is a sure way to slow down performance of any
77   * hash table.
78   *
79   * <p>This class and its views and iterators implement all of the
# Line 98 | Line 100 | public class ConcurrentHashMapV8<K, V>
100      private static final long serialVersionUID = 7249069246763182397L;
101  
102      /**
103 <     * A function computing a mapping from the given key to a value,
104 <     * or {@code null} if there is no mapping. This is a place-holder
103 <     * for an upcoming JDK8 interface.
103 >     * A function computing a mapping from the given key to a value.
104 >     * This is a place-holder for an upcoming JDK8 interface.
105       */
106      public static interface MappingFunction<K, V> {
107          /**
108 <         * Returns a value for the given key, or null if there is no
108 <         * mapping. If this function throws an (unchecked) exception,
109 <         * the exception is rethrown to its caller, and no mapping is
110 <         * recorded.  Because this function is invoked within
111 <         * atomicity control, the computation should be short and
112 <         * simple. The most common usage is to construct a new object
113 <         * serving as an initial mapped value.
108 >         * Returns a value for the given key, or null if there is no mapping
109           *
110           * @param key the (non-null) key
111 <         * @return a value, or null if none
111 >         * @return a value for the key, or null if none
112           */
113          V map(K key);
114      }
115  
116 +    /**
117 +     * A function computing a new mapping given a key and its current
118 +     * mapped value (or {@code null} if there is no current
119 +     * mapping). This is a place-holder for an upcoming JDK8
120 +     * interface.
121 +     */
122 +    public static interface RemappingFunction<K, V> {
123 +        /**
124 +         * Returns a new value given a key and its current value.
125 +         *
126 +         * @param key the (non-null) key
127 +         * @param value the current value, or null if there is no mapping
128 +         * @return a value for the key, or null if none
129 +         */
130 +        V remap(K key, V value);
131 +    }
132 +
133 +    /**
134 +     * A partitionable iterator. A Spliterator can be traversed
135 +     * directly, but can also be partitioned (before traversal) by
136 +     * creating another Spliterator that covers a non-overlapping
137 +     * portion of the elements, and so may be amenable to parallel
138 +     * execution.
139 +     *
140 +     * <p> This interface exports a subset of expected JDK8
141 +     * functionality.
142 +     *
143 +     * <p>Sample usage: Here is one (of the several) ways to compute
144 +     * the sum of the values held in a map using the ForkJoin
145 +     * framework. As illustrated here, Spliterators are well suited to
146 +     * designs in which a task repeatedly splits off half its work
147 +     * into forked subtasks until small enough to process directly,
148 +     * and then joins these subtasks. Variants of this style can be
149 +     * also be used in completion-based designs.
150 +     *
151 +     * <pre>
152 +     * {@code ConcurrentHashMapV8<String, Long> m = ...
153 +     * // Uses parallel depth of log2 of size / (parallelism * slack of 8).
154 +     * int depth = 32 - Integer.numberOfLeadingZeros(m.size() / (aForkJoinPool.getParallelism() * 8));
155 +     * long sum = aForkJoinPool.invoke(new SumValues(m.valueSpliterator(), depth, null));
156 +     * // ...
157 +     * static class SumValues extends RecursiveTask<Long> {
158 +     *   final Spliterator<Long> s;
159 +     *   final int depth;             // number of splits before processing
160 +     *   final SumValues nextJoin;    // records forked subtasks to join
161 +     *   SumValues(Spliterator<Long> s, int depth, SumValues nextJoin) {
162 +     *     this.s = s; this.depth = depth; this.nextJoin = nextJoin;
163 +     *   }
164 +     *   public Long compute() {
165 +     *     long sum = 0;
166 +     *     SumValues subtasks = null; // fork subtasks
167 +     *     for (int d = depth - 1; d >= 0; --d)
168 +     *       (subtasks = new SumValues(s.split(), d, subtasks)).fork();
169 +     *     while (s.hasNext())        // directly process remaining elements
170 +     *       sum += s.next();
171 +     *     for (SumValues t = subtasks; t != null; t = t.nextJoin)
172 +     *       sum += t.join();         // collect subtask results
173 +     *     return sum;
174 +     *   }
175 +     * }
176 +     * }</pre>
177 +     */
178 +    public static interface Spliterator<T> extends Iterator<T> {
179 +        /**
180 +         * Returns a Spliterator covering approximately half of the
181 +         * elements, guaranteed not to overlap with those subsequently
182 +         * returned by this Spliterator.  After invoking this method,
183 +         * the current Spliterator will <em>not</em> produce any of
184 +         * the elements of the returned Spliterator, but the two
185 +         * Spliterators together will produce all of the elements that
186 +         * would have been produced by this Spliterator had this
187 +         * method not been called. The exact number of elements
188 +         * produced by the returned Spliterator is not guaranteed, and
189 +         * may be zero (i.e., with {@code hasNext()} reporting {@code
190 +         * false}) if this Spliterator cannot be further split.
191 +         *
192 +         * @return a Spliterator covering approximately half of the
193 +         * elements
194 +         * @throws IllegalStateException if this Spliterator has
195 +         * already commenced traversing elements.
196 +         */
197 +        Spliterator<T> split();
198 +
199 +        /**
200 +         * Returns a Spliterator producing the same elements as this
201 +         * Spliterator. This method may be used for example to create
202 +         * a second Spliterator before a traversal, in order to later
203 +         * perform a second traversal.
204 +         *
205 +         * @return a Spliterator covering the same range as this Spliterator.
206 +         * @throws IllegalStateException if this Spliterator has
207 +         * already commenced traversing elements.
208 +         */
209 +        Spliterator<T> clone();
210 +    }
211 +
212      /*
213       * Overview:
214       *
# Line 134 | Line 225 | public class ConcurrentHashMapV8<K, V>
225       * work off Object types. And similarly, so do the internal
226       * methods of auxiliary iterator and view classes.  All public
227       * generic typed methods relay in/out of these internal methods,
228 <     * supplying null-checks and casts as needed.
228 >     * supplying null-checks and casts as needed. This also allows
229 >     * many of the public methods to be factored into a smaller number
230 >     * of internal methods (although sadly not so for the five
231 >     * variants of put-related operations). The validation-based
232 >     * approach explained below leads to a lot of code sprawl because
233 >     * retry-control precludes factoring into smaller methods.
234       *
235       * The table is lazily initialized to a power-of-two size upon the
236 <     * first insertion.  Each bin in the table contains a list of
237 <     * Nodes (most often, zero or one Node).  Table accesses require
238 <     * volatile/atomic reads, writes, and CASes.  Because there is no
239 <     * other way to arrange this without adding further indirections,
240 <     * we use intrinsics (sun.misc.Unsafe) operations.  The lists of
241 <     * nodes within bins are always accurately traversable under
242 <     * volatile reads, so long as lookups check hash code and
243 <     * non-nullness of value before checking key equality.
236 >     * first insertion.  Each bin in the table normally contains a
237 >     * list of Nodes (most often, the list has only zero or one Node).
238 >     * Table accesses require volatile/atomic reads, writes, and
239 >     * CASes.  Because there is no other way to arrange this without
240 >     * adding further indirections, we use intrinsics
241 >     * (sun.misc.Unsafe) operations.  The lists of nodes within bins
242 >     * are always accurately traversable under volatile reads, so long
243 >     * as lookups check hash code and non-nullness of value before
244 >     * checking key equality.
245       *
246       * We use the top two bits of Node hash fields for control
247       * purposes -- they are available anyway because of addressing
248       * constraints.  As explained further below, these top bits are
249 <     * usd as follows:
249 >     * used as follows:
250       *  00 - Normal
251       *  01 - Locked
252       *  11 - Locked and may have a thread waiting for lock
253       *  10 - Node is a forwarding node
254       *
255       * The lower 30 bits of each Node's hash field contain a
256 <     * transformation (for better randomization -- method "spread") of
257 <     * the key's hash code, except for forwarding nodes, for which the
258 <     * lower bits are zero (and so always have hash field == "MOVED").
256 >     * transformation of the key's hash code, except for forwarding
257 >     * nodes, for which the lower bits are zero (and so always have
258 >     * hash field == MOVED).
259       *
260 <     * Insertion (via put or putIfAbsent) of the first node in an
260 >     * Insertion (via put or its variants) of the first node in an
261       * empty bin is performed by just CASing it to the bin.  This is
262 <     * by far the most common case for put operations.  Other update
263 <     * operations (insert, delete, and replace) require locks.  We do
264 <     * not want to waste the space required to associate a distinct
265 <     * lock object with each bin, so instead use the first node of a
266 <     * bin list itself as a lock. Blocking support for these locks
267 <     * relies on the builtin "synchronized" monitors.  However, we
268 <     * also need a tryLock construction, so we overlay these by using
269 <     * bits of the Node hash field for lock control (see above), and
270 <     * so normally use builtin monitors only for blocking and
271 <     * signalling using wait/notifyAll constructions. See
272 <     * Node.tryAwaitLock.
262 >     * by far the most common case for put operations under most
263 >     * key/hash distributions.  Other update operations (insert,
264 >     * delete, and replace) require locks.  We do not want to waste
265 >     * the space required to associate a distinct lock object with
266 >     * each bin, so instead use the first node of a bin list itself as
267 >     * a lock. Blocking support for these locks relies on the builtin
268 >     * "synchronized" monitors.  However, we also need a tryLock
269 >     * construction, so we overlay these by using bits of the Node
270 >     * hash field for lock control (see above), and so normally use
271 >     * builtin monitors only for blocking and signalling using
272 >     * wait/notifyAll constructions. See Node.tryAwaitLock.
273       *
274       * Using the first node of a list as a lock does not by itself
275       * suffice though: When a node is locked, any update must first
276       * validate that it is still the first node after locking it, and
277       * retry if not. Because new nodes are always appended to lists,
278       * once a node is first in a bin, it remains first until deleted
279 <     * or the bin becomes invalidated.  However, operations that only
280 <     * conditionally update may inspect nodes until the point of
281 <     * update. This is a converse of sorts to the lazy locking
282 <     * technique described by Herlihy & Shavit.
279 >     * or the bin becomes invalidated (upon resizing).  However,
280 >     * operations that only conditionally update may inspect nodes
281 >     * until the point of update. This is a converse of sorts to the
282 >     * lazy locking technique described by Herlihy & Shavit.
283       *
284       * The main disadvantage of per-bin locks is that other update
285       * operations on other nodes in a bin list protected by the same
286       * lock can stall, for example when user equals() or mapping
287 <     * functions take a long time.  However, statistically, this is
288 <     * not a common enough problem to outweigh the time/space overhead
289 <     * of alternatives: Under random hash codes, the frequency of
193 <     * nodes in bins follows a Poisson distribution
287 >     * functions take a long time.  However, statistically, under
288 >     * random hash codes, this is not a common problem.  Ideally, the
289 >     * frequency of nodes in bins follows a Poisson distribution
290       * (http://en.wikipedia.org/wiki/Poisson_distribution) with a
291       * parameter of about 0.5 on average, given the resizing threshold
292       * of 0.75, although with a large variance because of resizing
293       * granularity. Ignoring variance, the expected occurrences of
294       * list size k are (exp(-0.5) * pow(0.5, k) / factorial(k)). The
295 <     * first few values are:
295 >     * first values are:
296       *
297 <     * 0:    0.607
298 <     * 1:    0.303
299 <     * 2:    0.076
300 <     * 3:    0.012
301 <     * more: 0.002
297 >     * 0:    0.60653066
298 >     * 1:    0.30326533
299 >     * 2:    0.07581633
300 >     * 3:    0.01263606
301 >     * 4:    0.00157952
302 >     * 5:    0.00015795
303 >     * 6:    0.00001316
304 >     * 7:    0.00000094
305 >     * 8:    0.00000006
306 >     * more: less than 1 in ten million
307       *
308       * Lock contention probability for two threads accessing distinct
309 <     * elements is roughly 1 / (8 * #elements).  Function "spread"
209 <     * performs hashCode randomization that improves the likelihood
210 <     * that these assumptions hold unless users define exactly the
211 <     * same value for too many hashCodes.
309 >     * elements is roughly 1 / (8 * #elements) under random hashes.
310       *
311 <     * The table is resized when occupancy exceeds an occupancy
311 >     * Actual hash code distributions encountered in practice
312 >     * sometimes deviate significantly from uniform randomness.  This
313 >     * includes the case when N > (1<<30), so some keys MUST collide.
314 >     * Similarly for dumb or hostile usages in which multiple keys are
315 >     * designed to have identical hash codes. Also, although we guard
316 >     * against the worst effects of this (see method spread), sets of
317 >     * hashes may differ only in bits that do not impact their bin
318 >     * index for a given power-of-two mask.  So we use a secondary
319 >     * strategy that applies when the number of nodes in a bin exceeds
320 >     * a threshold, and at least one of the keys implements
321 >     * Comparable.  These TreeBins use a balanced tree to hold nodes
322 >     * (a specialized form of red-black trees), bounding search time
323 >     * to O(log N).  Each search step in a TreeBin is around twice as
324 >     * slow as in a regular list, but given that N cannot exceed
325 >     * (1<<64) (before running out of addresses) this bounds search
326 >     * steps, lock hold times, etc, to reasonable constants (roughly
327 >     * 100 nodes inspected per operation worst case) so long as keys
328 >     * are Comparable (which is very common -- String, Long, etc).
329 >     * TreeBin nodes (TreeNodes) also maintain the same "next"
330 >     * traversal pointers as regular nodes, so can be traversed in
331 >     * iterators in the same way.
332 >     *
333 >     * The table is resized when occupancy exceeds a percentage
334       * threshold (nominally, 0.75, but see below).  Only a single
335       * thread performs the resize (using field "sizeCtl", to arrange
336       * exclusion), but the table otherwise remains usable for reads
# Line 231 | Line 351 | public class ConcurrentHashMapV8<K, V>
351       *
352       * Each bin transfer requires its bin lock. However, unlike other
353       * cases, a transfer can skip a bin if it fails to acquire its
354 <     * lock, and revisit it later. Method rebuild maintains a buffer
355 <     * of TRANSFER_BUFFER_SIZE bins that have been skipped because of
356 <     * failure to acquire a lock, and blocks only if none are
357 <     * available (i.e., only very rarely).  The transfer operation
358 <     * must also ensure that all accessible bins in both the old and
359 <     * new table are usable by any traversal.  When there are no lock
360 <     * acquisition failures, this is arranged simply by proceeding
361 <     * from the last bin (table.length - 1) up towards the first.
362 <     * Upon seeing a forwarding node, traversals (see class
363 <     * InternalIterator) arrange to move to the new table without
364 <     * revisiting nodes.  However, when any node is skipped during a
365 <     * transfer, all earlier table bins may have become visible, so
366 <     * are initialized with a reverse-forwarding node back to the old
367 <     * table until the new ones are established. (This sometimes
368 <     * requires transiently locking a forwarding node, which is
369 <     * possible under the above encoding.) These more expensive
354 >     * lock, and revisit it later (unless it is a TreeBin). Method
355 >     * rebuild maintains a buffer of TRANSFER_BUFFER_SIZE bins that
356 >     * have been skipped because of failure to acquire a lock, and
357 >     * blocks only if none are available (i.e., only very rarely).
358 >     * The transfer operation must also ensure that all accessible
359 >     * bins in both the old and new table are usable by any traversal.
360 >     * When there are no lock acquisition failures, this is arranged
361 >     * simply by proceeding from the last bin (table.length - 1) up
362 >     * towards the first.  Upon seeing a forwarding node, traversals
363 >     * (see class InternalIterator) arrange to move to the new table
364 >     * without revisiting nodes.  However, when any node is skipped
365 >     * during a transfer, all earlier table bins may have become
366 >     * visible, so are initialized with a reverse-forwarding node back
367 >     * to the old table until the new ones are established. (This
368 >     * sometimes requires transiently locking a forwarding node, which
369 >     * is possible under the above encoding.) These more expensive
370       * mechanics trigger only when necessary.
371       *
372       * The traversal scheme also applies to partial traversals of
373       * ranges of bins (via an alternate InternalIterator constructor)
374 <     * to support partitioned aggregate operations (that are not
375 <     * otherwise implemented yet).  Also, read-only operations give up
376 <     * if ever forwarded to a null table, which provides support for
377 <     * shutdown-style clearing, which is also not currently
258 <     * implemented.
374 >     * to support partitioned aggregate operations.  Also, read-only
375 >     * operations give up if ever forwarded to a null table, which
376 >     * provides support for shutdown-style clearing, which is also not
377 >     * currently implemented.
378       *
379       * Lazy table initialization minimizes footprint until first use,
380       * and also avoids resizings when the first operation is from a
# Line 266 | Line 385 | public class ConcurrentHashMapV8<K, V>
385       * The element count is maintained using a LongAdder, which avoids
386       * contention on updates but can encounter cache thrashing if read
387       * too frequently during concurrent access. To avoid reading so
388 <     * often, resizing is normally attempted only upon adding to a bin
389 <     * already holding two or more nodes. Under uniform hash
390 <     * distributions, the probability of this occurring at threshold
391 <     * is around 13%, meaning that only about 1 in 8 puts check
392 <     * threshold (and after resizing, many fewer do so). But this
393 <     * approximation has high variance for small table sizes, so we
394 <     * check on any collision for sizes <= 64.
388 >     * often, resizing is attempted either when a bin lock is
389 >     * contended, or upon adding to a bin already holding two or more
390 >     * nodes (checked before adding in the xIfAbsent methods, after
391 >     * adding in others). Under uniform hash distributions, the
392 >     * probability of this occurring at threshold is around 13%,
393 >     * meaning that only about 1 in 8 puts check threshold (and after
394 >     * resizing, many fewer do so). But this approximation has high
395 >     * variance for small table sizes, so we check on any collision
396 >     * for sizes <= 64. The bulk putAll operation further reduces
397 >     * contention by only committing count updates upon these size
398 >     * checks.
399       *
400       * Maintaining API and serialization compatibility with previous
401       * versions of this class introduces several oddities. Mainly: We
# Line 329 | Line 452 | public class ConcurrentHashMapV8<K, V>
452       */
453      private static final int TRANSFER_BUFFER_SIZE = 32;
454  
455 +    /**
456 +     * The bin count threshold for using a tree rather than list for a
457 +     * bin.  The value reflects the approximate break-even point for
458 +     * using tree-based operations.
459 +     */
460 +    private static final int TREE_THRESHOLD = 8;
461 +
462      /*
463       * Encodings for special uses of Node hash fields. See above for
464       * explanation.
465       */
466 <    static final int MOVED     = 0x80000000; // hash field for fowarding nodes
466 >    static final int MOVED     = 0x80000000; // hash field for forwarding nodes
467      static final int LOCKED    = 0x40000000; // set/tested only as a bit
468      static final int WAITING   = 0xc0000000; // both bits set/tested together
469      static final int HASH_BITS = 0x3fffffff; // usable bits of normal node hash
# Line 368 | Line 498 | public class ConcurrentHashMapV8<K, V>
498      /** For serialization compatibility. Null unless serialized; see below */
499      private Segment<K,V>[] segments;
500  
501 +    /* ---------------- Table element access -------------- */
502 +
503 +    /*
504 +     * Volatile access methods are used for table elements as well as
505 +     * elements of in-progress next table while resizing.  Uses are
506 +     * null checked by callers, and implicitly bounds-checked, relying
507 +     * on the invariants that tab arrays have non-zero size, and all
508 +     * indices are masked with (tab.length - 1) which is never
509 +     * negative and always less than length. Note that, to be correct
510 +     * wrt arbitrary concurrency errors by users, bounds checks must
511 +     * operate on local variables, which accounts for some odd-looking
512 +     * inline assignments below.
513 +     */
514 +
515 +    static final Node tabAt(Node[] tab, int i) { // used by InternalIterator
516 +        return (Node)UNSAFE.getObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE);
517 +    }
518 +
519 +    private static final boolean casTabAt(Node[] tab, int i, Node c, Node v) {
520 +        return UNSAFE.compareAndSwapObject(tab, ((long)i<<ASHIFT)+ABASE, c, v);
521 +    }
522 +
523 +    private static final void setTabAt(Node[] tab, int i, Node v) {
524 +        UNSAFE.putObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE, v);
525 +    }
526 +
527      /* ---------------- Nodes -------------- */
528  
529      /**
530       * Key-value entry. Note that this is never exported out as a
531 <     * user-visible Map.Entry (see WriteThroughEntry and SnapshotEntry
532 <     * below). Nodes with a negative hash field are special, and do
533 <     * not contain user keys or values.  Otherwise, keys are never
534 <     * null, and null val fields indicate that a node is in the
535 <     * process of being deleted or created. For purposes of read-only
536 <     * access, a key may be read before a val, but can only be used
537 <     * after checking val to be non-null.
531 >     * user-visible Map.Entry (see MapEntry below). Nodes with a hash
532 >     * field of MOVED are special, and do not contain user keys or
533 >     * values.  Otherwise, keys are never null, and null val fields
534 >     * indicate that a node is in the process of being deleted or
535 >     * created. For purposes of read-only access, a key may be read
536 >     * before a val, but can only be used after checking val to be
537 >     * non-null.
538       */
539 <    static final class Node {
539 >    static class Node {
540          volatile int hash;
541          final Object key;
542          volatile Object val;
# Line 417 | Line 573 | public class ConcurrentHashMapV8<K, V>
573           */
574          final void tryAwaitLock(Node[] tab, int i) {
575              if (tab != null && i >= 0 && i < tab.length) { // bounds check
576 +                int r = ThreadLocalRandom.current().nextInt(); // randomize spins
577                  int spins = MAX_SPINS, h;
578                  while (tabAt(tab, i) == this && ((h = hash) & LOCKED) != 0) {
579                      if (spins >= 0) {
580 <                        if (--spins == MAX_SPINS >>> 1)
581 <                            Thread.yield();  // heuristically yield mid-way
580 >                        r ^= r << 1; r ^= r >>> 3; r ^= r << 10; // xorshift
581 >                        if (r >= 0 && --spins == 0)
582 >                            Thread.yield();  // yield before block
583                      }
584                      else if (casHash(h, h | WAITING)) {
585                          synchronized (this) {
# Line 458 | Line 616 | public class ConcurrentHashMapV8<K, V>
616          }
617      }
618  
619 <    /* ---------------- Table element access -------------- */
619 >    /* ---------------- TreeBins -------------- */
620  
621 <    /*
622 <     * Volatile access methods are used for table elements as well as
465 <     * elements of in-progress next table while resizing.  Uses are
466 <     * null checked by callers, and implicitly bounds-checked, relying
467 <     * on the invariants that tab arrays have non-zero size, and all
468 <     * indices are masked with (tab.length - 1) which is never
469 <     * negative and always less than length. Note that, to be correct
470 <     * wrt arbitrary concurrency errors by users, bounds checks must
471 <     * operate on local variables, which accounts for some odd-looking
472 <     * inline assignments below.
621 >    /**
622 >     * Nodes for use in TreeBins
623       */
624 <
625 <    static final Node tabAt(Node[] tab, int i) { // used by InternalIterator
626 <        return (Node)UNSAFE.getObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE);
624 >    static final class TreeNode extends Node {
625 >        TreeNode parent;  // red-black tree links
626 >        TreeNode left;
627 >        TreeNode right;
628 >        TreeNode prev;    // needed to unlink next upon deletion
629 >        boolean red;
630 >
631 >        TreeNode(int hash, Object key, Object val, Node next, TreeNode parent) {
632 >            super(hash, key, val, next);
633 >            this.parent = parent;
634 >        }
635      }
636  
637 <    private static final boolean casTabAt(Node[] tab, int i, Node c, Node v) {
638 <        return UNSAFE.compareAndSwapObject(tab, ((long)i<<ASHIFT)+ABASE, c, v);
639 <    }
637 >    /**
638 >     * A specialized form of red-black tree for use in bins
639 >     * whose size exceeds a threshold.
640 >     *
641 >     * TreeBins use a special form of comparison for search and
642 >     * related operations (which is the main reason we cannot use
643 >     * existing collections such as TreeMaps). TreeBins contain
644 >     * Comparable elements, but may contain others, as well as
645 >     * elements that are Comparable but not necessarily Comparable<T>
646 >     * for the same T, so we cannot invoke compareTo among them. To
647 >     * handle this, the tree is ordered primarily by hash value, then
648 >     * by getClass().getName() order, and then by Comparator order
649 >     * among elements of the same class.  On lookup at a node, if
650 >     * elements are not comparable or compare as 0, both left and
651 >     * right children may need to be searched in the case of tied hash
652 >     * values. (This corresponds to the full list search that would be
653 >     * necessary if all elements were non-Comparable and had tied
654 >     * hashes.)  The red-black balancing code is updated from
655 >     * pre-jdk-collections
656 >     * (http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java)
657 >     * based in turn on Cormen, Leiserson, and Rivest "Introduction to
658 >     * Algorithms" (CLR).
659 >     *
660 >     * TreeBins also maintain a separate locking discipline than
661 >     * regular bins. Because they are forwarded via special MOVED
662 >     * nodes at bin heads (which can never change once established),
663 >     * we cannot use use those nodes as locks. Instead, TreeBin
664 >     * extends AbstractQueuedSynchronizer to support a simple form of
665 >     * read-write lock. For update operations and table validation,
666 >     * the exclusive form of lock behaves in the same way as bin-head
667 >     * locks. However, lookups use shared read-lock mechanics to allow
668 >     * multiple readers in the absence of writers.  Additionally,
669 >     * these lookups do not ever block: While the lock is not
670 >     * available, they proceed along the slow traversal path (via
671 >     * next-pointers) until the lock becomes available or the list is
672 >     * exhausted, whichever comes first. (These cases are not fast,
673 >     * but maximize aggregate expected throughput.)  The AQS mechanics
674 >     * for doing this are straightforward.  The lock state is held as
675 >     * AQS getState().  Read counts are negative; the write count (1)
676 >     * is positive.  There are no signalling preferences among readers
677 >     * and writers. Since we don't need to export full Lock API, we
678 >     * just override the minimal AQS methods and use them directly.
679 >     */
680 >    static final class TreeBin extends AbstractQueuedSynchronizer {
681 >        private static final long serialVersionUID = 2249069246763182397L;
682 >        transient TreeNode root;  // root of tree
683 >        transient TreeNode first; // head of next-pointer list
684  
685 <    private static final void setTabAt(Node[] tab, int i, Node v) {
686 <        UNSAFE.putObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE, v);
687 <    }
685 >        /* AQS overrides */
686 >        public final boolean isHeldExclusively() { return getState() > 0; }
687 >        public final boolean tryAcquire(int ignore) {
688 >            if (compareAndSetState(0, 1)) {
689 >                setExclusiveOwnerThread(Thread.currentThread());
690 >                return true;
691 >            }
692 >            return false;
693 >        }
694 >        public final boolean tryRelease(int ignore) {
695 >            setExclusiveOwnerThread(null);
696 >            setState(0);
697 >            return true;
698 >        }
699 >        public final int tryAcquireShared(int ignore) {
700 >            for (int c;;) {
701 >                if ((c = getState()) > 0)
702 >                    return -1;
703 >                if (compareAndSetState(c, c -1))
704 >                    return 1;
705 >            }
706 >        }
707 >        public final boolean tryReleaseShared(int ignore) {
708 >            int c;
709 >            do {} while (!compareAndSetState(c = getState(), c + 1));
710 >            return c == -1;
711 >        }
712 >
713 >        /** From CLR */
714 >        private void rotateLeft(TreeNode p) {
715 >            if (p != null) {
716 >                TreeNode r = p.right, pp, rl;
717 >                if ((rl = p.right = r.left) != null)
718 >                    rl.parent = p;
719 >                if ((pp = r.parent = p.parent) == null)
720 >                    root = r;
721 >                else if (pp.left == p)
722 >                    pp.left = r;
723 >                else
724 >                    pp.right = r;
725 >                r.left = p;
726 >                p.parent = r;
727 >            }
728 >        }
729  
730 <    /* ---------------- Internal access and update methods -------------- */
730 >        /** From CLR */
731 >        private void rotateRight(TreeNode p) {
732 >            if (p != null) {
733 >                TreeNode l = p.left, pp, lr;
734 >                if ((lr = p.left = l.right) != null)
735 >                    lr.parent = p;
736 >                if ((pp = l.parent = p.parent) == null)
737 >                    root = l;
738 >                else if (pp.right == p)
739 >                    pp.right = l;
740 >                else
741 >                    pp.left = l;
742 >                l.right = p;
743 >                p.parent = l;
744 >            }
745 >        }
746  
747 <    /**
748 <     * Applies a supplemental hash function to a given hashCode, which
749 <     * defends against poor quality hash functions.  The result must
750 <     * be have the top 2 bits clear. For reasonable performance, this
751 <     * function must have good avalanche properties; i.e., that each
752 <     * bit of the argument affects each bit of the result. (Although
753 <     * we don't care about the unused top 2 bits.)
754 <     */
755 <    private static final int spread(int h) {
756 <        // Apply base step of MurmurHash; see http://code.google.com/p/smhasher/
757 <        h ^= h >>> 16;
758 <        h *= 0x85ebca6b;
759 <        h ^= h >>> 13;
760 <        h *= 0xc2b2ae35;
761 <        return ((h >>> 16) ^ h) & HASH_BITS; // mask out top bits
762 <    }
747 >        /**
748 >         * Return the TreeNode (or null if not found) for the given key
749 >         * starting at given root.
750 >         */
751 >        @SuppressWarnings("unchecked") // suppress Comparable cast warning
752 >        final TreeNode getTreeNode(int h, Object k, TreeNode p) {
753 >            Class<?> c = k.getClass();
754 >            while (p != null) {
755 >                int dir, ph;  Object pk; Class<?> pc;
756 >                if ((ph = p.hash) == h) {
757 >                    if ((pk = p.key) == k || k.equals(pk))
758 >                        return p;
759 >                    if (c != (pc = pk.getClass()) ||
760 >                        !(k instanceof Comparable) ||
761 >                        (dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) {
762 >                        dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName());
763 >                        TreeNode r = null, s = null, pl, pr;
764 >                        if (dir >= 0) {
765 >                            if ((pl = p.left) != null && h <= pl.hash)
766 >                                s = pl;
767 >                        }
768 >                        else if ((pr = p.right) != null && h >= pr.hash)
769 >                            s = pr;
770 >                        if (s != null && (r = getTreeNode(h, k, s)) != null)
771 >                            return r;
772 >                    }
773 >                }
774 >                else
775 >                    dir = (h < ph) ? -1 : 1;
776 >                p = (dir > 0) ? p.right : p.left;
777 >            }
778 >            return null;
779 >        }
780  
781 <    /** Implementation for get and containsKey */
782 <    private final Object internalGet(Object k) {
783 <        int h = spread(k.hashCode());
784 <        retry: for (Node[] tab = table; tab != null;) {
785 <            Node e; Object ek, ev; int eh;    // locals to read fields once
786 <            for (e = tabAt(tab, (tab.length - 1) & h); e != null; e = e.next) {
787 <                if ((eh = e.hash) == MOVED) {
788 <                    tab = (Node[])e.key;      // restart with new table
789 <                    continue retry;
781 >        /**
782 >         * Wrapper for getTreeNode used by CHM.get. Tries to obtain
783 >         * read-lock to call getTreeNode, but during failure to get
784 >         * lock, searches along next links.
785 >         */
786 >        final Object getValue(int h, Object k) {
787 >            Node r = null;
788 >            int c = getState(); // Must read lock state first
789 >            for (Node e = first; e != null; e = e.next) {
790 >                if (c <= 0 && compareAndSetState(c, c - 1)) {
791 >                    try {
792 >                        r = getTreeNode(h, k, root);
793 >                    } finally {
794 >                        releaseShared(0);
795 >                    }
796 >                    break;
797                  }
798 <                if ((eh & HASH_BITS) == h && (ev = e.val) != null &&
799 <                    ((ek = e.key) == k || k.equals(ek)))
800 <                    return ev;
798 >                else if ((e.hash & HASH_BITS) == h && k.equals(e.key)) {
799 >                    r = e;
800 >                    break;
801 >                }
802 >                else
803 >                    c = getState();
804              }
805 <            break;
805 >            return r == null ? null : r.val;
806          }
522        return null;
523    }
807  
808 <    /** Implementation for put and putIfAbsent */
809 <    private final Object internalPut(Object k, Object v, boolean replace) {
810 <        int h = spread(k.hashCode());
811 <        Object oldVal = null;               // previous value or null if none
812 <        for (Node[] tab = table;;) {
813 <            int i; Node f; int fh; Object fk, fv;
814 <            if (tab == null)
815 <                tab = initTable();
816 <            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
817 <                if (casTabAt(tab, i, null, new Node(h, k, v, null)))
818 <                    break;                   // no lock when adding to empty bin
808 >        /**
809 >         * Find or add a node
810 >         * @return null if added
811 >         */
812 >        @SuppressWarnings("unchecked") // suppress Comparable cast warning
813 >        final TreeNode putTreeNode(int h, Object k, Object v) {
814 >            Class<?> c = k.getClass();
815 >            TreeNode pp = root, p = null;
816 >            int dir = 0;
817 >            while (pp != null) { // find existing node or leaf to insert at
818 >                int ph;  Object pk; Class<?> pc;
819 >                p = pp;
820 >                if ((ph = p.hash) == h) {
821 >                    if ((pk = p.key) == k || k.equals(pk))
822 >                        return p;
823 >                    if (c != (pc = pk.getClass()) ||
824 >                        !(k instanceof Comparable) ||
825 >                        (dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) {
826 >                        dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName());
827 >                        TreeNode r = null, s = null, pl, pr;
828 >                        if (dir >= 0) {
829 >                            if ((pl = p.left) != null && h <= pl.hash)
830 >                                s = pl;
831 >                        }
832 >                        else if ((pr = p.right) != null && h >= pr.hash)
833 >                            s = pr;
834 >                        if (s != null && (r = getTreeNode(h, k, s)) != null)
835 >                            return r;
836 >                    }
837 >                }
838 >                else
839 >                    dir = (h < ph) ? -1 : 1;
840 >                pp = (dir > 0) ? p.right : p.left;
841 >            }
842 >
843 >            TreeNode f = first;
844 >            TreeNode x = first = new TreeNode(h, k, v, f, p);
845 >            if (p == null)
846 >                root = x;
847 >            else { // attach and rebalance; adapted from CLR
848 >                TreeNode xp, xpp;
849 >                if (f != null)
850 >                    f.prev = x;
851 >                if (dir <= 0)
852 >                    p.left = x;
853 >                else
854 >                    p.right = x;
855 >                x.red = true;
856 >                while (x != null && (xp = x.parent) != null && xp.red &&
857 >                       (xpp = xp.parent) != null) {
858 >                    TreeNode xppl = xpp.left;
859 >                    if (xp == xppl) {
860 >                        TreeNode y = xpp.right;
861 >                        if (y != null && y.red) {
862 >                            y.red = false;
863 >                            xp.red = false;
864 >                            xpp.red = true;
865 >                            x = xpp;
866 >                        }
867 >                        else {
868 >                            if (x == xp.right) {
869 >                                rotateLeft(x = xp);
870 >                                xpp = (xp = x.parent) == null ? null : xp.parent;
871 >                            }
872 >                            if (xp != null) {
873 >                                xp.red = false;
874 >                                if (xpp != null) {
875 >                                    xpp.red = true;
876 >                                    rotateRight(xpp);
877 >                                }
878 >                            }
879 >                        }
880 >                    }
881 >                    else {
882 >                        TreeNode y = xppl;
883 >                        if (y != null && y.red) {
884 >                            y.red = false;
885 >                            xp.red = false;
886 >                            xpp.red = true;
887 >                            x = xpp;
888 >                        }
889 >                        else {
890 >                            if (x == xp.left) {
891 >                                rotateRight(x = xp);
892 >                                xpp = (xp = x.parent) == null ? null : xp.parent;
893 >                            }
894 >                            if (xp != null) {
895 >                                xp.red = false;
896 >                                if (xpp != null) {
897 >                                    xpp.red = true;
898 >                                    rotateLeft(xpp);
899 >                                }
900 >                            }
901 >                        }
902 >                    }
903 >                }
904 >                TreeNode r = root;
905 >                if (r != null && r.red)
906 >                    r.red = false;
907              }
908 <            else if ((fh = f.hash) == MOVED)
909 <                tab = (Node[])f.key;
910 <            else if (!replace && (fh & HASH_BITS) == h && (fv = f.val) != null &&
911 <                     ((fk = f.key) == k || k.equals(fk))) {
912 <                oldVal = fv;                // precheck 1st node for putIfAbsent
913 <                break;
908 >            return null;
909 >        }
910 >
911 >        /**
912 >         * Removes the given node, that must be present before this
913 >         * call.  This is messier than typical red-black deletion code
914 >         * because we cannot swap the contents of an interior node
915 >         * with a leaf successor that is pinned by "next" pointers
916 >         * that are accessible independently of lock. So instead we
917 >         * swap the tree linkages.
918 >         */
919 >        final void deleteTreeNode(TreeNode p) {
920 >            TreeNode next = (TreeNode)p.next; // unlink traversal pointers
921 >            TreeNode pred = p.prev;
922 >            if (pred == null)
923 >                first = next;
924 >            else
925 >                pred.next = next;
926 >            if (next != null)
927 >                next.prev = pred;
928 >            TreeNode replacement;
929 >            TreeNode pl = p.left;
930 >            TreeNode pr = p.right;
931 >            if (pl != null && pr != null) {
932 >                TreeNode s = pr, sl;
933 >                while ((sl = s.left) != null) // find successor
934 >                    s = sl;
935 >                boolean c = s.red; s.red = p.red; p.red = c; // swap colors
936 >                TreeNode sr = s.right;
937 >                TreeNode pp = p.parent;
938 >                if (s == pr) { // p was s's direct parent
939 >                    p.parent = s;
940 >                    s.right = p;
941 >                }
942 >                else {
943 >                    TreeNode sp = s.parent;
944 >                    if ((p.parent = sp) != null) {
945 >                        if (s == sp.left)
946 >                            sp.left = p;
947 >                        else
948 >                            sp.right = p;
949 >                    }
950 >                    if ((s.right = pr) != null)
951 >                        pr.parent = s;
952 >                }
953 >                p.left = null;
954 >                if ((p.right = sr) != null)
955 >                    sr.parent = p;
956 >                if ((s.left = pl) != null)
957 >                    pl.parent = s;
958 >                if ((s.parent = pp) == null)
959 >                    root = s;
960 >                else if (p == pp.left)
961 >                    pp.left = s;
962 >                else
963 >                    pp.right = s;
964 >                replacement = sr;
965              }
966 <            else if ((fh & LOCKED) != 0)
967 <                f.tryAwaitLock(tab, i);
968 <            else if (f.casHash(fh, fh | LOCKED)) {
969 <                boolean validated = false;
970 <                boolean checkSize = false;
971 <                try {
972 <                    if (tabAt(tab, i) == f) {
973 <                        validated = true;    // retry if 1st already deleted
974 <                        for (Node e = f;;) {
975 <                            Object ek, ev;
976 <                            if ((e.hash & HASH_BITS) == h &&
977 <                                (ev = e.val) != null &&
978 <                                ((ek = e.key) == k || k.equals(ek))) {
979 <                                oldVal = ev;
980 <                                if (replace)
981 <                                    e.val = v;
982 <                                break;
966 >            else
967 >                replacement = (pl != null) ? pl : pr;
968 >            TreeNode pp = p.parent;
969 >            if (replacement == null) {
970 >                if (pp == null) {
971 >                    root = null;
972 >                    return;
973 >                }
974 >                replacement = p;
975 >            }
976 >            else {
977 >                replacement.parent = pp;
978 >                if (pp == null)
979 >                    root = replacement;
980 >                else if (p == pp.left)
981 >                    pp.left = replacement;
982 >                else
983 >                    pp.right = replacement;
984 >                p.left = p.right = p.parent = null;
985 >            }
986 >            if (!p.red) { // rebalance, from CLR
987 >                TreeNode x = replacement;
988 >                while (x != null) {
989 >                    TreeNode xp, xpl;
990 >                    if (x.red || (xp = x.parent) == null) {
991 >                        x.red = false;
992 >                        break;
993 >                    }
994 >                    if (x == (xpl = xp.left)) {
995 >                        TreeNode sib = xp.right;
996 >                        if (sib != null && sib.red) {
997 >                            sib.red = false;
998 >                            xp.red = true;
999 >                            rotateLeft(xp);
1000 >                            sib = (xp = x.parent) == null ? null : xp.right;
1001 >                        }
1002 >                        if (sib == null)
1003 >                            x = xp;
1004 >                        else {
1005 >                            TreeNode sl = sib.left, sr = sib.right;
1006 >                            if ((sr == null || !sr.red) &&
1007 >                                (sl == null || !sl.red)) {
1008 >                                sib.red = true;
1009 >                                x = xp;
1010                              }
1011 <                            Node last = e;
1012 <                            if ((e = e.next) == null) {
1013 <                                last.next = new Node(h, k, v, null);
1014 <                                if (last != f || tab.length <= 64)
1015 <                                    checkSize = true;
1016 <                                break;
1011 >                            else {
1012 >                                if (sr == null || !sr.red) {
1013 >                                    if (sl != null)
1014 >                                        sl.red = false;
1015 >                                    sib.red = true;
1016 >                                    rotateRight(sib);
1017 >                                    sib = (xp = x.parent) == null ? null : xp.right;
1018 >                                }
1019 >                                if (sib != null) {
1020 >                                    sib.red = (xp == null) ? false : xp.red;
1021 >                                    if ((sr = sib.right) != null)
1022 >                                        sr.red = false;
1023 >                                }
1024 >                                if (xp != null) {
1025 >                                    xp.red = false;
1026 >                                    rotateLeft(xp);
1027 >                                }
1028 >                                x = root;
1029                              }
1030                          }
1031                      }
1032 <                } finally {                  // unlock and signal if needed
1033 <                    if (!f.casHash(fh | LOCKED, fh)) {
1034 <                        f.hash = fh;
1035 <                        synchronized (f) { f.notifyAll(); };
1032 >                    else { // symmetric
1033 >                        TreeNode sib = xpl;
1034 >                        if (sib != null && sib.red) {
1035 >                            sib.red = false;
1036 >                            xp.red = true;
1037 >                            rotateRight(xp);
1038 >                            sib = (xp = x.parent) == null ? null : xp.left;
1039 >                        }
1040 >                        if (sib == null)
1041 >                            x = xp;
1042 >                        else {
1043 >                            TreeNode sl = sib.left, sr = sib.right;
1044 >                            if ((sl == null || !sl.red) &&
1045 >                                (sr == null || !sr.red)) {
1046 >                                sib.red = true;
1047 >                                x = xp;
1048 >                            }
1049 >                            else {
1050 >                                if (sl == null || !sl.red) {
1051 >                                    if (sr != null)
1052 >                                        sr.red = false;
1053 >                                    sib.red = true;
1054 >                                    rotateLeft(sib);
1055 >                                    sib = (xp = x.parent) == null ? null : xp.left;
1056 >                                }
1057 >                                if (sib != null) {
1058 >                                    sib.red = (xp == null) ? false : xp.red;
1059 >                                    if ((sl = sib.left) != null)
1060 >                                        sl.red = false;
1061 >                                }
1062 >                                if (xp != null) {
1063 >                                    xp.red = false;
1064 >                                    rotateRight(xp);
1065 >                                }
1066 >                                x = root;
1067 >                            }
1068 >                        }
1069                      }
1070                  }
1071 <                if (validated) {
1072 <                    int sc;
1073 <                    if (checkSize && tab.length < MAXIMUM_CAPACITY &&
1074 <                        (sc = sizeCtl) >= 0 && counter.sum() >= (long)sc)
1075 <                        growTable();
1076 <                    break;
1071 >            }
1072 >            if (p == replacement && (pp = p.parent) != null) {
1073 >                if (p == pp.left) // detach pointers
1074 >                    pp.left = null;
1075 >                else if (p == pp.right)
1076 >                    pp.right = null;
1077 >                p.parent = null;
1078 >            }
1079 >        }
1080 >    }
1081 >
1082 >    /* ---------------- Collision reduction methods -------------- */
1083 >
1084 >    /**
1085 >     * Spreads higher bits to lower, and also forces top 2 bits to 0.
1086 >     * Because the table uses power-of-two masking, sets of hashes
1087 >     * that vary only in bits above the current mask will always
1088 >     * collide. (Among known examples are sets of Float keys holding
1089 >     * consecutive whole numbers in small tables.)  To counter this,
1090 >     * we apply a transform that spreads the impact of higher bits
1091 >     * downward. There is a tradeoff between speed, utility, and
1092 >     * quality of bit-spreading. Because many common sets of hashes
1093 >     * are already reasonably distributed across bits (so don't benefit
1094 >     * from spreading), and because we use trees to handle large sets
1095 >     * of collisions in bins, we don't need excessively high quality.
1096 >     */
1097 >    private static final int spread(int h) {
1098 >        h ^= (h >>> 18) ^ (h >>> 12);
1099 >        return (h ^ (h >>> 10)) & HASH_BITS;
1100 >    }
1101 >
1102 >    /**
1103 >     * Replaces a list bin with a tree bin. Call only when locked.
1104 >     * Fails to replace if the given key is non-comparable or table
1105 >     * is, or needs, resizing.
1106 >     */
1107 >    private final void replaceWithTreeBin(Node[] tab, int index, Object key) {
1108 >        if ((key instanceof Comparable) &&
1109 >            (tab.length >= MAXIMUM_CAPACITY || counter.sum() < (long)sizeCtl)) {
1110 >            TreeBin t = new TreeBin();
1111 >            for (Node e = tabAt(tab, index); e != null; e = e.next)
1112 >                t.putTreeNode(e.hash & HASH_BITS, e.key, e.val);
1113 >            setTabAt(tab, index, new Node(MOVED, t, null, null));
1114 >        }
1115 >    }
1116 >
1117 >    /* ---------------- Internal access and update methods -------------- */
1118 >
1119 >    /** Implementation for get and containsKey */
1120 >    private final Object internalGet(Object k) {
1121 >        int h = spread(k.hashCode());
1122 >        retry: for (Node[] tab = table; tab != null;) {
1123 >            Node e, p; Object ek, ev; int eh;      // locals to read fields once
1124 >            for (e = tabAt(tab, (tab.length - 1) & h); e != null; e = e.next) {
1125 >                if ((eh = e.hash) == MOVED) {
1126 >                    if ((ek = e.key) instanceof TreeBin)  // search TreeBin
1127 >                        return ((TreeBin)ek).getValue(h, k);
1128 >                    else {                        // restart with new table
1129 >                        tab = (Node[])ek;
1130 >                        continue retry;
1131 >                    }
1132                  }
1133 +                else if ((eh & HASH_BITS) == h && (ev = e.val) != null &&
1134 +                         ((ek = e.key) == k || k.equals(ek)))
1135 +                    return ev;
1136              }
1137 +            break;
1138          }
1139 <        if (oldVal == null)
587 <            counter.increment();             // update counter outside of locks
588 <        return oldVal;
1139 >        return null;
1140      }
1141  
1142      /**
# Line 597 | Line 1148 | public class ConcurrentHashMapV8<K, V>
1148          int h = spread(k.hashCode());
1149          Object oldVal = null;
1150          for (Node[] tab = table;;) {
1151 <            Node f; int i, fh;
1151 >            Node f; int i, fh; Object fk;
1152              if (tab == null ||
1153                  (f = tabAt(tab, i = (tab.length - 1) & h)) == null)
1154                  break;
1155 <            else if ((fh = f.hash) == MOVED)
1156 <                tab = (Node[])f.key;
1155 >            else if ((fh = f.hash) == MOVED) {
1156 >                if ((fk = f.key) instanceof TreeBin) {
1157 >                    TreeBin t = (TreeBin)fk;
1158 >                    boolean validated = false;
1159 >                    boolean deleted = false;
1160 >                    t.acquire(0);
1161 >                    try {
1162 >                        if (tabAt(tab, i) == f) {
1163 >                            validated = true;
1164 >                            TreeNode p = t.getTreeNode(h, k, t.root);
1165 >                            if (p != null) {
1166 >                                Object pv = p.val;
1167 >                                if (cv == null || cv == pv || cv.equals(pv)) {
1168 >                                    oldVal = pv;
1169 >                                    if ((p.val = v) == null) {
1170 >                                        deleted = true;
1171 >                                        t.deleteTreeNode(p);
1172 >                                    }
1173 >                                }
1174 >                            }
1175 >                        }
1176 >                    } finally {
1177 >                        t.release(0);
1178 >                    }
1179 >                    if (validated) {
1180 >                        if (deleted)
1181 >                            counter.add(-1L);
1182 >                        break;
1183 >                    }
1184 >                }
1185 >                else
1186 >                    tab = (Node[])fk;
1187 >            }
1188              else if ((fh & HASH_BITS) != h && f.next == null) // precheck
1189                  break;                          // rules out possible existence
1190 <            else if ((fh & LOCKED) != 0)
1190 >            else if ((fh & LOCKED) != 0) {
1191 >                checkForResize();               // try resizing if can't get lock
1192                  f.tryAwaitLock(tab, i);
1193 +            }
1194              else if (f.casHash(fh, fh | LOCKED)) {
1195                  boolean validated = false;
1196                  boolean deleted = false;
# Line 644 | Line 1228 | public class ConcurrentHashMapV8<K, V>
1228                  }
1229                  if (validated) {
1230                      if (deleted)
1231 <                        counter.decrement();
1231 >                        counter.add(-1L);
1232                      break;
1233                  }
1234              }
# Line 652 | Line 1236 | public class ConcurrentHashMapV8<K, V>
1236          return oldVal;
1237      }
1238  
1239 <    /** Implementation for computeIfAbsent and compute. Like put, but messier. */
1240 <    // Todo: Somehow reinstate non-termination check
1241 <    @SuppressWarnings("unchecked")
1242 <    private final V internalCompute(K k,
1243 <                                    MappingFunction<? super K, ? extends V> fn,
1244 <                                    boolean replace) {
1239 >    /*
1240 >     * Internal versions of the five insertion methods, each a
1241 >     * little more complicated than the last. All have
1242 >     * the same basic structure as the first (internalPut):
1243 >     *  1. If table uninitialized, create
1244 >     *  2. If bin empty, try to CAS new node
1245 >     *  3. If bin stale, use new table
1246 >     *  4. if bin converted to TreeBin, validate and relay to TreeBin methods
1247 >     *  5. Lock and validate; if valid, scan and add or update
1248 >     *
1249 >     * The others interweave other checks and/or alternative actions:
1250 >     *  * Plain put checks for and performs resize after insertion.
1251 >     *  * putIfAbsent prescans for mapping without lock (and fails to add
1252 >     *    if present), which also makes pre-emptive resize checks worthwhile.
1253 >     *  * computeIfAbsent extends form used in putIfAbsent with additional
1254 >     *    mechanics to deal with, calls, potential exceptions and null
1255 >     *    returns from function call.
1256 >     *  * compute uses the same function-call mechanics, but without
1257 >     *    the prescans
1258 >     *  * putAll attempts to pre-allocate enough table space
1259 >     *    and more lazily performs count updates and checks.
1260 >     *
1261 >     * Someday when details settle down a bit more, it might be worth
1262 >     * some factoring to reduce sprawl.
1263 >     */
1264 >
1265 >    /** Implementation for put */
1266 >    private final Object internalPut(Object k, Object v) {
1267          int h = spread(k.hashCode());
1268 <        V val = null;
1269 <        boolean added = false;
1270 <        Node[] tab = table;
1271 <        outer:for (;;) {
1268 >        int count = 0;
1269 >        for (Node[] tab = table;;) {
1270 >            int i; Node f; int fh; Object fk;
1271 >            if (tab == null)
1272 >                tab = initTable();
1273 >            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1274 >                if (casTabAt(tab, i, null, new Node(h, k, v, null)))
1275 >                    break;                   // no lock when adding to empty bin
1276 >            }
1277 >            else if ((fh = f.hash) == MOVED) {
1278 >                if ((fk = f.key) instanceof TreeBin) {
1279 >                    TreeBin t = (TreeBin)fk;
1280 >                    Object oldVal = null;
1281 >                    t.acquire(0);
1282 >                    try {
1283 >                        if (tabAt(tab, i) == f) {
1284 >                            count = 2;
1285 >                            TreeNode p = t.putTreeNode(h, k, v);
1286 >                            if (p != null) {
1287 >                                oldVal = p.val;
1288 >                                p.val = v;
1289 >                            }
1290 >                        }
1291 >                    } finally {
1292 >                        t.release(0);
1293 >                    }
1294 >                    if (count != 0) {
1295 >                        if (oldVal != null)
1296 >                            return oldVal;
1297 >                        break;
1298 >                    }
1299 >                }
1300 >                else
1301 >                    tab = (Node[])fk;
1302 >            }
1303 >            else if ((fh & LOCKED) != 0) {
1304 >                checkForResize();
1305 >                f.tryAwaitLock(tab, i);
1306 >            }
1307 >            else if (f.casHash(fh, fh | LOCKED)) {
1308 >                Object oldVal = null;
1309 >                try {                        // needed in case equals() throws
1310 >                    if (tabAt(tab, i) == f) {
1311 >                        count = 1;
1312 >                        for (Node e = f;; ++count) {
1313 >                            Object ek, ev;
1314 >                            if ((e.hash & HASH_BITS) == h &&
1315 >                                (ev = e.val) != null &&
1316 >                                ((ek = e.key) == k || k.equals(ek))) {
1317 >                                oldVal = ev;
1318 >                                e.val = v;
1319 >                                break;
1320 >                            }
1321 >                            Node last = e;
1322 >                            if ((e = e.next) == null) {
1323 >                                last.next = new Node(h, k, v, null);
1324 >                                if (count >= TREE_THRESHOLD)
1325 >                                    replaceWithTreeBin(tab, i, k);
1326 >                                break;
1327 >                            }
1328 >                        }
1329 >                    }
1330 >                } finally {                  // unlock and signal if needed
1331 >                    if (!f.casHash(fh | LOCKED, fh)) {
1332 >                        f.hash = fh;
1333 >                        synchronized (f) { f.notifyAll(); };
1334 >                    }
1335 >                }
1336 >                if (count != 0) {
1337 >                    if (oldVal != null)
1338 >                        return oldVal;
1339 >                    if (tab.length <= 64)
1340 >                        count = 2;
1341 >                    break;
1342 >                }
1343 >            }
1344 >        }
1345 >        counter.add(1L);
1346 >        if (count > 1)
1347 >            checkForResize();
1348 >        return null;
1349 >    }
1350 >
1351 >    /** Implementation for putIfAbsent */
1352 >    private final Object internalPutIfAbsent(Object k, Object v) {
1353 >        int h = spread(k.hashCode());
1354 >        int count = 0;
1355 >        for (Node[] tab = table;;) {
1356 >            int i; Node f; int fh; Object fk, fv;
1357 >            if (tab == null)
1358 >                tab = initTable();
1359 >            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1360 >                if (casTabAt(tab, i, null, new Node(h, k, v, null)))
1361 >                    break;
1362 >            }
1363 >            else if ((fh = f.hash) == MOVED) {
1364 >                if ((fk = f.key) instanceof TreeBin) {
1365 >                    TreeBin t = (TreeBin)fk;
1366 >                    Object oldVal = null;
1367 >                    t.acquire(0);
1368 >                    try {
1369 >                        if (tabAt(tab, i) == f) {
1370 >                            count = 2;
1371 >                            TreeNode p = t.putTreeNode(h, k, v);
1372 >                            if (p != null)
1373 >                                oldVal = p.val;
1374 >                        }
1375 >                    } finally {
1376 >                        t.release(0);
1377 >                    }
1378 >                    if (count != 0) {
1379 >                        if (oldVal != null)
1380 >                            return oldVal;
1381 >                        break;
1382 >                    }
1383 >                }
1384 >                else
1385 >                    tab = (Node[])fk;
1386 >            }
1387 >            else if ((fh & HASH_BITS) == h && (fv = f.val) != null &&
1388 >                     ((fk = f.key) == k || k.equals(fk)))
1389 >                return fv;
1390 >            else {
1391 >                Node g = f.next;
1392 >                if (g != null) { // at least 2 nodes -- search and maybe resize
1393 >                    for (Node e = g;;) {
1394 >                        Object ek, ev;
1395 >                        if ((e.hash & HASH_BITS) == h && (ev = e.val) != null &&
1396 >                            ((ek = e.key) == k || k.equals(ek)))
1397 >                            return ev;
1398 >                        if ((e = e.next) == null) {
1399 >                            checkForResize();
1400 >                            break;
1401 >                        }
1402 >                    }
1403 >                }
1404 >                if (((fh = f.hash) & LOCKED) != 0) {
1405 >                    checkForResize();
1406 >                    f.tryAwaitLock(tab, i);
1407 >                }
1408 >                else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) {
1409 >                    Object oldVal = null;
1410 >                    try {
1411 >                        if (tabAt(tab, i) == f) {
1412 >                            count = 1;
1413 >                            for (Node e = f;; ++count) {
1414 >                                Object ek, ev;
1415 >                                if ((e.hash & HASH_BITS) == h &&
1416 >                                    (ev = e.val) != null &&
1417 >                                    ((ek = e.key) == k || k.equals(ek))) {
1418 >                                    oldVal = ev;
1419 >                                    break;
1420 >                                }
1421 >                                Node last = e;
1422 >                                if ((e = e.next) == null) {
1423 >                                    last.next = new Node(h, k, v, null);
1424 >                                    if (count >= TREE_THRESHOLD)
1425 >                                        replaceWithTreeBin(tab, i, k);
1426 >                                    break;
1427 >                                }
1428 >                            }
1429 >                        }
1430 >                    } finally {
1431 >                        if (!f.casHash(fh | LOCKED, fh)) {
1432 >                            f.hash = fh;
1433 >                            synchronized (f) { f.notifyAll(); };
1434 >                        }
1435 >                    }
1436 >                    if (count != 0) {
1437 >                        if (oldVal != null)
1438 >                            return oldVal;
1439 >                        if (tab.length <= 64)
1440 >                            count = 2;
1441 >                        break;
1442 >                    }
1443 >                }
1444 >            }
1445 >        }
1446 >        counter.add(1L);
1447 >        if (count > 1)
1448 >            checkForResize();
1449 >        return null;
1450 >    }
1451 >
1452 >    /** Implementation for computeIfAbsent */
1453 >    private final Object internalComputeIfAbsent(K k,
1454 >                                                 MappingFunction<? super K, ?> mf) {
1455 >        int h = spread(k.hashCode());
1456 >        Object val = null;
1457 >        int count = 0;
1458 >        for (Node[] tab = table;;) {
1459              Node f; int i, fh; Object fk, fv;
1460              if (tab == null)
1461                  tab = initTable();
1462              else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1463                  Node node = new Node(fh = h | LOCKED, k, null, null);
671                boolean validated = false;
1464                  if (casTabAt(tab, i, null, node)) {
1465 <                    validated = true;
1465 >                    count = 1;
1466                      try {
1467 <                        val = fn.map(k);
676 <                        if (val != null) {
1467 >                        if ((val = mf.map(k)) != null)
1468                              node.val = val;
1469 <                            added = true;
1469 >                    } finally {
1470 >                        if (val == null)
1471 >                            setTabAt(tab, i, null);
1472 >                        if (!node.casHash(fh, h)) {
1473 >                            node.hash = h;
1474 >                            synchronized (node) { node.notifyAll(); };
1475 >                        }
1476 >                    }
1477 >                }
1478 >                if (count != 0)
1479 >                    break;
1480 >            }
1481 >            else if ((fh = f.hash) == MOVED) {
1482 >                if ((fk = f.key) instanceof TreeBin) {
1483 >                    TreeBin t = (TreeBin)fk;
1484 >                    boolean added = false;
1485 >                    t.acquire(0);
1486 >                    try {
1487 >                        if (tabAt(tab, i) == f) {
1488 >                            count = 1;
1489 >                            TreeNode p = t.getTreeNode(h, k, t.root);
1490 >                            if (p != null)
1491 >                                val = p.val;
1492 >                            else if ((val = mf.map(k)) != null) {
1493 >                                added = true;
1494 >                                count = 2;
1495 >                                t.putTreeNode(h, k, val);
1496 >                            }
1497                          }
1498                      } finally {
1499 +                        t.release(0);
1500 +                    }
1501 +                    if (count != 0) {
1502                          if (!added)
1503 +                            return val;
1504 +                        break;
1505 +                    }
1506 +                }
1507 +                else
1508 +                    tab = (Node[])fk;
1509 +            }
1510 +            else if ((fh & HASH_BITS) == h && (fv = f.val) != null &&
1511 +                     ((fk = f.key) == k || k.equals(fk)))
1512 +                return fv;
1513 +            else {
1514 +                Node g = f.next;
1515 +                if (g != null) {
1516 +                    for (Node e = g;;) {
1517 +                        Object ek, ev;
1518 +                        if ((e.hash & HASH_BITS) == h && (ev = e.val) != null &&
1519 +                            ((ek = e.key) == k || k.equals(ek)))
1520 +                            return ev;
1521 +                        if ((e = e.next) == null) {
1522 +                            checkForResize();
1523 +                            break;
1524 +                        }
1525 +                    }
1526 +                }
1527 +                if (((fh = f.hash) & LOCKED) != 0) {
1528 +                    checkForResize();
1529 +                    f.tryAwaitLock(tab, i);
1530 +                }
1531 +                else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) {
1532 +                    boolean added = false;
1533 +                    try {
1534 +                        if (tabAt(tab, i) == f) {
1535 +                            count = 1;
1536 +                            for (Node e = f;; ++count) {
1537 +                                Object ek, ev;
1538 +                                if ((e.hash & HASH_BITS) == h &&
1539 +                                    (ev = e.val) != null &&
1540 +                                    ((ek = e.key) == k || k.equals(ek))) {
1541 +                                    val = ev;
1542 +                                    break;
1543 +                                }
1544 +                                Node last = e;
1545 +                                if ((e = e.next) == null) {
1546 +                                    if ((val = mf.map(k)) != null) {
1547 +                                        added = true;
1548 +                                        last.next = new Node(h, k, val, null);
1549 +                                        if (count >= TREE_THRESHOLD)
1550 +                                            replaceWithTreeBin(tab, i, k);
1551 +                                    }
1552 +                                    break;
1553 +                                }
1554 +                            }
1555 +                        }
1556 +                    } finally {
1557 +                        if (!f.casHash(fh | LOCKED, fh)) {
1558 +                            f.hash = fh;
1559 +                            synchronized (f) { f.notifyAll(); };
1560 +                        }
1561 +                    }
1562 +                    if (count != 0) {
1563 +                        if (!added)
1564 +                            return val;
1565 +                        if (tab.length <= 64)
1566 +                            count = 2;
1567 +                        break;
1568 +                    }
1569 +                }
1570 +            }
1571 +        }
1572 +        if (val != null) {
1573 +            counter.add(1L);
1574 +            if (count > 1)
1575 +                checkForResize();
1576 +        }
1577 +        return val;
1578 +    }
1579 +
1580 +    /** Implementation for compute */
1581 +    @SuppressWarnings("unchecked")
1582 +    private final Object internalCompute(K k,
1583 +                                         RemappingFunction<? super K, V> mf) {
1584 +        int h = spread(k.hashCode());
1585 +        Object val = null;
1586 +        int delta = 0;
1587 +        int count = 0;
1588 +        for (Node[] tab = table;;) {
1589 +            Node f; int i, fh; Object fk;
1590 +            if (tab == null)
1591 +                tab = initTable();
1592 +            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1593 +                Node node = new Node(fh = h | LOCKED, k, null, null);
1594 +                if (casTabAt(tab, i, null, node)) {
1595 +                    try {
1596 +                        count = 1;
1597 +                        if ((val = mf.remap(k, null)) != null) {
1598 +                            node.val = val;
1599 +                            delta = 1;
1600 +                        }
1601 +                    } finally {
1602 +                        if (delta == 0)
1603                              setTabAt(tab, i, null);
1604                          if (!node.casHash(fh, h)) {
1605                              node.hash = h;
# Line 686 | Line 1607 | public class ConcurrentHashMapV8<K, V>
1607                          }
1608                      }
1609                  }
1610 <                if (validated)
1610 >                if (count != 0)
1611                      break;
1612              }
1613 <            else if ((fh = f.hash) == MOVED)
1614 <                tab = (Node[])f.key;
1615 <            else if (!replace && (fh & HASH_BITS) == h && (fv = f.val) != null &&
1616 <                     ((fk = f.key) == k || k.equals(fk))) {
1617 <                if (tabAt(tab, i) == f) {
1618 <                    val = (V)fv;
1619 <                    break;
1613 >            else if ((fh = f.hash) == MOVED) {
1614 >                if ((fk = f.key) instanceof TreeBin) {
1615 >                    TreeBin t = (TreeBin)fk;
1616 >                    t.acquire(0);
1617 >                    try {
1618 >                        if (tabAt(tab, i) == f) {
1619 >                            count = 1;
1620 >                            TreeNode p = t.getTreeNode(h, k, t.root);
1621 >                            Object pv = (p == null) ? null : p.val;
1622 >                            if ((val = mf.remap(k, (V)pv)) != null) {
1623 >                                if (p != null)
1624 >                                    p.val = val;
1625 >                                else {
1626 >                                    count = 2;
1627 >                                    delta = 1;
1628 >                                    t.putTreeNode(h, k, val);
1629 >                                }
1630 >                            }
1631 >                            else if (p != null) {
1632 >                                delta = -1;
1633 >                                t.deleteTreeNode(p);
1634 >                            }
1635 >                        }
1636 >                    } finally {
1637 >                        t.release(0);
1638 >                    }
1639 >                    if (count != 0)
1640 >                        break;
1641                  }
1642 +                else
1643 +                    tab = (Node[])fk;
1644              }
1645 <            else if ((fh & LOCKED) != 0)
1645 >            else if ((fh & LOCKED) != 0) {
1646 >                checkForResize();
1647                  f.tryAwaitLock(tab, i);
1648 +            }
1649              else if (f.casHash(fh, fh | LOCKED)) {
704                boolean validated = false;
705                boolean checkSize = false;
1650                  try {
1651                      if (tabAt(tab, i) == f) {
1652 <                        validated = true;
1653 <                        for (Node e = f;;) {
1654 <                            Object ek, ev, v;
1652 >                        count = 1;
1653 >                        for (Node e = f, pred = null;; ++count) {
1654 >                            Object ek, ev;
1655                              if ((e.hash & HASH_BITS) == h &&
1656                                  (ev = e.val) != null &&
1657                                  ((ek = e.key) == k || k.equals(ek))) {
1658 <                                if (replace && (v = fn.map(k)) != null)
1659 <                                    ev = e.val = v;
1660 <                                val = (V)ev;
1658 >                                val = mf.remap(k, (V)ev);
1659 >                                if (val != null)
1660 >                                    e.val = val;
1661 >                                else {
1662 >                                    delta = -1;
1663 >                                    Node en = e.next;
1664 >                                    if (pred != null)
1665 >                                        pred.next = en;
1666 >                                    else
1667 >                                        setTabAt(tab, i, en);
1668 >                                }
1669                                  break;
1670                              }
1671 <                            Node last = e;
1671 >                            pred = e;
1672                              if ((e = e.next) == null) {
1673 <                                if ((val = fn.map(k)) != null) {
1674 <                                    last.next = new Node(h, k, val, null);
1675 <                                    added = true;
1676 <                                    if (last != f || tab.length <= 64)
1677 <                                        checkSize = true;
1673 >                                if ((val = mf.remap(k, null)) != null) {
1674 >                                    pred.next = new Node(h, k, val, null);
1675 >                                    delta = 1;
1676 >                                    if (count >= TREE_THRESHOLD)
1677 >                                        replaceWithTreeBin(tab, i, k);
1678                                  }
1679                                  break;
1680                              }
# Line 734 | Line 1686 | public class ConcurrentHashMapV8<K, V>
1686                          synchronized (f) { f.notifyAll(); };
1687                      }
1688                  }
1689 <                if (validated) {
1690 <                    int sc;
1691 <                    if (checkSize && tab.length < MAXIMUM_CAPACITY &&
740 <                        (sc = sizeCtl) >= 0 && counter.sum() >= (long)sc)
741 <                        growTable();
1689 >                if (count != 0) {
1690 >                    if (tab.length <= 64)
1691 >                        count = 2;
1692                      break;
1693                  }
1694              }
1695          }
1696 <        if (added)
1697 <            counter.increment();
1696 >        if (delta != 0) {
1697 >            counter.add((long)delta);
1698 >            if (count > 1)
1699 >                checkForResize();
1700 >        }
1701          return val;
1702      }
1703  
1704 <    /**
1705 <     * Implementation for clear. Steps through each bin, removing all nodes.
1706 <     */
1707 <    private final void internalClear() {
1708 <        long delta = 0L; // negative number of deletions
1709 <        int i = 0;
1710 <        Node[] tab = table;
1711 <        while (tab != null && i < tab.length) {
1712 <            int fh;
1713 <            Node f = tabAt(tab, i);
1714 <            if (f == null)
1715 <                ++i;
1716 <            else if ((fh = f.hash) == MOVED)
1717 <                tab = (Node[])f.key;
1718 <            else if ((fh & LOCKED) != 0)
1719 <                f.tryAwaitLock(tab, i);
1720 <            else if (f.casHash(fh, fh | LOCKED)) {
1721 <                boolean validated = false;
1722 <                try {
1723 <                    if (tabAt(tab, i) == f) {
1724 <                        validated = true;
1725 <                        for (Node e = f; e != null; e = e.next) {
1726 <                            if (e.val != null) { // currently always true
1727 <                                e.val = null;
1728 <                                --delta;
1704 >    /** Implementation for putAll */
1705 >    private final void internalPutAll(Map<?, ?> m) {
1706 >        tryPresize(m.size());
1707 >        long delta = 0L;     // number of uncommitted additions
1708 >        boolean npe = false; // to throw exception on exit for nulls
1709 >        try {                // to clean up counts on other exceptions
1710 >            for (Map.Entry<?, ?> entry : m.entrySet()) {
1711 >                Object k, v;
1712 >                if (entry == null || (k = entry.getKey()) == null ||
1713 >                    (v = entry.getValue()) == null) {
1714 >                    npe = true;
1715 >                    break;
1716 >                }
1717 >                int h = spread(k.hashCode());
1718 >                for (Node[] tab = table;;) {
1719 >                    int i; Node f; int fh; Object fk;
1720 >                    if (tab == null)
1721 >                        tab = initTable();
1722 >                    else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null){
1723 >                        if (casTabAt(tab, i, null, new Node(h, k, v, null))) {
1724 >                            ++delta;
1725 >                            break;
1726 >                        }
1727 >                    }
1728 >                    else if ((fh = f.hash) == MOVED) {
1729 >                        if ((fk = f.key) instanceof TreeBin) {
1730 >                            TreeBin t = (TreeBin)fk;
1731 >                            boolean validated = false;
1732 >                            t.acquire(0);
1733 >                            try {
1734 >                                if (tabAt(tab, i) == f) {
1735 >                                    validated = true;
1736 >                                    TreeNode p = t.getTreeNode(h, k, t.root);
1737 >                                    if (p != null)
1738 >                                        p.val = v;
1739 >                                    else {
1740 >                                        t.putTreeNode(h, k, v);
1741 >                                        ++delta;
1742 >                                    }
1743 >                                }
1744 >                            } finally {
1745 >                                t.release(0);
1746                              }
1747 +                            if (validated)
1748 +                                break;
1749                          }
1750 <                        setTabAt(tab, i, null);
1750 >                        else
1751 >                            tab = (Node[])fk;
1752                      }
1753 <                } finally {
1754 <                    if (!f.casHash(fh | LOCKED, fh)) {
1755 <                        f.hash = fh;
1756 <                        synchronized (f) { f.notifyAll(); };
1753 >                    else if ((fh & LOCKED) != 0) {
1754 >                        counter.add(delta);
1755 >                        delta = 0L;
1756 >                        checkForResize();
1757 >                        f.tryAwaitLock(tab, i);
1758 >                    }
1759 >                    else if (f.casHash(fh, fh | LOCKED)) {
1760 >                        int count = 0;
1761 >                        try {
1762 >                            if (tabAt(tab, i) == f) {
1763 >                                count = 1;
1764 >                                for (Node e = f;; ++count) {
1765 >                                    Object ek, ev;
1766 >                                    if ((e.hash & HASH_BITS) == h &&
1767 >                                        (ev = e.val) != null &&
1768 >                                        ((ek = e.key) == k || k.equals(ek))) {
1769 >                                        e.val = v;
1770 >                                        break;
1771 >                                    }
1772 >                                    Node last = e;
1773 >                                    if ((e = e.next) == null) {
1774 >                                        ++delta;
1775 >                                        last.next = new Node(h, k, v, null);
1776 >                                        if (count >= TREE_THRESHOLD)
1777 >                                            replaceWithTreeBin(tab, i, k);
1778 >                                        break;
1779 >                                    }
1780 >                                }
1781 >                            }
1782 >                        } finally {
1783 >                            if (!f.casHash(fh | LOCKED, fh)) {
1784 >                                f.hash = fh;
1785 >                                synchronized (f) { f.notifyAll(); };
1786 >                            }
1787 >                        }
1788 >                        if (count != 0) {
1789 >                            if (count > 1) {
1790 >                                counter.add(delta);
1791 >                                delta = 0L;
1792 >                                checkForResize();
1793 >                            }
1794 >                            break;
1795 >                        }
1796                      }
1797                  }
786                if (validated)
787                    ++i;
1798              }
1799 +        } finally {
1800 +            if (delta != 0)
1801 +                counter.add(delta);
1802          }
1803 <        counter.add(delta);
1803 >        if (npe)
1804 >            throw new NullPointerException();
1805      }
1806  
1807 <    /* ----------------Table Initialization and Resizing -------------- */
1807 >    /* ---------------- Table Initialization and Resizing -------------- */
1808  
1809      /**
1810       * Returns a power of two table size for the given desired capacity.
# Line 819 | Line 1833 | public class ConcurrentHashMapV8<K, V>
1833                      if ((tab = table) == null) {
1834                          int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
1835                          tab = table = new Node[n];
1836 <                        sc = n - (n >>> 2) - 1;
1836 >                        sc = n - (n >>> 2);
1837                      }
1838                  } finally {
1839                      sizeCtl = sc;
# Line 831 | Line 1845 | public class ConcurrentHashMapV8<K, V>
1845      }
1846  
1847      /**
1848 <     * If not already resizing, creates next table and transfers bins.
1849 <     * Rechecks occupancy after a transfer to see if another resize is
1850 <     * already needed because resizings are lagging additions.
1851 <     */
1852 <    private final void growTable() {
1853 <        int sc = sizeCtl;
1854 <        if (sc >= 0 && UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1848 >     * If table is too small and not already resizing, creates next
1849 >     * table and transfers bins.  Rechecks occupancy after a transfer
1850 >     * to see if another resize is already needed because resizings
1851 >     * are lagging additions.
1852 >     */
1853 >    private final void checkForResize() {
1854 >        Node[] tab; int n, sc;
1855 >        while ((tab = table) != null &&
1856 >               (n = tab.length) < MAXIMUM_CAPACITY &&
1857 >               (sc = sizeCtl) >= 0 && counter.sum() >= (long)sc &&
1858 >               UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1859              try {
1860 <                Node[] tab; int n;
843 <                while ((tab = table) != null &&
844 <                       (n = tab.length) > 0 && n < MAXIMUM_CAPACITY &&
845 <                       counter.sum() >= (long)sc) {
1860 >                if (tab == table) {
1861                      table = rebuild(tab);
1862 <                    sc = (n << 1) - (n >>> 1) - 1;
1862 >                    sc = (n << 1) - (n >>> 1);
1863                  }
1864              } finally {
1865                  sizeCtl = sc;
# Line 852 | Line 1867 | public class ConcurrentHashMapV8<K, V>
1867          }
1868      }
1869  
1870 +    /**
1871 +     * Tries to presize table to accommodate the given number of elements.
1872 +     *
1873 +     * @param size number of elements (doesn't need to be perfectly accurate)
1874 +     */
1875 +    private final void tryPresize(int size) {
1876 +        int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
1877 +            tableSizeFor(size + (size >>> 1) + 1);
1878 +        int sc;
1879 +        while ((sc = sizeCtl) >= 0) {
1880 +            Node[] tab = table; int n;
1881 +            if (tab == null || (n = tab.length) == 0) {
1882 +                n = (sc > c) ? sc : c;
1883 +                if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1884 +                    try {
1885 +                        if (table == tab) {
1886 +                            table = new Node[n];
1887 +                            sc = n - (n >>> 2);
1888 +                        }
1889 +                    } finally {
1890 +                        sizeCtl = sc;
1891 +                    }
1892 +                }
1893 +            }
1894 +            else if (c <= sc || n >= MAXIMUM_CAPACITY)
1895 +                break;
1896 +            else if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1897 +                try {
1898 +                    if (table == tab) {
1899 +                        table = rebuild(tab);
1900 +                        sc = (n << 1) - (n >>> 1);
1901 +                    }
1902 +                } finally {
1903 +                    sizeCtl = sc;
1904 +                }
1905 +            }
1906 +        }
1907 +    }
1908 +
1909      /*
1910       * Moves and/or copies the nodes in each bin to new table. See
1911       * above for explanation.
# Line 876 | Line 1930 | public class ConcurrentHashMapV8<K, V>
1930                          continue;
1931                  }
1932                  else {             // transiently use a locked forwarding node
1933 <                    Node g =  new Node(MOVED|LOCKED, nextTab, null, null);
1933 >                    Node g = new Node(MOVED|LOCKED, nextTab, null, null);
1934                      if (!casTabAt(tab, i, f, g))
1935                          continue;
1936                      setTabAt(nextTab, i, null);
# Line 888 | Line 1942 | public class ConcurrentHashMapV8<K, V>
1942                      }
1943                  }
1944              }
1945 <            else if (((fh = f.hash) & LOCKED) == 0 && f.casHash(fh, fh|LOCKED)) {
1945 >            else if ((fh = f.hash) == MOVED) {
1946 >                Object fk = f.key;
1947 >                if (fk instanceof TreeBin) {
1948 >                    TreeBin t = (TreeBin)fk;
1949 >                    boolean validated = false;
1950 >                    t.acquire(0);
1951 >                    try {
1952 >                        if (tabAt(tab, i) == f) {
1953 >                            validated = true;
1954 >                            splitTreeBin(nextTab, i, t);
1955 >                            setTabAt(tab, i, fwd);
1956 >                        }
1957 >                    } finally {
1958 >                        t.release(0);
1959 >                    }
1960 >                    if (!validated)
1961 >                        continue;
1962 >                }
1963 >            }
1964 >            else if ((fh & LOCKED) == 0 && f.casHash(fh, fh|LOCKED)) {
1965                  boolean validated = false;
1966                  try {              // split to lo and hi lists; copying as needed
1967                      if (tabAt(tab, i) == f) {
1968                          validated = true;
1969 <                        Node e = f, lastRun = f;
897 <                        Node lo = null, hi = null;
898 <                        int runBit = e.hash & n;
899 <                        for (Node p = e.next; p != null; p = p.next) {
900 <                            int b = p.hash & n;
901 <                            if (b != runBit) {
902 <                                runBit = b;
903 <                                lastRun = p;
904 <                            }
905 <                        }
906 <                        if (runBit == 0)
907 <                            lo = lastRun;
908 <                        else
909 <                            hi = lastRun;
910 <                        for (Node p = e; p != lastRun; p = p.next) {
911 <                            int ph = p.hash & HASH_BITS;
912 <                            Object pk = p.key, pv = p.val;
913 <                            if ((ph & n) == 0)
914 <                                lo = new Node(ph, pk, pv, lo);
915 <                            else
916 <                                hi = new Node(ph, pk, pv, hi);
917 <                        }
918 <                        setTabAt(nextTab, i, lo);
919 <                        setTabAt(nextTab, i + n, hi);
1969 >                        splitBin(nextTab, i, f);
1970                          setTabAt(tab, i, fwd);
1971                      }
1972                  } finally {
# Line 961 | Line 2011 | public class ConcurrentHashMapV8<K, V>
2011          }
2012      }
2013  
2014 +    /**
2015 +     * Split a normal bin with list headed by e into lo and hi parts;
2016 +     * install in given table
2017 +     */
2018 +    private static void splitBin(Node[] nextTab, int i, Node e) {
2019 +        int bit = nextTab.length >>> 1; // bit to split on
2020 +        int runBit = e.hash & bit;
2021 +        Node lastRun = e, lo = null, hi = null;
2022 +        for (Node p = e.next; p != null; p = p.next) {
2023 +            int b = p.hash & bit;
2024 +            if (b != runBit) {
2025 +                runBit = b;
2026 +                lastRun = p;
2027 +            }
2028 +        }
2029 +        if (runBit == 0)
2030 +            lo = lastRun;
2031 +        else
2032 +            hi = lastRun;
2033 +        for (Node p = e; p != lastRun; p = p.next) {
2034 +            int ph = p.hash & HASH_BITS;
2035 +            Object pk = p.key, pv = p.val;
2036 +            if ((ph & bit) == 0)
2037 +                lo = new Node(ph, pk, pv, lo);
2038 +            else
2039 +                hi = new Node(ph, pk, pv, hi);
2040 +        }
2041 +        setTabAt(nextTab, i, lo);
2042 +        setTabAt(nextTab, i + bit, hi);
2043 +    }
2044 +
2045 +    /**
2046 +     * Split a tree bin into lo and hi parts; install in given table
2047 +     */
2048 +    private static void splitTreeBin(Node[] nextTab, int i, TreeBin t) {
2049 +        int bit = nextTab.length >>> 1;
2050 +        TreeBin lt = new TreeBin();
2051 +        TreeBin ht = new TreeBin();
2052 +        int lc = 0, hc = 0;
2053 +        for (Node e = t.first; e != null; e = e.next) {
2054 +            int h = e.hash & HASH_BITS;
2055 +            Object k = e.key, v = e.val;
2056 +            if ((h & bit) == 0) {
2057 +                ++lc;
2058 +                lt.putTreeNode(h, k, v);
2059 +            }
2060 +            else {
2061 +                ++hc;
2062 +                ht.putTreeNode(h, k, v);
2063 +            }
2064 +        }
2065 +        Node ln, hn; // throw away trees if too small
2066 +        if (lc <= (TREE_THRESHOLD >>> 1)) {
2067 +            ln = null;
2068 +            for (Node p = lt.first; p != null; p = p.next)
2069 +                ln = new Node(p.hash, p.key, p.val, ln);
2070 +        }
2071 +        else
2072 +            ln = new Node(MOVED, lt, null, null);
2073 +        setTabAt(nextTab, i, ln);
2074 +        if (hc <= (TREE_THRESHOLD >>> 1)) {
2075 +            hn = null;
2076 +            for (Node p = ht.first; p != null; p = p.next)
2077 +                hn = new Node(p.hash, p.key, p.val, hn);
2078 +        }
2079 +        else
2080 +            hn = new Node(MOVED, ht, null, null);
2081 +        setTabAt(nextTab, i + bit, hn);
2082 +    }
2083 +
2084 +    /**
2085 +     * Implementation for clear. Steps through each bin, removing all
2086 +     * nodes.
2087 +     */
2088 +    private final void internalClear() {
2089 +        long delta = 0L; // negative number of deletions
2090 +        int i = 0;
2091 +        Node[] tab = table;
2092 +        while (tab != null && i < tab.length) {
2093 +            int fh; Object fk;
2094 +            Node f = tabAt(tab, i);
2095 +            if (f == null)
2096 +                ++i;
2097 +            else if ((fh = f.hash) == MOVED) {
2098 +                if ((fk = f.key) instanceof TreeBin) {
2099 +                    TreeBin t = (TreeBin)fk;
2100 +                    t.acquire(0);
2101 +                    try {
2102 +                        if (tabAt(tab, i) == f) {
2103 +                            for (Node p = t.first; p != null; p = p.next) {
2104 +                                p.val = null;
2105 +                                --delta;
2106 +                            }
2107 +                            t.first = null;
2108 +                            t.root = null;
2109 +                            ++i;
2110 +                        }
2111 +                    } finally {
2112 +                        t.release(0);
2113 +                    }
2114 +                }
2115 +                else
2116 +                    tab = (Node[])fk;
2117 +            }
2118 +            else if ((fh & LOCKED) != 0) {
2119 +                counter.add(delta); // opportunistically update count
2120 +                delta = 0L;
2121 +                f.tryAwaitLock(tab, i);
2122 +            }
2123 +            else if (f.casHash(fh, fh | LOCKED)) {
2124 +                try {
2125 +                    if (tabAt(tab, i) == f) {
2126 +                        for (Node e = f; e != null; e = e.next) {
2127 +                            e.val = null;
2128 +                            --delta;
2129 +                        }
2130 +                        setTabAt(tab, i, null);
2131 +                        ++i;
2132 +                    }
2133 +                } finally {
2134 +                    if (!f.casHash(fh | LOCKED, fh)) {
2135 +                        f.hash = fh;
2136 +                        synchronized (f) { f.notifyAll(); };
2137 +                    }
2138 +                }
2139 +            }
2140 +        }
2141 +        if (delta != 0)
2142 +            counter.add(delta);
2143 +    }
2144 +
2145      /* ----------------Table Traversal -------------- */
2146  
2147      /**
# Line 969 | Line 2150 | public class ConcurrentHashMapV8<K, V>
2150       *
2151       * At each step, the iterator snapshots the key ("nextKey") and
2152       * value ("nextVal") of a valid node (i.e., one that, at point of
2153 <     * snapshot, has a nonnull user value). Because val fields can
2153 >     * snapshot, has a non-null user value). Because val fields can
2154       * change (including to null, indicating deletion), field nextVal
2155       * might not be accurate at point of use, but still maintains the
2156       * weak consistency property of holding a value that was once
2157       * valid.
2158       *
2159       * Internal traversals directly access these fields, as in:
2160 <     * {@code while (it.next != null) { process(it.nextKey); it.advance(); }}
2160 >     * {@code while (it.advance() != null) { process(it.nextKey); }}
2161       *
2162 <     * Exported iterators (subclasses of ViewIterator) extract key,
2163 <     * value, or key-value pairs as return values of Iterator.next(),
2164 <     * and encapsulate the it.next check as hasNext();
2165 <     *
2166 <     * The iterator visits each valid node that was reachable upon
2167 <     * iterator construction once. It might miss some that were added
2168 <     * to a bin after the bin was visited, which is OK wrt consistency
2169 <     * guarantees. Maintaining this property in the face of possible
2170 <     * ongoing resizes requires a fair amount of bookkeeping state
2171 <     * that is difficult to optimize away amidst volatile accesses.
2172 <     * Even so, traversal maintains reasonable throughput.
2162 >     * Exported iterators must track whether the iterator has advanced
2163 >     * (in hasNext vs next) (by setting/checking/nulling field
2164 >     * nextVal), and then extract key, value, or key-value pairs as
2165 >     * return values of next().
2166 >     *
2167 >     * The iterator visits once each still-valid node that was
2168 >     * reachable upon iterator construction. It might miss some that
2169 >     * were added to a bin after the bin was visited, which is OK wrt
2170 >     * consistency guarantees. Maintaining this property in the face
2171 >     * of possible ongoing resizes requires a fair amount of
2172 >     * bookkeeping state that is difficult to optimize away amidst
2173 >     * volatile accesses.  Even so, traversal maintains reasonable
2174 >     * throughput.
2175       *
2176       * Normally, iteration proceeds bin-by-bin traversing lists.
2177       * However, if the table has been resized, then all future steps
# Line 997 | Line 2180 | public class ConcurrentHashMapV8<K, V>
2180       * paranoically cope with potential sharing by users of iterators
2181       * across threads, iteration terminates if a bounds checks fails
2182       * for a table read.
1000     *
1001     * The range-based constructor enables creation of parallel
1002     * range-splitting traversals. (Not yet implemented.)
2183       */
2184 <    static class InternalIterator {
2184 >    static class InternalIterator<K,V> {
2185 >        final ConcurrentHashMapV8<K, V> map;
2186          Node next;           // the next entry to use
2187          Node last;           // the last entry used
2188          Object nextKey;      // cached key field of next
# Line 1009 | Line 2190 | public class ConcurrentHashMapV8<K, V>
2190          Node[] tab;          // current table; updated if resized
2191          int index;           // index of bin to use next
2192          int baseIndex;       // current index of initial table
2193 <        final int baseLimit; // index bound for initial table
2193 >        int baseLimit;       // index bound for initial table
2194          final int baseSize;  // initial table size
2195  
2196          /** Creates iterator for all entries in the table. */
2197 <        InternalIterator(Node[] tab) {
2198 <            this.tab = tab;
2197 >        InternalIterator(ConcurrentHashMapV8<K, V> map) {
2198 >            this.tab = (this.map = map).table;
2199              baseLimit = baseSize = (tab == null) ? 0 : tab.length;
1019            index = baseIndex = 0;
1020            next = null;
1021            advance();
1022        }
1023
1024        /** Creates iterator for the given range of the table */
1025        InternalIterator(Node[] tab, int lo, int hi) {
1026            this.tab = tab;
1027            baseSize = (tab == null) ? 0 : tab.length;
1028            baseLimit = (hi <= baseSize) ? hi : baseSize;
1029            index = baseIndex = lo;
1030            next = null;
1031            advance();
2200          }
2201  
2202 <        /** Advances next. See above for explanation. */
2203 <        final void advance() {
2202 >        /** Creates iterator for clone() and split() methods */
2203 >        InternalIterator(InternalIterator<K,V> it, boolean split) {
2204 >            this.map = it.map;
2205 >            this.tab = it.tab;
2206 >            this.baseSize = it.baseSize;
2207 >            int lo = it.baseIndex;
2208 >            int hi = this.baseLimit = it.baseLimit;
2209 >            this.index = this.baseIndex =
2210 >                (split) ? (it.baseLimit = (lo + hi + 1) >>> 1) : lo;
2211 >        }
2212 >
2213 >        /**
2214 >         * Advances next; returns nextVal or null if terminated
2215 >         * See above for explanation.
2216 >         */
2217 >        final Object advance() {
2218              Node e = last = next;
2219 +            Object ev = null;
2220              outer: do {
2221                  if (e != null)                  // advance past used/skipped node
2222                      e = e.next;
2223                  while (e == null) {             // get to next non-null bin
2224 <                    Node[] t; int b, i, n;      // checks must use locals
2224 >                    Node[] t; int b, i, n; Object ek; // checks must use locals
2225                      if ((b = baseIndex) >= baseLimit || (i = index) < 0 ||
2226                          (t = tab) == null || i >= (n = t.length))
2227                          break outer;
2228 <                    else if ((e = tabAt(t, i)) != null && e.hash == MOVED)
2229 <                        tab = (Node[])e.key;    // restarts due to null val
2230 <                    else                        // visit upper slots if present
2231 <                        index = (i += baseSize) < n ? i : (baseIndex = b + 1);
2228 >                    else if ((e = tabAt(t, i)) != null && e.hash == MOVED) {
2229 >                        if ((ek = e.key) instanceof TreeBin)
2230 >                            e = ((TreeBin)ek).first;
2231 >                        else {
2232 >                            tab = (Node[])ek;
2233 >                            continue;           // restarts due to null val
2234 >                        }
2235 >                    }                           // visit upper slots if present
2236 >                    index = (i += baseSize) < n ? i : (baseIndex = b + 1);
2237                  }
2238                  nextKey = e.key;
2239 <            } while ((nextVal = e.val) == null);// skip deleted or special nodes
2239 >            } while ((ev = e.val) == null);    // skip deleted or special nodes
2240              next = e;
2241 +            return nextVal = ev;
2242          }
2243 +
2244 +        public final void remove() {
2245 +            if (nextVal == null)
2246 +                advance();
2247 +            Node e = last;
2248 +            if (e == null)
2249 +                throw new IllegalStateException();
2250 +            last = null;
2251 +            map.remove(e.key);
2252 +        }
2253 +
2254 +        public final boolean hasNext() {
2255 +            return nextVal != null || advance() != null;
2256 +        }
2257 +
2258 +        public final boolean hasMoreElements() { return hasNext(); }
2259      }
2260  
2261      /* ---------------- Public operations -------------- */
# Line 1090 | Line 2295 | public class ConcurrentHashMapV8<K, V>
2295      public ConcurrentHashMapV8(Map<? extends K, ? extends V> m) {
2296          this.counter = new LongAdder();
2297          this.sizeCtl = DEFAULT_CAPACITY;
2298 <        putAll(m);
2298 >        internalPutAll(m);
2299      }
2300  
2301      /**
# Line 1137 | Line 2342 | public class ConcurrentHashMapV8<K, V>
2342          if (initialCapacity < concurrencyLevel)   // Use at least as many bins
2343              initialCapacity = concurrencyLevel;   // as estimated threads
2344          long size = (long)(1.0 + (long)initialCapacity / loadFactor);
2345 <        int cap =  ((size >= (long)MAXIMUM_CAPACITY) ?
2346 <                    MAXIMUM_CAPACITY: tableSizeFor((int)size));
2345 >        int cap = ((size >= (long)MAXIMUM_CAPACITY) ?
2346 >                   MAXIMUM_CAPACITY: tableSizeFor((int)size));
2347          this.counter = new LongAdder();
2348          this.sizeCtl = cap;
2349      }
# Line 1212 | Line 2417 | public class ConcurrentHashMapV8<K, V>
2417          if (value == null)
2418              throw new NullPointerException();
2419          Object v;
2420 <        InternalIterator it = new InternalIterator(table);
2421 <        while (it.next != null) {
2422 <            if ((v = it.nextVal) == value || value.equals(v))
2420 >        InternalIterator<K,V> it = new InternalIterator<K,V>(this);
2421 >        while ((v = it.advance()) != null) {
2422 >            if (v == value || value.equals(v))
2423                  return true;
1219            it.advance();
2424          }
2425          return false;
2426      }
# Line 1257 | Line 2461 | public class ConcurrentHashMapV8<K, V>
2461      public V put(K key, V value) {
2462          if (key == null || value == null)
2463              throw new NullPointerException();
2464 <        return (V)internalPut(key, value, true);
2464 >        return (V)internalPut(key, value);
2465      }
2466  
2467      /**
# Line 1271 | Line 2475 | public class ConcurrentHashMapV8<K, V>
2475      public V putIfAbsent(K key, V value) {
2476          if (key == null || value == null)
2477              throw new NullPointerException();
2478 <        return (V)internalPut(key, value, false);
2478 >        return (V)internalPutIfAbsent(key, value);
2479      }
2480  
2481      /**
# Line 1282 | Line 2486 | public class ConcurrentHashMapV8<K, V>
2486       * @param m mappings to be stored in this map
2487       */
2488      public void putAll(Map<? extends K, ? extends V> m) {
2489 <        if (m == null)
1286 <            throw new NullPointerException();
1287 <        /*
1288 <         * If uninitialized, try to preallocate big enough table
1289 <         */
1290 <        if (table == null) {
1291 <            int size = m.size();
1292 <            int n = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
1293 <                tableSizeFor(size + (size >>> 1) + 1);
1294 <            int sc = sizeCtl;
1295 <            if (n < sc)
1296 <                n = sc;
1297 <            if (sc >= 0 &&
1298 <                UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1299 <                try {
1300 <                    if (table == null) {
1301 <                        table = new Node[n];
1302 <                        sc = n - (n >>> 2) - 1;
1303 <                    }
1304 <                } finally {
1305 <                    sizeCtl = sc;
1306 <                }
1307 <            }
1308 <        }
1309 <        for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) {
1310 <            Object ek = e.getKey(), ev = e.getValue();
1311 <            if (ek == null || ev == null)
1312 <                throw new NullPointerException();
1313 <            internalPut(ek, ev, true);
1314 <        }
2489 >        internalPutAll(m);
2490      }
2491  
2492      /**
2493       * If the specified key is not already associated with a value,
2494 <     * computes its value using the given mappingFunction, and if
2495 <     * non-null, enters it into the map.  This is equivalent to
2496 <     *  <pre> {@code
2494 >     * computes its value using the given mappingFunction and enters
2495 >     * it into the map unless null.  This is equivalent to
2496 >     * <pre> {@code
2497       * if (map.containsKey(key))
2498       *   return map.get(key);
2499       * value = mappingFunction.map(key);
# Line 1326 | Line 2501 | public class ConcurrentHashMapV8<K, V>
2501       *   map.put(key, value);
2502       * return value;}</pre>
2503       *
2504 <     * except that the action is performed atomically.  Some attempted
2505 <     * update operations on this map by other threads may be blocked
2506 <     * while computation is in progress, so the computation should be
2507 <     * short and simple, and must not attempt to update any other
2508 <     * mappings of this Map. The most appropriate usage is to
2504 >     * except that the action is performed atomically.  If the
2505 >     * function returns {@code null} no mapping is recorded. If the
2506 >     * function itself throws an (unchecked) exception, the exception
2507 >     * is rethrown to its caller, and no mapping is recorded.  Some
2508 >     * attempted update operations on this map by other threads may be
2509 >     * blocked while computation is in progress, so the computation
2510 >     * should be short and simple, and must not attempt to update any
2511 >     * other mappings of this Map. The most appropriate usage is to
2512       * construct a new object serving as an initial mapped value, or
2513       * memoized result, as in:
2514 +     *
2515       *  <pre> {@code
2516       * map.computeIfAbsent(key, new MappingFunction<K, V>() {
2517       *   public V map(K k) { return new Value(f(k)); }});}</pre>
# Line 1340 | Line 2519 | public class ConcurrentHashMapV8<K, V>
2519       * @param key key with which the specified value is to be associated
2520       * @param mappingFunction the function to compute a value
2521       * @return the current (existing or computed) value associated with
2522 <     *         the specified key, or {@code null} if the computation
1344 <     *         returned {@code null}
2522 >     *         the specified key, or null if the computed value is null.
2523       * @throws NullPointerException if the specified key or mappingFunction
2524       *         is null
2525       * @throws IllegalStateException if the computation detectably
# Line 1350 | Line 2528 | public class ConcurrentHashMapV8<K, V>
2528       * @throws RuntimeException or Error if the mappingFunction does so,
2529       *         in which case the mapping is left unestablished
2530       */
2531 +    @SuppressWarnings("unchecked")
2532      public V computeIfAbsent(K key, MappingFunction<? super K, ? extends V> mappingFunction) {
2533          if (key == null || mappingFunction == null)
2534              throw new NullPointerException();
2535 <        return internalCompute(key, mappingFunction, false);
2535 >        return (V)internalComputeIfAbsent(key, mappingFunction);
2536      }
2537  
2538      /**
2539 <     * Computes the value associated with the given key using the given
2540 <     * mappingFunction, and if non-null, enters it into the map.  This
2541 <     * is equivalent to
2539 >     * Computes a new mapping value given a key and
2540 >     * its current mapped value (or {@code null} if there is no current
2541 >     * mapping). This is equivalent to
2542       *  <pre> {@code
2543 <     * value = mappingFunction.map(key);
2544 <     * if (value != null)
2545 <     *   map.put(key, value);
2546 <     * else
2547 <     *   value = map.get(key);
2548 <     * return value;}</pre>
2549 <     *
2550 <     * except that the action is performed atomically.  Some attempted
2551 <     * update operations on this map by other threads may be blocked
2552 <     * while computation is in progress, so the computation should be
2553 <     * short and simple, and must not attempt to update any other
2554 <     * mappings of this Map.
2543 >     *   value = remappingFunction.remap(key, map.get(key));
2544 >     *   if (value != null)
2545 >     *     map.put(key, value);
2546 >     *   else
2547 >     *     map.remove(key);
2548 >     * }</pre>
2549 >     *
2550 >     * except that the action is performed atomically.  If the
2551 >     * function returns {@code null}, the mapping is removed.  If the
2552 >     * function itself throws an (unchecked) exception, the exception
2553 >     * is rethrown to its caller, and the current mapping is left
2554 >     * unchanged.  Some attempted update operations on this map by
2555 >     * other threads may be blocked while computation is in progress,
2556 >     * so the computation should be short and simple, and must not
2557 >     * attempt to update any other mappings of this Map. For example,
2558 >     * to either create or append new messages to a value mapping:
2559 >     *
2560 >     * <pre> {@code
2561 >     * Map<Key, String> map = ...;
2562 >     * final String msg = ...;
2563 >     * map.compute(key, new RemappingFunction<Key, String>() {
2564 >     *   public String remap(Key k, String v) {
2565 >     *    return (v == null) ? msg : v + msg;});}}</pre>
2566       *
2567       * @param key key with which the specified value is to be associated
2568 <     * @param mappingFunction the function to compute a value
2569 <     * @return the current value associated with
2570 <     *         the specified key, or {@code null} if the computation
2571 <     *         returned {@code null} and the value was not otherwise present
1382 <     * @throws NullPointerException if the specified key or mappingFunction
2568 >     * @param remappingFunction the function to compute a value
2569 >     * @return the new value associated with
2570 >     *         the specified key, or null if none.
2571 >     * @throws NullPointerException if the specified key or remappingFunction
2572       *         is null
2573       * @throws IllegalStateException if the computation detectably
2574       *         attempts a recursive update to this map that would
2575       *         otherwise never complete
2576 <     * @throws RuntimeException or Error if the mappingFunction does so,
2576 >     * @throws RuntimeException or Error if the remappingFunction does so,
2577       *         in which case the mapping is unchanged
2578       */
2579 <    public V compute(K key, MappingFunction<? super K, ? extends V> mappingFunction) {
2580 <        if (key == null || mappingFunction == null)
2579 >    @SuppressWarnings("unchecked")
2580 >    public V compute(K key, RemappingFunction<? super K, V> remappingFunction) {
2581 >        if (key == null || remappingFunction == null)
2582              throw new NullPointerException();
2583 <        return internalCompute(key, mappingFunction, true);
2583 >        return (V)internalCompute(key, remappingFunction);
2584      }
2585  
2586      /**
# Line 1538 | Line 2728 | public class ConcurrentHashMapV8<K, V>
2728      }
2729  
2730      /**
2731 +     * Returns a partionable iterator of the keys in this map.
2732 +     *
2733 +     * @return a partionable iterator of the keys in this map
2734 +     */
2735 +    public Spliterator<K> keySpliterator() {
2736 +        return new KeyIterator<K,V>(this);
2737 +    }
2738 +
2739 +    /**
2740 +     * Returns a partionable iterator of the values in this map.
2741 +     *
2742 +     * @return a partionable iterator of the values in this map
2743 +     */
2744 +    public Spliterator<V> valueSpliterator() {
2745 +        return new ValueIterator<K,V>(this);
2746 +    }
2747 +
2748 +    /**
2749 +     * Returns a partionable iterator of the entries in this map.
2750 +     *
2751 +     * @return a partionable iterator of the entries in this map
2752 +     */
2753 +    public Spliterator<Map.Entry<K,V>> entrySpliterator() {
2754 +        return new EntryIterator<K,V>(this);
2755 +    }
2756 +
2757 +    /**
2758       * Returns the hash code value for this {@link Map}, i.e.,
2759       * the sum of, for each key-value pair in the map,
2760       * {@code key.hashCode() ^ value.hashCode()}.
# Line 1546 | Line 2763 | public class ConcurrentHashMapV8<K, V>
2763       */
2764      public int hashCode() {
2765          int h = 0;
2766 <        InternalIterator it = new InternalIterator(table);
2767 <        while (it.next != null) {
2768 <            h += it.nextKey.hashCode() ^ it.nextVal.hashCode();
2769 <            it.advance();
2766 >        InternalIterator<K,V> it = new InternalIterator<K,V>(this);
2767 >        Object v;
2768 >        while ((v = it.advance()) != null) {
2769 >            h += it.nextKey.hashCode() ^ v.hashCode();
2770          }
2771          return h;
2772      }
# Line 1566 | Line 2783 | public class ConcurrentHashMapV8<K, V>
2783       * @return a string representation of this map
2784       */
2785      public String toString() {
2786 <        InternalIterator it = new InternalIterator(table);
2786 >        InternalIterator<K,V> it = new InternalIterator<K,V>(this);
2787          StringBuilder sb = new StringBuilder();
2788          sb.append('{');
2789 <        if (it.next != null) {
2789 >        Object v;
2790 >        if ((v = it.advance()) != null) {
2791              for (;;) {
2792 <                Object k = it.nextKey, v = it.nextVal;
2792 >                Object k = it.nextKey;
2793                  sb.append(k == this ? "(this Map)" : k);
2794                  sb.append('=');
2795                  sb.append(v == this ? "(this Map)" : v);
2796 <                it.advance();
1579 <                if (it.next == null)
2796 >                if ((v = it.advance()) == null)
2797                      break;
2798                  sb.append(',').append(' ');
2799              }
# Line 1599 | Line 2816 | public class ConcurrentHashMapV8<K, V>
2816              if (!(o instanceof Map))
2817                  return false;
2818              Map<?,?> m = (Map<?,?>) o;
2819 <            InternalIterator it = new InternalIterator(table);
2820 <            while (it.next != null) {
2821 <                Object val = it.nextVal;
2819 >            InternalIterator<K,V> it = new InternalIterator<K,V>(this);
2820 >            Object val;
2821 >            while ((val = it.advance()) != null) {
2822                  Object v = m.get(it.nextKey);
2823                  if (v == null || (v != val && !v.equals(val)))
2824                      return false;
1608                it.advance();
2825              }
2826              for (Map.Entry<?,?> e : m.entrySet()) {
2827                  Object mk, mv, v;
# Line 1621 | Line 2837 | public class ConcurrentHashMapV8<K, V>
2837  
2838      /* ----------------Iterators -------------- */
2839  
2840 <    /**
2841 <     * Base class for key, value, and entry iterators.  Adds a map
2842 <     * reference to InternalIterator to support Iterator.remove.
2843 <     */
2844 <    static abstract class ViewIterator<K,V> extends InternalIterator {
1629 <        final ConcurrentHashMapV8<K, V> map;
1630 <        ViewIterator(ConcurrentHashMapV8<K, V> map) {
1631 <            super(map.table);
1632 <            this.map = map;
2840 >    static final class KeyIterator<K,V> extends InternalIterator<K,V>
2841 >        implements Spliterator<K>, Enumeration<K> {
2842 >        KeyIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
2843 >        KeyIterator(InternalIterator<K,V> it, boolean split) {
2844 >            super(it, split);
2845          }
2846 <
2847 <        public final void remove() {
1636 <            if (last == null)
2846 >        public KeyIterator<K,V> split() {
2847 >            if (last != null || (next != null && nextVal == null))
2848                  throw new IllegalStateException();
2849 <            map.remove(last.key);
2850 <            last = null;
2849 >            return new KeyIterator<K,V>(this, true);
2850 >        }
2851 >        public KeyIterator<K,V> clone() {
2852 >            if (last != null || (next != null && nextVal == null))
2853 >                throw new IllegalStateException();
2854 >            return new KeyIterator<K,V>(this, false);
2855          }
1641
1642        public final boolean hasNext()         { return next != null; }
1643        public final boolean hasMoreElements() { return next != null; }
1644    }
1645
1646    static final class KeyIterator<K,V> extends ViewIterator<K,V>
1647        implements Iterator<K>, Enumeration<K> {
1648        KeyIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
2856  
2857          @SuppressWarnings("unchecked")
2858          public final K next() {
2859 <            if (next == null)
2859 >            if (nextVal == null && advance() == null)
2860                  throw new NoSuchElementException();
2861              Object k = nextKey;
2862 <            advance();
2863 <            return (K)k;
2862 >            nextVal = null;
2863 >            return (K) k;
2864          }
2865  
2866          public final K nextElement() { return next(); }
2867      }
2868  
2869 <    static final class ValueIterator<K,V> extends ViewIterator<K,V>
2870 <        implements Iterator<V>, Enumeration<V> {
2869 >    static final class ValueIterator<K,V> extends InternalIterator<K,V>
2870 >        implements Spliterator<V>, Enumeration<V> {
2871          ValueIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
2872 +        ValueIterator(InternalIterator<K,V> it, boolean split) {
2873 +            super(it, split);
2874 +        }
2875 +        public ValueIterator<K,V> split() {
2876 +            if (last != null || (next != null && nextVal == null))
2877 +                throw new IllegalStateException();
2878 +            return new ValueIterator<K,V>(this, true);
2879 +        }
2880 +
2881 +        public ValueIterator<K,V> clone() {
2882 +            if (last != null || (next != null && nextVal == null))
2883 +                throw new IllegalStateException();
2884 +            return new ValueIterator<K,V>(this, false);
2885 +        }
2886  
2887          @SuppressWarnings("unchecked")
2888          public final V next() {
2889 <            if (next == null)
2889 >            Object v;
2890 >            if ((v = nextVal) == null && (v = advance()) == null)
2891                  throw new NoSuchElementException();
2892 <            Object v = nextVal;
2893 <            advance();
1672 <            return (V)v;
2892 >            nextVal = null;
2893 >            return (V) v;
2894          }
2895  
2896          public final V nextElement() { return next(); }
2897      }
2898  
2899 <    static final class EntryIterator<K,V> extends ViewIterator<K,V>
2900 <        implements Iterator<Map.Entry<K,V>> {
2899 >    static final class EntryIterator<K,V> extends InternalIterator<K,V>
2900 >        implements Spliterator<Map.Entry<K,V>> {
2901          EntryIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
2902 <
2903 <        @SuppressWarnings("unchecked")
2904 <        public final Map.Entry<K,V> next() {
2905 <            if (next == null)
2906 <                throw new NoSuchElementException();
2907 <            Object k = nextKey;
2908 <            Object v = nextVal;
2909 <            advance();
2910 <            return new WriteThroughEntry<K,V>((K)k, (V)v, map);
2902 >        EntryIterator(InternalIterator<K,V> it, boolean split) {
2903 >            super(it, split);
2904 >        }
2905 >        public EntryIterator<K,V> split() {
2906 >            if (last != null || (next != null && nextVal == null))
2907 >                throw new IllegalStateException();
2908 >            return new EntryIterator<K,V>(this, true);
2909 >        }
2910 >        public EntryIterator<K,V> clone() {
2911 >            if (last != null || (next != null && nextVal == null))
2912 >                throw new IllegalStateException();
2913 >            return new EntryIterator<K,V>(this, false);
2914          }
1691    }
1692
1693    static final class SnapshotEntryIterator<K,V> extends ViewIterator<K,V>
1694        implements Iterator<Map.Entry<K,V>> {
1695        SnapshotEntryIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
2915  
2916          @SuppressWarnings("unchecked")
2917          public final Map.Entry<K,V> next() {
2918 <            if (next == null)
2918 >            Object v;
2919 >            if ((v = nextVal) == null && (v = advance()) == null)
2920                  throw new NoSuchElementException();
2921              Object k = nextKey;
2922 <            Object v = nextVal;
2923 <            advance();
1704 <            return new SnapshotEntry<K,V>((K)k, (V)v);
2922 >            nextVal = null;
2923 >            return new MapEntry<K,V>((K)k, (V)v, map);
2924          }
2925      }
2926  
2927      /**
2928 <     * Base of writeThrough and Snapshot entry classes
2928 >     * Exported Entry for iterators
2929       */
2930 <    static abstract class MapEntry<K,V> implements Map.Entry<K, V> {
2930 >    static final class MapEntry<K,V> implements Map.Entry<K, V> {
2931          final K key; // non-null
2932          V val;       // non-null
2933 <        MapEntry(K key, V val)        { this.key = key; this.val = val; }
2933 >        final ConcurrentHashMapV8<K, V> map;
2934 >        MapEntry(K key, V val, ConcurrentHashMapV8<K, V> map) {
2935 >            this.key = key;
2936 >            this.val = val;
2937 >            this.map = map;
2938 >        }
2939          public final K getKey()       { return key; }
2940          public final V getValue()     { return val; }
2941          public final int hashCode()   { return key.hashCode() ^ val.hashCode(); }
# Line 1726 | Line 2950 | public class ConcurrentHashMapV8<K, V>
2950                      (v == val || v.equals(val)));
2951          }
2952  
1729        public abstract V setValue(V value);
1730    }
1731
1732    /**
1733     * Entry used by EntryIterator.next(), that relays setValue
1734     * changes to the underlying map.
1735     */
1736    static final class WriteThroughEntry<K,V> extends MapEntry<K,V>
1737        implements Map.Entry<K, V> {
1738        final ConcurrentHashMapV8<K, V> map;
1739        WriteThroughEntry(K key, V val, ConcurrentHashMapV8<K, V> map) {
1740            super(key, val);
1741            this.map = map;
1742        }
1743
2953          /**
2954           * Sets our entry's value and writes through to the map. The
2955 <         * value to return is somewhat arbitrary here. Since a
2956 <         * WriteThroughEntry does not necessarily track asynchronous
2957 <         * changes, the most recent "previous" value could be
2958 <         * different from what we return (or could even have been
2959 <         * removed in which case the put will re-establish). We do not
1751 <         * and cannot guarantee more.
2955 >         * value to return is somewhat arbitrary here. Since a we do
2956 >         * not necessarily track asynchronous changes, the most recent
2957 >         * "previous" value could be different from what we return (or
2958 >         * could even have been removed in which case the put will
2959 >         * re-establish). We do not and cannot guarantee more.
2960           */
2961          public final V setValue(V value) {
2962              if (value == null) throw new NullPointerException();
# Line 1759 | Line 2967 | public class ConcurrentHashMapV8<K, V>
2967          }
2968      }
2969  
1762    /**
1763     * Internal version of entry, that doesn't write though changes
1764     */
1765    static final class SnapshotEntry<K,V> extends MapEntry<K,V>
1766        implements Map.Entry<K, V> {
1767        SnapshotEntry(K key, V val) { super(key, val); }
1768        public final V setValue(V value) { // only locally update
1769            if (value == null) throw new NullPointerException();
1770            V v = val;
1771            val = value;
1772            return v;
1773        }
1774    }
1775
2970      /* ----------------Views -------------- */
2971  
2972      /**
2973 <     * Base class for views. This is done mainly to allow adding
1780 <     * customized parallel traversals (not yet implemented.)
2973 >     * Base class for views.
2974       */
2975      static abstract class MapView<K, V> {
2976          final ConcurrentHashMapV8<K, V> map;
# Line 1787 | Line 2980 | public class ConcurrentHashMapV8<K, V>
2980          public final void clear()               { map.clear(); }
2981  
2982          // implementations below rely on concrete classes supplying these
2983 <        abstract Iterator<?> iter();
2983 >        abstract public Iterator<?> iterator();
2984          abstract public boolean contains(Object o);
2985          abstract public boolean remove(Object o);
2986  
# Line 1800 | Line 2993 | public class ConcurrentHashMapV8<K, V>
2993              int n = (int)sz;
2994              Object[] r = new Object[n];
2995              int i = 0;
2996 <            Iterator<?> it = iter();
2996 >            Iterator<?> it = iterator();
2997              while (it.hasNext()) {
2998                  if (i == n) {
2999                      if (n >= MAX_ARRAY_SIZE)
# Line 1827 | Line 3020 | public class ConcurrentHashMapV8<K, V>
3020                  .newInstance(a.getClass().getComponentType(), m);
3021              int n = r.length;
3022              int i = 0;
3023 <            Iterator<?> it = iter();
3023 >            Iterator<?> it = iterator();
3024              while (it.hasNext()) {
3025                  if (i == n) {
3026                      if (n >= MAX_ARRAY_SIZE)
# Line 1849 | Line 3042 | public class ConcurrentHashMapV8<K, V>
3042  
3043          public final int hashCode() {
3044              int h = 0;
3045 <            for (Iterator<?> it = iter(); it.hasNext();)
3045 >            for (Iterator<?> it = iterator(); it.hasNext();)
3046                  h += it.next().hashCode();
3047              return h;
3048          }
# Line 1857 | Line 3050 | public class ConcurrentHashMapV8<K, V>
3050          public final String toString() {
3051              StringBuilder sb = new StringBuilder();
3052              sb.append('[');
3053 <            Iterator<?> it = iter();
3053 >            Iterator<?> it = iterator();
3054              if (it.hasNext()) {
3055                  for (;;) {
3056                      Object e = it.next();
# Line 1881 | Line 3074 | public class ConcurrentHashMapV8<K, V>
3074              return true;
3075          }
3076  
3077 <        public final boolean removeAll(Collection c) {
3077 >        public final boolean removeAll(Collection<?> c) {
3078              boolean modified = false;
3079 <            for (Iterator<?> it = iter(); it.hasNext();) {
3079 >            for (Iterator<?> it = iterator(); it.hasNext();) {
3080                  if (c.contains(it.next())) {
3081                      it.remove();
3082                      modified = true;
# Line 1894 | Line 3087 | public class ConcurrentHashMapV8<K, V>
3087  
3088          public final boolean retainAll(Collection<?> c) {
3089              boolean modified = false;
3090 <            for (Iterator<?> it = iter(); it.hasNext();) {
3090 >            for (Iterator<?> it = iterator(); it.hasNext();) {
3091                  if (!c.contains(it.next())) {
3092                      it.remove();
3093                      modified = true;
# Line 1909 | Line 3102 | public class ConcurrentHashMapV8<K, V>
3102          KeySet(ConcurrentHashMapV8<K, V> map)   { super(map); }
3103          public final boolean contains(Object o) { return map.containsKey(o); }
3104          public final boolean remove(Object o)   { return map.remove(o) != null; }
1912
3105          public final Iterator<K> iterator() {
3106              return new KeyIterator<K,V>(map);
3107          }
1916        final Iterator<?> iter() {
1917            return new KeyIterator<K,V>(map);
1918        }
3108          public final boolean add(K e) {
3109              throw new UnsupportedOperationException();
3110          }
# Line 1931 | Line 3120 | public class ConcurrentHashMapV8<K, V>
3120      }
3121  
3122      static final class Values<K,V> extends MapView<K,V>
3123 <        implements Collection<V>  {
3123 >        implements Collection<V> {
3124          Values(ConcurrentHashMapV8<K, V> map)   { super(map); }
3125          public final boolean contains(Object o) { return map.containsValue(o); }
1937
3126          public final boolean remove(Object o) {
3127              if (o != null) {
3128                  Iterator<V> it = new ValueIterator<K,V>(map);
# Line 1950 | Line 3138 | public class ConcurrentHashMapV8<K, V>
3138          public final Iterator<V> iterator() {
3139              return new ValueIterator<K,V>(map);
3140          }
1953        final Iterator<?> iter() {
1954            return new ValueIterator<K,V>(map);
1955        }
3141          public final boolean add(V e) {
3142              throw new UnsupportedOperationException();
3143          }
# Line 1961 | Line 3146 | public class ConcurrentHashMapV8<K, V>
3146          }
3147      }
3148  
3149 <    static final class EntrySet<K,V>  extends MapView<K,V>
3149 >    static final class EntrySet<K,V> extends MapView<K,V>
3150          implements Set<Map.Entry<K,V>> {
3151          EntrySet(ConcurrentHashMapV8<K, V> map) { super(map); }
1967
3152          public final boolean contains(Object o) {
3153              Object k, v, r; Map.Entry<?,?> e;
3154              return ((o instanceof Map.Entry) &&
# Line 1973 | Line 3157 | public class ConcurrentHashMapV8<K, V>
3157                      (v = e.getValue()) != null &&
3158                      (v == r || v.equals(r)));
3159          }
1976
3160          public final boolean remove(Object o) {
3161              Object k, v; Map.Entry<?,?> e;
3162              return ((o instanceof Map.Entry) &&
# Line 1981 | Line 3164 | public class ConcurrentHashMapV8<K, V>
3164                      (v = e.getValue()) != null &&
3165                      map.remove(k, v));
3166          }
1984
3167          public final Iterator<Map.Entry<K,V>> iterator() {
3168              return new EntryIterator<K,V>(map);
3169          }
1988        final Iterator<?> iter() {
1989            return new SnapshotEntryIterator<K,V>(map);
1990        }
3170          public final boolean add(Entry<K,V> e) {
3171              throw new UnsupportedOperationException();
3172          }
# Line 2033 | Line 3212 | public class ConcurrentHashMapV8<K, V>
3212                  segments[i] = new Segment<K,V>(LOAD_FACTOR);
3213          }
3214          s.defaultWriteObject();
3215 <        InternalIterator it = new InternalIterator(table);
3216 <        while (it.next != null) {
3215 >        InternalIterator<K,V> it = new InternalIterator<K,V>(this);
3216 >        Object v;
3217 >        while ((v = it.advance()) != null) {
3218              s.writeObject(it.nextKey);
3219 <            s.writeObject(it.nextVal);
2040 <            it.advance();
3219 >            s.writeObject(v);
3220          }
3221          s.writeObject(null);
3222          s.writeObject(null);
# Line 2063 | Line 3242 | public class ConcurrentHashMapV8<K, V>
3242              K k = (K) s.readObject();
3243              V v = (V) s.readObject();
3244              if (k != null && v != null) {
3245 <                p = new Node(spread(k.hashCode()), k, v, p);
3245 >                int h = spread(k.hashCode());
3246 >                p = new Node(h, k, v, p);
3247                  ++size;
3248              }
3249              else
# Line 2079 | Line 3259 | public class ConcurrentHashMapV8<K, V>
3259                  n = tableSizeFor(sz + (sz >>> 1) + 1);
3260              }
3261              int sc = sizeCtl;
3262 +            boolean collide = false;
3263              if (n > sc &&
3264                  UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
3265                  try {
# Line 2089 | Line 3270 | public class ConcurrentHashMapV8<K, V>
3270                          while (p != null) {
3271                              int j = p.hash & mask;
3272                              Node next = p.next;
3273 <                            p.next = tabAt(tab, j);
3273 >                            Node q = p.next = tabAt(tab, j);
3274                              setTabAt(tab, j, p);
3275 +                            if (!collide && q != null && q.hash == p.hash)
3276 +                                collide = true;
3277                              p = next;
3278                          }
3279                          table = tab;
3280                          counter.add(size);
3281 <                        sc = n - (n >>> 2) - 1;
3281 >                        sc = n - (n >>> 2);
3282                      }
3283                  } finally {
3284                      sizeCtl = sc;
3285                  }
3286 +                if (collide) { // rescan and convert to TreeBins
3287 +                    Node[] tab = table;
3288 +                    for (int i = 0; i < tab.length; ++i) {
3289 +                        int c = 0;
3290 +                        for (Node e = tabAt(tab, i); e != null; e = e.next) {
3291 +                            if (++c > TREE_THRESHOLD &&
3292 +                                (e.key instanceof Comparable)) {
3293 +                                replaceWithTreeBin(tab, i, e.key);
3294 +                                break;
3295 +                            }
3296 +                        }
3297 +                    }
3298 +                }
3299              }
3300              if (!init) { // Can only happen if unsafely published.
3301                  while (p != null) {
3302 <                    internalPut(p.key, p.val, true);
3302 >                    internalPut(p.key, p.val);
3303                      p = p.next;
3304                  }
3305              }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines