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.16 by dl, Fri Sep 9 13:02:01 2011 UTC vs.
Revision 1.46 by dl, Thu Jul 5 18:05:28 2012 UTC

# Line 6 | Line 6
6  
7   package jsr166e;
8   import jsr166e.LongAdder;
9 + import java.util.Arrays;
10   import java.util.Map;
11   import java.util.Set;
12   import java.util.Collection;
# Line 19 | 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 54 | Line 58 | import java.io.Serializable;
58   * <p> The table is dynamically expanded when there are too many
59   * collisions (i.e., keys that have distinct hash codes but fall into
60   * the same slot modulo the table size), with the expected average
61 < * effect of maintaining roughly two bins per mapping. There may be
62 < * much variance around this average as mappings are added and
63 < * removed, but overall, this maintains a commonly accepted time/space
64 < * tradeoff for hash tables.  However, resizing this or any other kind
65 < * of hash table may be a relatively slow operation. When possible, it
66 < * is a good idea to provide a size estimate as an optional {@code
61 > * effect of maintaining roughly two bins per mapping (corresponding
62 > * to a 0.75 load factor threshold for resizing). There may be much
63 > * variance around this average as mappings are added and removed, but
64 > * overall, this maintains a commonly accepted time/space tradeoff for
65 > * hash tables.  However, resizing this or any other kind of hash
66 > * table may be a relatively slow operation. When possible, it is a
67 > * good idea to provide a size estimate as an optional {@code
68   * initialCapacity} constructor argument. An additional optional
69   * {@code loadFactor} constructor argument provides a further means of
70   * customizing initial table capacity by specifying the table density
# Line 68 | 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 95 | 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
100 <     * 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
105 <         * mapping. If this function throws an (unchecked) exception,
106 <         * the exception is rethrown to its caller, and no mapping is
107 <         * recorded.  Because this function is invoked within
108 <         * atomicity control, the computation should be short and
109 <         * simple. The most common usage is to construct a new object
110 <         * 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 also
149 +     * 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      /*
201       * Overview:
202       *
203       * The primary design goal of this hash table is to maintain
204       * concurrent readability (typically method get(), but also
205       * iterators and related methods) while minimizing update
206 <     * contention.
206 >     * contention. Secondary goals are to keep space consumption about
207 >     * the same or better than java.util.HashMap, and to support high
208 >     * initial insertion rates on an empty table by many threads.
209       *
210       * Each key-value mapping is held in a Node.  Because Node fields
211       * can contain special values, they are defined using plain Object
# Line 129 | Line 213 | public class ConcurrentHashMapV8<K, V>
213       * work off Object types. And similarly, so do the internal
214       * methods of auxiliary iterator and view classes.  All public
215       * generic typed methods relay in/out of these internal methods,
216 <     * supplying null-checks and casts as needed.
216 >     * supplying null-checks and casts as needed. This also allows
217 >     * many of the public methods to be factored into a smaller number
218 >     * of internal methods (although sadly not so for the five
219 >     * variants of put-related operations). The validation-based
220 >     * approach explained below leads to a lot of code sprawl because
221 >     * retry-control precludes factoring into smaller methods.
222       *
223       * The table is lazily initialized to a power-of-two size upon the
224 <     * first insertion.  Each bin in the table contains a list of
225 <     * Nodes (most often, zero or one Node).  Table accesses require
226 <     * volatile/atomic reads, writes, and CASes.  Because there is no
227 <     * other way to arrange this without adding further indirections,
228 <     * we use intrinsics (sun.misc.Unsafe) operations.  The lists of
229 <     * nodes within bins are always accurately traversable under
230 <     * volatile reads, so long as lookups check hash code and
231 <     * non-nullness of value before checking key equality. (All valid
232 <     * hash codes are nonnegative. Negative values are reserved for
233 <     * special forwarding nodes; see below.)
224 >     * first insertion.  Each bin in the table normally contains a
225 >     * list of Nodes (most often, the list has only zero or one Node).
226 >     * Table accesses require volatile/atomic reads, writes, and
227 >     * CASes.  Because there is no other way to arrange this without
228 >     * adding further indirections, we use intrinsics
229 >     * (sun.misc.Unsafe) operations.  The lists of nodes within bins
230 >     * are always accurately traversable under volatile reads, so long
231 >     * as lookups check hash code and non-nullness of value before
232 >     * checking key equality.
233 >     *
234 >     * We use the top two bits of Node hash fields for control
235 >     * purposes -- they are available anyway because of addressing
236 >     * constraints.  As explained further below, these top bits are
237 >     * used as follows:
238 >     *  00 - Normal
239 >     *  01 - Locked
240 >     *  11 - Locked and may have a thread waiting for lock
241 >     *  10 - Node is a forwarding node
242 >     *
243 >     * The lower 30 bits of each Node's hash field contain a
244 >     * transformation of the key's hash code, except for forwarding
245 >     * nodes, for which the lower bits are zero (and so always have
246 >     * hash field == MOVED).
247       *
248 <     * Insertion (via put or putIfAbsent) of the first node in an
248 >     * Insertion (via put or its variants) of the first node in an
249       * empty bin is performed by just CASing it to the bin.  This is
250 <     * on average by far the most common case for put operations.
251 <     * Other update operations (insert, delete, and replace) require
252 <     * locks.  We do not want to waste the space required to associate
253 <     * a distinct lock object with each bin, so instead use the first
254 <     * node of a bin list itself as a lock, using plain "synchronized"
255 <     * locks. These save space and we can live with block-structured
256 <     * lock/unlock operations. Using the first node of a list as a
257 <     * lock does not by itself suffice though: When a node is locked,
258 <     * any update must first validate that it is still the first node,
259 <     * and retry if not. Because new nodes are always appended to
260 <     * lists, once a node is first in a bin, it remains first until
261 <     * deleted or the bin becomes invalidated.  However, operations
262 <     * that only conditionally update can and sometimes do inspect
263 <     * nodes until the point of update. This is a converse of sorts to
264 <     * the lazy locking technique described by Herlihy & Shavit.
250 >     * by far the most common case for put operations under most
251 >     * key/hash distributions.  Other update operations (insert,
252 >     * delete, and replace) require locks.  We do not want to waste
253 >     * the space required to associate a distinct lock object with
254 >     * each bin, so instead use the first node of a bin list itself as
255 >     * a lock. Blocking support for these locks relies on the builtin
256 >     * "synchronized" monitors.  However, we also need a tryLock
257 >     * construction, so we overlay these by using bits of the Node
258 >     * hash field for lock control (see above), and so normally use
259 >     * builtin monitors only for blocking and signalling using
260 >     * wait/notifyAll constructions. See Node.tryAwaitLock.
261 >     *
262 >     * Using the first node of a list as a lock does not by itself
263 >     * suffice though: When a node is locked, any update must first
264 >     * validate that it is still the first node after locking it, and
265 >     * retry if not. Because new nodes are always appended to lists,
266 >     * once a node is first in a bin, it remains first until deleted
267 >     * or the bin becomes invalidated (upon resizing).  However,
268 >     * operations that only conditionally update may inspect nodes
269 >     * until the point of update. This is a converse of sorts to the
270 >     * lazy locking technique described by Herlihy & Shavit.
271       *
272 <     * The main disadvantage of this approach is that most update
272 >     * The main disadvantage of per-bin locks is that other update
273       * operations on other nodes in a bin list protected by the same
274       * lock can stall, for example when user equals() or mapping
275 <     * functions take a long time.  However, statistically, this is
276 <     * not a common enough problem to outweigh the time/space overhead
277 <     * of alternatives: Under random hash codes, the frequency of
170 <     * nodes in bins follows a Poisson distribution
275 >     * functions take a long time.  However, statistically, under
276 >     * random hash codes, this is not a common problem.  Ideally, the
277 >     * frequency of nodes in bins follows a Poisson distribution
278       * (http://en.wikipedia.org/wiki/Poisson_distribution) with a
279       * parameter of about 0.5 on average, given the resizing threshold
280       * of 0.75, although with a large variance because of resizing
281       * granularity. Ignoring variance, the expected occurrences of
282       * list size k are (exp(-0.5) * pow(0.5, k) / factorial(k)). The
283 <     * first few values are:
283 >     * first values are:
284       *
285 <     * 0:    0.607
286 <     * 1:    0.303
287 <     * 2:    0.076
288 <     * 3:    0.012
289 <     * more: 0.002
285 >     * 0:    0.60653066
286 >     * 1:    0.30326533
287 >     * 2:    0.07581633
288 >     * 3:    0.01263606
289 >     * 4:    0.00157952
290 >     * 5:    0.00015795
291 >     * 6:    0.00001316
292 >     * 7:    0.00000094
293 >     * 8:    0.00000006
294 >     * more: less than 1 in ten million
295       *
296       * Lock contention probability for two threads accessing distinct
297 <     * elements is roughly 1 / (8 * #elements).  Function "spread"
298 <     * performs hashCode randomization that improves the likelihood
299 <     * that these assumptions hold unless users define exactly the
300 <     * same value for too many hashCodes.
301 <     *
302 <     * The table is resized when occupancy exceeds a threshold.  Only
303 <     * a single thread performs the resize (using field "resizing", to
304 <     * arrange exclusion), but the table otherwise remains usable for
305 <     * reads and updates. Resizing proceeds by transferring bins, one
306 <     * by one, from the table to the next table.  Upon transfer, the
307 <     * old table bin contains only a special forwarding node (with
308 <     * negative hash field) that contains the next table as its
309 <     * key. On encountering a forwarding node, access and update
310 <     * operations restart, using the new table. To ensure concurrent
311 <     * readability of traversals, transfers must proceed from the last
312 <     * bin (table.length - 1) up towards the first.  Upon seeing a
313 <     * forwarding node, traversals (see class InternalIterator)
314 <     * arrange to move to the new table for the rest of the traversal
315 <     * without revisiting nodes.  This constrains bin transfers to a
316 <     * particular order, and so can block indefinitely waiting for the
317 <     * next lock, and other threads cannot help with the transfer.
318 <     * However, expected stalls are infrequent enough to not warrant
319 <     * the additional overhead of access and iteration schemes that
320 <     * could admit out-of-order or concurrent bin transfers.
297 >     * elements is roughly 1 / (8 * #elements) under random hashes.
298 >     *
299 >     * Actual hash code distributions encountered in practice
300 >     * sometimes deviate significantly from uniform randomness.  This
301 >     * includes the case when N > (1<<30), so some keys MUST collide.
302 >     * Similarly for dumb or hostile usages in which multiple keys are
303 >     * designed to have identical hash codes. Also, although we guard
304 >     * against the worst effects of this (see method spread), sets of
305 >     * hashes may differ only in bits that do not impact their bin
306 >     * index for a given power-of-two mask.  So we use a secondary
307 >     * strategy that applies when the number of nodes in a bin exceeds
308 >     * a threshold, and at least one of the keys implements
309 >     * Comparable.  These TreeBins use a balanced tree to hold nodes
310 >     * (a specialized form of red-black trees), bounding search time
311 >     * to O(log N).  Each search step in a TreeBin is around twice as
312 >     * slow as in a regular list, but given that N cannot exceed
313 >     * (1<<64) (before running out of addresses) this bounds search
314 >     * steps, lock hold times, etc, to reasonable constants (roughly
315 >     * 100 nodes inspected per operation worst case) so long as keys
316 >     * are Comparable (which is very common -- String, Long, etc).
317 >     * TreeBin nodes (TreeNodes) also maintain the same "next"
318 >     * traversal pointers as regular nodes, so can be traversed in
319 >     * iterators in the same way.
320 >     *
321 >     * The table is resized when occupancy exceeds a percentage
322 >     * threshold (nominally, 0.75, but see below).  Only a single
323 >     * thread performs the resize (using field "sizeCtl", to arrange
324 >     * exclusion), but the table otherwise remains usable for reads
325 >     * and updates. Resizing proceeds by transferring bins, one by
326 >     * one, from the table to the next table.  Because we are using
327 >     * power-of-two expansion, the elements from each bin must either
328 >     * stay at same index, or move with a power of two offset. We
329 >     * eliminate unnecessary node creation by catching cases where old
330 >     * nodes can be reused because their next fields won't change.  On
331 >     * average, only about one-sixth of them need cloning when a table
332 >     * doubles. The nodes they replace will be garbage collectable as
333 >     * soon as they are no longer referenced by any reader thread that
334 >     * may be in the midst of concurrently traversing table.  Upon
335 >     * transfer, the old table bin contains only a special forwarding
336 >     * node (with hash field "MOVED") that contains the next table as
337 >     * its key. On encountering a forwarding node, access and update
338 >     * operations restart, using the new table.
339 >     *
340 >     * Each bin transfer requires its bin lock. However, unlike other
341 >     * cases, a transfer can skip a bin if it fails to acquire its
342 >     * lock, and revisit it later (unless it is a TreeBin). Method
343 >     * rebuild maintains a buffer of TRANSFER_BUFFER_SIZE bins that
344 >     * have been skipped because of failure to acquire a lock, and
345 >     * blocks only if none are available (i.e., only very rarely).
346 >     * The transfer operation must also ensure that all accessible
347 >     * bins in both the old and new table are usable by any traversal.
348 >     * When there are no lock acquisition failures, this is arranged
349 >     * simply by proceeding from the last bin (table.length - 1) up
350 >     * towards the first.  Upon seeing a forwarding node, traversals
351 >     * (see class InternalIterator) arrange to move to the new table
352 >     * without revisiting nodes.  However, when any node is skipped
353 >     * during a transfer, all earlier table bins may have become
354 >     * visible, so are initialized with a reverse-forwarding node back
355 >     * to the old table until the new ones are established. (This
356 >     * sometimes requires transiently locking a forwarding node, which
357 >     * is possible under the above encoding.) These more expensive
358 >     * mechanics trigger only when necessary.
359       *
360 <     * This traversal scheme also applies to partial traversals of
360 >     * The traversal scheme also applies to partial traversals of
361       * ranges of bins (via an alternate InternalIterator constructor)
362 <     * to support partitioned aggregate operations (that are not
363 <     * otherwise implemented yet).  Also, read-only operations give up
364 <     * if ever forwarded to a null table, which provides support for
365 <     * shutdown-style clearing, which is also not currently
216 <     * implemented.
362 >     * to support partitioned aggregate operations.  Also, read-only
363 >     * operations give up if ever forwarded to a null table, which
364 >     * provides support for shutdown-style clearing, which is also not
365 >     * currently implemented.
366       *
367       * Lazy table initialization minimizes footprint until first use,
368       * and also avoids resizings when the first operation is from a
369       * putAll, constructor with map argument, or deserialization.
370 <     * These cases attempt to override the targetCapacity used in
371 <     * growTable. These harmlessly fail to take effect in cases of
223 <     * races with other ongoing resizings. Uses of the threshold and
224 <     * targetCapacity during attempted initializations or resizings
225 <     * are racy but fall back on checks to preserve correctness.
370 >     * These cases attempt to override the initial capacity settings,
371 >     * but harmlessly fail to take effect in cases of races.
372       *
373       * The element count is maintained using a LongAdder, which avoids
374       * contention on updates but can encounter cache thrashing if read
375       * too frequently during concurrent access. To avoid reading so
376 <     * often, resizing is normally attempted only upon adding to a bin
377 <     * already holding two or more nodes. Under uniform hash
378 <     * distributions, the probability of this occurring at threshold
379 <     * is around 13%, meaning that only about 1 in 8 puts check
380 <     * threshold (and after resizing, many fewer do so). But this
381 <     * approximation has high variance for small table sizes, so we
382 <     * check on any collision for sizes <= 64.  Further, to increase
383 <     * the probability that a resize occurs soon enough, we offset the
384 <     * threshold (see THRESHOLD_OFFSET) by the expected number of puts
385 <     * between checks.
376 >     * often, resizing is attempted either when a bin lock is
377 >     * contended, or upon adding to a bin already holding two or more
378 >     * nodes (checked before adding in the xIfAbsent methods, after
379 >     * adding in others). Under uniform hash distributions, the
380 >     * probability of this occurring at threshold is around 13%,
381 >     * meaning that only about 1 in 8 puts check threshold (and after
382 >     * resizing, many fewer do so). But this approximation has high
383 >     * variance for small table sizes, so we check on any collision
384 >     * for sizes <= 64. The bulk putAll operation further reduces
385 >     * contention by only committing count updates upon these size
386 >     * checks.
387       *
388       * Maintaining API and serialization compatibility with previous
389       * versions of this class introduces several oddities. Mainly: We
390       * leave untouched but unused constructor arguments refering to
391 <     * concurrencyLevel. We also declare an unused "Segment" class
392 <     * that is instantiated in minimal form only when serializing.
391 >     * concurrencyLevel. We accept a loadFactor constructor argument,
392 >     * but apply it only to initial table capacity (which is the only
393 >     * time that we can guarantee to honor it.) We also declare an
394 >     * unused "Segment" class that is instantiated in minimal form
395 >     * only when serializing.
396       */
397  
398      /* ---------------- Constants -------------- */
# Line 250 | Line 400 | public class ConcurrentHashMapV8<K, V>
400      /**
401       * The largest possible table capacity.  This value must be
402       * exactly 1<<30 to stay within Java array allocation and indexing
403 <     * bounds for power of two table sizes.
403 >     * bounds for power of two table sizes, and is further required
404 >     * because the top two bits of 32bit hash fields are used for
405 >     * control purposes.
406       */
407      private static final int MAXIMUM_CAPACITY = 1 << 30;
408  
# Line 261 | Line 413 | public class ConcurrentHashMapV8<K, V>
413      private static final int DEFAULT_CAPACITY = 16;
414  
415      /**
416 <     * The load factor for this table. Overrides of this value in
417 <     * constructors affect only the initial table capacity.  The
266 <     * actual floating point value isn't normally used, because it is
267 <     * simpler to rely on the expression {@code n - (n >>> 2)} for the
268 <     * associated resizing threshold.
416 >     * The largest possible (non-power of two) array size.
417 >     * Needed by toArray and related methods.
418       */
419 <    private static final float LOAD_FACTOR = 0.75f;
419 >    static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
420  
421      /**
422 <     * The count value to offset thresholds to compensate for checking
423 <     * for the need to resize only when inserting into bins with two
275 <     * or more elements. See above for explanation.
422 >     * The default concurrency level for this table. Unused but
423 >     * defined for compatibility with previous versions of this class.
424       */
425 <    private static final int THRESHOLD_OFFSET = 8;
425 >    private static final int DEFAULT_CONCURRENCY_LEVEL = 16;
426  
427      /**
428 <     * The default concurrency level for this table. Unused except as
429 <     * a sizing hint, but defined for compatibility with previous
430 <     * versions of this class.
428 >     * The load factor for this table. Overrides of this value in
429 >     * constructors affect only the initial table capacity.  The
430 >     * actual floating point value isn't normally used -- it is
431 >     * simpler to use expressions such as {@code n - (n >>> 2)} for
432 >     * the associated resizing threshold.
433       */
434 <    private static final int DEFAULT_CONCURRENCY_LEVEL = 16;
285 <
286 <    /* ---------------- Nodes -------------- */
434 >    private static final float LOAD_FACTOR = 0.75f;
435  
436      /**
437 <     * Key-value entry. Note that this is never exported out as a
438 <     * user-visible Map.Entry. Nodes with a negative hash field are
439 <     * special, and do not contain user keys or values.  Otherwise,
292 <     * keys are never null, and null val fields indicate that a node
293 <     * is in the process of being deleted or created. For purposes of
294 <     * read-only, access, a key may be read before a val, but can only
295 <     * be used after checking val.  (For an update operation, when a
296 <     * lock is held on a node, order doesn't matter.)
437 >     * The buffer size for skipped bins during transfers. The
438 >     * value is arbitrary but should be large enough to avoid
439 >     * most locking stalls during resizes.
440       */
441 <    static final class Node {
299 <        final int hash;
300 <        final Object key;
301 <        volatile Object val;
302 <        volatile Node next;
303 <
304 <        Node(int hash, Object key, Object val, Node next) {
305 <            this.hash = hash;
306 <            this.key = key;
307 <            this.val = val;
308 <            this.next = next;
309 <        }
310 <    }
441 >    private static final int TRANSFER_BUFFER_SIZE = 32;
442  
443      /**
444 <     * Sign bit of node hash value indicating to use table in node.key.
444 >     * The bin count threshold for using a tree rather than list for a
445 >     * bin.  The value reflects the approximate break-even point for
446 >     * using tree-based operations.
447       */
448 <    private static final int SIGN_BIT = 0x80000000;
448 >    private static final int TREE_THRESHOLD = 8;
449 >
450 >    /*
451 >     * Encodings for special uses of Node hash fields. See above for
452 >     * explanation.
453 >     */
454 >    static final int MOVED     = 0x80000000; // hash field for forwarding nodes
455 >    static final int LOCKED    = 0x40000000; // set/tested only as a bit
456 >    static final int WAITING   = 0xc0000000; // both bits set/tested together
457 >    static final int HASH_BITS = 0x3fffffff; // usable bits of normal node hash
458  
459      /* ---------------- Fields -------------- */
460  
# Line 322 | Line 464 | public class ConcurrentHashMapV8<K, V>
464       */
465      transient volatile Node[] table;
466  
467 <    /** The counter maintaining number of elements. */
467 >    /**
468 >     * The counter maintaining number of elements.
469 >     */
470      private transient final LongAdder counter;
471 <    /** Nonzero when table is being initialized or resized. Updated via CAS. */
472 <    private transient volatile int resizing;
473 <    /** The next element count value upon which to resize the table. */
474 <    private transient int threshold;
475 <    /** The target capacity; volatile to cover initialization races. */
476 <    private transient volatile int targetCapacity;
471 >
472 >    /**
473 >     * Table initialization and resizing control.  When negative, the
474 >     * table is being initialized or resized. Otherwise, when table is
475 >     * null, holds the initial table size to use upon creation, or 0
476 >     * for default. After initialization, holds the next element count
477 >     * value upon which to resize the table.
478 >     */
479 >    private transient volatile int sizeCtl;
480  
481      // views
482      private transient KeySet<K,V> keySet;
# Line 365 | Line 512 | public class ConcurrentHashMapV8<K, V>
512          UNSAFE.putObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE, v);
513      }
514  
515 <    /* ----------------Table Initialization and Resizing -------------- */
515 >    /* ---------------- Nodes -------------- */
516  
517      /**
518 <     * Returns a power of two table size for the given desired capacity.
519 <     * See Hackers Delight, sec 3.2
518 >     * Key-value entry. Note that this is never exported out as a
519 >     * user-visible Map.Entry (see MapEntry below). Nodes with a hash
520 >     * field of MOVED are special, and do not contain user keys or
521 >     * values.  Otherwise, keys are never null, and null val fields
522 >     * indicate that a node is in the process of being deleted or
523 >     * created. For purposes of read-only access, a key may be read
524 >     * before a val, but can only be used after checking val to be
525 >     * non-null.
526       */
527 <    private static final int tableSizeFor(int c) {
528 <        int n = c - 1;
529 <        n |= n >>> 1;
530 <        n |= n >>> 2;
531 <        n |= n >>> 4;
379 <        n |= n >>> 8;
380 <        n |= n >>> 16;
381 <        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
382 <    }
527 >    static class Node {
528 >        volatile int hash;
529 >        final Object key;
530 >        volatile Object val;
531 >        volatile Node next;
532  
533 <    /**
534 <     * If not already resizing, initializes or creates next table and
535 <     * transfers bins. Initial table size uses the capacity recorded
536 <     * in targetCapacity.  Rechecks occupancy after a transfer to see
537 <     * if another resize is already needed because resizings are
538 <     * lagging additions.
539 <     *
540 <     * @return current table
541 <     */
542 <    private final Node[] growTable() {
543 <        if (resizing == 0 &&
544 <            UNSAFE.compareAndSwapInt(this, resizingOffset, 0, 1)) {
545 <            try {
546 <                for (;;) {
547 <                    Node[] tab = table;
548 <                    int n, c, m;
549 <                    if (tab == null)
550 <                        n = (c = targetCapacity) > 0 ? c : DEFAULT_CAPACITY;
551 <                    else if ((m = tab.length) < MAXIMUM_CAPACITY &&
552 <                             counter.sum() >= (long)threshold)
553 <                        n = m << 1;
554 <                    else
555 <                        break;
556 <                    threshold = n - (n >>> 2) - THRESHOLD_OFFSET;
557 <                    Node[] nextTab = new Node[n];
558 <                    if (tab != null)
559 <                        transfer(tab, nextTab,
560 <                                 new Node(SIGN_BIT, nextTab, null, null));
561 <                    table = nextTab;
562 <                    if (tab == null)
533 >        Node(int hash, Object key, Object val, Node next) {
534 >            this.hash = hash;
535 >            this.key = key;
536 >            this.val = val;
537 >            this.next = next;
538 >        }
539 >
540 >        /** CompareAndSet the hash field */
541 >        final boolean casHash(int cmp, int val) {
542 >            return UNSAFE.compareAndSwapInt(this, hashOffset, cmp, val);
543 >        }
544 >
545 >        /** The number of spins before blocking for a lock */
546 >        static final int MAX_SPINS =
547 >            Runtime.getRuntime().availableProcessors() > 1 ? 64 : 1;
548 >
549 >        /**
550 >         * Spins a while if LOCKED bit set and this node is the first
551 >         * of its bin, and then sets WAITING bits on hash field and
552 >         * blocks (once) if they are still set.  It is OK for this
553 >         * method to return even if lock is not available upon exit,
554 >         * which enables these simple single-wait mechanics.
555 >         *
556 >         * The corresponding signalling operation is performed within
557 >         * callers: Upon detecting that WAITING has been set when
558 >         * unlocking lock (via a failed CAS from non-waiting LOCKED
559 >         * state), unlockers acquire the sync lock and perform a
560 >         * notifyAll.
561 >         */
562 >        final void tryAwaitLock(Node[] tab, int i) {
563 >            if (tab != null && i >= 0 && i < tab.length) { // bounds check
564 >                int r = ThreadLocalRandom.current().nextInt(); // randomize spins
565 >                int spins = MAX_SPINS, h;
566 >                while (tabAt(tab, i) == this && ((h = hash) & LOCKED) != 0) {
567 >                    if (spins >= 0) {
568 >                        r ^= r << 1; r ^= r >>> 3; r ^= r << 10; // xorshift
569 >                        if (r >= 0 && --spins == 0)
570 >                            Thread.yield();  // yield before block
571 >                    }
572 >                    else if (casHash(h, h | WAITING)) {
573 >                        synchronized (this) {
574 >                            if (tabAt(tab, i) == this &&
575 >                                (hash & WAITING) == WAITING) {
576 >                                try {
577 >                                    wait();
578 >                                } catch (InterruptedException ie) {
579 >                                    Thread.currentThread().interrupt();
580 >                                }
581 >                            }
582 >                            else
583 >                                notifyAll(); // possibly won race vs signaller
584 >                        }
585                          break;
586 +                    }
587                  }
416            } finally {
417                resizing = 0;
588              }
589          }
590 <        else if (table == null)
591 <            Thread.yield(); // lost initialization race; just spin
592 <        return table;
590 >
591 >        // Unsafe mechanics for casHash
592 >        private static final sun.misc.Unsafe UNSAFE;
593 >        private static final long hashOffset;
594 >
595 >        static {
596 >            try {
597 >                UNSAFE = getUnsafe();
598 >                Class<?> k = Node.class;
599 >                hashOffset = UNSAFE.objectFieldOffset
600 >                    (k.getDeclaredField("hash"));
601 >            } catch (Exception e) {
602 >                throw new Error(e);
603 >            }
604 >        }
605      }
606  
607 <    /*
608 <     * Reclassifies nodes in each bin to new table.  Because we are
609 <     * using power-of-two expansion, the elements from each bin must
610 <     * either stay at same index, or move with a power of two
429 <     * offset. We eliminate unnecessary node creation by catching
430 <     * cases where old nodes can be reused because their next fields
431 <     * won't change.  Statistically, only about one-sixth of them need
432 <     * cloning when a table doubles. The nodes they replace will be
433 <     * garbage collectable as soon as they are no longer referenced by
434 <     * any reader thread that may be in the midst of concurrently
435 <     * traversing table.
436 <     *
437 <     * Transfers are done from the bottom up to preserve iterator
438 <     * traversability. On each step, the old bin is locked,
439 <     * moved/copied, and then replaced with a forwarding node.
607 >    /* ---------------- TreeBins -------------- */
608 >
609 >    /**
610 >     * Nodes for use in TreeBins
611       */
612 <    private static final void transfer(Node[] tab, Node[] nextTab, Node fwd) {
613 <        int n = tab.length;
614 <        Node ignore = nextTab[n + n - 1]; // force bounds check
615 <        for (int i = n - 1; i >= 0; --i) {
616 <            for (Node e;;) {
617 <                if ((e = tabAt(tab, i)) != null) {
618 <                    boolean validated = false;
619 <                    synchronized (e) {
620 <                        if (tabAt(tab, i) == e) {
621 <                            validated = true;
622 <                            Node lo = null, hi = null, lastRun = e;
623 <                            int runBit = e.hash & n;
624 <                            for (Node p = e.next; p != null; p = p.next) {
625 <                                int b = p.hash & n;
626 <                                if (b != runBit) {
627 <                                    runBit = b;
628 <                                    lastRun = p;
612 >    static final class TreeNode extends Node {
613 >        TreeNode parent;  // red-black tree links
614 >        TreeNode left;
615 >        TreeNode right;
616 >        TreeNode prev;    // needed to unlink next upon deletion
617 >        boolean red;
618 >
619 >        TreeNode(int hash, Object key, Object val, Node next, TreeNode parent) {
620 >            super(hash, key, val, next);
621 >            this.parent = parent;
622 >        }
623 >    }
624 >
625 >    /**
626 >     * A specialized form of red-black tree for use in bins
627 >     * whose size exceeds a threshold.
628 >     *
629 >     * TreeBins use a special form of comparison for search and
630 >     * related operations (which is the main reason we cannot use
631 >     * existing collections such as TreeMaps). TreeBins contain
632 >     * Comparable elements, but may contain others, as well as
633 >     * elements that are Comparable but not necessarily Comparable<T>
634 >     * for the same T, so we cannot invoke compareTo among them. To
635 >     * handle this, the tree is ordered primarily by hash value, then
636 >     * by getClass().getName() order, and then by Comparator order
637 >     * among elements of the same class.  On lookup at a node, if
638 >     * elements are not comparable or compare as 0, both left and
639 >     * right children may need to be searched in the case of tied hash
640 >     * values. (This corresponds to the full list search that would be
641 >     * necessary if all elements were non-Comparable and had tied
642 >     * hashes.)  The red-black balancing code is updated from
643 >     * pre-jdk-collections
644 >     * (http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java)
645 >     * based in turn on Cormen, Leiserson, and Rivest "Introduction to
646 >     * Algorithms" (CLR).
647 >     *
648 >     * TreeBins also maintain a separate locking discipline than
649 >     * regular bins. Because they are forwarded via special MOVED
650 >     * nodes at bin heads (which can never change once established),
651 >     * we cannot use use those nodes as locks. Instead, TreeBin
652 >     * extends AbstractQueuedSynchronizer to support a simple form of
653 >     * read-write lock. For update operations and table validation,
654 >     * the exclusive form of lock behaves in the same way as bin-head
655 >     * locks. However, lookups use shared read-lock mechanics to allow
656 >     * multiple readers in the absence of writers.  Additionally,
657 >     * these lookups do not ever block: While the lock is not
658 >     * available, they proceed along the slow traversal path (via
659 >     * next-pointers) until the lock becomes available or the list is
660 >     * exhausted, whichever comes first. (These cases are not fast,
661 >     * but maximize aggregate expected throughput.)  The AQS mechanics
662 >     * for doing this are straightforward.  The lock state is held as
663 >     * AQS getState().  Read counts are negative; the write count (1)
664 >     * is positive.  There are no signalling preferences among readers
665 >     * and writers. Since we don't need to export full Lock API, we
666 >     * just override the minimal AQS methods and use them directly.
667 >     */
668 >    static final class TreeBin extends AbstractQueuedSynchronizer {
669 >        private static final long serialVersionUID = 2249069246763182397L;
670 >        transient TreeNode root;  // root of tree
671 >        transient TreeNode first; // head of next-pointer list
672 >
673 >        /* AQS overrides */
674 >        public final boolean isHeldExclusively() { return getState() > 0; }
675 >        public final boolean tryAcquire(int ignore) {
676 >            if (compareAndSetState(0, 1)) {
677 >                setExclusiveOwnerThread(Thread.currentThread());
678 >                return true;
679 >            }
680 >            return false;
681 >        }
682 >        public final boolean tryRelease(int ignore) {
683 >            setExclusiveOwnerThread(null);
684 >            setState(0);
685 >            return true;
686 >        }
687 >        public final int tryAcquireShared(int ignore) {
688 >            for (int c;;) {
689 >                if ((c = getState()) > 0)
690 >                    return -1;
691 >                if (compareAndSetState(c, c -1))
692 >                    return 1;
693 >            }
694 >        }
695 >        public final boolean tryReleaseShared(int ignore) {
696 >            int c;
697 >            do {} while (!compareAndSetState(c = getState(), c + 1));
698 >            return c == -1;
699 >        }
700 >
701 >        /** From CLR */
702 >        private void rotateLeft(TreeNode p) {
703 >            if (p != null) {
704 >                TreeNode r = p.right, pp, rl;
705 >                if ((rl = p.right = r.left) != null)
706 >                    rl.parent = p;
707 >                if ((pp = r.parent = p.parent) == null)
708 >                    root = r;
709 >                else if (pp.left == p)
710 >                    pp.left = r;
711 >                else
712 >                    pp.right = r;
713 >                r.left = p;
714 >                p.parent = r;
715 >            }
716 >        }
717 >
718 >        /** From CLR */
719 >        private void rotateRight(TreeNode p) {
720 >            if (p != null) {
721 >                TreeNode l = p.left, pp, lr;
722 >                if ((lr = p.left = l.right) != null)
723 >                    lr.parent = p;
724 >                if ((pp = l.parent = p.parent) == null)
725 >                    root = l;
726 >                else if (pp.right == p)
727 >                    pp.right = l;
728 >                else
729 >                    pp.left = l;
730 >                l.right = p;
731 >                p.parent = l;
732 >            }
733 >        }
734 >
735 >        /**
736 >         * Return the TreeNode (or null if not found) for the given key
737 >         * starting at given root.
738 >         */
739 >        @SuppressWarnings("unchecked") // suppress Comparable cast warning
740 >        final TreeNode getTreeNode(int h, Object k, TreeNode p) {
741 >            Class<?> c = k.getClass();
742 >            while (p != null) {
743 >                int dir, ph;  Object pk; Class<?> pc;
744 >                if ((ph = p.hash) == h) {
745 >                    if ((pk = p.key) == k || k.equals(pk))
746 >                        return p;
747 >                    if (c != (pc = pk.getClass()) ||
748 >                        !(k instanceof Comparable) ||
749 >                        (dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) {
750 >                        dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName());
751 >                        TreeNode r = null, s = null, pl, pr;
752 >                        if (dir >= 0) {
753 >                            if ((pl = p.left) != null && h <= pl.hash)
754 >                                s = pl;
755 >                        }
756 >                        else if ((pr = p.right) != null && h >= pr.hash)
757 >                            s = pr;
758 >                        if (s != null && (r = getTreeNode(h, k, s)) != null)
759 >                            return r;
760 >                    }
761 >                }
762 >                else
763 >                    dir = (h < ph) ? -1 : 1;
764 >                p = (dir > 0) ? p.right : p.left;
765 >            }
766 >            return null;
767 >        }
768 >
769 >        /**
770 >         * Wrapper for getTreeNode used by CHM.get. Tries to obtain
771 >         * read-lock to call getTreeNode, but during failure to get
772 >         * lock, searches along next links.
773 >         */
774 >        final Object getValue(int h, Object k) {
775 >            Node r = null;
776 >            int c = getState(); // Must read lock state first
777 >            for (Node e = first; e != null; e = e.next) {
778 >                if (c <= 0 && compareAndSetState(c, c - 1)) {
779 >                    try {
780 >                        r = getTreeNode(h, k, root);
781 >                    } finally {
782 >                        releaseShared(0);
783 >                    }
784 >                    break;
785 >                }
786 >                else if ((e.hash & HASH_BITS) == h && k.equals(e.key)) {
787 >                    r = e;
788 >                    break;
789 >                }
790 >                else
791 >                    c = getState();
792 >            }
793 >            return r == null ? null : r.val;
794 >        }
795 >
796 >        /**
797 >         * Finds or adds a node.
798 >         * @return null if added
799 >         */
800 >        @SuppressWarnings("unchecked") // suppress Comparable cast warning
801 >        final TreeNode putTreeNode(int h, Object k, Object v) {
802 >            Class<?> c = k.getClass();
803 >            TreeNode pp = root, p = null;
804 >            int dir = 0;
805 >            while (pp != null) { // find existing node or leaf to insert at
806 >                int ph;  Object pk; Class<?> pc;
807 >                p = pp;
808 >                if ((ph = p.hash) == h) {
809 >                    if ((pk = p.key) == k || k.equals(pk))
810 >                        return p;
811 >                    if (c != (pc = pk.getClass()) ||
812 >                        !(k instanceof Comparable) ||
813 >                        (dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) {
814 >                        dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName());
815 >                        TreeNode r = null, s = null, pl, pr;
816 >                        if (dir >= 0) {
817 >                            if ((pl = p.left) != null && h <= pl.hash)
818 >                                s = pl;
819 >                        }
820 >                        else if ((pr = p.right) != null && h >= pr.hash)
821 >                            s = pr;
822 >                        if (s != null && (r = getTreeNode(h, k, s)) != null)
823 >                            return r;
824 >                    }
825 >                }
826 >                else
827 >                    dir = (h < ph) ? -1 : 1;
828 >                pp = (dir > 0) ? p.right : p.left;
829 >            }
830 >
831 >            TreeNode f = first;
832 >            TreeNode x = first = new TreeNode(h, k, v, f, p);
833 >            if (p == null)
834 >                root = x;
835 >            else { // attach and rebalance; adapted from CLR
836 >                TreeNode xp, xpp;
837 >                if (f != null)
838 >                    f.prev = x;
839 >                if (dir <= 0)
840 >                    p.left = x;
841 >                else
842 >                    p.right = x;
843 >                x.red = true;
844 >                while (x != null && (xp = x.parent) != null && xp.red &&
845 >                       (xpp = xp.parent) != null) {
846 >                    TreeNode xppl = xpp.left;
847 >                    if (xp == xppl) {
848 >                        TreeNode y = xpp.right;
849 >                        if (y != null && y.red) {
850 >                            y.red = false;
851 >                            xp.red = false;
852 >                            xpp.red = true;
853 >                            x = xpp;
854 >                        }
855 >                        else {
856 >                            if (x == xp.right) {
857 >                                rotateLeft(x = xp);
858 >                                xpp = (xp = x.parent) == null ? null : xp.parent;
859 >                            }
860 >                            if (xp != null) {
861 >                                xp.red = false;
862 >                                if (xpp != null) {
863 >                                    xpp.red = true;
864 >                                    rotateRight(xpp);
865                                  }
866                              }
867 <                            if (runBit == 0)
868 <                                lo = lastRun;
869 <                            else
870 <                                hi = lastRun;
871 <                            for (Node p = e; p != lastRun; p = p.next) {
872 <                                int ph = p.hash;
873 <                                Object pk = p.key, pv = p.val;
874 <                                if ((ph & n) == 0)
875 <                                    lo = new Node(ph, pk, pv, lo);
876 <                                else
877 <                                    hi = new Node(ph, pk, pv, hi);
867 >                        }
868 >                    }
869 >                    else {
870 >                        TreeNode y = xppl;
871 >                        if (y != null && y.red) {
872 >                            y.red = false;
873 >                            xp.red = false;
874 >                            xpp.red = true;
875 >                            x = xpp;
876 >                        }
877 >                        else {
878 >                            if (x == xp.left) {
879 >                                rotateRight(x = xp);
880 >                                xpp = (xp = x.parent) == null ? null : xp.parent;
881 >                            }
882 >                            if (xp != null) {
883 >                                xp.red = false;
884 >                                if (xpp != null) {
885 >                                    xpp.red = true;
886 >                                    rotateLeft(xpp);
887 >                                }
888                              }
472                            setTabAt(nextTab, i, lo);
473                            setTabAt(nextTab, i + n, hi);
474                            setTabAt(tab, i, fwd);
889                          }
890                      }
891 <                    if (validated)
891 >                }
892 >                TreeNode r = root;
893 >                if (r != null && r.red)
894 >                    r.red = false;
895 >            }
896 >            return null;
897 >        }
898 >
899 >        /**
900 >         * Removes the given node, that must be present before this
901 >         * call.  This is messier than typical red-black deletion code
902 >         * because we cannot swap the contents of an interior node
903 >         * with a leaf successor that is pinned by "next" pointers
904 >         * that are accessible independently of lock. So instead we
905 >         * swap the tree linkages.
906 >         */
907 >        final void deleteTreeNode(TreeNode p) {
908 >            TreeNode next = (TreeNode)p.next; // unlink traversal pointers
909 >            TreeNode pred = p.prev;
910 >            if (pred == null)
911 >                first = next;
912 >            else
913 >                pred.next = next;
914 >            if (next != null)
915 >                next.prev = pred;
916 >            TreeNode replacement;
917 >            TreeNode pl = p.left;
918 >            TreeNode pr = p.right;
919 >            if (pl != null && pr != null) {
920 >                TreeNode s = pr, sl;
921 >                while ((sl = s.left) != null) // find successor
922 >                    s = sl;
923 >                boolean c = s.red; s.red = p.red; p.red = c; // swap colors
924 >                TreeNode sr = s.right;
925 >                TreeNode pp = p.parent;
926 >                if (s == pr) { // p was s's direct parent
927 >                    p.parent = s;
928 >                    s.right = p;
929 >                }
930 >                else {
931 >                    TreeNode sp = s.parent;
932 >                    if ((p.parent = sp) != null) {
933 >                        if (s == sp.left)
934 >                            sp.left = p;
935 >                        else
936 >                            sp.right = p;
937 >                    }
938 >                    if ((s.right = pr) != null)
939 >                        pr.parent = s;
940 >                }
941 >                p.left = null;
942 >                if ((p.right = sr) != null)
943 >                    sr.parent = p;
944 >                if ((s.left = pl) != null)
945 >                    pl.parent = s;
946 >                if ((s.parent = pp) == null)
947 >                    root = s;
948 >                else if (p == pp.left)
949 >                    pp.left = s;
950 >                else
951 >                    pp.right = s;
952 >                replacement = sr;
953 >            }
954 >            else
955 >                replacement = (pl != null) ? pl : pr;
956 >            TreeNode pp = p.parent;
957 >            if (replacement == null) {
958 >                if (pp == null) {
959 >                    root = null;
960 >                    return;
961 >                }
962 >                replacement = p;
963 >            }
964 >            else {
965 >                replacement.parent = pp;
966 >                if (pp == null)
967 >                    root = replacement;
968 >                else if (p == pp.left)
969 >                    pp.left = replacement;
970 >                else
971 >                    pp.right = replacement;
972 >                p.left = p.right = p.parent = null;
973 >            }
974 >            if (!p.red) { // rebalance, from CLR
975 >                TreeNode x = replacement;
976 >                while (x != null) {
977 >                    TreeNode xp, xpl;
978 >                    if (x.red || (xp = x.parent) == null) {
979 >                        x.red = false;
980                          break;
981 +                    }
982 +                    if (x == (xpl = xp.left)) {
983 +                        TreeNode sib = xp.right;
984 +                        if (sib != null && sib.red) {
985 +                            sib.red = false;
986 +                            xp.red = true;
987 +                            rotateLeft(xp);
988 +                            sib = (xp = x.parent) == null ? null : xp.right;
989 +                        }
990 +                        if (sib == null)
991 +                            x = xp;
992 +                        else {
993 +                            TreeNode sl = sib.left, sr = sib.right;
994 +                            if ((sr == null || !sr.red) &&
995 +                                (sl == null || !sl.red)) {
996 +                                sib.red = true;
997 +                                x = xp;
998 +                            }
999 +                            else {
1000 +                                if (sr == null || !sr.red) {
1001 +                                    if (sl != null)
1002 +                                        sl.red = false;
1003 +                                    sib.red = true;
1004 +                                    rotateRight(sib);
1005 +                                    sib = (xp = x.parent) == null ? null : xp.right;
1006 +                                }
1007 +                                if (sib != null) {
1008 +                                    sib.red = (xp == null) ? false : xp.red;
1009 +                                    if ((sr = sib.right) != null)
1010 +                                        sr.red = false;
1011 +                                }
1012 +                                if (xp != null) {
1013 +                                    xp.red = false;
1014 +                                    rotateLeft(xp);
1015 +                                }
1016 +                                x = root;
1017 +                            }
1018 +                        }
1019 +                    }
1020 +                    else { // symmetric
1021 +                        TreeNode sib = xpl;
1022 +                        if (sib != null && sib.red) {
1023 +                            sib.red = false;
1024 +                            xp.red = true;
1025 +                            rotateRight(xp);
1026 +                            sib = (xp = x.parent) == null ? null : xp.left;
1027 +                        }
1028 +                        if (sib == null)
1029 +                            x = xp;
1030 +                        else {
1031 +                            TreeNode sl = sib.left, sr = sib.right;
1032 +                            if ((sl == null || !sl.red) &&
1033 +                                (sr == null || !sr.red)) {
1034 +                                sib.red = true;
1035 +                                x = xp;
1036 +                            }
1037 +                            else {
1038 +                                if (sl == null || !sl.red) {
1039 +                                    if (sr != null)
1040 +                                        sr.red = false;
1041 +                                    sib.red = true;
1042 +                                    rotateLeft(sib);
1043 +                                    sib = (xp = x.parent) == null ? null : xp.left;
1044 +                                }
1045 +                                if (sib != null) {
1046 +                                    sib.red = (xp == null) ? false : xp.red;
1047 +                                    if ((sl = sib.left) != null)
1048 +                                        sl.red = false;
1049 +                                }
1050 +                                if (xp != null) {
1051 +                                    xp.red = false;
1052 +                                    rotateRight(xp);
1053 +                                }
1054 +                                x = root;
1055 +                            }
1056 +                        }
1057 +                    }
1058                  }
1059 <                else if (casTabAt(tab, i, e, fwd))
1060 <                    break;
1059 >            }
1060 >            if (p == replacement && (pp = p.parent) != null) {
1061 >                if (p == pp.left) // detach pointers
1062 >                    pp.left = null;
1063 >                else if (p == pp.right)
1064 >                    pp.right = null;
1065 >                p.parent = null;
1066              }
1067          }
1068      }
1069  
1070 <    /* ---------------- Internal access and update methods -------------- */
1070 >    /* ---------------- Collision reduction methods -------------- */
1071  
1072      /**
1073 <     * Applies a supplemental hash function to a given hashCode, which
1074 <     * defends against poor quality hash functions.  The result must
1075 <     * be non-negative, and for reasonable performance must have good
1076 <     * avalanche properties; i.e., that each bit of the argument
1077 <     * affects each bit (except sign bit) of the result.
1073 >     * Spreads higher bits to lower, and also forces top 2 bits to 0.
1074 >     * Because the table uses power-of-two masking, sets of hashes
1075 >     * that vary only in bits above the current mask will always
1076 >     * collide. (Among known examples are sets of Float keys holding
1077 >     * consecutive whole numbers in small tables.)  To counter this,
1078 >     * we apply a transform that spreads the impact of higher bits
1079 >     * downward. There is a tradeoff between speed, utility, and
1080 >     * quality of bit-spreading. Because many common sets of hashes
1081 >     * are already reasonably distributed across bits (so don't benefit
1082 >     * from spreading), and because we use trees to handle large sets
1083 >     * of collisions in bins, we don't need excessively high quality.
1084       */
1085      private static final int spread(int h) {
1086 <        // Apply base step of MurmurHash; see http://code.google.com/p/smhasher/
1087 <        h ^= h >>> 16;
1088 <        h *= 0x85ebca6b;
1089 <        h ^= h >>> 13;
1090 <        h *= 0xc2b2ae35;
1091 <        return (h >>> 16) ^ (h & 0x7fffffff); // mask out sign bit
1086 >        h ^= (h >>> 18) ^ (h >>> 12);
1087 >        return (h ^ (h >>> 10)) & HASH_BITS;
1088 >    }
1089 >
1090 >    /**
1091 >     * Replaces a list bin with a tree bin. Call only when locked.
1092 >     * Fails to replace if the given key is non-comparable or table
1093 >     * is, or needs, resizing.
1094 >     */
1095 >    private final void replaceWithTreeBin(Node[] tab, int index, Object key) {
1096 >        if ((key instanceof Comparable) &&
1097 >            (tab.length >= MAXIMUM_CAPACITY || counter.sum() < (long)sizeCtl)) {
1098 >            TreeBin t = new TreeBin();
1099 >            for (Node e = tabAt(tab, index); e != null; e = e.next)
1100 >                t.putTreeNode(e.hash & HASH_BITS, e.key, e.val);
1101 >            setTabAt(tab, index, new Node(MOVED, t, null, null));
1102 >        }
1103      }
1104  
1105 +    /* ---------------- Internal access and update methods -------------- */
1106 +
1107      /** Implementation for get and containsKey */
1108      private final Object internalGet(Object k) {
1109          int h = spread(k.hashCode());
1110          retry: for (Node[] tab = table; tab != null;) {
1111 <            Node e; Object ek, ev; int eh;  // locals to read fields once
1111 >            Node e, p; Object ek, ev; int eh;      // locals to read fields once
1112              for (e = tabAt(tab, (tab.length - 1) & h); e != null; e = e.next) {
1113 <                if ((eh = e.hash) == h) {
1114 <                    if ((ev = e.val) != null &&
1115 <                        ((ek = e.key) == k || k.equals(ek)))
1116 <                        return ev;
1117 <                }
1118 <                else if (eh < 0) {          // sign bit set
1119 <                    tab = (Node[])e.key;    // bin was moved during resize
517 <                    continue retry;
1113 >                if ((eh = e.hash) == MOVED) {
1114 >                    if ((ek = e.key) instanceof TreeBin)  // search TreeBin
1115 >                        return ((TreeBin)ek).getValue(h, k);
1116 >                    else {                        // restart with new table
1117 >                        tab = (Node[])ek;
1118 >                        continue retry;
1119 >                    }
1120                  }
1121 +                else if ((eh & HASH_BITS) == h && (ev = e.val) != null &&
1122 +                         ((ek = e.key) == k || k.equals(ek)))
1123 +                    return ev;
1124              }
1125              break;
1126          }
1127          return null;
1128      }
1129  
1130 <    /** Implementation for put and putIfAbsent */
1131 <    private final Object internalPut(Object k, Object v, boolean replace) {
1130 >    /**
1131 >     * Implementation for the four public remove/replace methods:
1132 >     * Replaces node value with v, conditional upon match of cv if
1133 >     * non-null.  If resulting value is null, delete.
1134 >     */
1135 >    private final Object internalReplace(Object k, Object v, Object cv) {
1136 >        int h = spread(k.hashCode());
1137 >        Object oldVal = null;
1138 >        for (Node[] tab = table;;) {
1139 >            Node f; int i, fh; Object fk;
1140 >            if (tab == null ||
1141 >                (f = tabAt(tab, i = (tab.length - 1) & h)) == null)
1142 >                break;
1143 >            else if ((fh = f.hash) == MOVED) {
1144 >                if ((fk = f.key) instanceof TreeBin) {
1145 >                    TreeBin t = (TreeBin)fk;
1146 >                    boolean validated = false;
1147 >                    boolean deleted = false;
1148 >                    t.acquire(0);
1149 >                    try {
1150 >                        if (tabAt(tab, i) == f) {
1151 >                            validated = true;
1152 >                            TreeNode p = t.getTreeNode(h, k, t.root);
1153 >                            if (p != null) {
1154 >                                Object pv = p.val;
1155 >                                if (cv == null || cv == pv || cv.equals(pv)) {
1156 >                                    oldVal = pv;
1157 >                                    if ((p.val = v) == null) {
1158 >                                        deleted = true;
1159 >                                        t.deleteTreeNode(p);
1160 >                                    }
1161 >                                }
1162 >                            }
1163 >                        }
1164 >                    } finally {
1165 >                        t.release(0);
1166 >                    }
1167 >                    if (validated) {
1168 >                        if (deleted)
1169 >                            counter.add(-1L);
1170 >                        break;
1171 >                    }
1172 >                }
1173 >                else
1174 >                    tab = (Node[])fk;
1175 >            }
1176 >            else if ((fh & HASH_BITS) != h && f.next == null) // precheck
1177 >                break;                          // rules out possible existence
1178 >            else if ((fh & LOCKED) != 0) {
1179 >                checkForResize();               // try resizing if can't get lock
1180 >                f.tryAwaitLock(tab, i);
1181 >            }
1182 >            else if (f.casHash(fh, fh | LOCKED)) {
1183 >                boolean validated = false;
1184 >                boolean deleted = false;
1185 >                try {
1186 >                    if (tabAt(tab, i) == f) {
1187 >                        validated = true;
1188 >                        for (Node e = f, pred = null;;) {
1189 >                            Object ek, ev;
1190 >                            if ((e.hash & HASH_BITS) == h &&
1191 >                                ((ev = e.val) != null) &&
1192 >                                ((ek = e.key) == k || k.equals(ek))) {
1193 >                                if (cv == null || cv == ev || cv.equals(ev)) {
1194 >                                    oldVal = ev;
1195 >                                    if ((e.val = v) == null) {
1196 >                                        deleted = true;
1197 >                                        Node en = e.next;
1198 >                                        if (pred != null)
1199 >                                            pred.next = en;
1200 >                                        else
1201 >                                            setTabAt(tab, i, en);
1202 >                                    }
1203 >                                }
1204 >                                break;
1205 >                            }
1206 >                            pred = e;
1207 >                            if ((e = e.next) == null)
1208 >                                break;
1209 >                        }
1210 >                    }
1211 >                } finally {
1212 >                    if (!f.casHash(fh | LOCKED, fh)) {
1213 >                        f.hash = fh;
1214 >                        synchronized (f) { f.notifyAll(); };
1215 >                    }
1216 >                }
1217 >                if (validated) {
1218 >                    if (deleted)
1219 >                        counter.add(-1L);
1220 >                    break;
1221 >                }
1222 >            }
1223 >        }
1224 >        return oldVal;
1225 >    }
1226 >
1227 >    /*
1228 >     * Internal versions of the five insertion methods, each a
1229 >     * little more complicated than the last. All have
1230 >     * the same basic structure as the first (internalPut):
1231 >     *  1. If table uninitialized, create
1232 >     *  2. If bin empty, try to CAS new node
1233 >     *  3. If bin stale, use new table
1234 >     *  4. if bin converted to TreeBin, validate and relay to TreeBin methods
1235 >     *  5. Lock and validate; if valid, scan and add or update
1236 >     *
1237 >     * The others interweave other checks and/or alternative actions:
1238 >     *  * Plain put checks for and performs resize after insertion.
1239 >     *  * putIfAbsent prescans for mapping without lock (and fails to add
1240 >     *    if present), which also makes pre-emptive resize checks worthwhile.
1241 >     *  * computeIfAbsent extends form used in putIfAbsent with additional
1242 >     *    mechanics to deal with, calls, potential exceptions and null
1243 >     *    returns from function call.
1244 >     *  * compute uses the same function-call mechanics, but without
1245 >     *    the prescans
1246 >     *  * putAll attempts to pre-allocate enough table space
1247 >     *    and more lazily performs count updates and checks.
1248 >     *
1249 >     * Someday when details settle down a bit more, it might be worth
1250 >     * some factoring to reduce sprawl.
1251 >     */
1252 >
1253 >    /** Implementation for put */
1254 >    private final Object internalPut(Object k, Object v) {
1255          int h = spread(k.hashCode());
1256 <        Object oldVal = null;               // previous value or null if none
1256 >        int count = 0;
1257          for (Node[] tab = table;;) {
1258 <            Node e; int i; Object ek, ev;
1258 >            int i; Node f; int fh; Object fk;
1259              if (tab == null)
1260 <                tab = growTable();
1261 <            else if ((e = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1260 >                tab = initTable();
1261 >            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1262                  if (casTabAt(tab, i, null, new Node(h, k, v, null)))
1263                      break;                   // no lock when adding to empty bin
1264              }
1265 <            else if (e.hash < 0)             // resized -- restart with new table
1266 <                tab = (Node[])e.key;
1267 <            else if (!replace && e.hash == h && (ev = e.val) != null &&
1268 <                     ((ek = e.key) == k || k.equals(ek))) {
1269 <                if (tabAt(tab, i) == e) {    // inspect and validate 1st node
1270 <                    oldVal = ev;             // without lock for putIfAbsent
1271 <                    break;
1265 >            else if ((fh = f.hash) == MOVED) {
1266 >                if ((fk = f.key) instanceof TreeBin) {
1267 >                    TreeBin t = (TreeBin)fk;
1268 >                    Object oldVal = null;
1269 >                    t.acquire(0);
1270 >                    try {
1271 >                        if (tabAt(tab, i) == f) {
1272 >                            count = 2;
1273 >                            TreeNode p = t.putTreeNode(h, k, v);
1274 >                            if (p != null) {
1275 >                                oldVal = p.val;
1276 >                                p.val = v;
1277 >                            }
1278 >                        }
1279 >                    } finally {
1280 >                        t.release(0);
1281 >                    }
1282 >                    if (count != 0) {
1283 >                        if (oldVal != null)
1284 >                            return oldVal;
1285 >                        break;
1286 >                    }
1287                  }
1288 +                else
1289 +                    tab = (Node[])fk;
1290              }
1291 <            else {
1292 <                boolean validated = false;
1293 <                boolean checkSize = false;
1294 <                synchronized (e) {           // lock the 1st node of bin list
1295 <                    if (tabAt(tab, i) == e) {
1296 <                        validated = true;    // retry if 1st already deleted
1297 <                        for (Node first = e;;) {
1298 <                            if (e.hash == h &&
1299 <                                ((ek = e.key) == k || k.equals(ek)) &&
1300 <                                (ev = e.val) != null) {
1291 >            else if ((fh & LOCKED) != 0) {
1292 >                checkForResize();
1293 >                f.tryAwaitLock(tab, i);
1294 >            }
1295 >            else if (f.casHash(fh, fh | LOCKED)) {
1296 >                Object oldVal = null;
1297 >                try {                        // needed in case equals() throws
1298 >                    if (tabAt(tab, i) == f) {
1299 >                        count = 1;
1300 >                        for (Node e = f;; ++count) {
1301 >                            Object ek, ev;
1302 >                            if ((e.hash & HASH_BITS) == h &&
1303 >                                (ev = e.val) != null &&
1304 >                                ((ek = e.key) == k || k.equals(ek))) {
1305                                  oldVal = ev;
1306 <                                if (replace)
558 <                                    e.val = v;
1306 >                                e.val = v;
1307                                  break;
1308                              }
1309                              Node last = e;
1310                              if ((e = e.next) == null) {
1311                                  last.next = new Node(h, k, v, null);
1312 <                                if (last != first || tab.length <= 64)
1313 <                                    checkSize = true;
1312 >                                if (count >= TREE_THRESHOLD)
1313 >                                    replaceWithTreeBin(tab, i, k);
1314                                  break;
1315                              }
1316                          }
1317                      }
1318 +                } finally {                  // unlock and signal if needed
1319 +                    if (!f.casHash(fh | LOCKED, fh)) {
1320 +                        f.hash = fh;
1321 +                        synchronized (f) { f.notifyAll(); };
1322 +                    }
1323                  }
1324 <                if (validated) {
1325 <                    if (checkSize && tab.length < MAXIMUM_CAPACITY &&
1326 <                        resizing == 0 && counter.sum() >= (long)threshold)
1327 <                        growTable();
1324 >                if (count != 0) {
1325 >                    if (oldVal != null)
1326 >                        return oldVal;
1327 >                    if (tab.length <= 64)
1328 >                        count = 2;
1329                      break;
1330                  }
1331              }
1332          }
1333 <        if (oldVal == null)
1334 <            counter.increment();             // update counter outside of locks
1335 <        return oldVal;
1333 >        counter.add(1L);
1334 >        if (count > 1)
1335 >            checkForResize();
1336 >        return null;
1337      }
1338  
1339 <    /**
1340 <     * Implementation for the four public remove/replace methods:
586 <     * Replaces node value with v, conditional upon match of cv if
587 <     * non-null.  If resulting value is null, delete.
588 <     */
589 <    private final Object internalReplace(Object k, Object v, Object cv) {
1339 >    /** Implementation for putIfAbsent */
1340 >    private final Object internalPutIfAbsent(Object k, Object v) {
1341          int h = spread(k.hashCode());
1342 +        int count = 0;
1343          for (Node[] tab = table;;) {
1344 <            Node e; int i;
1345 <            if (tab == null ||
1346 <                (e = tabAt(tab, i = (tab.length - 1) & h)) == null)
1347 <                return null;
1348 <            else if (e.hash < 0)
1349 <                tab = (Node[])e.key;
1344 >            int i; Node f; int fh; Object fk, fv;
1345 >            if (tab == null)
1346 >                tab = initTable();
1347 >            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1348 >                if (casTabAt(tab, i, null, new Node(h, k, v, null)))
1349 >                    break;
1350 >            }
1351 >            else if ((fh = f.hash) == MOVED) {
1352 >                if ((fk = f.key) instanceof TreeBin) {
1353 >                    TreeBin t = (TreeBin)fk;
1354 >                    Object oldVal = null;
1355 >                    t.acquire(0);
1356 >                    try {
1357 >                        if (tabAt(tab, i) == f) {
1358 >                            count = 2;
1359 >                            TreeNode p = t.putTreeNode(h, k, v);
1360 >                            if (p != null)
1361 >                                oldVal = p.val;
1362 >                        }
1363 >                    } finally {
1364 >                        t.release(0);
1365 >                    }
1366 >                    if (count != 0) {
1367 >                        if (oldVal != null)
1368 >                            return oldVal;
1369 >                        break;
1370 >                    }
1371 >                }
1372 >                else
1373 >                    tab = (Node[])fk;
1374 >            }
1375 >            else if ((fh & HASH_BITS) == h && (fv = f.val) != null &&
1376 >                     ((fk = f.key) == k || k.equals(fk)))
1377 >                return fv;
1378              else {
1379 <                Object oldVal = null;
1380 <                boolean validated = false;
1381 <                boolean deleted = false;
1382 <                synchronized (e) {
1383 <                    if (tabAt(tab, i) == e) {
1384 <                        validated = true;
1385 <                        Node pred = null;
1386 <                        do {
1387 <                            Object ek, ev;
1388 <                            if (e.hash == h &&
1389 <                                ((ek = e.key) == k || k.equals(ek)) &&
1390 <                                ((ev = e.val) != null)) {
1391 <                                if (cv == null || cv == ev || cv.equals(ev)) {
1379 >                Node g = f.next;
1380 >                if (g != null) { // at least 2 nodes -- search and maybe resize
1381 >                    for (Node e = g;;) {
1382 >                        Object ek, ev;
1383 >                        if ((e.hash & HASH_BITS) == h && (ev = e.val) != null &&
1384 >                            ((ek = e.key) == k || k.equals(ek)))
1385 >                            return ev;
1386 >                        if ((e = e.next) == null) {
1387 >                            checkForResize();
1388 >                            break;
1389 >                        }
1390 >                    }
1391 >                }
1392 >                if (((fh = f.hash) & LOCKED) != 0) {
1393 >                    checkForResize();
1394 >                    f.tryAwaitLock(tab, i);
1395 >                }
1396 >                else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) {
1397 >                    Object oldVal = null;
1398 >                    try {
1399 >                        if (tabAt(tab, i) == f) {
1400 >                            count = 1;
1401 >                            for (Node e = f;; ++count) {
1402 >                                Object ek, ev;
1403 >                                if ((e.hash & HASH_BITS) == h &&
1404 >                                    (ev = e.val) != null &&
1405 >                                    ((ek = e.key) == k || k.equals(ek))) {
1406                                      oldVal = ev;
1407 <                                    if ((e.val = v) == null) {
1408 <                                        deleted = true;
1409 <                                        Node en = e.next;
1410 <                                        if (pred != null)
1411 <                                            pred.next = en;
1412 <                                        else
1413 <                                            setTabAt(tab, i, en);
1414 <                                    }
1407 >                                    break;
1408 >                                }
1409 >                                Node last = e;
1410 >                                if ((e = e.next) == null) {
1411 >                                    last.next = new Node(h, k, v, null);
1412 >                                    if (count >= TREE_THRESHOLD)
1413 >                                        replaceWithTreeBin(tab, i, k);
1414 >                                    break;
1415                                  }
622                                break;
1416                              }
1417 <                        } while ((e = (pred = e).next) != null);
1417 >                        }
1418 >                    } finally {
1419 >                        if (!f.casHash(fh | LOCKED, fh)) {
1420 >                            f.hash = fh;
1421 >                            synchronized (f) { f.notifyAll(); };
1422 >                        }
1423 >                    }
1424 >                    if (count != 0) {
1425 >                        if (oldVal != null)
1426 >                            return oldVal;
1427 >                        if (tab.length <= 64)
1428 >                            count = 2;
1429 >                        break;
1430                      }
626                }
627                if (validated) {
628                    if (deleted)
629                        counter.decrement();
630                    return oldVal;
1431                  }
1432              }
1433          }
1434 +        counter.add(1L);
1435 +        if (count > 1)
1436 +            checkForResize();
1437 +        return null;
1438      }
1439  
1440 <    /** Implementation for computeIfAbsent and compute. Like put, but messier. */
1441 <    @SuppressWarnings("unchecked")
1442 <    private final V internalCompute(K k,
639 <                                    MappingFunction<? super K, ? extends V> f,
640 <                                    boolean replace) {
1440 >    /** Implementation for computeIfAbsent */
1441 >    private final Object internalComputeIfAbsent(K k,
1442 >                                                 MappingFunction<? super K, ?> mf) {
1443          int h = spread(k.hashCode());
1444 <        V val = null;
1445 <        boolean added = false;
1446 <        Node[] tab = table;
1447 <        outer:for (;;) {
646 <            Node e; int i; Object ek, ev;
1444 >        Object val = null;
1445 >        int count = 0;
1446 >        for (Node[] tab = table;;) {
1447 >            Node f; int i, fh; Object fk, fv;
1448              if (tab == null)
1449 <                tab = growTable();
1450 <            else if ((e = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1451 <                Node node = new Node(h, k, null, null);
1452 <                boolean validated = false;
1453 <                synchronized (node) {  // must lock while computing value
1454 <                    if (casTabAt(tab, i, null, node)) {
1455 <                        validated = true;
1456 <                        try {
1457 <                            val = f.map(k);
1458 <                            if (val != null) {
1459 <                                node.val = val;
1449 >                tab = initTable();
1450 >            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1451 >                Node node = new Node(fh = h | LOCKED, k, null, null);
1452 >                if (casTabAt(tab, i, null, node)) {
1453 >                    count = 1;
1454 >                    try {
1455 >                        if ((val = mf.map(k)) != null)
1456 >                            node.val = val;
1457 >                    } finally {
1458 >                        if (val == null)
1459 >                            setTabAt(tab, i, null);
1460 >                        if (!node.casHash(fh, h)) {
1461 >                            node.hash = h;
1462 >                            synchronized (node) { node.notifyAll(); };
1463 >                        }
1464 >                    }
1465 >                }
1466 >                if (count != 0)
1467 >                    break;
1468 >            }
1469 >            else if ((fh = f.hash) == MOVED) {
1470 >                if ((fk = f.key) instanceof TreeBin) {
1471 >                    TreeBin t = (TreeBin)fk;
1472 >                    boolean added = false;
1473 >                    t.acquire(0);
1474 >                    try {
1475 >                        if (tabAt(tab, i) == f) {
1476 >                            count = 1;
1477 >                            TreeNode p = t.getTreeNode(h, k, t.root);
1478 >                            if (p != null)
1479 >                                val = p.val;
1480 >                            else if ((val = mf.map(k)) != null) {
1481                                  added = true;
1482 +                                count = 2;
1483 +                                t.putTreeNode(h, k, val);
1484                              }
661                        } finally {
662                            if (!added)
663                                setTabAt(tab, i, null);
1485                          }
1486 +                    } finally {
1487 +                        t.release(0);
1488 +                    }
1489 +                    if (count != 0) {
1490 +                        if (!added)
1491 +                            return val;
1492 +                        break;
1493                      }
1494                  }
1495 <                if (validated)
1496 <                    break;
1495 >                else
1496 >                    tab = (Node[])fk;
1497              }
1498 <            else if (e.hash < 0)
1499 <                tab = (Node[])e.key;
1500 <            else if (!replace && e.hash == h && (ev = e.val) != null &&
1501 <                     ((ek = e.key) == k || k.equals(ek))) {
1502 <                if (tabAt(tab, i) == e) {
1503 <                    val = (V)ev;
1498 >            else if ((fh & HASH_BITS) == h && (fv = f.val) != null &&
1499 >                     ((fk = f.key) == k || k.equals(fk)))
1500 >                return fv;
1501 >            else {
1502 >                Node g = f.next;
1503 >                if (g != null) {
1504 >                    for (Node e = g;;) {
1505 >                        Object ek, ev;
1506 >                        if ((e.hash & HASH_BITS) == h && (ev = e.val) != null &&
1507 >                            ((ek = e.key) == k || k.equals(ek)))
1508 >                            return ev;
1509 >                        if ((e = e.next) == null) {
1510 >                            checkForResize();
1511 >                            break;
1512 >                        }
1513 >                    }
1514 >                }
1515 >                if (((fh = f.hash) & LOCKED) != 0) {
1516 >                    checkForResize();
1517 >                    f.tryAwaitLock(tab, i);
1518 >                }
1519 >                else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) {
1520 >                    boolean added = false;
1521 >                    try {
1522 >                        if (tabAt(tab, i) == f) {
1523 >                            count = 1;
1524 >                            for (Node e = f;; ++count) {
1525 >                                Object ek, ev;
1526 >                                if ((e.hash & HASH_BITS) == h &&
1527 >                                    (ev = e.val) != null &&
1528 >                                    ((ek = e.key) == k || k.equals(ek))) {
1529 >                                    val = ev;
1530 >                                    break;
1531 >                                }
1532 >                                Node last = e;
1533 >                                if ((e = e.next) == null) {
1534 >                                    if ((val = mf.map(k)) != null) {
1535 >                                        added = true;
1536 >                                        last.next = new Node(h, k, val, null);
1537 >                                        if (count >= TREE_THRESHOLD)
1538 >                                            replaceWithTreeBin(tab, i, k);
1539 >                                    }
1540 >                                    break;
1541 >                                }
1542 >                            }
1543 >                        }
1544 >                    } finally {
1545 >                        if (!f.casHash(fh | LOCKED, fh)) {
1546 >                            f.hash = fh;
1547 >                            synchronized (f) { f.notifyAll(); };
1548 >                        }
1549 >                    }
1550 >                    if (count != 0) {
1551 >                        if (!added)
1552 >                            return val;
1553 >                        if (tab.length <= 64)
1554 >                            count = 2;
1555 >                        break;
1556 >                    }
1557 >                }
1558 >            }
1559 >        }
1560 >        if (val != null) {
1561 >            counter.add(1L);
1562 >            if (count > 1)
1563 >                checkForResize();
1564 >        }
1565 >        return val;
1566 >    }
1567 >
1568 >    /** Implementation for compute */
1569 >    @SuppressWarnings("unchecked")
1570 >    private final Object internalCompute(K k,
1571 >                                         RemappingFunction<? super K, V> mf) {
1572 >        int h = spread(k.hashCode());
1573 >        Object val = null;
1574 >        int delta = 0;
1575 >        int count = 0;
1576 >        for (Node[] tab = table;;) {
1577 >            Node f; int i, fh; Object fk;
1578 >            if (tab == null)
1579 >                tab = initTable();
1580 >            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1581 >                Node node = new Node(fh = h | LOCKED, k, null, null);
1582 >                if (casTabAt(tab, i, null, node)) {
1583 >                    try {
1584 >                        count = 1;
1585 >                        if ((val = mf.remap(k, null)) != null) {
1586 >                            node.val = val;
1587 >                            delta = 1;
1588 >                        }
1589 >                    } finally {
1590 >                        if (delta == 0)
1591 >                            setTabAt(tab, i, null);
1592 >                        if (!node.casHash(fh, h)) {
1593 >                            node.hash = h;
1594 >                            synchronized (node) { node.notifyAll(); };
1595 >                        }
1596 >                    }
1597 >                }
1598 >                if (count != 0)
1599                      break;
1600 +            }
1601 +            else if ((fh = f.hash) == MOVED) {
1602 +                if ((fk = f.key) instanceof TreeBin) {
1603 +                    TreeBin t = (TreeBin)fk;
1604 +                    t.acquire(0);
1605 +                    try {
1606 +                        if (tabAt(tab, i) == f) {
1607 +                            count = 1;
1608 +                            TreeNode p = t.getTreeNode(h, k, t.root);
1609 +                            Object pv = (p == null) ? null : p.val;
1610 +                            if ((val = mf.remap(k, (V)pv)) != null) {
1611 +                                if (p != null)
1612 +                                    p.val = val;
1613 +                                else {
1614 +                                    count = 2;
1615 +                                    delta = 1;
1616 +                                    t.putTreeNode(h, k, val);
1617 +                                }
1618 +                            }
1619 +                            else if (p != null) {
1620 +                                delta = -1;
1621 +                                t.deleteTreeNode(p);
1622 +                            }
1623 +                        }
1624 +                    } finally {
1625 +                        t.release(0);
1626 +                    }
1627 +                    if (count != 0)
1628 +                        break;
1629                  }
1630 +                else
1631 +                    tab = (Node[])fk;
1632              }
1633 <            else if (Thread.holdsLock(e))
1634 <                throw new IllegalStateException("Recursive map computation");
1635 <            else {
1636 <                boolean validated = false;
1637 <                boolean checkSize = false;
1638 <                synchronized (e) {
1639 <                    if (tabAt(tab, i) == e) {
1640 <                        validated = true;
1641 <                        for (Node first = e;;) {
1642 <                            if (e.hash == h &&
1643 <                                ((ek = e.key) == k || k.equals(ek)) &&
1644 <                                ((ev = e.val) != null)) {
1645 <                                Object fv;
1646 <                                if (replace && (fv = f.map(k)) != null)
1647 <                                    ev = e.val = fv;
1648 <                                val = (V)ev;
1633 >            else if ((fh & LOCKED) != 0) {
1634 >                checkForResize();
1635 >                f.tryAwaitLock(tab, i);
1636 >            }
1637 >            else if (f.casHash(fh, fh | LOCKED)) {
1638 >                try {
1639 >                    if (tabAt(tab, i) == f) {
1640 >                        count = 1;
1641 >                        for (Node e = f, pred = null;; ++count) {
1642 >                            Object ek, ev;
1643 >                            if ((e.hash & HASH_BITS) == h &&
1644 >                                (ev = e.val) != null &&
1645 >                                ((ek = e.key) == k || k.equals(ek))) {
1646 >                                val = mf.remap(k, (V)ev);
1647 >                                if (val != null)
1648 >                                    e.val = val;
1649 >                                else {
1650 >                                    delta = -1;
1651 >                                    Node en = e.next;
1652 >                                    if (pred != null)
1653 >                                        pred.next = en;
1654 >                                    else
1655 >                                        setTabAt(tab, i, en);
1656 >                                }
1657                                  break;
1658                              }
1659 <                            Node last = e;
1659 >                            pred = e;
1660                              if ((e = e.next) == null) {
1661 <                                if ((val = f.map(k)) != null) {
1662 <                                    last.next = new Node(h, k, val, null);
1663 <                                    added = true;
1664 <                                    if (last != first || tab.length <= 64)
1665 <                                        checkSize = true;
1661 >                                if ((val = mf.remap(k, null)) != null) {
1662 >                                    pred.next = new Node(h, k, val, null);
1663 >                                    delta = 1;
1664 >                                    if (count >= TREE_THRESHOLD)
1665 >                                        replaceWithTreeBin(tab, i, k);
1666                                  }
1667                                  break;
1668                              }
1669                          }
1670                      }
1671 +                } finally {
1672 +                    if (!f.casHash(fh | LOCKED, fh)) {
1673 +                        f.hash = fh;
1674 +                        synchronized (f) { f.notifyAll(); };
1675 +                    }
1676                  }
1677 <                if (validated) {
1678 <                    if (checkSize && tab.length < MAXIMUM_CAPACITY &&
1679 <                        resizing == 0 && counter.sum() >= (long)threshold)
713 <                        growTable();
1677 >                if (count != 0) {
1678 >                    if (tab.length <= 64)
1679 >                        count = 2;
1680                      break;
1681                  }
1682              }
1683          }
1684 <        if (added)
1685 <            counter.increment();
1684 >        if (delta != 0) {
1685 >            counter.add((long)delta);
1686 >            if (count > 1)
1687 >                checkForResize();
1688 >        }
1689          return val;
1690      }
1691  
1692 +    /** Implementation for putAll */
1693 +    private final void internalPutAll(Map<?, ?> m) {
1694 +        tryPresize(m.size());
1695 +        long delta = 0L;     // number of uncommitted additions
1696 +        boolean npe = false; // to throw exception on exit for nulls
1697 +        try {                // to clean up counts on other exceptions
1698 +            for (Map.Entry<?, ?> entry : m.entrySet()) {
1699 +                Object k, v;
1700 +                if (entry == null || (k = entry.getKey()) == null ||
1701 +                    (v = entry.getValue()) == null) {
1702 +                    npe = true;
1703 +                    break;
1704 +                }
1705 +                int h = spread(k.hashCode());
1706 +                for (Node[] tab = table;;) {
1707 +                    int i; Node f; int fh; Object fk;
1708 +                    if (tab == null)
1709 +                        tab = initTable();
1710 +                    else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null){
1711 +                        if (casTabAt(tab, i, null, new Node(h, k, v, null))) {
1712 +                            ++delta;
1713 +                            break;
1714 +                        }
1715 +                    }
1716 +                    else if ((fh = f.hash) == MOVED) {
1717 +                        if ((fk = f.key) instanceof TreeBin) {
1718 +                            TreeBin t = (TreeBin)fk;
1719 +                            boolean validated = false;
1720 +                            t.acquire(0);
1721 +                            try {
1722 +                                if (tabAt(tab, i) == f) {
1723 +                                    validated = true;
1724 +                                    TreeNode p = t.getTreeNode(h, k, t.root);
1725 +                                    if (p != null)
1726 +                                        p.val = v;
1727 +                                    else {
1728 +                                        t.putTreeNode(h, k, v);
1729 +                                        ++delta;
1730 +                                    }
1731 +                                }
1732 +                            } finally {
1733 +                                t.release(0);
1734 +                            }
1735 +                            if (validated)
1736 +                                break;
1737 +                        }
1738 +                        else
1739 +                            tab = (Node[])fk;
1740 +                    }
1741 +                    else if ((fh & LOCKED) != 0) {
1742 +                        counter.add(delta);
1743 +                        delta = 0L;
1744 +                        checkForResize();
1745 +                        f.tryAwaitLock(tab, i);
1746 +                    }
1747 +                    else if (f.casHash(fh, fh | LOCKED)) {
1748 +                        int count = 0;
1749 +                        try {
1750 +                            if (tabAt(tab, i) == f) {
1751 +                                count = 1;
1752 +                                for (Node e = f;; ++count) {
1753 +                                    Object ek, ev;
1754 +                                    if ((e.hash & HASH_BITS) == h &&
1755 +                                        (ev = e.val) != null &&
1756 +                                        ((ek = e.key) == k || k.equals(ek))) {
1757 +                                        e.val = v;
1758 +                                        break;
1759 +                                    }
1760 +                                    Node last = e;
1761 +                                    if ((e = e.next) == null) {
1762 +                                        ++delta;
1763 +                                        last.next = new Node(h, k, v, null);
1764 +                                        if (count >= TREE_THRESHOLD)
1765 +                                            replaceWithTreeBin(tab, i, k);
1766 +                                        break;
1767 +                                    }
1768 +                                }
1769 +                            }
1770 +                        } finally {
1771 +                            if (!f.casHash(fh | LOCKED, fh)) {
1772 +                                f.hash = fh;
1773 +                                synchronized (f) { f.notifyAll(); };
1774 +                            }
1775 +                        }
1776 +                        if (count != 0) {
1777 +                            if (count > 1) {
1778 +                                counter.add(delta);
1779 +                                delta = 0L;
1780 +                                checkForResize();
1781 +                            }
1782 +                            break;
1783 +                        }
1784 +                    }
1785 +                }
1786 +            }
1787 +        } finally {
1788 +            if (delta != 0)
1789 +                counter.add(delta);
1790 +        }
1791 +        if (npe)
1792 +            throw new NullPointerException();
1793 +    }
1794 +
1795 +    /* ---------------- Table Initialization and Resizing -------------- */
1796 +
1797      /**
1798 <     * Implementation for clear. Steps through each bin, removing all nodes.
1798 >     * Returns a power of two table size for the given desired capacity.
1799 >     * See Hackers Delight, sec 3.2
1800 >     */
1801 >    private static final int tableSizeFor(int c) {
1802 >        int n = c - 1;
1803 >        n |= n >>> 1;
1804 >        n |= n >>> 2;
1805 >        n |= n >>> 4;
1806 >        n |= n >>> 8;
1807 >        n |= n >>> 16;
1808 >        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
1809 >    }
1810 >
1811 >    /**
1812 >     * Initializes table, using the size recorded in sizeCtl.
1813 >     */
1814 >    private final Node[] initTable() {
1815 >        Node[] tab; int sc;
1816 >        while ((tab = table) == null) {
1817 >            if ((sc = sizeCtl) < 0)
1818 >                Thread.yield(); // lost initialization race; just spin
1819 >            else if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1820 >                try {
1821 >                    if ((tab = table) == null) {
1822 >                        int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
1823 >                        tab = table = new Node[n];
1824 >                        sc = n - (n >>> 2);
1825 >                    }
1826 >                } finally {
1827 >                    sizeCtl = sc;
1828 >                }
1829 >                break;
1830 >            }
1831 >        }
1832 >        return tab;
1833 >    }
1834 >
1835 >    /**
1836 >     * If table is too small and not already resizing, creates next
1837 >     * table and transfers bins.  Rechecks occupancy after a transfer
1838 >     * to see if another resize is already needed because resizings
1839 >     * are lagging additions.
1840 >     */
1841 >    private final void checkForResize() {
1842 >        Node[] tab; int n, sc;
1843 >        while ((tab = table) != null &&
1844 >               (n = tab.length) < MAXIMUM_CAPACITY &&
1845 >               (sc = sizeCtl) >= 0 && counter.sum() >= (long)sc &&
1846 >               UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1847 >            try {
1848 >                if (tab == table) {
1849 >                    table = rebuild(tab);
1850 >                    sc = (n << 1) - (n >>> 1);
1851 >                }
1852 >            } finally {
1853 >                sizeCtl = sc;
1854 >            }
1855 >        }
1856 >    }
1857 >
1858 >    /**
1859 >     * Tries to presize table to accommodate the given number of elements.
1860 >     *
1861 >     * @param size number of elements (doesn't need to be perfectly accurate)
1862 >     */
1863 >    private final void tryPresize(int size) {
1864 >        int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
1865 >            tableSizeFor(size + (size >>> 1) + 1);
1866 >        int sc;
1867 >        while ((sc = sizeCtl) >= 0) {
1868 >            Node[] tab = table; int n;
1869 >            if (tab == null || (n = tab.length) == 0) {
1870 >                n = (sc > c) ? sc : c;
1871 >                if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1872 >                    try {
1873 >                        if (table == tab) {
1874 >                            table = new Node[n];
1875 >                            sc = n - (n >>> 2);
1876 >                        }
1877 >                    } finally {
1878 >                        sizeCtl = sc;
1879 >                    }
1880 >                }
1881 >            }
1882 >            else if (c <= sc || n >= MAXIMUM_CAPACITY)
1883 >                break;
1884 >            else if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1885 >                try {
1886 >                    if (table == tab) {
1887 >                        table = rebuild(tab);
1888 >                        sc = (n << 1) - (n >>> 1);
1889 >                    }
1890 >                } finally {
1891 >                    sizeCtl = sc;
1892 >                }
1893 >            }
1894 >        }
1895 >    }
1896 >
1897 >    /*
1898 >     * Moves and/or copies the nodes in each bin to new table. See
1899 >     * above for explanation.
1900 >     *
1901 >     * @return the new table
1902 >     */
1903 >    private static final Node[] rebuild(Node[] tab) {
1904 >        int n = tab.length;
1905 >        Node[] nextTab = new Node[n << 1];
1906 >        Node fwd = new Node(MOVED, nextTab, null, null);
1907 >        int[] buffer = null;       // holds bins to revisit; null until needed
1908 >        Node rev = null;           // reverse forwarder; null until needed
1909 >        int nbuffered = 0;         // the number of bins in buffer list
1910 >        int bufferIndex = 0;       // buffer index of current buffered bin
1911 >        int bin = n - 1;           // current non-buffered bin or -1 if none
1912 >
1913 >        for (int i = bin;;) {      // start upwards sweep
1914 >            int fh; Node f;
1915 >            if ((f = tabAt(tab, i)) == null) {
1916 >                if (bin >= 0) {    // no lock needed (or available)
1917 >                    if (!casTabAt(tab, i, f, fwd))
1918 >                        continue;
1919 >                }
1920 >                else {             // transiently use a locked forwarding node
1921 >                    Node g = new Node(MOVED|LOCKED, nextTab, null, null);
1922 >                    if (!casTabAt(tab, i, f, g))
1923 >                        continue;
1924 >                    setTabAt(nextTab, i, null);
1925 >                    setTabAt(nextTab, i + n, null);
1926 >                    setTabAt(tab, i, fwd);
1927 >                    if (!g.casHash(MOVED|LOCKED, MOVED)) {
1928 >                        g.hash = MOVED;
1929 >                        synchronized (g) { g.notifyAll(); }
1930 >                    }
1931 >                }
1932 >            }
1933 >            else if ((fh = f.hash) == MOVED) {
1934 >                Object fk = f.key;
1935 >                if (fk instanceof TreeBin) {
1936 >                    TreeBin t = (TreeBin)fk;
1937 >                    boolean validated = false;
1938 >                    t.acquire(0);
1939 >                    try {
1940 >                        if (tabAt(tab, i) == f) {
1941 >                            validated = true;
1942 >                            splitTreeBin(nextTab, i, t);
1943 >                            setTabAt(tab, i, fwd);
1944 >                        }
1945 >                    } finally {
1946 >                        t.release(0);
1947 >                    }
1948 >                    if (!validated)
1949 >                        continue;
1950 >                }
1951 >            }
1952 >            else if ((fh & LOCKED) == 0 && f.casHash(fh, fh|LOCKED)) {
1953 >                boolean validated = false;
1954 >                try {              // split to lo and hi lists; copying as needed
1955 >                    if (tabAt(tab, i) == f) {
1956 >                        validated = true;
1957 >                        splitBin(nextTab, i, f);
1958 >                        setTabAt(tab, i, fwd);
1959 >                    }
1960 >                } finally {
1961 >                    if (!f.casHash(fh | LOCKED, fh)) {
1962 >                        f.hash = fh;
1963 >                        synchronized (f) { f.notifyAll(); };
1964 >                    }
1965 >                }
1966 >                if (!validated)
1967 >                    continue;
1968 >            }
1969 >            else {
1970 >                if (buffer == null) // initialize buffer for revisits
1971 >                    buffer = new int[TRANSFER_BUFFER_SIZE];
1972 >                if (bin < 0 && bufferIndex > 0) {
1973 >                    int j = buffer[--bufferIndex];
1974 >                    buffer[bufferIndex] = i;
1975 >                    i = j;         // swap with another bin
1976 >                    continue;
1977 >                }
1978 >                if (bin < 0 || nbuffered >= TRANSFER_BUFFER_SIZE) {
1979 >                    f.tryAwaitLock(tab, i);
1980 >                    continue;      // no other options -- block
1981 >                }
1982 >                if (rev == null)   // initialize reverse-forwarder
1983 >                    rev = new Node(MOVED, tab, null, null);
1984 >                if (tabAt(tab, i) != f || (f.hash & LOCKED) == 0)
1985 >                    continue;      // recheck before adding to list
1986 >                buffer[nbuffered++] = i;
1987 >                setTabAt(nextTab, i, rev);     // install place-holders
1988 >                setTabAt(nextTab, i + n, rev);
1989 >            }
1990 >
1991 >            if (bin > 0)
1992 >                i = --bin;
1993 >            else if (buffer != null && nbuffered > 0) {
1994 >                bin = -1;
1995 >                i = buffer[bufferIndex = --nbuffered];
1996 >            }
1997 >            else
1998 >                return nextTab;
1999 >        }
2000 >    }
2001 >
2002 >    /**
2003 >     * Splits a normal bin with list headed by e into lo and hi parts;
2004 >     * installs in given table.
2005 >     */
2006 >    private static void splitBin(Node[] nextTab, int i, Node e) {
2007 >        int bit = nextTab.length >>> 1; // bit to split on
2008 >        int runBit = e.hash & bit;
2009 >        Node lastRun = e, lo = null, hi = null;
2010 >        for (Node p = e.next; p != null; p = p.next) {
2011 >            int b = p.hash & bit;
2012 >            if (b != runBit) {
2013 >                runBit = b;
2014 >                lastRun = p;
2015 >            }
2016 >        }
2017 >        if (runBit == 0)
2018 >            lo = lastRun;
2019 >        else
2020 >            hi = lastRun;
2021 >        for (Node p = e; p != lastRun; p = p.next) {
2022 >            int ph = p.hash & HASH_BITS;
2023 >            Object pk = p.key, pv = p.val;
2024 >            if ((ph & bit) == 0)
2025 >                lo = new Node(ph, pk, pv, lo);
2026 >            else
2027 >                hi = new Node(ph, pk, pv, hi);
2028 >        }
2029 >        setTabAt(nextTab, i, lo);
2030 >        setTabAt(nextTab, i + bit, hi);
2031 >    }
2032 >
2033 >    /**
2034 >     * Splits a tree bin into lo and hi parts; installs in given table.
2035 >     */
2036 >    private static void splitTreeBin(Node[] nextTab, int i, TreeBin t) {
2037 >        int bit = nextTab.length >>> 1;
2038 >        TreeBin lt = new TreeBin();
2039 >        TreeBin ht = new TreeBin();
2040 >        int lc = 0, hc = 0;
2041 >        for (Node e = t.first; e != null; e = e.next) {
2042 >            int h = e.hash & HASH_BITS;
2043 >            Object k = e.key, v = e.val;
2044 >            if ((h & bit) == 0) {
2045 >                ++lc;
2046 >                lt.putTreeNode(h, k, v);
2047 >            }
2048 >            else {
2049 >                ++hc;
2050 >                ht.putTreeNode(h, k, v);
2051 >            }
2052 >        }
2053 >        Node ln, hn; // throw away trees if too small
2054 >        if (lc <= (TREE_THRESHOLD >>> 1)) {
2055 >            ln = null;
2056 >            for (Node p = lt.first; p != null; p = p.next)
2057 >                ln = new Node(p.hash, p.key, p.val, ln);
2058 >        }
2059 >        else
2060 >            ln = new Node(MOVED, lt, null, null);
2061 >        setTabAt(nextTab, i, ln);
2062 >        if (hc <= (TREE_THRESHOLD >>> 1)) {
2063 >            hn = null;
2064 >            for (Node p = ht.first; p != null; p = p.next)
2065 >                hn = new Node(p.hash, p.key, p.val, hn);
2066 >        }
2067 >        else
2068 >            hn = new Node(MOVED, ht, null, null);
2069 >        setTabAt(nextTab, i + bit, hn);
2070 >    }
2071 >
2072 >    /**
2073 >     * Implementation for clear. Steps through each bin, removing all
2074 >     * nodes.
2075       */
2076      private final void internalClear() {
2077          long delta = 0L; // negative number of deletions
2078          int i = 0;
2079          Node[] tab = table;
2080          while (tab != null && i < tab.length) {
2081 <            Node e = tabAt(tab, i);
2082 <            if (e == null)
2081 >            int fh; Object fk;
2082 >            Node f = tabAt(tab, i);
2083 >            if (f == null)
2084                  ++i;
2085 <            else if (e.hash < 0)
2086 <                tab = (Node[])e.key;
2087 <            else {
2088 <                boolean validated = false;
2089 <                synchronized (e) {
2090 <                    if (tabAt(tab, i) == e) {
2091 <                        validated = true;
2092 <                        Node en;
742 <                        do {
743 <                            en = e.next;
744 <                            if (e.val != null) { // currently always true
745 <                                e.val = null;
2085 >            else if ((fh = f.hash) == MOVED) {
2086 >                if ((fk = f.key) instanceof TreeBin) {
2087 >                    TreeBin t = (TreeBin)fk;
2088 >                    t.acquire(0);
2089 >                    try {
2090 >                        if (tabAt(tab, i) == f) {
2091 >                            for (Node p = t.first; p != null; p = p.next) {
2092 >                                p.val = null;
2093                                  --delta;
2094                              }
2095 <                        } while ((e = en) != null);
2095 >                            t.first = null;
2096 >                            t.root = null;
2097 >                            ++i;
2098 >                        }
2099 >                    } finally {
2100 >                        t.release(0);
2101 >                    }
2102 >                }
2103 >                else
2104 >                    tab = (Node[])fk;
2105 >            }
2106 >            else if ((fh & LOCKED) != 0) {
2107 >                counter.add(delta); // opportunistically update count
2108 >                delta = 0L;
2109 >                f.tryAwaitLock(tab, i);
2110 >            }
2111 >            else if (f.casHash(fh, fh | LOCKED)) {
2112 >                try {
2113 >                    if (tabAt(tab, i) == f) {
2114 >                        for (Node e = f; e != null; e = e.next) {
2115 >                            e.val = null;
2116 >                            --delta;
2117 >                        }
2118                          setTabAt(tab, i, null);
2119 +                        ++i;
2120 +                    }
2121 +                } finally {
2122 +                    if (!f.casHash(fh | LOCKED, fh)) {
2123 +                        f.hash = fh;
2124 +                        synchronized (f) { f.notifyAll(); };
2125                      }
2126                  }
752                if (validated)
753                    ++i;
2127              }
2128          }
2129 <        counter.add(delta);
2129 >        if (delta != 0)
2130 >            counter.add(delta);
2131      }
2132  
2133      /* ----------------Table Traversal -------------- */
# Line 764 | Line 2138 | public class ConcurrentHashMapV8<K, V>
2138       *
2139       * At each step, the iterator snapshots the key ("nextKey") and
2140       * value ("nextVal") of a valid node (i.e., one that, at point of
2141 <     * snapshot, has a nonnull user value). Because val fields can
2141 >     * snapshot, has a non-null user value). Because val fields can
2142       * change (including to null, indicating deletion), field nextVal
2143       * might not be accurate at point of use, but still maintains the
2144       * weak consistency property of holding a value that was once
2145       * valid.
2146       *
2147       * Internal traversals directly access these fields, as in:
2148 <     * {@code while (it.next != null) { process(nextKey); it.advance(); }}
2148 >     * {@code while (it.advance() != null) { process(it.nextKey); }}
2149       *
2150 <     * Exported iterators (subclasses of ViewIterator) extract key,
2151 <     * value, or key-value pairs as return values of Iterator.next(),
2152 <     * and encapulate the it.next check as hasNext();
2153 <     *
2154 <     * The iterator visits each valid node that was reachable upon
2155 <     * iterator construction once. It might miss some that were added
2156 <     * to a bin after the bin was visited, which is OK wrt consistency
2157 <     * guarantees. Maintaining this property in the face of possible
2158 <     * ongoing resizes requires a fair amount of bookkeeping state
2159 <     * that is difficult to optimize away amidst volatile accesses.
2160 <     * Even so, traversal maintains reasonable throughput.
2150 >     * Exported iterators must track whether the iterator has advanced
2151 >     * (in hasNext vs next) (by setting/checking/nulling field
2152 >     * nextVal), and then extract key, value, or key-value pairs as
2153 >     * return values of next().
2154 >     *
2155 >     * The iterator visits once each still-valid node that was
2156 >     * reachable upon iterator construction. It might miss some that
2157 >     * were added to a bin after the bin was visited, which is OK wrt
2158 >     * consistency guarantees. Maintaining this property in the face
2159 >     * of possible ongoing resizes requires a fair amount of
2160 >     * bookkeeping state that is difficult to optimize away amidst
2161 >     * volatile accesses.  Even so, traversal maintains reasonable
2162 >     * throughput.
2163       *
2164       * Normally, iteration proceeds bin-by-bin traversing lists.
2165       * However, if the table has been resized, then all future steps
# Line 792 | Line 2168 | public class ConcurrentHashMapV8<K, V>
2168       * paranoically cope with potential sharing by users of iterators
2169       * across threads, iteration terminates if a bounds checks fails
2170       * for a table read.
795     *
796     * The range-based constructor enables creation of parallel
797     * range-splitting traversals. (Not yet implemented.)
2171       */
2172 <    static class InternalIterator {
2172 >    static class InternalIterator<K,V> {
2173 >        final ConcurrentHashMapV8<K, V> map;
2174          Node next;           // the next entry to use
2175          Node last;           // the last entry used
2176          Object nextKey;      // cached key field of next
# Line 804 | Line 2178 | public class ConcurrentHashMapV8<K, V>
2178          Node[] tab;          // current table; updated if resized
2179          int index;           // index of bin to use next
2180          int baseIndex;       // current index of initial table
2181 <        final int baseLimit; // index bound for initial table
2181 >        int baseLimit;       // index bound for initial table
2182          final int baseSize;  // initial table size
2183  
2184          /** Creates iterator for all entries in the table. */
2185 <        InternalIterator(Node[] tab) {
2186 <            this.tab = tab;
2185 >        InternalIterator(ConcurrentHashMapV8<K, V> map) {
2186 >            this.tab = (this.map = map).table;
2187              baseLimit = baseSize = (tab == null) ? 0 : tab.length;
814            index = baseIndex = 0;
815            next = null;
816            advance();
817        }
818
819        /** Creates iterator for the given range of the table */
820        InternalIterator(Node[] tab, int lo, int hi) {
821            this.tab = tab;
822            baseSize = (tab == null) ? 0 : tab.length;
823            baseLimit = (hi <= baseSize) ? hi : baseSize;
824            index = baseIndex = lo;
825            next = null;
826            advance();
2188          }
2189  
2190 <        /** Advances next. See above for explanation. */
2191 <        final void advance() {
2190 >        /** Creates iterator for clone() and split() methods */
2191 >        InternalIterator(InternalIterator<K,V> it, boolean split) {
2192 >            this.map = it.map;
2193 >            this.tab = it.tab;
2194 >            this.baseSize = it.baseSize;
2195 >            int lo = it.baseIndex;
2196 >            int hi = this.baseLimit = it.baseLimit;
2197 >            this.index = this.baseIndex =
2198 >                (split) ? (it.baseLimit = (lo + hi + 1) >>> 1) : lo;
2199 >        }
2200 >
2201 >        /**
2202 >         * Advances next; returns nextVal or null if terminated
2203 >         * See above for explanation.
2204 >         */
2205 >        final Object advance() {
2206              Node e = last = next;
2207 +            Object ev = null;
2208              outer: do {
2209 <                if (e != null)                   // pass used or skipped node
2209 >                if (e != null)                  // advance past used/skipped node
2210                      e = e.next;
2211 <                while (e == null) {              // get to next non-null bin
2212 <                    Node[] t; int b, i, n;       // checks must use locals
2211 >                while (e == null) {             // get to next non-null bin
2212 >                    Node[] t; int b, i, n; Object ek; // checks must use locals
2213                      if ((b = baseIndex) >= baseLimit || (i = index) < 0 ||
2214                          (t = tab) == null || i >= (n = t.length))
2215                          break outer;
2216 <                    else if ((e = tabAt(t, i)) != null && e.hash < 0)
2217 <                        tab = (Node[])e.key;     // restarts due to null val
2218 <                    else                         // visit upper slots if present
2219 <                        index = (i += baseSize) < n ? i : (baseIndex = b + 1);
2216 >                    else if ((e = tabAt(t, i)) != null && e.hash == MOVED) {
2217 >                        if ((ek = e.key) instanceof TreeBin)
2218 >                            e = ((TreeBin)ek).first;
2219 >                        else {
2220 >                            tab = (Node[])ek;
2221 >                            continue;           // restarts due to null val
2222 >                        }
2223 >                    }                           // visit upper slots if present
2224 >                    index = (i += baseSize) < n ? i : (baseIndex = b + 1);
2225                  }
2226                  nextKey = e.key;
2227 <            } while ((nextVal = e.val) == null); // skip deleted or special nodes
2227 >            } while ((ev = e.val) == null);    // skip deleted or special nodes
2228              next = e;
2229 +            return nextVal = ev;
2230          }
2231 +
2232 +        public final void remove() {
2233 +            if (nextVal == null)
2234 +                advance();
2235 +            Node e = last;
2236 +            if (e == null)
2237 +                throw new IllegalStateException();
2238 +            last = null;
2239 +            map.remove(e.key);
2240 +        }
2241 +
2242 +        public final boolean hasNext() {
2243 +            return nextVal != null || advance() != null;
2244 +        }
2245 +
2246 +        public final boolean hasMoreElements() { return hasNext(); }
2247      }
2248  
2249      /* ---------------- Public operations -------------- */
# Line 855 | Line 2253 | public class ConcurrentHashMapV8<K, V>
2253       */
2254      public ConcurrentHashMapV8() {
2255          this.counter = new LongAdder();
858        this.targetCapacity = DEFAULT_CAPACITY;
2256      }
2257  
2258      /**
# Line 866 | Line 2263 | public class ConcurrentHashMapV8<K, V>
2263       * @param initialCapacity The implementation performs internal
2264       * sizing to accommodate this many elements.
2265       * @throws IllegalArgumentException if the initial capacity of
2266 <     * elements is negative.
2266 >     * elements is negative
2267       */
2268      public ConcurrentHashMapV8(int initialCapacity) {
2269          if (initialCapacity < 0)
# Line 875 | Line 2272 | public class ConcurrentHashMapV8<K, V>
2272                     MAXIMUM_CAPACITY :
2273                     tableSizeFor(initialCapacity + (initialCapacity >>> 1) + 1));
2274          this.counter = new LongAdder();
2275 <        this.targetCapacity = cap;
2275 >        this.sizeCtl = cap;
2276      }
2277  
2278      /**
# Line 885 | Line 2282 | public class ConcurrentHashMapV8<K, V>
2282       */
2283      public ConcurrentHashMapV8(Map<? extends K, ? extends V> m) {
2284          this.counter = new LongAdder();
2285 <        this.targetCapacity = DEFAULT_CAPACITY;
2286 <        putAll(m);
2285 >        this.sizeCtl = DEFAULT_CAPACITY;
2286 >        internalPutAll(m);
2287      }
2288  
2289      /**
# Line 898 | Line 2295 | public class ConcurrentHashMapV8<K, V>
2295       * performs internal sizing to accommodate this many elements,
2296       * given the specified load factor.
2297       * @param loadFactor the load factor (table density) for
2298 <     * establishing the initial table size.
2298 >     * establishing the initial table size
2299       * @throws IllegalArgumentException if the initial capacity of
2300       * elements is negative or the load factor is nonpositive
2301       *
# Line 918 | Line 2315 | public class ConcurrentHashMapV8<K, V>
2315       * performs internal sizing to accommodate this many elements,
2316       * given the specified load factor.
2317       * @param loadFactor the load factor (table density) for
2318 <     * establishing the initial table size.
2318 >     * establishing the initial table size
2319       * @param concurrencyLevel the estimated number of concurrently
2320       * updating threads. The implementation may use this value as
2321       * a sizing hint.
2322       * @throws IllegalArgumentException if the initial capacity is
2323       * negative or the load factor or concurrencyLevel are
2324 <     * nonpositive.
2324 >     * nonpositive
2325       */
2326      public ConcurrentHashMapV8(int initialCapacity,
2327                                 float loadFactor, int concurrencyLevel) {
# Line 933 | Line 2330 | public class ConcurrentHashMapV8<K, V>
2330          if (initialCapacity < concurrencyLevel)   // Use at least as many bins
2331              initialCapacity = concurrencyLevel;   // as estimated threads
2332          long size = (long)(1.0 + (long)initialCapacity / loadFactor);
2333 <        int cap =  ((size >= (long)MAXIMUM_CAPACITY) ?
2334 <                    MAXIMUM_CAPACITY: tableSizeFor((int)size));
2333 >        int cap = ((size >= (long)MAXIMUM_CAPACITY) ?
2334 >                   MAXIMUM_CAPACITY: tableSizeFor((int)size));
2335          this.counter = new LongAdder();
2336 <        this.targetCapacity = cap;
2336 >        this.sizeCtl = cap;
2337      }
2338  
2339      /**
# Line 956 | Line 2353 | public class ConcurrentHashMapV8<K, V>
2353                  (int)n);
2354      }
2355  
2356 +    final long longSize() { // accurate version of size needed for views
2357 +        long n = counter.sum();
2358 +        return (n < 0L) ? 0L : n;
2359 +    }
2360 +
2361      /**
2362       * Returns the value to which the specified key is mapped,
2363       * or {@code null} if this map contains no mapping for the key.
# Line 980 | Line 2382 | public class ConcurrentHashMapV8<K, V>
2382       * @param  key   possible key
2383       * @return {@code true} if and only if the specified object
2384       *         is a key in this table, as determined by the
2385 <     *         {@code equals} method; {@code false} otherwise.
2385 >     *         {@code equals} method; {@code false} otherwise
2386       * @throws NullPointerException if the specified key is null
2387       */
2388      public boolean containsKey(Object key) {
# Line 1003 | Line 2405 | public class ConcurrentHashMapV8<K, V>
2405          if (value == null)
2406              throw new NullPointerException();
2407          Object v;
2408 <        InternalIterator it = new InternalIterator(table);
2409 <        while (it.next != null) {
2410 <            if ((v = it.nextVal) == value || value.equals(v))
2408 >        InternalIterator<K,V> it = new InternalIterator<K,V>(this);
2409 >        while ((v = it.advance()) != null) {
2410 >            if (v == value || value.equals(v))
2411                  return true;
1010            it.advance();
2412          }
2413          return false;
2414      }
# Line 1048 | Line 2449 | public class ConcurrentHashMapV8<K, V>
2449      public V put(K key, V value) {
2450          if (key == null || value == null)
2451              throw new NullPointerException();
2452 <        return (V)internalPut(key, value, true);
2452 >        return (V)internalPut(key, value);
2453      }
2454  
2455      /**
# Line 1062 | Line 2463 | public class ConcurrentHashMapV8<K, V>
2463      public V putIfAbsent(K key, V value) {
2464          if (key == null || value == null)
2465              throw new NullPointerException();
2466 <        return (V)internalPut(key, value, false);
2466 >        return (V)internalPutIfAbsent(key, value);
2467      }
2468  
2469      /**
# Line 1073 | Line 2474 | public class ConcurrentHashMapV8<K, V>
2474       * @param m mappings to be stored in this map
2475       */
2476      public void putAll(Map<? extends K, ? extends V> m) {
2477 <        if (m == null)
1077 <            throw new NullPointerException();
1078 <        /*
1079 <         * If uninitialized, try to adjust targetCapacity to
1080 <         * accommodate the given number of elements.
1081 <         */
1082 <        if (table == null) {
1083 <            int size = m.size();
1084 <            int cap = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
1085 <                tableSizeFor(size + (size >>> 1) + 1);
1086 <            if (cap > targetCapacity)
1087 <                targetCapacity = cap;
1088 <        }
1089 <        for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
1090 <            put(e.getKey(), e.getValue());
2477 >        internalPutAll(m);
2478      }
2479  
2480      /**
2481       * If the specified key is not already associated with a value,
2482 <     * computes its value using the given mappingFunction, and if
2483 <     * non-null, enters it into the map.  This is equivalent to
2484 <     *  <pre> {@code
2482 >     * computes its value using the given mappingFunction and enters
2483 >     * it into the map unless null.  This is equivalent to
2484 >     * <pre> {@code
2485       * if (map.containsKey(key))
2486       *   return map.get(key);
2487       * value = mappingFunction.map(key);
# Line 1102 | Line 2489 | public class ConcurrentHashMapV8<K, V>
2489       *   map.put(key, value);
2490       * return value;}</pre>
2491       *
2492 <     * except that the action is performed atomically.  Some attempted
2493 <     * update operations on this map by other threads may be blocked
2494 <     * while computation is in progress, so the computation should be
2495 <     * short and simple, and must not attempt to update any other
2496 <     * mappings of this Map. The most appropriate usage is to
2492 >     * except that the action is performed atomically.  If the
2493 >     * function returns {@code null} no mapping is recorded. If the
2494 >     * function itself throws an (unchecked) exception, the exception
2495 >     * is rethrown to its caller, and no mapping is recorded.  Some
2496 >     * attempted update operations on this map by other threads may be
2497 >     * blocked while computation is in progress, so the computation
2498 >     * should be short and simple, and must not attempt to update any
2499 >     * other mappings of this Map. The most appropriate usage is to
2500       * construct a new object serving as an initial mapped value, or
2501       * memoized result, as in:
2502 +     *
2503       *  <pre> {@code
2504       * map.computeIfAbsent(key, new MappingFunction<K, V>() {
2505       *   public V map(K k) { return new Value(f(k)); }});}</pre>
# Line 1116 | Line 2507 | public class ConcurrentHashMapV8<K, V>
2507       * @param key key with which the specified value is to be associated
2508       * @param mappingFunction the function to compute a value
2509       * @return the current (existing or computed) value associated with
2510 <     *         the specified key, or {@code null} if the computation
1120 <     *         returned {@code null}.
2510 >     *         the specified key, or null if the computed value is null.
2511       * @throws NullPointerException if the specified key or mappingFunction
2512 <     *         is null,
2512 >     *         is null
2513       * @throws IllegalStateException if the computation detectably
2514       *         attempts a recursive update to this map that would
2515 <     *         otherwise never complete.
2515 >     *         otherwise never complete
2516       * @throws RuntimeException or Error if the mappingFunction does so,
2517 <     *         in which case the mapping is left unestablished.
2517 >     *         in which case the mapping is left unestablished
2518       */
2519 +    @SuppressWarnings("unchecked")
2520      public V computeIfAbsent(K key, MappingFunction<? super K, ? extends V> mappingFunction) {
2521          if (key == null || mappingFunction == null)
2522              throw new NullPointerException();
2523 <        return internalCompute(key, mappingFunction, false);
2523 >        return (V)internalComputeIfAbsent(key, mappingFunction);
2524      }
2525  
2526      /**
2527 <     * Computes the value associated with the given key using the given
2528 <     * mappingFunction, and if non-null, enters it into the map.  This
2529 <     * is equivalent to
2527 >     * Computes a new mapping value given a key and
2528 >     * its current mapped value (or {@code null} if there is no current
2529 >     * mapping). This is equivalent to
2530       *  <pre> {@code
2531 <     * value = mappingFunction.map(key);
2532 <     * if (value != null)
2533 <     *   map.put(key, value);
2534 <     * else
2535 <     *   value = map.get(key);
2536 <     * return value;}</pre>
2537 <     *
2538 <     * except that the action is performed atomically.  Some attempted
2539 <     * update operations on this map by other threads may be blocked
2540 <     * while computation is in progress, so the computation should be
2541 <     * short and simple, and must not attempt to update any other
2542 <     * mappings of this Map.
2531 >     *   value = remappingFunction.remap(key, map.get(key));
2532 >     *   if (value != null)
2533 >     *     map.put(key, value);
2534 >     *   else
2535 >     *     map.remove(key);
2536 >     * }</pre>
2537 >     *
2538 >     * except that the action is performed atomically.  If the
2539 >     * function returns {@code null}, the mapping is removed.  If the
2540 >     * function itself throws an (unchecked) exception, the exception
2541 >     * is rethrown to its caller, and the current mapping is left
2542 >     * unchanged.  Some attempted update operations on this map by
2543 >     * other threads may be blocked while computation is in progress,
2544 >     * so the computation should be short and simple, and must not
2545 >     * attempt to update any other mappings of this Map. For example,
2546 >     * to either create or append new messages to a value mapping:
2547 >     *
2548 >     * <pre> {@code
2549 >     * Map<Key, String> map = ...;
2550 >     * final String msg = ...;
2551 >     * map.compute(key, new RemappingFunction<Key, String>() {
2552 >     *   public String remap(Key k, String v) {
2553 >     *    return (v == null) ? msg : v + msg;});}}</pre>
2554       *
2555       * @param key key with which the specified value is to be associated
2556 <     * @param mappingFunction the function to compute a value
2557 <     * @return the current value associated with
2558 <     *         the specified key, or {@code null} if the computation
2559 <     *         returned {@code null} and the value was not otherwise present.
2560 <     * @throws NullPointerException if the specified key or mappingFunction
1159 <     *         is null,
2556 >     * @param remappingFunction the function to compute a value
2557 >     * @return the new value associated with
2558 >     *         the specified key, or null if none.
2559 >     * @throws NullPointerException if the specified key or remappingFunction
2560 >     *         is null
2561       * @throws IllegalStateException if the computation detectably
2562       *         attempts a recursive update to this map that would
2563 <     *         otherwise never complete.
2564 <     * @throws RuntimeException or Error if the mappingFunction does so,
2565 <     *         in which case the mapping is unchanged.
2563 >     *         otherwise never complete
2564 >     * @throws RuntimeException or Error if the remappingFunction does so,
2565 >     *         in which case the mapping is unchanged
2566       */
2567 <    public V compute(K key, MappingFunction<? super K, ? extends V> mappingFunction) {
2568 <        if (key == null || mappingFunction == null)
2567 >    @SuppressWarnings("unchecked")
2568 >    public V compute(K key, RemappingFunction<? super K, V> remappingFunction) {
2569 >        if (key == null || remappingFunction == null)
2570              throw new NullPointerException();
2571 <        return internalCompute(key, mappingFunction, true);
2571 >        return (V)internalCompute(key, remappingFunction);
2572      }
2573  
2574      /**
# Line 1314 | Line 2716 | public class ConcurrentHashMapV8<K, V>
2716      }
2717  
2718      /**
2719 +     * Returns a partionable iterator of the keys in this map.
2720 +     *
2721 +     * @return a partionable iterator of the keys in this map
2722 +     */
2723 +    public Spliterator<K> keySpliterator() {
2724 +        return new KeyIterator<K,V>(this);
2725 +    }
2726 +
2727 +    /**
2728 +     * Returns a partionable iterator of the values in this map.
2729 +     *
2730 +     * @return a partionable iterator of the values in this map
2731 +     */
2732 +    public Spliterator<V> valueSpliterator() {
2733 +        return new ValueIterator<K,V>(this);
2734 +    }
2735 +
2736 +    /**
2737 +     * Returns a partionable iterator of the entries in this map.
2738 +     *
2739 +     * @return a partionable iterator of the entries in this map
2740 +     */
2741 +    public Spliterator<Map.Entry<K,V>> entrySpliterator() {
2742 +        return new EntryIterator<K,V>(this);
2743 +    }
2744 +
2745 +    /**
2746       * Returns the hash code value for this {@link Map}, i.e.,
2747       * the sum of, for each key-value pair in the map,
2748       * {@code key.hashCode() ^ value.hashCode()}.
# Line 1322 | Line 2751 | public class ConcurrentHashMapV8<K, V>
2751       */
2752      public int hashCode() {
2753          int h = 0;
2754 <        InternalIterator it = new InternalIterator(table);
2755 <        while (it.next != null) {
2756 <            h += it.nextKey.hashCode() ^ it.nextVal.hashCode();
2757 <            it.advance();
2754 >        InternalIterator<K,V> it = new InternalIterator<K,V>(this);
2755 >        Object v;
2756 >        while ((v = it.advance()) != null) {
2757 >            h += it.nextKey.hashCode() ^ v.hashCode();
2758          }
2759          return h;
2760      }
# Line 1342 | Line 2771 | public class ConcurrentHashMapV8<K, V>
2771       * @return a string representation of this map
2772       */
2773      public String toString() {
2774 <        InternalIterator it = new InternalIterator(table);
2774 >        InternalIterator<K,V> it = new InternalIterator<K,V>(this);
2775          StringBuilder sb = new StringBuilder();
2776          sb.append('{');
2777 <        if (it.next != null) {
2777 >        Object v;
2778 >        if ((v = it.advance()) != null) {
2779              for (;;) {
2780 <                Object k = it.nextKey, v = it.nextVal;
2780 >                Object k = it.nextKey;
2781                  sb.append(k == this ? "(this Map)" : k);
2782                  sb.append('=');
2783                  sb.append(v == this ? "(this Map)" : v);
2784 <                it.advance();
1355 <                if (it.next == null)
2784 >                if ((v = it.advance()) == null)
2785                      break;
2786                  sb.append(',').append(' ');
2787              }
# Line 1375 | Line 2804 | public class ConcurrentHashMapV8<K, V>
2804              if (!(o instanceof Map))
2805                  return false;
2806              Map<?,?> m = (Map<?,?>) o;
2807 <            InternalIterator it = new InternalIterator(table);
2808 <            while (it.next != null) {
2809 <                Object val = it.nextVal;
2807 >            InternalIterator<K,V> it = new InternalIterator<K,V>(this);
2808 >            Object val;
2809 >            while ((val = it.advance()) != null) {
2810                  Object v = m.get(it.nextKey);
2811                  if (v == null || (v != val && !v.equals(val)))
2812                      return false;
1384                it.advance();
2813              }
2814              for (Map.Entry<?,?> e : m.entrySet()) {
2815                  Object mk, mv, v;
# Line 1397 | Line 2825 | public class ConcurrentHashMapV8<K, V>
2825  
2826      /* ----------------Iterators -------------- */
2827  
2828 <    /**
2829 <     * Base class for key, value, and entry iterators.  Adds a map
2830 <     * reference to InternalIterator to support Iterator.remove.
2831 <     */
2832 <    static abstract class ViewIterator<K,V> extends InternalIterator {
1405 <        final ConcurrentHashMapV8<K, V> map;
1406 <        ViewIterator(ConcurrentHashMapV8<K, V> map) {
1407 <            super(map.table);
1408 <            this.map = map;
2828 >    static final class KeyIterator<K,V> extends InternalIterator<K,V>
2829 >        implements Spliterator<K>, Enumeration<K> {
2830 >        KeyIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
2831 >        KeyIterator(InternalIterator<K,V> it, boolean split) {
2832 >            super(it, split);
2833          }
2834 <
2835 <        public final void remove() {
1412 <            if (last == null)
2834 >        public KeyIterator<K,V> split() {
2835 >            if (last != null || (next != null && nextVal == null))
2836                  throw new IllegalStateException();
2837 <            map.remove(last.key);
2838 <            last = null;
2837 >            return new KeyIterator<K,V>(this, true);
2838 >        }
2839 >        public KeyIterator<K,V> clone() {
2840 >            if (last != null || (next != null && nextVal == null))
2841 >                throw new IllegalStateException();
2842 >            return new KeyIterator<K,V>(this, false);
2843          }
1417
1418        public final boolean hasNext()         { return next != null; }
1419        public final boolean hasMoreElements() { return next != null; }
1420    }
1421
1422    static final class KeyIterator<K,V> extends ViewIterator<K,V>
1423        implements Iterator<K>, Enumeration<K> {
1424        KeyIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
2844  
2845          @SuppressWarnings("unchecked")
2846          public final K next() {
2847 <            if (next == null)
2847 >            if (nextVal == null && advance() == null)
2848                  throw new NoSuchElementException();
2849              Object k = nextKey;
2850 <            advance();
2851 <            return (K)k;
2850 >            nextVal = null;
2851 >            return (K) k;
2852          }
2853  
2854          public final K nextElement() { return next(); }
2855      }
2856  
2857 <    static final class ValueIterator<K,V> extends ViewIterator<K,V>
2858 <        implements Iterator<V>, Enumeration<V> {
2857 >    static final class ValueIterator<K,V> extends InternalIterator<K,V>
2858 >        implements Spliterator<V>, Enumeration<V> {
2859          ValueIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
2860 +        ValueIterator(InternalIterator<K,V> it, boolean split) {
2861 +            super(it, split);
2862 +        }
2863 +        public ValueIterator<K,V> split() {
2864 +            if (last != null || (next != null && nextVal == null))
2865 +                throw new IllegalStateException();
2866 +            return new ValueIterator<K,V>(this, true);
2867 +        }
2868 +
2869 +        public ValueIterator<K,V> clone() {
2870 +            if (last != null || (next != null && nextVal == null))
2871 +                throw new IllegalStateException();
2872 +            return new ValueIterator<K,V>(this, false);
2873 +        }
2874  
2875          @SuppressWarnings("unchecked")
2876          public final V next() {
2877 <            if (next == null)
2877 >            Object v;
2878 >            if ((v = nextVal) == null && (v = advance()) == null)
2879                  throw new NoSuchElementException();
2880 <            Object v = nextVal;
2881 <            advance();
1448 <            return (V)v;
2880 >            nextVal = null;
2881 >            return (V) v;
2882          }
2883  
2884          public final V nextElement() { return next(); }
2885      }
2886  
2887 <    static final class EntryIterator<K,V> extends ViewIterator<K,V>
2888 <        implements Iterator<Map.Entry<K,V>> {
2887 >    static final class EntryIterator<K,V> extends InternalIterator<K,V>
2888 >        implements Spliterator<Map.Entry<K,V>> {
2889          EntryIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
2890 +        EntryIterator(InternalIterator<K,V> it, boolean split) {
2891 +            super(it, split);
2892 +        }
2893 +        public EntryIterator<K,V> split() {
2894 +            if (last != null || (next != null && nextVal == null))
2895 +                throw new IllegalStateException();
2896 +            return new EntryIterator<K,V>(this, true);
2897 +        }
2898 +        public EntryIterator<K,V> clone() {
2899 +            if (last != null || (next != null && nextVal == null))
2900 +                throw new IllegalStateException();
2901 +            return new EntryIterator<K,V>(this, false);
2902 +        }
2903  
2904          @SuppressWarnings("unchecked")
2905          public final Map.Entry<K,V> next() {
2906 <            if (next == null)
2906 >            Object v;
2907 >            if ((v = nextVal) == null && (v = advance()) == null)
2908                  throw new NoSuchElementException();
2909              Object k = nextKey;
2910 <            Object v = nextVal;
2911 <            advance();
1465 <            return new WriteThroughEntry<K,V>(map, (K)k, (V)v);
2910 >            nextVal = null;
2911 >            return new MapEntry<K,V>((K)k, (V)v, map);
2912          }
2913      }
2914  
2915      /**
2916 <     * Custom Entry class used by EntryIterator.next(), that relays
1471 <     * setValue changes to the underlying map.
2916 >     * Exported Entry for iterators
2917       */
2918 <    static final class WriteThroughEntry<K,V> implements Map.Entry<K, V> {
1474 <        final ConcurrentHashMapV8<K, V> map;
2918 >    static final class MapEntry<K,V> implements Map.Entry<K, V> {
2919          final K key; // non-null
2920          V val;       // non-null
2921 <        WriteThroughEntry(ConcurrentHashMapV8<K, V> map, K key, V val) {
2922 <            this.map = map; this.key = key; this.val = val;
2921 >        final ConcurrentHashMapV8<K, V> map;
2922 >        MapEntry(K key, V val, ConcurrentHashMapV8<K, V> map) {
2923 >            this.key = key;
2924 >            this.val = val;
2925 >            this.map = map;
2926          }
1480
2927          public final K getKey()       { return key; }
2928          public final V getValue()     { return val; }
2929          public final int hashCode()   { return key.hashCode() ^ val.hashCode(); }
# Line 1494 | Line 2940 | public class ConcurrentHashMapV8<K, V>
2940  
2941          /**
2942           * Sets our entry's value and writes through to the map. The
2943 <         * value to return is somewhat arbitrary here. Since a
2944 <         * WriteThroughEntry does not necessarily track asynchronous
2945 <         * changes, the most recent "previous" value could be
2946 <         * different from what we return (or could even have been
2947 <         * removed in which case the put will re-establish). We do not
1502 <         * and cannot guarantee more.
2943 >         * value to return is somewhat arbitrary here. Since a we do
2944 >         * not necessarily track asynchronous changes, the most recent
2945 >         * "previous" value could be different from what we return (or
2946 >         * could even have been removed in which case the put will
2947 >         * re-establish). We do not and cannot guarantee more.
2948           */
2949          public final V setValue(V value) {
2950              if (value == null) throw new NullPointerException();
# Line 1512 | Line 2957 | public class ConcurrentHashMapV8<K, V>
2957  
2958      /* ----------------Views -------------- */
2959  
2960 <    /*
2961 <     * These currently just extend java.util.AbstractX classes, but
1517 <     * may need a new custom base to support partitioned traversal.
2960 >    /**
2961 >     * Base class for views.
2962       */
2963 <
1520 <    static final class KeySet<K,V> extends AbstractSet<K> {
2963 >    static abstract class MapView<K, V> {
2964          final ConcurrentHashMapV8<K, V> map;
2965 <        KeySet(ConcurrentHashMapV8<K, V> map)   { this.map = map; }
1523 <
2965 >        MapView(ConcurrentHashMapV8<K, V> map)  { this.map = map; }
2966          public final int size()                 { return map.size(); }
2967          public final boolean isEmpty()          { return map.isEmpty(); }
2968          public final void clear()               { map.clear(); }
2969 +
2970 +        // implementations below rely on concrete classes supplying these
2971 +        abstract public Iterator<?> iterator();
2972 +        abstract public boolean contains(Object o);
2973 +        abstract public boolean remove(Object o);
2974 +
2975 +        private static final String oomeMsg = "Required array size too large";
2976 +
2977 +        public final Object[] toArray() {
2978 +            long sz = map.longSize();
2979 +            if (sz > (long)(MAX_ARRAY_SIZE))
2980 +                throw new OutOfMemoryError(oomeMsg);
2981 +            int n = (int)sz;
2982 +            Object[] r = new Object[n];
2983 +            int i = 0;
2984 +            Iterator<?> it = iterator();
2985 +            while (it.hasNext()) {
2986 +                if (i == n) {
2987 +                    if (n >= MAX_ARRAY_SIZE)
2988 +                        throw new OutOfMemoryError(oomeMsg);
2989 +                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
2990 +                        n = MAX_ARRAY_SIZE;
2991 +                    else
2992 +                        n += (n >>> 1) + 1;
2993 +                    r = Arrays.copyOf(r, n);
2994 +                }
2995 +                r[i++] = it.next();
2996 +            }
2997 +            return (i == n) ? r : Arrays.copyOf(r, i);
2998 +        }
2999 +
3000 +        @SuppressWarnings("unchecked")
3001 +        public final <T> T[] toArray(T[] a) {
3002 +            long sz = map.longSize();
3003 +            if (sz > (long)(MAX_ARRAY_SIZE))
3004 +                throw new OutOfMemoryError(oomeMsg);
3005 +            int m = (int)sz;
3006 +            T[] r = (a.length >= m) ? a :
3007 +                (T[])java.lang.reflect.Array
3008 +                .newInstance(a.getClass().getComponentType(), m);
3009 +            int n = r.length;
3010 +            int i = 0;
3011 +            Iterator<?> it = iterator();
3012 +            while (it.hasNext()) {
3013 +                if (i == n) {
3014 +                    if (n >= MAX_ARRAY_SIZE)
3015 +                        throw new OutOfMemoryError(oomeMsg);
3016 +                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
3017 +                        n = MAX_ARRAY_SIZE;
3018 +                    else
3019 +                        n += (n >>> 1) + 1;
3020 +                    r = Arrays.copyOf(r, n);
3021 +                }
3022 +                r[i++] = (T)it.next();
3023 +            }
3024 +            if (a == r && i < n) {
3025 +                r[i] = null; // null-terminate
3026 +                return r;
3027 +            }
3028 +            return (i == n) ? r : Arrays.copyOf(r, i);
3029 +        }
3030 +
3031 +        public final int hashCode() {
3032 +            int h = 0;
3033 +            for (Iterator<?> it = iterator(); it.hasNext();)
3034 +                h += it.next().hashCode();
3035 +            return h;
3036 +        }
3037 +
3038 +        public final String toString() {
3039 +            StringBuilder sb = new StringBuilder();
3040 +            sb.append('[');
3041 +            Iterator<?> it = iterator();
3042 +            if (it.hasNext()) {
3043 +                for (;;) {
3044 +                    Object e = it.next();
3045 +                    sb.append(e == this ? "(this Collection)" : e);
3046 +                    if (!it.hasNext())
3047 +                        break;
3048 +                    sb.append(',').append(' ');
3049 +                }
3050 +            }
3051 +            return sb.append(']').toString();
3052 +        }
3053 +
3054 +        public final boolean containsAll(Collection<?> c) {
3055 +            if (c != this) {
3056 +                for (Iterator<?> it = c.iterator(); it.hasNext();) {
3057 +                    Object e = it.next();
3058 +                    if (e == null || !contains(e))
3059 +                        return false;
3060 +                }
3061 +            }
3062 +            return true;
3063 +        }
3064 +
3065 +        public final boolean removeAll(Collection<?> c) {
3066 +            boolean modified = false;
3067 +            for (Iterator<?> it = iterator(); it.hasNext();) {
3068 +                if (c.contains(it.next())) {
3069 +                    it.remove();
3070 +                    modified = true;
3071 +                }
3072 +            }
3073 +            return modified;
3074 +        }
3075 +
3076 +        public final boolean retainAll(Collection<?> c) {
3077 +            boolean modified = false;
3078 +            for (Iterator<?> it = iterator(); it.hasNext();) {
3079 +                if (!c.contains(it.next())) {
3080 +                    it.remove();
3081 +                    modified = true;
3082 +                }
3083 +            }
3084 +            return modified;
3085 +        }
3086 +
3087 +    }
3088 +
3089 +    static final class KeySet<K,V> extends MapView<K,V> implements Set<K> {
3090 +        KeySet(ConcurrentHashMapV8<K, V> map)   { super(map); }
3091          public final boolean contains(Object o) { return map.containsKey(o); }
3092          public final boolean remove(Object o)   { return map.remove(o) != null; }
3093          public final Iterator<K> iterator() {
3094              return new KeyIterator<K,V>(map);
3095          }
3096 +        public final boolean add(K e) {
3097 +            throw new UnsupportedOperationException();
3098 +        }
3099 +        public final boolean addAll(Collection<? extends K> c) {
3100 +            throw new UnsupportedOperationException();
3101 +        }
3102 +        public boolean equals(Object o) {
3103 +            Set<?> c;
3104 +            return ((o instanceof Set) &&
3105 +                    ((c = (Set<?>)o) == this ||
3106 +                     (containsAll(c) && c.containsAll(this))));
3107 +        }
3108      }
3109  
3110 <    static final class Values<K,V> extends AbstractCollection<V> {
3111 <        final ConcurrentHashMapV8<K, V> map;
3112 <        Values(ConcurrentHashMapV8<K, V> map)   { this.map = map; }
1537 <
1538 <        public final int size()                 { return map.size(); }
1539 <        public final boolean isEmpty()          { return map.isEmpty(); }
1540 <        public final void clear()               { map.clear(); }
3110 >    static final class Values<K,V> extends MapView<K,V>
3111 >        implements Collection<V> {
3112 >        Values(ConcurrentHashMapV8<K, V> map)   { super(map); }
3113          public final boolean contains(Object o) { return map.containsValue(o); }
3114 +        public final boolean remove(Object o) {
3115 +            if (o != null) {
3116 +                Iterator<V> it = new ValueIterator<K,V>(map);
3117 +                while (it.hasNext()) {
3118 +                    if (o.equals(it.next())) {
3119 +                        it.remove();
3120 +                        return true;
3121 +                    }
3122 +                }
3123 +            }
3124 +            return false;
3125 +        }
3126          public final Iterator<V> iterator() {
3127              return new ValueIterator<K,V>(map);
3128          }
3129 <    }
3130 <
3131 <    static final class EntrySet<K,V> extends AbstractSet<Map.Entry<K,V>> {
3132 <        final ConcurrentHashMapV8<K, V> map;
3133 <        EntrySet(ConcurrentHashMapV8<K, V> map) { this.map = map; }
1550 <
1551 <        public final int size()                 { return map.size(); }
1552 <        public final boolean isEmpty()          { return map.isEmpty(); }
1553 <        public final void clear()               { map.clear(); }
1554 <        public final Iterator<Map.Entry<K,V>> iterator() {
1555 <            return new EntryIterator<K,V>(map);
3129 >        public final boolean add(V e) {
3130 >            throw new UnsupportedOperationException();
3131 >        }
3132 >        public final boolean addAll(Collection<? extends V> c) {
3133 >            throw new UnsupportedOperationException();
3134          }
3135 +    }
3136  
3137 +    static final class EntrySet<K,V> extends MapView<K,V>
3138 +        implements Set<Map.Entry<K,V>> {
3139 +        EntrySet(ConcurrentHashMapV8<K, V> map) { super(map); }
3140          public final boolean contains(Object o) {
3141              Object k, v, r; Map.Entry<?,?> e;
3142              return ((o instanceof Map.Entry) &&
# Line 1563 | Line 3145 | public class ConcurrentHashMapV8<K, V>
3145                      (v = e.getValue()) != null &&
3146                      (v == r || v.equals(r)));
3147          }
1566
3148          public final boolean remove(Object o) {
3149              Object k, v; Map.Entry<?,?> e;
3150              return ((o instanceof Map.Entry) &&
# Line 1571 | Line 3152 | public class ConcurrentHashMapV8<K, V>
3152                      (v = e.getValue()) != null &&
3153                      map.remove(k, v));
3154          }
3155 +        public final Iterator<Map.Entry<K,V>> iterator() {
3156 +            return new EntryIterator<K,V>(map);
3157 +        }
3158 +        public final boolean add(Entry<K,V> e) {
3159 +            throw new UnsupportedOperationException();
3160 +        }
3161 +        public final boolean addAll(Collection<? extends Entry<K,V>> c) {
3162 +            throw new UnsupportedOperationException();
3163 +        }
3164 +        public boolean equals(Object o) {
3165 +            Set<?> c;
3166 +            return ((o instanceof Set) &&
3167 +                    ((c = (Set<?>)o) == this ||
3168 +                     (containsAll(c) && c.containsAll(this))));
3169 +        }
3170      }
3171  
3172      /* ---------------- Serialization Support -------------- */
# Line 1604 | Line 3200 | public class ConcurrentHashMapV8<K, V>
3200                  segments[i] = new Segment<K,V>(LOAD_FACTOR);
3201          }
3202          s.defaultWriteObject();
3203 <        InternalIterator it = new InternalIterator(table);
3204 <        while (it.next != null) {
3203 >        InternalIterator<K,V> it = new InternalIterator<K,V>(this);
3204 >        Object v;
3205 >        while ((v = it.advance()) != null) {
3206              s.writeObject(it.nextKey);
3207 <            s.writeObject(it.nextVal);
1611 <            it.advance();
3207 >            s.writeObject(v);
3208          }
3209          s.writeObject(null);
3210          s.writeObject(null);
# Line 1624 | Line 3220 | public class ConcurrentHashMapV8<K, V>
3220              throws java.io.IOException, ClassNotFoundException {
3221          s.defaultReadObject();
3222          this.segments = null; // unneeded
3223 <        // initalize transient final field
3223 >        // initialize transient final field
3224          UNSAFE.putObjectVolatile(this, counterOffset, new LongAdder());
1629        this.targetCapacity = DEFAULT_CAPACITY;
3225  
3226          // Create all nodes, then place in table once size is known
3227          long size = 0L;
# Line 1635 | Line 3230 | public class ConcurrentHashMapV8<K, V>
3230              K k = (K) s.readObject();
3231              V v = (V) s.readObject();
3232              if (k != null && v != null) {
3233 <                p = new Node(spread(k.hashCode()), k, v, p);
3233 >                int h = spread(k.hashCode());
3234 >                p = new Node(h, k, v, p);
3235                  ++size;
3236              }
3237              else
# Line 1643 | Line 3239 | public class ConcurrentHashMapV8<K, V>
3239          }
3240          if (p != null) {
3241              boolean init = false;
3242 <            if (resizing == 0 &&
3243 <                UNSAFE.compareAndSwapInt(this, resizingOffset, 0, 1)) {
3242 >            int n;
3243 >            if (size >= (long)(MAXIMUM_CAPACITY >>> 1))
3244 >                n = MAXIMUM_CAPACITY;
3245 >            else {
3246 >                int sz = (int)size;
3247 >                n = tableSizeFor(sz + (sz >>> 1) + 1);
3248 >            }
3249 >            int sc = sizeCtl;
3250 >            boolean collide = false;
3251 >            if (n > sc &&
3252 >                UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
3253                  try {
3254                      if (table == null) {
3255                          init = true;
1651                        int n;
1652                        if (size >= (long)(MAXIMUM_CAPACITY >>> 1))
1653                            n = MAXIMUM_CAPACITY;
1654                        else {
1655                            int sz = (int)size;
1656                            n = tableSizeFor(sz + (sz >>> 1) + 1);
1657                        }
1658                        threshold = n - (n >>> 2) - THRESHOLD_OFFSET;
3256                          Node[] tab = new Node[n];
3257                          int mask = n - 1;
3258                          while (p != null) {
3259                              int j = p.hash & mask;
3260                              Node next = p.next;
3261 <                            p.next = tabAt(tab, j);
3261 >                            Node q = p.next = tabAt(tab, j);
3262                              setTabAt(tab, j, p);
3263 +                            if (!collide && q != null && q.hash == p.hash)
3264 +                                collide = true;
3265                              p = next;
3266                          }
3267                          table = tab;
3268                          counter.add(size);
3269 +                        sc = n - (n >>> 2);
3270                      }
3271                  } finally {
3272 <                    resizing = 0;
3272 >                    sizeCtl = sc;
3273 >                }
3274 >                if (collide) { // rescan and convert to TreeBins
3275 >                    Node[] tab = table;
3276 >                    for (int i = 0; i < tab.length; ++i) {
3277 >                        int c = 0;
3278 >                        for (Node e = tabAt(tab, i); e != null; e = e.next) {
3279 >                            if (++c > TREE_THRESHOLD &&
3280 >                                (e.key instanceof Comparable)) {
3281 >                                replaceWithTreeBin(tab, i, e.key);
3282 >                                break;
3283 >                            }
3284 >                        }
3285 >                    }
3286                  }
3287              }
3288              if (!init) { // Can only happen if unsafely published.
3289                  while (p != null) {
3290 <                    internalPut(p.key, p.val, true);
3290 >                    internalPut(p.key, p.val);
3291                      p = p.next;
3292                  }
3293              }
# Line 1684 | Line 3297 | public class ConcurrentHashMapV8<K, V>
3297      // Unsafe mechanics
3298      private static final sun.misc.Unsafe UNSAFE;
3299      private static final long counterOffset;
3300 <    private static final long resizingOffset;
3300 >    private static final long sizeCtlOffset;
3301      private static final long ABASE;
3302      private static final int ASHIFT;
3303  
# Line 1695 | Line 3308 | public class ConcurrentHashMapV8<K, V>
3308              Class<?> k = ConcurrentHashMapV8.class;
3309              counterOffset = UNSAFE.objectFieldOffset
3310                  (k.getDeclaredField("counter"));
3311 <            resizingOffset = UNSAFE.objectFieldOffset
3312 <                (k.getDeclaredField("resizing"));
3311 >            sizeCtlOffset = UNSAFE.objectFieldOffset
3312 >                (k.getDeclaredField("sizeCtl"));
3313              Class<?> sc = Node[].class;
3314              ABASE = UNSAFE.arrayBaseOffset(sc);
3315              ss = UNSAFE.arrayIndexScale(sc);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines