ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ConcurrentHashMap.java
(Generate patch)

Comparing jsr166/src/main/java/util/concurrent/ConcurrentHashMap.java (file contents):
Revision 1.135 by jsr166, Sun Oct 21 04:14:31 2012 UTC vs.
Revision 1.228 by jsr166, Tue Jun 18 18:39:14 2013 UTC

# Line 5 | Line 5
5   */
6  
7   package java.util.concurrent;
8 import java.util.concurrent.atomic.LongAdder;
9 import java.util.concurrent.ForkJoinPool;
10 import java.util.concurrent.ForkJoinTask;
8  
9 < import java.util.Comparator;
9 > import java.io.ObjectStreamField;
10 > import java.io.Serializable;
11 > import java.lang.reflect.ParameterizedType;
12 > import java.lang.reflect.Type;
13   import java.util.Arrays;
14 import java.util.Map;
15 import java.util.Set;
14   import java.util.Collection;
15 < import java.util.AbstractMap;
16 < import java.util.AbstractSet;
17 < import java.util.AbstractCollection;
20 < import java.util.Hashtable;
15 > import java.util.Comparator;
16 > import java.util.ConcurrentModificationException;
17 > import java.util.Enumeration;
18   import java.util.HashMap;
19 + import java.util.Hashtable;
20   import java.util.Iterator;
21 < import java.util.Enumeration;
24 < import java.util.ConcurrentModificationException;
21 > import java.util.Map;
22   import java.util.NoSuchElementException;
23 + import java.util.Set;
24 + import java.util.Spliterator;
25   import java.util.concurrent.ConcurrentMap;
26 < import java.util.concurrent.ThreadLocalRandom;
28 < import java.util.concurrent.locks.LockSupport;
29 < import java.util.concurrent.locks.AbstractQueuedSynchronizer;
26 > import java.util.concurrent.ForkJoinPool;
27   import java.util.concurrent.atomic.AtomicReference;
28 <
29 < import java.io.Serializable;
28 > import java.util.concurrent.locks.LockSupport;
29 > import java.util.concurrent.locks.ReentrantLock;
30 > import java.util.function.BiConsumer;
31 > import java.util.function.BiFunction;
32 > import java.util.function.BinaryOperator;
33 > import java.util.function.Consumer;
34 > import java.util.function.DoubleBinaryOperator;
35 > import java.util.function.Function;
36 > import java.util.function.IntBinaryOperator;
37 > import java.util.function.LongBinaryOperator;
38 > import java.util.function.ToDoubleBiFunction;
39 > import java.util.function.ToDoubleFunction;
40 > import java.util.function.ToIntBiFunction;
41 > import java.util.function.ToIntFunction;
42 > import java.util.function.ToLongBiFunction;
43 > import java.util.function.ToLongFunction;
44 > import java.util.stream.Stream;
45  
46   /**
47   * A hash table supporting full concurrency of retrievals and
# Line 43 | Line 55 | import java.io.Serializable;
55   * interoperable with {@code Hashtable} in programs that rely on its
56   * thread safety but not on its synchronization details.
57   *
58 < * <p> Retrieval operations (including {@code get}) generally do not
58 > * <p>Retrieval operations (including {@code get}) generally do not
59   * block, so may overlap with update operations (including {@code put}
60   * and {@code remove}). Retrievals reflect the results of the most
61   * recently <em>completed</em> update operations holding upon their
# Line 64 | Line 76 | import java.io.Serializable;
76   * that may be adequate for monitoring or estimation purposes, but not
77   * for program control.
78   *
79 < * <p> The table is dynamically expanded when there are too many
79 > * <p>The table is dynamically expanded when there are too many
80   * collisions (i.e., keys that have distinct hash codes but fall into
81   * the same slot modulo the table size), with the expected average
82   * effect of maintaining roughly two bins per mapping (corresponding
# Line 83 | Line 95 | import java.io.Serializable;
95   * expected {@code concurrencyLevel} as an additional hint for
96   * internal sizing.  Note that using many keys with exactly the same
97   * {@code hashCode()} is a sure way to slow down performance of any
98 < * hash table.
98 > * hash table. To ameliorate impact, when keys are {@link Comparable},
99 > * this class may use comparison order among keys to help break ties.
100 > *
101 > * <p>A {@link Set} projection of a ConcurrentHashMap may be created
102 > * (using {@link #newKeySet()} or {@link #newKeySet(int)}), or viewed
103 > * (using {@link #keySet(Object)} when only keys are of interest, and the
104 > * mapped values are (perhaps transiently) not used or all take the
105 > * same mapping value.
106 > *
107 > * <p>A ConcurrentHashMap can be used as scalable frequency map (a
108 > * form of histogram or multiset) by using {@link
109 > * java.util.concurrent.atomic.LongAdder} values and initializing via
110 > * {@link #computeIfAbsent computeIfAbsent}. For example, to add a count
111 > * to a {@code ConcurrentHashMap<String,LongAdder> freqs}, you can use
112 > * {@code freqs.computeIfAbsent(k -> new LongAdder()).increment();}
113   *
114   * <p>This class and its views and iterators implement all of the
115   * <em>optional</em> methods of the {@link Map} and {@link Iterator}
116   * interfaces.
117   *
118 < * <p> Like {@link Hashtable} but unlike {@link HashMap}, this class
118 > * <p>Like {@link Hashtable} but unlike {@link HashMap}, this class
119   * does <em>not</em> allow {@code null} to be used as a key or value.
120   *
121 + * <p>ConcurrentHashMaps support a set of sequential and parallel bulk
122 + * operations that, unlike most {@link Stream} methods, are designed
123 + * to be safely, and often sensibly, applied even with maps that are
124 + * being concurrently updated by other threads; for example, when
125 + * computing a snapshot summary of the values in a shared registry.
126 + * There are three kinds of operation, each with four forms, accepting
127 + * functions with Keys, Values, Entries, and (Key, Value) arguments
128 + * and/or return values. Because the elements of a ConcurrentHashMap
129 + * are not ordered in any particular way, and may be processed in
130 + * different orders in different parallel executions, the correctness
131 + * of supplied functions should not depend on any ordering, or on any
132 + * other objects or values that may transiently change while
133 + * computation is in progress; and except for forEach actions, should
134 + * ideally be side-effect-free. Bulk operations on {@link Map.Entry}
135 + * objects do not support method {@code setValue}.
136 + *
137 + * <ul>
138 + * <li> forEach: Perform a given action on each element.
139 + * A variant form applies a given transformation on each element
140 + * before performing the action.</li>
141 + *
142 + * <li> search: Return the first available non-null result of
143 + * applying a given function on each element; skipping further
144 + * search when a result is found.</li>
145 + *
146 + * <li> reduce: Accumulate each element.  The supplied reduction
147 + * function cannot rely on ordering (more formally, it should be
148 + * both associative and commutative).  There are five variants:
149 + *
150 + * <ul>
151 + *
152 + * <li> Plain reductions. (There is not a form of this method for
153 + * (key, value) function arguments since there is no corresponding
154 + * return type.)</li>
155 + *
156 + * <li> Mapped reductions that accumulate the results of a given
157 + * function applied to each element.</li>
158 + *
159 + * <li> Reductions to scalar doubles, longs, and ints, using a
160 + * given basis value.</li>
161 + *
162 + * </ul>
163 + * </li>
164 + * </ul>
165 + *
166 + * <p>These bulk operations accept a {@code parallelismThreshold}
167 + * argument. Methods proceed sequentially if the current map size is
168 + * estimated to be less than the given threshold. Using a value of
169 + * {@code Long.MAX_VALUE} suppresses all parallelism.  Using a value
170 + * of {@code 1} results in maximal parallelism by partitioning into
171 + * enough subtasks to fully utilize the {@link
172 + * ForkJoinPool#commonPool()} that is used for all parallel
173 + * computations. Normally, you would initially choose one of these
174 + * extreme values, and then measure performance of using in-between
175 + * values that trade off overhead versus throughput.
176 + *
177 + * <p>The concurrency properties of bulk operations follow
178 + * from those of ConcurrentHashMap: Any non-null result returned
179 + * from {@code get(key)} and related access methods bears a
180 + * happens-before relation with the associated insertion or
181 + * update.  The result of any bulk operation reflects the
182 + * composition of these per-element relations (but is not
183 + * necessarily atomic with respect to the map as a whole unless it
184 + * is somehow known to be quiescent).  Conversely, because keys
185 + * and values in the map are never null, null serves as a reliable
186 + * atomic indicator of the current lack of any result.  To
187 + * maintain this property, null serves as an implicit basis for
188 + * all non-scalar reduction operations. For the double, long, and
189 + * int versions, the basis should be one that, when combined with
190 + * any other value, returns that other value (more formally, it
191 + * should be the identity element for the reduction). Most common
192 + * reductions have these properties; for example, computing a sum
193 + * with basis 0 or a minimum with basis MAX_VALUE.
194 + *
195 + * <p>Search and transformation functions provided as arguments
196 + * should similarly return null to indicate the lack of any result
197 + * (in which case it is not used). In the case of mapped
198 + * reductions, this also enables transformations to serve as
199 + * filters, returning null (or, in the case of primitive
200 + * specializations, the identity basis) if the element should not
201 + * be combined. You can create compound transformations and
202 + * filterings by composing them yourself under this "null means
203 + * there is nothing there now" rule before using them in search or
204 + * reduce operations.
205 + *
206 + * <p>Methods accepting and/or returning Entry arguments maintain
207 + * key-value associations. They may be useful for example when
208 + * finding the key for the greatest value. Note that "plain" Entry
209 + * arguments can be supplied using {@code new
210 + * AbstractMap.SimpleEntry(k,v)}.
211 + *
212 + * <p>Bulk operations may complete abruptly, throwing an
213 + * exception encountered in the application of a supplied
214 + * function. Bear in mind when handling such exceptions that other
215 + * concurrently executing functions could also have thrown
216 + * exceptions, or would have done so if the first exception had
217 + * not occurred.
218 + *
219 + * <p>Speedups for parallel compared to sequential forms are common
220 + * but not guaranteed.  Parallel operations involving brief functions
221 + * on small maps may execute more slowly than sequential forms if the
222 + * underlying work to parallelize the computation is more expensive
223 + * than the computation itself.  Similarly, parallelization may not
224 + * lead to much actual parallelism if all processors are busy
225 + * performing unrelated tasks.
226 + *
227 + * <p>All arguments to all task methods must be non-null.
228 + *
229   * <p>This class is a member of the
230   * <a href="{@docRoot}/../technotes/guides/collections/index.html">
231   * Java Collections Framework</a>.
232   *
99 * <p><em> During transition to JDK8, this
100 * class declares and uses nested functional interfaces with different
101 * names but the same forms as those expected for JDK8.<em>
102 *
233   * @since 1.5
234   * @author Doug Lea
235   * @param <K> the type of keys maintained by this map
236   * @param <V> the type of mapped values
237   */
238 < public class ConcurrentHashMap<K, V>
109 <    implements ConcurrentMap<K, V>, Serializable {
238 > public class ConcurrentHashMap<K,V> implements ConcurrentMap<K,V>, Serializable {
239      private static final long serialVersionUID = 7249069246763182397L;
240  
112    /**
113     * A partitionable iterator. A Spliterator can be traversed
114     * directly, but can also be partitioned (before traversal) by
115     * creating another Spliterator that covers a non-overlapping
116     * portion of the elements, and so may be amenable to parallel
117     * execution.
118     *
119     * <p> This interface exports a subset of expected JDK8
120     * functionality.
121     *
122     * <p>Sample usage: Here is one (of the several) ways to compute
123     * the sum of the values held in a map using the ForkJoin
124     * framework. As illustrated here, Spliterators are well suited to
125     * designs in which a task repeatedly splits off half its work
126     * into forked subtasks until small enough to process directly,
127     * and then joins these subtasks. Variants of this style can also
128     * be used in completion-based designs.
129     *
130     * <pre>
131     * {@code ConcurrentHashMap<String, Long> m = ...
132     * // split as if have 8 * parallelism, for load balance
133     * int n = m.size();
134     * int p = aForkJoinPool.getParallelism() * 8;
135     * int split = (n < p)? n : p;
136     * long sum = aForkJoinPool.invoke(new SumValues(m.valueSpliterator(), split, null));
137     * // ...
138     * static class SumValues extends RecursiveTask<Long> {
139     *   final Spliterator<Long> s;
140     *   final int split;             // split while > 1
141     *   final SumValues nextJoin;    // records forked subtasks to join
142     *   SumValues(Spliterator<Long> s, int depth, SumValues nextJoin) {
143     *     this.s = s; this.depth = depth; this.nextJoin = nextJoin;
144     *   }
145     *   public Long compute() {
146     *     long sum = 0;
147     *     SumValues subtasks = null; // fork subtasks
148     *     for (int s = split >>> 1; s > 0; s >>>= 1)
149     *       (subtasks = new SumValues(s.split(), s, subtasks)).fork();
150     *     while (s.hasNext())        // directly process remaining elements
151     *       sum += s.next();
152     *     for (SumValues t = subtasks; t != null; t = t.nextJoin)
153     *       sum += t.join();         // collect subtask results
154     *     return sum;
155     *   }
156     * }
157     * }</pre>
158     */
159    public static interface Spliterator<T> extends Iterator<T> {
160        /**
161         * Returns a Spliterator covering approximately half of the
162         * elements, guaranteed not to overlap with those subsequently
163         * returned by this Spliterator.  After invoking this method,
164         * the current Spliterator will <em>not</em> produce any of
165         * the elements of the returned Spliterator, but the two
166         * Spliterators together will produce all of the elements that
167         * would have been produced by this Spliterator had this
168         * method not been called. The exact number of elements
169         * produced by the returned Spliterator is not guaranteed, and
170         * may be zero (i.e., with {@code hasNext()} reporting {@code
171         * false}) if this Spliterator cannot be further split.
172         *
173         * @return a Spliterator covering approximately half of the
174         * elements
175         * @throws IllegalStateException if this Spliterator has
176         * already commenced traversing elements
177         */
178        Spliterator<T> split();
179    }
180
241      /*
242       * Overview:
243       *
# Line 188 | Line 248 | public class ConcurrentHashMap<K, V>
248       * the same or better than java.util.HashMap, and to support high
249       * initial insertion rates on an empty table by many threads.
250       *
251 <     * Each key-value mapping is held in a Node.  Because Node fields
252 <     * can contain special values, they are defined using plain Object
253 <     * types. Similarly in turn, all internal methods that use them
254 <     * work off Object types. And similarly, so do the internal
255 <     * methods of auxiliary iterator and view classes.  All public
256 <     * generic typed methods relay in/out of these internal methods,
257 <     * supplying null-checks and casts as needed. This also allows
258 <     * many of the public methods to be factored into a smaller number
259 <     * of internal methods (although sadly not so for the five
260 <     * variants of put-related operations). The validation-based
261 <     * approach explained below leads to a lot of code sprawl because
262 <     * retry-control precludes factoring into smaller methods.
251 >     * This map usually acts as a binned (bucketed) hash table.  Each
252 >     * key-value mapping is held in a Node.  Most nodes are instances
253 >     * of the basic Node class with hash, key, value, and next
254 >     * fields. However, various subclasses exist: TreeNodes are
255 >     * arranged in balanced trees, not lists.  TreeBins hold the roots
256 >     * of sets of TreeNodes. ForwardingNodes are placed at the heads
257 >     * of bins during resizing. ReservationNodes are used as
258 >     * placeholders while establishing values in computeIfAbsent and
259 >     * related methods.  The types TreeBin, ForwardingNode, and
260 >     * ReservationNode do not hold normal user keys, values, or
261 >     * hashes, and are readily distinguishable during search etc
262 >     * because they have negative hash fields and null key and value
263 >     * fields. (These special nodes are either uncommon or transient,
264 >     * so the impact of carrying around some unused fields is
265 >     * insignficant.)
266       *
267       * The table is lazily initialized to a power-of-two size upon the
268       * first insertion.  Each bin in the table normally contains a
# Line 207 | Line 270 | public class ConcurrentHashMap<K, V>
270       * Table accesses require volatile/atomic reads, writes, and
271       * CASes.  Because there is no other way to arrange this without
272       * adding further indirections, we use intrinsics
273 <     * (sun.misc.Unsafe) operations.  The lists of nodes within bins
274 <     * are always accurately traversable under volatile reads, so long
275 <     * as lookups check hash code and non-nullness of value before
276 <     * checking key equality.
277 <     *
278 <     * We use the top two bits of Node hash fields for control
216 <     * purposes -- they are available anyway because of addressing
217 <     * constraints.  As explained further below, these top bits are
218 <     * used as follows:
219 <     *  00 - Normal
220 <     *  01 - Locked
221 <     *  11 - Locked and may have a thread waiting for lock
222 <     *  10 - Node is a forwarding node
223 <     *
224 <     * The lower 30 bits of each Node's hash field contain a
225 <     * transformation of the key's hash code, except for forwarding
226 <     * nodes, for which the lower bits are zero (and so always have
227 <     * hash field == MOVED).
273 >     * (sun.misc.Unsafe) operations.
274 >     *
275 >     * We use the top (sign) bit of Node hash fields for control
276 >     * purposes -- it is available anyway because of addressing
277 >     * constraints.  Nodes with negative hash fields are specially
278 >     * handled or ignored in map methods.
279       *
280       * Insertion (via put or its variants) of the first node in an
281       * empty bin is performed by just CASing it to the bin.  This is
# Line 233 | Line 284 | public class ConcurrentHashMap<K, V>
284       * delete, and replace) require locks.  We do not want to waste
285       * the space required to associate a distinct lock object with
286       * each bin, so instead use the first node of a bin list itself as
287 <     * a lock. Blocking support for these locks relies on the builtin
288 <     * "synchronized" monitors.  However, we also need a tryLock
238 <     * construction, so we overlay these by using bits of the Node
239 <     * hash field for lock control (see above), and so normally use
240 <     * builtin monitors only for blocking and signalling using
241 <     * wait/notifyAll constructions. See Node.tryAwaitLock.
287 >     * a lock. Locking support for these locks relies on builtin
288 >     * "synchronized" monitors.
289       *
290       * Using the first node of a list as a lock does not by itself
291       * suffice though: When a node is locked, any update must first
292       * validate that it is still the first node after locking it, and
293       * retry if not. Because new nodes are always appended to lists,
294       * once a node is first in a bin, it remains first until deleted
295 <     * or the bin becomes invalidated (upon resizing).  However,
249 <     * operations that only conditionally update may inspect nodes
250 <     * until the point of update. This is a converse of sorts to the
251 <     * lazy locking technique described by Herlihy & Shavit.
295 >     * or the bin becomes invalidated (upon resizing).
296       *
297       * The main disadvantage of per-bin locks is that other update
298       * operations on other nodes in a bin list protected by the same
# Line 281 | Line 325 | public class ConcurrentHashMap<K, V>
325       * sometimes deviate significantly from uniform randomness.  This
326       * includes the case when N > (1<<30), so some keys MUST collide.
327       * Similarly for dumb or hostile usages in which multiple keys are
328 <     * designed to have identical hash codes. Also, although we guard
329 <     * against the worst effects of this (see method spread), sets of
330 <     * hashes may differ only in bits that do not impact their bin
331 <     * index for a given power-of-two mask.  So we use a secondary
332 <     * strategy that applies when the number of nodes in a bin exceeds
333 <     * a threshold, and at least one of the keys implements
290 <     * Comparable.  These TreeBins use a balanced tree to hold nodes
291 <     * (a specialized form of red-black trees), bounding search time
292 <     * to O(log N).  Each search step in a TreeBin is around twice as
328 >     * designed to have identical hash codes or ones that differs only
329 >     * in masked-out high bits. So we use a secondary strategy that
330 >     * applies when the number of nodes in a bin exceeds a
331 >     * threshold. These TreeBins use a balanced tree to hold nodes (a
332 >     * specialized form of red-black trees), bounding search time to
333 >     * O(log N).  Each search step in a TreeBin is at least twice as
334       * slow as in a regular list, but given that N cannot exceed
335       * (1<<64) (before running out of addresses) this bounds search
336       * steps, lock hold times, etc, to reasonable constants (roughly
# Line 300 | Line 341 | public class ConcurrentHashMap<K, V>
341       * iterators in the same way.
342       *
343       * The table is resized when occupancy exceeds a percentage
344 <     * threshold (nominally, 0.75, but see below).  Only a single
345 <     * thread performs the resize (using field "sizeCtl", to arrange
346 <     * exclusion), but the table otherwise remains usable for reads
347 <     * and updates. Resizing proceeds by transferring bins, one by
348 <     * one, from the table to the next table.  Because we are using
349 <     * power-of-two expansion, the elements from each bin must either
350 <     * stay at same index, or move with a power of two offset. We
351 <     * eliminate unnecessary node creation by catching cases where old
352 <     * nodes can be reused because their next fields won't change.  On
353 <     * average, only about one-sixth of them need cloning when a table
354 <     * doubles. The nodes they replace will be garbage collectable as
355 <     * soon as they are no longer referenced by any reader thread that
356 <     * may be in the midst of concurrently traversing table.  Upon
357 <     * transfer, the old table bin contains only a special forwarding
358 <     * node (with hash field "MOVED") that contains the next table as
359 <     * its key. On encountering a forwarding node, access and update
360 <     * operations restart, using the new table.
361 <     *
362 <     * Each bin transfer requires its bin lock. However, unlike other
363 <     * cases, a transfer can skip a bin if it fails to acquire its
364 <     * lock, and revisit it later (unless it is a TreeBin). Method
365 <     * rebuild maintains a buffer of TRANSFER_BUFFER_SIZE bins that
366 <     * have been skipped because of failure to acquire a lock, and
367 <     * blocks only if none are available (i.e., only very rarely).
368 <     * The transfer operation must also ensure that all accessible
369 <     * bins in both the old and new table are usable by any traversal.
370 <     * When there are no lock acquisition failures, this is arranged
371 <     * simply by proceeding from the last bin (table.length - 1) up
372 <     * towards the first.  Upon seeing a forwarding node, traversals
373 <     * (see class Iter) arrange to move to the new table
374 <     * without revisiting nodes.  However, when any node is skipped
375 <     * during a transfer, all earlier table bins may have become
376 <     * visible, so are initialized with a reverse-forwarding node back
377 <     * to the old table until the new ones are established. (This
378 <     * sometimes requires transiently locking a forwarding node, which
379 <     * is possible under the above encoding.) These more expensive
380 <     * mechanics trigger only when necessary.
344 >     * threshold (nominally, 0.75, but see below).  Any thread
345 >     * noticing an overfull bin may assist in resizing after the
346 >     * initiating thread allocates and sets up the replacement
347 >     * array. However, rather than stalling, these other threads may
348 >     * proceed with insertions etc.  The use of TreeBins shields us
349 >     * from the worst case effects of overfilling while resizes are in
350 >     * progress.  Resizing proceeds by transferring bins, one by one,
351 >     * from the table to the next table. To enable concurrency, the
352 >     * next table must be (incrementally) prefilled with place-holders
353 >     * serving as reverse forwarders to the old table.  Because we are
354 >     * using power-of-two expansion, the elements from each bin must
355 >     * either stay at same index, or move with a power of two
356 >     * offset. We eliminate unnecessary node creation by catching
357 >     * cases where old nodes can be reused because their next fields
358 >     * won't change.  On average, only about one-sixth of them need
359 >     * cloning when a table doubles. The nodes they replace will be
360 >     * garbage collectable as soon as they are no longer referenced by
361 >     * any reader thread that may be in the midst of concurrently
362 >     * traversing table.  Upon transfer, the old table bin contains
363 >     * only a special forwarding node (with hash field "MOVED") that
364 >     * contains the next table as its key. On encountering a
365 >     * forwarding node, access and update operations restart, using
366 >     * the new table.
367 >     *
368 >     * Each bin transfer requires its bin lock, which can stall
369 >     * waiting for locks while resizing. However, because other
370 >     * threads can join in and help resize rather than contend for
371 >     * locks, average aggregate waits become shorter as resizing
372 >     * progresses.  The transfer operation must also ensure that all
373 >     * accessible bins in both the old and new table are usable by any
374 >     * traversal.  This is arranged by proceeding from the last bin
375 >     * (table.length - 1) up towards the first.  Upon seeing a
376 >     * forwarding node, traversals (see class Traverser) arrange to
377 >     * move to the new table without revisiting nodes.  However, to
378 >     * ensure that no intervening nodes are skipped, bin splitting can
379 >     * only begin after the associated reverse-forwarders are in
380 >     * place.
381       *
382       * The traversal scheme also applies to partial traversals of
383       * ranges of bins (via an alternate Traverser constructor)
# Line 351 | Line 392 | public class ConcurrentHashMap<K, V>
392       * These cases attempt to override the initial capacity settings,
393       * but harmlessly fail to take effect in cases of races.
394       *
395 <     * The element count is maintained using a LongAdder, which avoids
396 <     * contention on updates but can encounter cache thrashing if read
397 <     * too frequently during concurrent access. To avoid reading so
398 <     * often, resizing is attempted either when a bin lock is
399 <     * contended, or upon adding to a bin already holding two or more
400 <     * nodes (checked before adding in the xIfAbsent methods, after
401 <     * adding in others). Under uniform hash distributions, the
402 <     * probability of this occurring at threshold is around 13%,
403 <     * meaning that only about 1 in 8 puts check threshold (and after
404 <     * resizing, many fewer do so). But this approximation has high
405 <     * variance for small table sizes, so we check on any collision
406 <     * for sizes <= 64. The bulk putAll operation further reduces
407 <     * contention by only committing count updates upon these size
408 <     * checks.
395 >     * The element count is maintained using a specialization of
396 >     * LongAdder. We need to incorporate a specialization rather than
397 >     * just use a LongAdder in order to access implicit
398 >     * contention-sensing that leads to creation of multiple
399 >     * CounterCells.  The counter mechanics avoid contention on
400 >     * updates but can encounter cache thrashing if read too
401 >     * frequently during concurrent access. To avoid reading so often,
402 >     * resizing under contention is attempted only upon adding to a
403 >     * bin already holding two or more nodes. Under uniform hash
404 >     * distributions, the probability of this occurring at threshold
405 >     * is around 13%, meaning that only about 1 in 8 puts check
406 >     * threshold (and after resizing, many fewer do so).
407 >     *
408 >     * TreeBins use a special form of comparison for search and
409 >     * related operations (which is the main reason we cannot use
410 >     * existing collections such as TreeMaps). TreeBins contain
411 >     * Comparable elements, but may contain others, as well as
412 >     * elements that are Comparable but not necessarily Comparable
413 >     * for the same T, so we cannot invoke compareTo among them. To
414 >     * handle this, the tree is ordered primarily by hash value, then
415 >     * by Comparable.compareTo order if applicable.  On lookup at a
416 >     * node, if elements are not comparable or compare as 0 then both
417 >     * left and right children may need to be searched in the case of
418 >     * tied hash values. (This corresponds to the full list search
419 >     * that would be necessary if all elements were non-Comparable and
420 >     * had tied hashes.)  The red-black balancing code is updated from
421 >     * pre-jdk-collections
422 >     * (http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java)
423 >     * based in turn on Cormen, Leiserson, and Rivest "Introduction to
424 >     * Algorithms" (CLR).
425 >     *
426 >     * TreeBins also require an additional locking mechanism.  While
427 >     * list traversal is always possible by readers even during
428 >     * updates, tree traversal is not, mainly beause of tree-rotations
429 >     * that may change the root node and/or its linkages.  TreeBins
430 >     * include a simple read-write lock mechanism parasitic on the
431 >     * main bin-synchronization strategy: Structural adjustments
432 >     * associated with an insertion or removal are already bin-locked
433 >     * (and so cannot conflict with other writers) but must wait for
434 >     * ongoing readers to finish. Since there can be only one such
435 >     * waiter, we use a simple scheme using a single "waiter" field to
436 >     * block writers.  However, readers need never block.  If the root
437 >     * lock is held, they proceed along the slow traversal path (via
438 >     * next-pointers) until the lock becomes available or the list is
439 >     * exhausted, whichever comes first. These cases are not fast, but
440 >     * maximize aggregate expected throughput.
441       *
442       * Maintaining API and serialization compatibility with previous
443       * versions of this class introduces several oddities. Mainly: We
# Line 374 | Line 447 | public class ConcurrentHashMap<K, V>
447       * time that we can guarantee to honor it.) We also declare an
448       * unused "Segment" class that is instantiated in minimal form
449       * only when serializing.
450 +     *
451 +     * This file is organized to make things a little easier to follow
452 +     * while reading than they might otherwise: First the main static
453 +     * declarations and utilities, then fields, then main public
454 +     * methods (with a few factorings of multiple public methods into
455 +     * internal ones), then sizing methods, trees, traversers, and
456 +     * bulk operations.
457       */
458  
459      /* ---------------- Constants -------------- */
# Line 415 | Line 495 | public class ConcurrentHashMap<K, V>
495      private static final float LOAD_FACTOR = 0.75f;
496  
497      /**
418     * The buffer size for skipped bins during transfers. The
419     * value is arbitrary but should be large enough to avoid
420     * most locking stalls during resizes.
421     */
422    private static final int TRANSFER_BUFFER_SIZE = 32;
423
424    /**
498       * The bin count threshold for using a tree rather than list for a
499 <     * bin.  The value reflects the approximate break-even point for
500 <     * using tree-based operations.
501 <     */
502 <    private static final int TREE_THRESHOLD = 8;
503 <
431 <    /*
432 <     * Encodings for special uses of Node hash fields. See above for
433 <     * explanation.
499 >     * bin.  Bins are converted to trees when adding an element to a
500 >     * bin with at least this many nodes. The value must be greater
501 >     * than 2, and should be at least 8 to mesh with assumptions in
502 >     * tree removal about conversion back to plain bins upon
503 >     * shrinkage.
504       */
505 <    static final int MOVED     = 0x80000000; // hash field for forwarding nodes
436 <    static final int LOCKED    = 0x40000000; // set/tested only as a bit
437 <    static final int WAITING   = 0xc0000000; // both bits set/tested together
438 <    static final int HASH_BITS = 0x3fffffff; // usable bits of normal node hash
439 <
440 <    /* ---------------- Fields -------------- */
505 >    static final int TREEIFY_THRESHOLD = 8;
506  
507      /**
508 <     * The array of bins. Lazily initialized upon first insertion.
509 <     * Size is always a power of two. Accessed directly by iterators.
508 >     * The bin count threshold for untreeifying a (split) bin during a
509 >     * resize operation. Should be less than TREEIFY_THRESHOLD, and at
510 >     * most 6 to mesh with shrinkage detection under removal.
511       */
512 <    transient volatile Node[] table;
512 >    static final int UNTREEIFY_THRESHOLD = 6;
513  
514      /**
515 <     * The counter maintaining number of elements.
515 >     * The smallest table capacity for which bins may be treeified.
516 >     * (Otherwise the table is resized if too many nodes in a bin.)
517 >     * The value should be at least 4 * TREEIFY_THRESHOLD to avoid
518 >     * conflicts between resizing and treeification thresholds.
519       */
520 <    private transient final LongAdder counter;
520 >    static final int MIN_TREEIFY_CAPACITY = 64;
521  
522      /**
523 <     * Table initialization and resizing control.  When negative, the
524 <     * table is being initialized or resized. Otherwise, when table is
525 <     * null, holds the initial table size to use upon creation, or 0
526 <     * for default. After initialization, holds the next element count
527 <     * value upon which to resize the table.
523 >     * Minimum number of rebinnings per transfer step. Ranges are
524 >     * subdivided to allow multiple resizer threads.  This value
525 >     * serves as a lower bound to avoid resizers encountering
526 >     * excessive memory contention.  The value should be at least
527 >     * DEFAULT_CAPACITY.
528       */
529 <    private transient volatile int sizeCtl;
461 <
462 <    // views
463 <    private transient KeySet<K,V> keySet;
464 <    private transient Values<K,V> values;
465 <    private transient EntrySet<K,V> entrySet;
466 <
467 <    /** For serialization compatibility. Null unless serialized; see below */
468 <    private Segment<K,V>[] segments;
469 <
470 <    /* ---------------- Table element access -------------- */
529 >    private static final int MIN_TRANSFER_STRIDE = 16;
530  
531      /*
532 <     * Volatile access methods are used for table elements as well as
474 <     * elements of in-progress next table while resizing.  Uses are
475 <     * null checked by callers, and implicitly bounds-checked, relying
476 <     * on the invariants that tab arrays have non-zero size, and all
477 <     * indices are masked with (tab.length - 1) which is never
478 <     * negative and always less than length. Note that, to be correct
479 <     * wrt arbitrary concurrency errors by users, bounds checks must
480 <     * operate on local variables, which accounts for some odd-looking
481 <     * inline assignments below.
532 >     * Encodings for Node hash fields. See above for explanation.
533       */
534 <
535 <    static final Node tabAt(Node[] tab, int i) { // used by Iter
536 <        return (Node)UNSAFE.getObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE);
537 <    }
538 <
539 <    private static final boolean casTabAt(Node[] tab, int i, Node c, Node v) {
540 <        return UNSAFE.compareAndSwapObject(tab, ((long)i<<ASHIFT)+ABASE, c, v);
541 <    }
542 <
543 <    private static final void setTabAt(Node[] tab, int i, Node v) {
544 <        UNSAFE.putObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE, v);
545 <    }
534 >    static final int MOVED     = 0x8fffffff; // (-1) hash for forwarding nodes
535 >    static final int TREEBIN   = 0x80000000; // hash for heads of treea
536 >    static final int RESERVED  = 0x80000001; // hash for transient reservations
537 >    static final int HASH_BITS = 0x7fffffff; // usable bits of normal node hash
538 >
539 >    /** Number of CPUS, to place bounds on some sizings */
540 >    static final int NCPU = Runtime.getRuntime().availableProcessors();
541 >
542 >    /** For serialization compatibility. */
543 >    private static final ObjectStreamField[] serialPersistentFields = {
544 >        new ObjectStreamField("segments", Segment[].class),
545 >        new ObjectStreamField("segmentMask", Integer.TYPE),
546 >        new ObjectStreamField("segmentShift", Integer.TYPE)
547 >    };
548  
549      /* ---------------- Nodes -------------- */
550  
551      /**
552 <     * Key-value entry. Note that this is never exported out as a
553 <     * user-visible Map.Entry (see MapEntry below). Nodes with a hash
554 <     * field of MOVED are special, and do not contain user keys or
555 <     * values.  Otherwise, keys are never null, and null val fields
556 <     * indicate that a node is in the process of being deleted or
557 <     * created. For purposes of read-only access, a key may be read
558 <     * before a val, but can only be used after checking val to be
559 <     * non-null.
560 <     */
561 <    static class Node {
562 <        volatile int hash;
563 <        final Object key;
511 <        volatile Object val;
512 <        volatile Node next;
552 >     * Key-value entry.  This class is never exported out as a
553 >     * user-mutable Map.Entry (i.e., one supporting setValue; see
554 >     * MapEntry below), but can be used for read-only traversals used
555 >     * in bulk tasks.  Subclasses of Node with a negativehash field
556 >     * are special, and contain null keys and values (but are never
557 >     * exported).  Otherwise, keys and vals are never null.
558 >     */
559 >    static class Node<K,V> implements Map.Entry<K,V> {
560 >        final int hash;
561 >        final K key;
562 >        volatile V val;
563 >        Node<K,V> next;
564  
565 <        Node(int hash, Object key, Object val, Node next) {
565 >        Node(int hash, K key, V val, Node<K,V> next) {
566              this.hash = hash;
567              this.key = key;
568              this.val = val;
569              this.next = next;
570          }
571  
572 <        /** CompareAndSet the hash field */
573 <        final boolean casHash(int cmp, int val) {
574 <            return UNSAFE.compareAndSwapInt(this, hashOffset, cmp, val);
575 <        }
576 <
577 <        /** The number of spins before blocking for a lock */
527 <        static final int MAX_SPINS =
528 <            Runtime.getRuntime().availableProcessors() > 1 ? 64 : 1;
529 <
530 <        /**
531 <         * Spins a while if LOCKED bit set and this node is the first
532 <         * of its bin, and then sets WAITING bits on hash field and
533 <         * blocks (once) if they are still set.  It is OK for this
534 <         * method to return even if lock is not available upon exit,
535 <         * which enables these simple single-wait mechanics.
536 <         *
537 <         * The corresponding signalling operation is performed within
538 <         * callers: Upon detecting that WAITING has been set when
539 <         * unlocking lock (via a failed CAS from non-waiting LOCKED
540 <         * state), unlockers acquire the sync lock and perform a
541 <         * notifyAll.
542 <         *
543 <         * The initial sanity check on tab and bounds is not currently
544 <         * necessary in the only usages of this method, but enables
545 <         * use in other future contexts.
546 <         */
547 <        final void tryAwaitLock(Node[] tab, int i) {
548 <            if (tab != null && i >= 0 && i < tab.length) { // sanity check
549 <                int r = ThreadLocalRandom.current().nextInt(); // randomize spins
550 <                int spins = MAX_SPINS, h;
551 <                while (tabAt(tab, i) == this && ((h = hash) & LOCKED) != 0) {
552 <                    if (spins >= 0) {
553 <                        r ^= r << 1; r ^= r >>> 3; r ^= r << 10; // xorshift
554 <                        if (r >= 0 && --spins == 0)
555 <                            Thread.yield();  // yield before block
556 <                    }
557 <                    else if (casHash(h, h | WAITING)) {
558 <                        synchronized (this) {
559 <                            if (tabAt(tab, i) == this &&
560 <                                (hash & WAITING) == WAITING) {
561 <                                try {
562 <                                    wait();
563 <                                } catch (InterruptedException ie) {
564 <                                    Thread.currentThread().interrupt();
565 <                                }
566 <                            }
567 <                            else
568 <                                notifyAll(); // possibly won race vs signaller
569 <                        }
570 <                        break;
571 <                    }
572 <                }
573 <            }
574 <        }
575 <
576 <        // Unsafe mechanics for casHash
577 <        private static final sun.misc.Unsafe UNSAFE;
578 <        private static final long hashOffset;
579 <
580 <        static {
581 <            try {
582 <                UNSAFE = sun.misc.Unsafe.getUnsafe();
583 <                Class<?> k = Node.class;
584 <                hashOffset = UNSAFE.objectFieldOffset
585 <                    (k.getDeclaredField("hash"));
586 <            } catch (Exception e) {
587 <                throw new Error(e);
588 <            }
589 <        }
590 <    }
591 <
592 <    /* ---------------- TreeBins -------------- */
593 <
594 <    /**
595 <     * Nodes for use in TreeBins
596 <     */
597 <    static final class TreeNode extends Node {
598 <        TreeNode parent;  // red-black tree links
599 <        TreeNode left;
600 <        TreeNode right;
601 <        TreeNode prev;    // needed to unlink next upon deletion
602 <        boolean red;
603 <
604 <        TreeNode(int hash, Object key, Object val, Node next, TreeNode parent) {
605 <            super(hash, key, val, next);
606 <            this.parent = parent;
607 <        }
608 <    }
609 <
610 <    /**
611 <     * A specialized form of red-black tree for use in bins
612 <     * whose size exceeds a threshold.
613 <     *
614 <     * TreeBins use a special form of comparison for search and
615 <     * related operations (which is the main reason we cannot use
616 <     * existing collections such as TreeMaps). TreeBins contain
617 <     * Comparable elements, but may contain others, as well as
618 <     * elements that are Comparable but not necessarily Comparable<T>
619 <     * for the same T, so we cannot invoke compareTo among them. To
620 <     * handle this, the tree is ordered primarily by hash value, then
621 <     * by getClass().getName() order, and then by Comparator order
622 <     * among elements of the same class.  On lookup at a node, if
623 <     * elements are not comparable or compare as 0, both left and
624 <     * right children may need to be searched in the case of tied hash
625 <     * values. (This corresponds to the full list search that would be
626 <     * necessary if all elements were non-Comparable and had tied
627 <     * hashes.)  The red-black balancing code is updated from
628 <     * pre-jdk-collections
629 <     * (http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java)
630 <     * based in turn on Cormen, Leiserson, and Rivest "Introduction to
631 <     * Algorithms" (CLR).
632 <     *
633 <     * TreeBins also maintain a separate locking discipline than
634 <     * regular bins. Because they are forwarded via special MOVED
635 <     * nodes at bin heads (which can never change once established),
636 <     * we cannot use those nodes as locks. Instead, TreeBin
637 <     * extends AbstractQueuedSynchronizer to support a simple form of
638 <     * read-write lock. For update operations and table validation,
639 <     * the exclusive form of lock behaves in the same way as bin-head
640 <     * locks. However, lookups use shared read-lock mechanics to allow
641 <     * multiple readers in the absence of writers.  Additionally,
642 <     * these lookups do not ever block: While the lock is not
643 <     * available, they proceed along the slow traversal path (via
644 <     * next-pointers) until the lock becomes available or the list is
645 <     * exhausted, whichever comes first. (These cases are not fast,
646 <     * but maximize aggregate expected throughput.)  The AQS mechanics
647 <     * for doing this are straightforward.  The lock state is held as
648 <     * AQS getState().  Read counts are negative; the write count (1)
649 <     * is positive.  There are no signalling preferences among readers
650 <     * and writers. Since we don't need to export full Lock API, we
651 <     * just override the minimal AQS methods and use them directly.
652 <     */
653 <    static final class TreeBin extends AbstractQueuedSynchronizer {
654 <        private static final long serialVersionUID = 2249069246763182397L;
655 <        transient TreeNode root;  // root of tree
656 <        transient TreeNode first; // head of next-pointer list
657 <
658 <        /* AQS overrides */
659 <        public final boolean isHeldExclusively() { return getState() > 0; }
660 <        public final boolean tryAcquire(int ignore) {
661 <            if (compareAndSetState(0, 1)) {
662 <                setExclusiveOwnerThread(Thread.currentThread());
663 <                return true;
664 <            }
665 <            return false;
666 <        }
667 <        public final boolean tryRelease(int ignore) {
668 <            setExclusiveOwnerThread(null);
669 <            setState(0);
670 <            return true;
671 <        }
672 <        public final int tryAcquireShared(int ignore) {
673 <            for (int c;;) {
674 <                if ((c = getState()) > 0)
675 <                    return -1;
676 <                if (compareAndSetState(c, c -1))
677 <                    return 1;
678 <            }
679 <        }
680 <        public final boolean tryReleaseShared(int ignore) {
681 <            int c;
682 <            do {} while (!compareAndSetState(c = getState(), c + 1));
683 <            return c == -1;
684 <        }
685 <
686 <        /** From CLR */
687 <        private void rotateLeft(TreeNode p) {
688 <            if (p != null) {
689 <                TreeNode r = p.right, pp, rl;
690 <                if ((rl = p.right = r.left) != null)
691 <                    rl.parent = p;
692 <                if ((pp = r.parent = p.parent) == null)
693 <                    root = r;
694 <                else if (pp.left == p)
695 <                    pp.left = r;
696 <                else
697 <                    pp.right = r;
698 <                r.left = p;
699 <                p.parent = r;
700 <            }
701 <        }
702 <
703 <        /** From CLR */
704 <        private void rotateRight(TreeNode p) {
705 <            if (p != null) {
706 <                TreeNode l = p.left, pp, lr;
707 <                if ((lr = p.left = l.right) != null)
708 <                    lr.parent = p;
709 <                if ((pp = l.parent = p.parent) == null)
710 <                    root = l;
711 <                else if (pp.right == p)
712 <                    pp.right = l;
713 <                else
714 <                    pp.left = l;
715 <                l.right = p;
716 <                p.parent = l;
717 <            }
718 <        }
719 <
720 <        /**
721 <         * Returns the TreeNode (or null if not found) for the given key
722 <         * starting at given root.
723 <         */
724 <        @SuppressWarnings("unchecked") final TreeNode getTreeNode
725 <            (int h, Object k, TreeNode p) {
726 <            Class<?> c = k.getClass();
727 <            while (p != null) {
728 <                int dir, ph;  Object pk; Class<?> pc;
729 <                if ((ph = p.hash) == h) {
730 <                    if ((pk = p.key) == k || k.equals(pk))
731 <                        return p;
732 <                    if (c != (pc = pk.getClass()) ||
733 <                        !(k instanceof Comparable) ||
734 <                        (dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) {
735 <                        dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName());
736 <                        TreeNode r = null, s = null, pl, pr;
737 <                        if (dir >= 0) {
738 <                            if ((pl = p.left) != null && h <= pl.hash)
739 <                                s = pl;
740 <                        }
741 <                        else if ((pr = p.right) != null && h >= pr.hash)
742 <                            s = pr;
743 <                        if (s != null && (r = getTreeNode(h, k, s)) != null)
744 <                            return r;
745 <                    }
746 <                }
747 <                else
748 <                    dir = (h < ph) ? -1 : 1;
749 <                p = (dir > 0) ? p.right : p.left;
750 <            }
751 <            return null;
572 >        public final K getKey()       { return key; }
573 >        public final V getValue()     { return val; }
574 >        public final int hashCode()   { return key.hashCode() ^ val.hashCode(); }
575 >        public final String toString(){ return key + "=" + val; }
576 >        public final V setValue(V value) {
577 >            throw new UnsupportedOperationException();
578          }
579  
580 <        /**
581 <         * Wrapper for getTreeNode used by CHM.get. Tries to obtain
582 <         * read-lock to call getTreeNode, but during failure to get
583 <         * lock, searches along next links.
584 <         */
585 <        final Object getValue(int h, Object k) {
586 <            Node r = null;
761 <            int c = getState(); // Must read lock state first
762 <            for (Node e = first; e != null; e = e.next) {
763 <                if (c <= 0 && compareAndSetState(c, c - 1)) {
764 <                    try {
765 <                        r = getTreeNode(h, k, root);
766 <                    } finally {
767 <                        releaseShared(0);
768 <                    }
769 <                    break;
770 <                }
771 <                else if ((e.hash & HASH_BITS) == h && k.equals(e.key)) {
772 <                    r = e;
773 <                    break;
774 <                }
775 <                else
776 <                    c = getState();
777 <            }
778 <            return r == null ? null : r.val;
580 >        public final boolean equals(Object o) {
581 >            Object k, v, u; Map.Entry<?,?> e;
582 >            return ((o instanceof Map.Entry) &&
583 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
584 >                    (v = e.getValue()) != null &&
585 >                    (k == key || k.equals(key)) &&
586 >                    (v == (u = val) || v.equals(u)));
587          }
588  
589          /**
590 <         * Finds or adds a node.
783 <         * @return null if added
590 >         * Virtualized support for map.get(); overridden in subclasses.
591           */
592 <        @SuppressWarnings("unchecked") final TreeNode putTreeNode
593 <            (int h, Object k, Object v) {
594 <            Class<?> c = k.getClass();
595 <            TreeNode pp = root, p = null;
596 <            int dir = 0;
597 <            while (pp != null) { // find existing node or leaf to insert at
598 <                int ph;  Object pk; Class<?> pc;
599 <                p = pp;
600 <                if ((ph = p.hash) == h) {
794 <                    if ((pk = p.key) == k || k.equals(pk))
795 <                        return p;
796 <                    if (c != (pc = pk.getClass()) ||
797 <                        !(k instanceof Comparable) ||
798 <                        (dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) {
799 <                        dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName());
800 <                        TreeNode r = null, s = null, pl, pr;
801 <                        if (dir >= 0) {
802 <                            if ((pl = p.left) != null && h <= pl.hash)
803 <                                s = pl;
804 <                        }
805 <                        else if ((pr = p.right) != null && h >= pr.hash)
806 <                            s = pr;
807 <                        if (s != null && (r = getTreeNode(h, k, s)) != null)
808 <                            return r;
809 <                    }
810 <                }
811 <                else
812 <                    dir = (h < ph) ? -1 : 1;
813 <                pp = (dir > 0) ? p.right : p.left;
814 <            }
815 <
816 <            TreeNode f = first;
817 <            TreeNode x = first = new TreeNode(h, k, v, f, p);
818 <            if (p == null)
819 <                root = x;
820 <            else { // attach and rebalance; adapted from CLR
821 <                TreeNode xp, xpp;
822 <                if (f != null)
823 <                    f.prev = x;
824 <                if (dir <= 0)
825 <                    p.left = x;
826 <                else
827 <                    p.right = x;
828 <                x.red = true;
829 <                while (x != null && (xp = x.parent) != null && xp.red &&
830 <                       (xpp = xp.parent) != null) {
831 <                    TreeNode xppl = xpp.left;
832 <                    if (xp == xppl) {
833 <                        TreeNode y = xpp.right;
834 <                        if (y != null && y.red) {
835 <                            y.red = false;
836 <                            xp.red = false;
837 <                            xpp.red = true;
838 <                            x = xpp;
839 <                        }
840 <                        else {
841 <                            if (x == xp.right) {
842 <                                rotateLeft(x = xp);
843 <                                xpp = (xp = x.parent) == null ? null : xp.parent;
844 <                            }
845 <                            if (xp != null) {
846 <                                xp.red = false;
847 <                                if (xpp != null) {
848 <                                    xpp.red = true;
849 <                                    rotateRight(xpp);
850 <                                }
851 <                            }
852 <                        }
853 <                    }
854 <                    else {
855 <                        TreeNode y = xppl;
856 <                        if (y != null && y.red) {
857 <                            y.red = false;
858 <                            xp.red = false;
859 <                            xpp.red = true;
860 <                            x = xpp;
861 <                        }
862 <                        else {
863 <                            if (x == xp.left) {
864 <                                rotateRight(x = xp);
865 <                                xpp = (xp = x.parent) == null ? null : xp.parent;
866 <                            }
867 <                            if (xp != null) {
868 <                                xp.red = false;
869 <                                if (xpp != null) {
870 <                                    xpp.red = true;
871 <                                    rotateLeft(xpp);
872 <                                }
873 <                            }
874 <                        }
875 <                    }
876 <                }
877 <                TreeNode r = root;
878 <                if (r != null && r.red)
879 <                    r.red = false;
592 >        Node<K,V> find(int h, Object k) {
593 >            Node<K,V> e = this;
594 >            if (k != null) {
595 >                do {
596 >                    K ek;
597 >                    if (e.hash == h &&
598 >                        ((ek = e.key) == k || (ek != null && k.equals(ek))))
599 >                        return e;
600 >                } while ((e = e.next) != null);
601              }
602              return null;
603          }
883
884        /**
885         * Removes the given node, that must be present before this
886         * call.  This is messier than typical red-black deletion code
887         * because we cannot swap the contents of an interior node
888         * with a leaf successor that is pinned by "next" pointers
889         * that are accessible independently of lock. So instead we
890         * swap the tree linkages.
891         */
892        final void deleteTreeNode(TreeNode p) {
893            TreeNode next = (TreeNode)p.next; // unlink traversal pointers
894            TreeNode pred = p.prev;
895            if (pred == null)
896                first = next;
897            else
898                pred.next = next;
899            if (next != null)
900                next.prev = pred;
901            TreeNode replacement;
902            TreeNode pl = p.left;
903            TreeNode pr = p.right;
904            if (pl != null && pr != null) {
905                TreeNode s = pr, sl;
906                while ((sl = s.left) != null) // find successor
907                    s = sl;
908                boolean c = s.red; s.red = p.red; p.red = c; // swap colors
909                TreeNode sr = s.right;
910                TreeNode pp = p.parent;
911                if (s == pr) { // p was s's direct parent
912                    p.parent = s;
913                    s.right = p;
914                }
915                else {
916                    TreeNode sp = s.parent;
917                    if ((p.parent = sp) != null) {
918                        if (s == sp.left)
919                            sp.left = p;
920                        else
921                            sp.right = p;
922                    }
923                    if ((s.right = pr) != null)
924                        pr.parent = s;
925                }
926                p.left = null;
927                if ((p.right = sr) != null)
928                    sr.parent = p;
929                if ((s.left = pl) != null)
930                    pl.parent = s;
931                if ((s.parent = pp) == null)
932                    root = s;
933                else if (p == pp.left)
934                    pp.left = s;
935                else
936                    pp.right = s;
937                replacement = sr;
938            }
939            else
940                replacement = (pl != null) ? pl : pr;
941            TreeNode pp = p.parent;
942            if (replacement == null) {
943                if (pp == null) {
944                    root = null;
945                    return;
946                }
947                replacement = p;
948            }
949            else {
950                replacement.parent = pp;
951                if (pp == null)
952                    root = replacement;
953                else if (p == pp.left)
954                    pp.left = replacement;
955                else
956                    pp.right = replacement;
957                p.left = p.right = p.parent = null;
958            }
959            if (!p.red) { // rebalance, from CLR
960                TreeNode x = replacement;
961                while (x != null) {
962                    TreeNode xp, xpl;
963                    if (x.red || (xp = x.parent) == null) {
964                        x.red = false;
965                        break;
966                    }
967                    if (x == (xpl = xp.left)) {
968                        TreeNode sib = xp.right;
969                        if (sib != null && sib.red) {
970                            sib.red = false;
971                            xp.red = true;
972                            rotateLeft(xp);
973                            sib = (xp = x.parent) == null ? null : xp.right;
974                        }
975                        if (sib == null)
976                            x = xp;
977                        else {
978                            TreeNode sl = sib.left, sr = sib.right;
979                            if ((sr == null || !sr.red) &&
980                                (sl == null || !sl.red)) {
981                                sib.red = true;
982                                x = xp;
983                            }
984                            else {
985                                if (sr == null || !sr.red) {
986                                    if (sl != null)
987                                        sl.red = false;
988                                    sib.red = true;
989                                    rotateRight(sib);
990                                    sib = (xp = x.parent) == null ? null : xp.right;
991                                }
992                                if (sib != null) {
993                                    sib.red = (xp == null) ? false : xp.red;
994                                    if ((sr = sib.right) != null)
995                                        sr.red = false;
996                                }
997                                if (xp != null) {
998                                    xp.red = false;
999                                    rotateLeft(xp);
1000                                }
1001                                x = root;
1002                            }
1003                        }
1004                    }
1005                    else { // symmetric
1006                        TreeNode sib = xpl;
1007                        if (sib != null && sib.red) {
1008                            sib.red = false;
1009                            xp.red = true;
1010                            rotateRight(xp);
1011                            sib = (xp = x.parent) == null ? null : xp.left;
1012                        }
1013                        if (sib == null)
1014                            x = xp;
1015                        else {
1016                            TreeNode sl = sib.left, sr = sib.right;
1017                            if ((sl == null || !sl.red) &&
1018                                (sr == null || !sr.red)) {
1019                                sib.red = true;
1020                                x = xp;
1021                            }
1022                            else {
1023                                if (sl == null || !sl.red) {
1024                                    if (sr != null)
1025                                        sr.red = false;
1026                                    sib.red = true;
1027                                    rotateLeft(sib);
1028                                    sib = (xp = x.parent) == null ? null : xp.left;
1029                                }
1030                                if (sib != null) {
1031                                    sib.red = (xp == null) ? false : xp.red;
1032                                    if ((sl = sib.left) != null)
1033                                        sl.red = false;
1034                                }
1035                                if (xp != null) {
1036                                    xp.red = false;
1037                                    rotateRight(xp);
1038                                }
1039                                x = root;
1040                            }
1041                        }
1042                    }
1043                }
1044            }
1045            if (p == replacement && (pp = p.parent) != null) {
1046                if (p == pp.left) // detach pointers
1047                    pp.left = null;
1048                else if (p == pp.right)
1049                    pp.right = null;
1050                p.parent = null;
1051            }
1052        }
604      }
605  
606 <    /* ---------------- Collision reduction methods -------------- */
606 >    /* ---------------- Static utilities -------------- */
607  
608      /**
609 <     * Spreads higher bits to lower, and also forces top 2 bits to 0.
610 <     * Because the table uses power-of-two masking, sets of hashes
611 <     * that vary only in bits above the current mask will always
612 <     * collide. (Among known examples are sets of Float keys holding
613 <     * consecutive whole numbers in small tables.)  To counter this,
614 <     * we apply a transform that spreads the impact of higher bits
609 >     * Spreads (XORs) higher bits of hash to lower and also forces top
610 >     * bit to 0. Because the table uses power-of-two masking, sets of
611 >     * hashes that vary only in bits above the current mask will
612 >     * always collide. (Among known examples are sets of Float keys
613 >     * holding consecutive whole numbers in small tables.)  So we
614 >     * apply a transform that spreads the impact of higher bits
615       * downward. There is a tradeoff between speed, utility, and
616       * quality of bit-spreading. Because many common sets of hashes
617 <     * are already reasonably distributed across bits (so don't benefit
618 <     * from spreading), and because we use trees to handle large sets
619 <     * of collisions in bins, we don't need excessively high quality.
620 <     */
621 <    private static final int spread(int h) {
622 <        h ^= (h >>> 18) ^ (h >>> 12);
1072 <        return (h ^ (h >>> 10)) & HASH_BITS;
1073 <    }
1074 <
1075 <    /**
1076 <     * Replaces a list bin with a tree bin. Call only when locked.
1077 <     * Fails to replace if the given key is non-comparable or table
1078 <     * is, or needs, resizing.
1079 <     */
1080 <    private final void replaceWithTreeBin(Node[] tab, int index, Object key) {
1081 <        if ((key instanceof Comparable) &&
1082 <            (tab.length >= MAXIMUM_CAPACITY || counter.sum() < (long)sizeCtl)) {
1083 <            TreeBin t = new TreeBin();
1084 <            for (Node e = tabAt(tab, index); e != null; e = e.next)
1085 <                t.putTreeNode(e.hash & HASH_BITS, e.key, e.val);
1086 <            setTabAt(tab, index, new Node(MOVED, t, null, null));
1087 <        }
1088 <    }
1089 <
1090 <    /* ---------------- Internal access and update methods -------------- */
1091 <
1092 <    /** Implementation for get and containsKey */
1093 <    private final Object internalGet(Object k) {
1094 <        int h = spread(k.hashCode());
1095 <        retry: for (Node[] tab = table; tab != null;) {
1096 <            Node e, p; Object ek, ev; int eh;      // locals to read fields once
1097 <            for (e = tabAt(tab, (tab.length - 1) & h); e != null; e = e.next) {
1098 <                if ((eh = e.hash) == MOVED) {
1099 <                    if ((ek = e.key) instanceof TreeBin)  // search TreeBin
1100 <                        return ((TreeBin)ek).getValue(h, k);
1101 <                    else {                        // restart with new table
1102 <                        tab = (Node[])ek;
1103 <                        continue retry;
1104 <                    }
1105 <                }
1106 <                else if ((eh & HASH_BITS) == h && (ev = e.val) != null &&
1107 <                         ((ek = e.key) == k || k.equals(ek)))
1108 <                    return ev;
1109 <            }
1110 <            break;
1111 <        }
1112 <        return null;
1113 <    }
1114 <
1115 <    /**
1116 <     * Implementation for the four public remove/replace methods:
1117 <     * Replaces node value with v, conditional upon match of cv if
1118 <     * non-null.  If resulting value is null, delete.
617 >     * are already reasonably distributed (so don't benefit from
618 >     * spreading), and because we use trees to handle large sets of
619 >     * collisions in bins, we just XOR some shifted bits in the
620 >     * cheapest possible way to reduce systematic lossage, as well as
621 >     * to incorporate impact of the highest bits that would otherwise
622 >     * never be used in index calculations because of table bounds.
623       */
624 <    private final Object internalReplace(Object k, Object v, Object cv) {
625 <        int h = spread(k.hashCode());
1122 <        Object oldVal = null;
1123 <        for (Node[] tab = table;;) {
1124 <            Node f; int i, fh; Object fk;
1125 <            if (tab == null ||
1126 <                (f = tabAt(tab, i = (tab.length - 1) & h)) == null)
1127 <                break;
1128 <            else if ((fh = f.hash) == MOVED) {
1129 <                if ((fk = f.key) instanceof TreeBin) {
1130 <                    TreeBin t = (TreeBin)fk;
1131 <                    boolean validated = false;
1132 <                    boolean deleted = false;
1133 <                    t.acquire(0);
1134 <                    try {
1135 <                        if (tabAt(tab, i) == f) {
1136 <                            validated = true;
1137 <                            TreeNode p = t.getTreeNode(h, k, t.root);
1138 <                            if (p != null) {
1139 <                                Object pv = p.val;
1140 <                                if (cv == null || cv == pv || cv.equals(pv)) {
1141 <                                    oldVal = pv;
1142 <                                    if ((p.val = v) == null) {
1143 <                                        deleted = true;
1144 <                                        t.deleteTreeNode(p);
1145 <                                    }
1146 <                                }
1147 <                            }
1148 <                        }
1149 <                    } finally {
1150 <                        t.release(0);
1151 <                    }
1152 <                    if (validated) {
1153 <                        if (deleted)
1154 <                            counter.add(-1L);
1155 <                        break;
1156 <                    }
1157 <                }
1158 <                else
1159 <                    tab = (Node[])fk;
1160 <            }
1161 <            else if ((fh & HASH_BITS) != h && f.next == null) // precheck
1162 <                break;                          // rules out possible existence
1163 <            else if ((fh & LOCKED) != 0) {
1164 <                checkForResize();               // try resizing if can't get lock
1165 <                f.tryAwaitLock(tab, i);
1166 <            }
1167 <            else if (f.casHash(fh, fh | LOCKED)) {
1168 <                boolean validated = false;
1169 <                boolean deleted = false;
1170 <                try {
1171 <                    if (tabAt(tab, i) == f) {
1172 <                        validated = true;
1173 <                        for (Node e = f, pred = null;;) {
1174 <                            Object ek, ev;
1175 <                            if ((e.hash & HASH_BITS) == h &&
1176 <                                ((ev = e.val) != null) &&
1177 <                                ((ek = e.key) == k || k.equals(ek))) {
1178 <                                if (cv == null || cv == ev || cv.equals(ev)) {
1179 <                                    oldVal = ev;
1180 <                                    if ((e.val = v) == null) {
1181 <                                        deleted = true;
1182 <                                        Node en = e.next;
1183 <                                        if (pred != null)
1184 <                                            pred.next = en;
1185 <                                        else
1186 <                                            setTabAt(tab, i, en);
1187 <                                    }
1188 <                                }
1189 <                                break;
1190 <                            }
1191 <                            pred = e;
1192 <                            if ((e = e.next) == null)
1193 <                                break;
1194 <                        }
1195 <                    }
1196 <                } finally {
1197 <                    if (!f.casHash(fh | LOCKED, fh)) {
1198 <                        f.hash = fh;
1199 <                        synchronized (f) { f.notifyAll(); };
1200 <                    }
1201 <                }
1202 <                if (validated) {
1203 <                    if (deleted)
1204 <                        counter.add(-1L);
1205 <                    break;
1206 <                }
1207 <            }
1208 <        }
1209 <        return oldVal;
1210 <    }
1211 <
1212 <    /*
1213 <     * Internal versions of the six insertion methods, each a
1214 <     * little more complicated than the last. All have
1215 <     * the same basic structure as the first (internalPut):
1216 <     *  1. If table uninitialized, create
1217 <     *  2. If bin empty, try to CAS new node
1218 <     *  3. If bin stale, use new table
1219 <     *  4. if bin converted to TreeBin, validate and relay to TreeBin methods
1220 <     *  5. Lock and validate; if valid, scan and add or update
1221 <     *
1222 <     * The others interweave other checks and/or alternative actions:
1223 <     *  * Plain put checks for and performs resize after insertion.
1224 <     *  * putIfAbsent prescans for mapping without lock (and fails to add
1225 <     *    if present), which also makes pre-emptive resize checks worthwhile.
1226 <     *  * computeIfAbsent extends form used in putIfAbsent with additional
1227 <     *    mechanics to deal with, calls, potential exceptions and null
1228 <     *    returns from function call.
1229 <     *  * compute uses the same function-call mechanics, but without
1230 <     *    the prescans
1231 <     *  * merge acts as putIfAbsent in the absent case, but invokes the
1232 <     *    update function if present
1233 <     *  * putAll attempts to pre-allocate enough table space
1234 <     *    and more lazily performs count updates and checks.
1235 <     *
1236 <     * Someday when details settle down a bit more, it might be worth
1237 <     * some factoring to reduce sprawl.
1238 <     */
1239 <
1240 <    /** Implementation for put */
1241 <    private final Object internalPut(Object k, Object v) {
1242 <        int h = spread(k.hashCode());
1243 <        int count = 0;
1244 <        for (Node[] tab = table;;) {
1245 <            int i; Node f; int fh; Object fk;
1246 <            if (tab == null)
1247 <                tab = initTable();
1248 <            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1249 <                if (casTabAt(tab, i, null, new Node(h, k, v, null)))
1250 <                    break;                   // no lock when adding to empty bin
1251 <            }
1252 <            else if ((fh = f.hash) == MOVED) {
1253 <                if ((fk = f.key) instanceof TreeBin) {
1254 <                    TreeBin t = (TreeBin)fk;
1255 <                    Object oldVal = null;
1256 <                    t.acquire(0);
1257 <                    try {
1258 <                        if (tabAt(tab, i) == f) {
1259 <                            count = 2;
1260 <                            TreeNode p = t.putTreeNode(h, k, v);
1261 <                            if (p != null) {
1262 <                                oldVal = p.val;
1263 <                                p.val = v;
1264 <                            }
1265 <                        }
1266 <                    } finally {
1267 <                        t.release(0);
1268 <                    }
1269 <                    if (count != 0) {
1270 <                        if (oldVal != null)
1271 <                            return oldVal;
1272 <                        break;
1273 <                    }
1274 <                }
1275 <                else
1276 <                    tab = (Node[])fk;
1277 <            }
1278 <            else if ((fh & LOCKED) != 0) {
1279 <                checkForResize();
1280 <                f.tryAwaitLock(tab, i);
1281 <            }
1282 <            else if (f.casHash(fh, fh | LOCKED)) {
1283 <                Object oldVal = null;
1284 <                try {                        // needed in case equals() throws
1285 <                    if (tabAt(tab, i) == f) {
1286 <                        count = 1;
1287 <                        for (Node e = f;; ++count) {
1288 <                            Object ek, ev;
1289 <                            if ((e.hash & HASH_BITS) == h &&
1290 <                                (ev = e.val) != null &&
1291 <                                ((ek = e.key) == k || k.equals(ek))) {
1292 <                                oldVal = ev;
1293 <                                e.val = v;
1294 <                                break;
1295 <                            }
1296 <                            Node last = e;
1297 <                            if ((e = e.next) == null) {
1298 <                                last.next = new Node(h, k, v, null);
1299 <                                if (count >= TREE_THRESHOLD)
1300 <                                    replaceWithTreeBin(tab, i, k);
1301 <                                break;
1302 <                            }
1303 <                        }
1304 <                    }
1305 <                } finally {                  // unlock and signal if needed
1306 <                    if (!f.casHash(fh | LOCKED, fh)) {
1307 <                        f.hash = fh;
1308 <                        synchronized (f) { f.notifyAll(); };
1309 <                    }
1310 <                }
1311 <                if (count != 0) {
1312 <                    if (oldVal != null)
1313 <                        return oldVal;
1314 <                    if (tab.length <= 64)
1315 <                        count = 2;
1316 <                    break;
1317 <                }
1318 <            }
1319 <        }
1320 <        counter.add(1L);
1321 <        if (count > 1)
1322 <            checkForResize();
1323 <        return null;
1324 <    }
1325 <
1326 <    /** Implementation for putIfAbsent */
1327 <    private final Object internalPutIfAbsent(Object k, Object v) {
1328 <        int h = spread(k.hashCode());
1329 <        int count = 0;
1330 <        for (Node[] tab = table;;) {
1331 <            int i; Node f; int fh; Object fk, fv;
1332 <            if (tab == null)
1333 <                tab = initTable();
1334 <            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1335 <                if (casTabAt(tab, i, null, new Node(h, k, v, null)))
1336 <                    break;
1337 <            }
1338 <            else if ((fh = f.hash) == MOVED) {
1339 <                if ((fk = f.key) instanceof TreeBin) {
1340 <                    TreeBin t = (TreeBin)fk;
1341 <                    Object oldVal = null;
1342 <                    t.acquire(0);
1343 <                    try {
1344 <                        if (tabAt(tab, i) == f) {
1345 <                            count = 2;
1346 <                            TreeNode p = t.putTreeNode(h, k, v);
1347 <                            if (p != null)
1348 <                                oldVal = p.val;
1349 <                        }
1350 <                    } finally {
1351 <                        t.release(0);
1352 <                    }
1353 <                    if (count != 0) {
1354 <                        if (oldVal != null)
1355 <                            return oldVal;
1356 <                        break;
1357 <                    }
1358 <                }
1359 <                else
1360 <                    tab = (Node[])fk;
1361 <            }
1362 <            else if ((fh & HASH_BITS) == h && (fv = f.val) != null &&
1363 <                     ((fk = f.key) == k || k.equals(fk)))
1364 <                return fv;
1365 <            else {
1366 <                Node g = f.next;
1367 <                if (g != null) { // at least 2 nodes -- search and maybe resize
1368 <                    for (Node e = g;;) {
1369 <                        Object ek, ev;
1370 <                        if ((e.hash & HASH_BITS) == h && (ev = e.val) != null &&
1371 <                            ((ek = e.key) == k || k.equals(ek)))
1372 <                            return ev;
1373 <                        if ((e = e.next) == null) {
1374 <                            checkForResize();
1375 <                            break;
1376 <                        }
1377 <                    }
1378 <                }
1379 <                if (((fh = f.hash) & LOCKED) != 0) {
1380 <                    checkForResize();
1381 <                    f.tryAwaitLock(tab, i);
1382 <                }
1383 <                else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) {
1384 <                    Object oldVal = null;
1385 <                    try {
1386 <                        if (tabAt(tab, i) == f) {
1387 <                            count = 1;
1388 <                            for (Node e = f;; ++count) {
1389 <                                Object ek, ev;
1390 <                                if ((e.hash & HASH_BITS) == h &&
1391 <                                    (ev = e.val) != null &&
1392 <                                    ((ek = e.key) == k || k.equals(ek))) {
1393 <                                    oldVal = ev;
1394 <                                    break;
1395 <                                }
1396 <                                Node last = e;
1397 <                                if ((e = e.next) == null) {
1398 <                                    last.next = new Node(h, k, v, null);
1399 <                                    if (count >= TREE_THRESHOLD)
1400 <                                        replaceWithTreeBin(tab, i, k);
1401 <                                    break;
1402 <                                }
1403 <                            }
1404 <                        }
1405 <                    } finally {
1406 <                        if (!f.casHash(fh | LOCKED, fh)) {
1407 <                            f.hash = fh;
1408 <                            synchronized (f) { f.notifyAll(); };
1409 <                        }
1410 <                    }
1411 <                    if (count != 0) {
1412 <                        if (oldVal != null)
1413 <                            return oldVal;
1414 <                        if (tab.length <= 64)
1415 <                            count = 2;
1416 <                        break;
1417 <                    }
1418 <                }
1419 <            }
1420 <        }
1421 <        counter.add(1L);
1422 <        if (count > 1)
1423 <            checkForResize();
1424 <        return null;
1425 <    }
1426 <
1427 <    /** Implementation for computeIfAbsent */
1428 <    private final Object internalComputeIfAbsent(K k,
1429 <                                                 Fun<? super K, ?> mf) {
1430 <        int h = spread(k.hashCode());
1431 <        Object val = null;
1432 <        int count = 0;
1433 <        for (Node[] tab = table;;) {
1434 <            Node f; int i, fh; Object fk, fv;
1435 <            if (tab == null)
1436 <                tab = initTable();
1437 <            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1438 <                Node node = new Node(fh = h | LOCKED, k, null, null);
1439 <                if (casTabAt(tab, i, null, node)) {
1440 <                    count = 1;
1441 <                    try {
1442 <                        if ((val = mf.apply(k)) != null)
1443 <                            node.val = val;
1444 <                    } finally {
1445 <                        if (val == null)
1446 <                            setTabAt(tab, i, null);
1447 <                        if (!node.casHash(fh, h)) {
1448 <                            node.hash = h;
1449 <                            synchronized (node) { node.notifyAll(); };
1450 <                        }
1451 <                    }
1452 <                }
1453 <                if (count != 0)
1454 <                    break;
1455 <            }
1456 <            else if ((fh = f.hash) == MOVED) {
1457 <                if ((fk = f.key) instanceof TreeBin) {
1458 <                    TreeBin t = (TreeBin)fk;
1459 <                    boolean added = false;
1460 <                    t.acquire(0);
1461 <                    try {
1462 <                        if (tabAt(tab, i) == f) {
1463 <                            count = 1;
1464 <                            TreeNode p = t.getTreeNode(h, k, t.root);
1465 <                            if (p != null)
1466 <                                val = p.val;
1467 <                            else if ((val = mf.apply(k)) != null) {
1468 <                                added = true;
1469 <                                count = 2;
1470 <                                t.putTreeNode(h, k, val);
1471 <                            }
1472 <                        }
1473 <                    } finally {
1474 <                        t.release(0);
1475 <                    }
1476 <                    if (count != 0) {
1477 <                        if (!added)
1478 <                            return val;
1479 <                        break;
1480 <                    }
1481 <                }
1482 <                else
1483 <                    tab = (Node[])fk;
1484 <            }
1485 <            else if ((fh & HASH_BITS) == h && (fv = f.val) != null &&
1486 <                     ((fk = f.key) == k || k.equals(fk)))
1487 <                return fv;
1488 <            else {
1489 <                Node g = f.next;
1490 <                if (g != null) {
1491 <                    for (Node e = g;;) {
1492 <                        Object ek, ev;
1493 <                        if ((e.hash & HASH_BITS) == h && (ev = e.val) != null &&
1494 <                            ((ek = e.key) == k || k.equals(ek)))
1495 <                            return ev;
1496 <                        if ((e = e.next) == null) {
1497 <                            checkForResize();
1498 <                            break;
1499 <                        }
1500 <                    }
1501 <                }
1502 <                if (((fh = f.hash) & LOCKED) != 0) {
1503 <                    checkForResize();
1504 <                    f.tryAwaitLock(tab, i);
1505 <                }
1506 <                else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) {
1507 <                    boolean added = false;
1508 <                    try {
1509 <                        if (tabAt(tab, i) == f) {
1510 <                            count = 1;
1511 <                            for (Node e = f;; ++count) {
1512 <                                Object ek, ev;
1513 <                                if ((e.hash & HASH_BITS) == h &&
1514 <                                    (ev = e.val) != null &&
1515 <                                    ((ek = e.key) == k || k.equals(ek))) {
1516 <                                    val = ev;
1517 <                                    break;
1518 <                                }
1519 <                                Node last = e;
1520 <                                if ((e = e.next) == null) {
1521 <                                    if ((val = mf.apply(k)) != null) {
1522 <                                        added = true;
1523 <                                        last.next = new Node(h, k, val, null);
1524 <                                        if (count >= TREE_THRESHOLD)
1525 <                                            replaceWithTreeBin(tab, i, k);
1526 <                                    }
1527 <                                    break;
1528 <                                }
1529 <                            }
1530 <                        }
1531 <                    } finally {
1532 <                        if (!f.casHash(fh | LOCKED, fh)) {
1533 <                            f.hash = fh;
1534 <                            synchronized (f) { f.notifyAll(); };
1535 <                        }
1536 <                    }
1537 <                    if (count != 0) {
1538 <                        if (!added)
1539 <                            return val;
1540 <                        if (tab.length <= 64)
1541 <                            count = 2;
1542 <                        break;
1543 <                    }
1544 <                }
1545 <            }
1546 <        }
1547 <        if (val != null) {
1548 <            counter.add(1L);
1549 <            if (count > 1)
1550 <                checkForResize();
1551 <        }
1552 <        return val;
624 >    static final int spread(int h) {
625 >        return (h ^ (h >>> 16)) & HASH_BITS;
626      }
627  
1555    /** Implementation for compute */
1556    @SuppressWarnings("unchecked") private final Object internalCompute
1557        (K k, boolean onlyIfPresent, BiFun<? super K, ? super V, ? extends V> mf) {
1558        int h = spread(k.hashCode());
1559        Object val = null;
1560        int delta = 0;
1561        int count = 0;
1562        for (Node[] tab = table;;) {
1563            Node f; int i, fh; Object fk;
1564            if (tab == null)
1565                tab = initTable();
1566            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1567                if (onlyIfPresent)
1568                    break;
1569                Node node = new Node(fh = h | LOCKED, k, null, null);
1570                if (casTabAt(tab, i, null, node)) {
1571                    try {
1572                        count = 1;
1573                        if ((val = mf.apply(k, null)) != null) {
1574                            node.val = val;
1575                            delta = 1;
1576                        }
1577                    } finally {
1578                        if (delta == 0)
1579                            setTabAt(tab, i, null);
1580                        if (!node.casHash(fh, h)) {
1581                            node.hash = h;
1582                            synchronized (node) { node.notifyAll(); };
1583                        }
1584                    }
1585                }
1586                if (count != 0)
1587                    break;
1588            }
1589            else if ((fh = f.hash) == MOVED) {
1590                if ((fk = f.key) instanceof TreeBin) {
1591                    TreeBin t = (TreeBin)fk;
1592                    t.acquire(0);
1593                    try {
1594                        if (tabAt(tab, i) == f) {
1595                            count = 1;
1596                            TreeNode p = t.getTreeNode(h, k, t.root);
1597                            Object pv = (p == null) ? null : p.val;
1598                            if ((val = mf.apply(k, (V)pv)) != null) {
1599                                if (p != null)
1600                                    p.val = val;
1601                                else {
1602                                    count = 2;
1603                                    delta = 1;
1604                                    t.putTreeNode(h, k, val);
1605                                }
1606                            }
1607                            else if (p != null) {
1608                                delta = -1;
1609                                t.deleteTreeNode(p);
1610                            }
1611                        }
1612                    } finally {
1613                        t.release(0);
1614                    }
1615                    if (count != 0)
1616                        break;
1617                }
1618                else
1619                    tab = (Node[])fk;
1620            }
1621            else if ((fh & LOCKED) != 0) {
1622                checkForResize();
1623                f.tryAwaitLock(tab, i);
1624            }
1625            else if (f.casHash(fh, fh | LOCKED)) {
1626                try {
1627                    if (tabAt(tab, i) == f) {
1628                        count = 1;
1629                        for (Node e = f, pred = null;; ++count) {
1630                            Object ek, ev;
1631                            if ((e.hash & HASH_BITS) == h &&
1632                                (ev = e.val) != null &&
1633                                ((ek = e.key) == k || k.equals(ek))) {
1634                                val = mf.apply(k, (V)ev);
1635                                if (val != null)
1636                                    e.val = val;
1637                                else {
1638                                    delta = -1;
1639                                    Node en = e.next;
1640                                    if (pred != null)
1641                                        pred.next = en;
1642                                    else
1643                                        setTabAt(tab, i, en);
1644                                }
1645                                break;
1646                            }
1647                            pred = e;
1648                            if ((e = e.next) == null) {
1649                                if (!onlyIfPresent && (val = mf.apply(k, null)) != null) {
1650                                    pred.next = new Node(h, k, val, null);
1651                                    delta = 1;
1652                                    if (count >= TREE_THRESHOLD)
1653                                        replaceWithTreeBin(tab, i, k);
1654                                }
1655                                break;
1656                            }
1657                        }
1658                    }
1659                } finally {
1660                    if (!f.casHash(fh | LOCKED, fh)) {
1661                        f.hash = fh;
1662                        synchronized (f) { f.notifyAll(); };
1663                    }
1664                }
1665                if (count != 0) {
1666                    if (tab.length <= 64)
1667                        count = 2;
1668                    break;
1669                }
1670            }
1671        }
1672        if (delta != 0) {
1673            counter.add((long)delta);
1674            if (count > 1)
1675                checkForResize();
1676        }
1677        return val;
1678    }
1679
1680    /** Implementation for merge */
1681    @SuppressWarnings("unchecked") private final Object internalMerge
1682        (K k, V v, BiFun<? super V, ? super V, ? extends V> mf) {
1683        int h = spread(k.hashCode());
1684        Object val = null;
1685        int delta = 0;
1686        int count = 0;
1687        for (Node[] tab = table;;) {
1688            int i; Node f; int fh; Object fk, fv;
1689            if (tab == null)
1690                tab = initTable();
1691            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1692                if (casTabAt(tab, i, null, new Node(h, k, v, null))) {
1693                    delta = 1;
1694                    val = v;
1695                    break;
1696                }
1697            }
1698            else if ((fh = f.hash) == MOVED) {
1699                if ((fk = f.key) instanceof TreeBin) {
1700                    TreeBin t = (TreeBin)fk;
1701                    t.acquire(0);
1702                    try {
1703                        if (tabAt(tab, i) == f) {
1704                            count = 1;
1705                            TreeNode p = t.getTreeNode(h, k, t.root);
1706                            val = (p == null) ? v : mf.apply((V)p.val, v);
1707                            if (val != null) {
1708                                if (p != null)
1709                                    p.val = val;
1710                                else {
1711                                    count = 2;
1712                                    delta = 1;
1713                                    t.putTreeNode(h, k, val);
1714                                }
1715                            }
1716                            else if (p != null) {
1717                                delta = -1;
1718                                t.deleteTreeNode(p);
1719                            }
1720                        }
1721                    } finally {
1722                        t.release(0);
1723                    }
1724                    if (count != 0)
1725                        break;
1726                }
1727                else
1728                    tab = (Node[])fk;
1729            }
1730            else if ((fh & LOCKED) != 0) {
1731                checkForResize();
1732                f.tryAwaitLock(tab, i);
1733            }
1734            else if (f.casHash(fh, fh | LOCKED)) {
1735                try {
1736                    if (tabAt(tab, i) == f) {
1737                        count = 1;
1738                        for (Node e = f, pred = null;; ++count) {
1739                            Object ek, ev;
1740                            if ((e.hash & HASH_BITS) == h &&
1741                                (ev = e.val) != null &&
1742                                ((ek = e.key) == k || k.equals(ek))) {
1743                                val = mf.apply(v, (V)ev);
1744                                if (val != null)
1745                                    e.val = val;
1746                                else {
1747                                    delta = -1;
1748                                    Node en = e.next;
1749                                    if (pred != null)
1750                                        pred.next = en;
1751                                    else
1752                                        setTabAt(tab, i, en);
1753                                }
1754                                break;
1755                            }
1756                            pred = e;
1757                            if ((e = e.next) == null) {
1758                                val = v;
1759                                pred.next = new Node(h, k, val, null);
1760                                delta = 1;
1761                                if (count >= TREE_THRESHOLD)
1762                                    replaceWithTreeBin(tab, i, k);
1763                                break;
1764                            }
1765                        }
1766                    }
1767                } finally {
1768                    if (!f.casHash(fh | LOCKED, fh)) {
1769                        f.hash = fh;
1770                        synchronized (f) { f.notifyAll(); };
1771                    }
1772                }
1773                if (count != 0) {
1774                    if (tab.length <= 64)
1775                        count = 2;
1776                    break;
1777                }
1778            }
1779        }
1780        if (delta != 0) {
1781            counter.add((long)delta);
1782            if (count > 1)
1783                checkForResize();
1784        }
1785        return val;
1786    }
1787
1788    /** Implementation for putAll */
1789    private final void internalPutAll(Map<?, ?> m) {
1790        tryPresize(m.size());
1791        long delta = 0L;     // number of uncommitted additions
1792        boolean npe = false; // to throw exception on exit for nulls
1793        try {                // to clean up counts on other exceptions
1794            for (Map.Entry<?, ?> entry : m.entrySet()) {
1795                Object k, v;
1796                if (entry == null || (k = entry.getKey()) == null ||
1797                    (v = entry.getValue()) == null) {
1798                    npe = true;
1799                    break;
1800                }
1801                int h = spread(k.hashCode());
1802                for (Node[] tab = table;;) {
1803                    int i; Node f; int fh; Object fk;
1804                    if (tab == null)
1805                        tab = initTable();
1806                    else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null){
1807                        if (casTabAt(tab, i, null, new Node(h, k, v, null))) {
1808                            ++delta;
1809                            break;
1810                        }
1811                    }
1812                    else if ((fh = f.hash) == MOVED) {
1813                        if ((fk = f.key) instanceof TreeBin) {
1814                            TreeBin t = (TreeBin)fk;
1815                            boolean validated = false;
1816                            t.acquire(0);
1817                            try {
1818                                if (tabAt(tab, i) == f) {
1819                                    validated = true;
1820                                    TreeNode p = t.getTreeNode(h, k, t.root);
1821                                    if (p != null)
1822                                        p.val = v;
1823                                    else {
1824                                        t.putTreeNode(h, k, v);
1825                                        ++delta;
1826                                    }
1827                                }
1828                            } finally {
1829                                t.release(0);
1830                            }
1831                            if (validated)
1832                                break;
1833                        }
1834                        else
1835                            tab = (Node[])fk;
1836                    }
1837                    else if ((fh & LOCKED) != 0) {
1838                        counter.add(delta);
1839                        delta = 0L;
1840                        checkForResize();
1841                        f.tryAwaitLock(tab, i);
1842                    }
1843                    else if (f.casHash(fh, fh | LOCKED)) {
1844                        int count = 0;
1845                        try {
1846                            if (tabAt(tab, i) == f) {
1847                                count = 1;
1848                                for (Node e = f;; ++count) {
1849                                    Object ek, ev;
1850                                    if ((e.hash & HASH_BITS) == h &&
1851                                        (ev = e.val) != null &&
1852                                        ((ek = e.key) == k || k.equals(ek))) {
1853                                        e.val = v;
1854                                        break;
1855                                    }
1856                                    Node last = e;
1857                                    if ((e = e.next) == null) {
1858                                        ++delta;
1859                                        last.next = new Node(h, k, v, null);
1860                                        if (count >= TREE_THRESHOLD)
1861                                            replaceWithTreeBin(tab, i, k);
1862                                        break;
1863                                    }
1864                                }
1865                            }
1866                        } finally {
1867                            if (!f.casHash(fh | LOCKED, fh)) {
1868                                f.hash = fh;
1869                                synchronized (f) { f.notifyAll(); };
1870                            }
1871                        }
1872                        if (count != 0) {
1873                            if (count > 1) {
1874                                counter.add(delta);
1875                                delta = 0L;
1876                                checkForResize();
1877                            }
1878                            break;
1879                        }
1880                    }
1881                }
1882            }
1883        } finally {
1884            if (delta != 0)
1885                counter.add(delta);
1886        }
1887        if (npe)
1888            throw new NullPointerException();
1889    }
1890
1891    /* ---------------- Table Initialization and Resizing -------------- */
1892
628      /**
629       * Returns a power of two table size for the given desired capacity.
630       * See Hackers Delight, sec 3.2
# Line 1905 | Line 640 | public class ConcurrentHashMap<K, V>
640      }
641  
642      /**
643 <     * Initializes table, using the size recorded in sizeCtl.
643 >     * Returns x's Class if it is of the form "class C implements
644 >     * Comparable<C>", else null.
645       */
646 <    private final Node[] initTable() {
647 <        Node[] tab; int sc;
648 <        while ((tab = table) == null) {
649 <            if ((sc = sizeCtl) < 0)
650 <                Thread.yield(); // lost initialization race; just spin
651 <            else if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
652 <                try {
653 <                    if ((tab = table) == null) {
654 <                        int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
655 <                        tab = table = new Node[n];
656 <                        sc = n - (n >>> 2);
657 <                    }
658 <                } finally {
1923 <                    sizeCtl = sc;
646 >    static Class<?> comparableClassFor(Object x) {
647 >        if (x instanceof Comparable) {
648 >            Class<?> c; Type[] ts, as; Type t; ParameterizedType p;
649 >            if ((c = x.getClass()) == String.class) // bypass checks
650 >                return c;
651 >            if ((ts = c.getGenericInterfaces()) != null) {
652 >                for (int i = 0; i < ts.length; ++i) {
653 >                    if (((t = ts[i]) instanceof ParameterizedType) &&
654 >                        ((p = (ParameterizedType)t).getRawType() ==
655 >                         Comparable.class) &&
656 >                        (as = p.getActualTypeArguments()) != null &&
657 >                        as.length == 1 && as[0] == c) // type arg is c
658 >                        return c;
659                  }
1925                break;
1926            }
1927        }
1928        return tab;
1929    }
1930
1931    /**
1932     * If table is too small and not already resizing, creates next
1933     * table and transfers bins.  Rechecks occupancy after a transfer
1934     * to see if another resize is already needed because resizings
1935     * are lagging additions.
1936     */
1937    private final void checkForResize() {
1938        Node[] tab; int n, sc;
1939        while ((tab = table) != null &&
1940               (n = tab.length) < MAXIMUM_CAPACITY &&
1941               (sc = sizeCtl) >= 0 && counter.sum() >= (long)sc &&
1942               UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1943            try {
1944                if (tab == table) {
1945                    table = rebuild(tab);
1946                    sc = (n << 1) - (n >>> 1);
1947                }
1948            } finally {
1949                sizeCtl = sc;
660              }
661          }
662 +        return null;
663      }
664  
665      /**
666 <     * Tries to presize table to accommodate the given number of elements.
667 <     *
1957 <     * @param size number of elements (doesn't need to be perfectly accurate)
666 >     * Returns k.compareTo(x) if x matches kc (k's screened comparable
667 >     * class), else 0.
668       */
669 <    private final void tryPresize(int size) {
670 <        int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
671 <            tableSizeFor(size + (size >>> 1) + 1);
672 <        int sc;
1963 <        while ((sc = sizeCtl) >= 0) {
1964 <            Node[] tab = table; int n;
1965 <            if (tab == null || (n = tab.length) == 0) {
1966 <                n = (sc > c) ? sc : c;
1967 <                if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1968 <                    try {
1969 <                        if (table == tab) {
1970 <                            table = new Node[n];
1971 <                            sc = n - (n >>> 2);
1972 <                        }
1973 <                    } finally {
1974 <                        sizeCtl = sc;
1975 <                    }
1976 <                }
1977 <            }
1978 <            else if (c <= sc || n >= MAXIMUM_CAPACITY)
1979 <                break;
1980 <            else if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1981 <                try {
1982 <                    if (table == tab) {
1983 <                        table = rebuild(tab);
1984 <                        sc = (n << 1) - (n >>> 1);
1985 <                    }
1986 <                } finally {
1987 <                    sizeCtl = sc;
1988 <                }
1989 <            }
1990 <        }
669 >    @SuppressWarnings({"rawtypes","unchecked"}) // for cast to Comparable
670 >    static int compareComparables(Class<?> kc, Object k, Object x) {
671 >        return (x == null || x.getClass() != kc ? 0 :
672 >                ((Comparable)k).compareTo(x));
673      }
674  
675 +    /* ---------------- Table element access -------------- */
676 +
677      /*
678 <     * Moves and/or copies the nodes in each bin to new table. See
679 <     * above for explanation.
680 <     *
681 <     * @return the new table
682 <     */
683 <    private static final Node[] rebuild(Node[] tab) {
684 <        int n = tab.length;
685 <        Node[] nextTab = new Node[n << 1];
686 <        Node fwd = new Node(MOVED, nextTab, null, null);
687 <        int[] buffer = null;       // holds bins to revisit; null until needed
688 <        Node rev = null;           // reverse forwarder; null until needed
689 <        int nbuffered = 0;         // the number of bins in buffer list
690 <        int bufferIndex = 0;       // buffer index of current buffered bin
691 <        int bin = n - 1;           // current non-buffered bin or -1 if none
692 <
693 <        for (int i = bin;;) {      // start upwards sweep
694 <            int fh; Node f;
695 <            if ((f = tabAt(tab, i)) == null) {
696 <                if (bin >= 0) {    // Unbuffered; no lock needed (or available)
697 <                    if (!casTabAt(tab, i, f, fwd))
698 <                        continue;
699 <                }
2016 <                else {             // transiently use a locked forwarding node
2017 <                    Node g = new Node(MOVED|LOCKED, nextTab, null, null);
2018 <                    if (!casTabAt(tab, i, f, g))
2019 <                        continue;
2020 <                    setTabAt(nextTab, i, null);
2021 <                    setTabAt(nextTab, i + n, null);
2022 <                    setTabAt(tab, i, fwd);
2023 <                    if (!g.casHash(MOVED|LOCKED, MOVED)) {
2024 <                        g.hash = MOVED;
2025 <                        synchronized (g) { g.notifyAll(); }
2026 <                    }
2027 <                }
2028 <            }
2029 <            else if ((fh = f.hash) == MOVED) {
2030 <                Object fk = f.key;
2031 <                if (fk instanceof TreeBin) {
2032 <                    TreeBin t = (TreeBin)fk;
2033 <                    boolean validated = false;
2034 <                    t.acquire(0);
2035 <                    try {
2036 <                        if (tabAt(tab, i) == f) {
2037 <                            validated = true;
2038 <                            splitTreeBin(nextTab, i, t);
2039 <                            setTabAt(tab, i, fwd);
2040 <                        }
2041 <                    } finally {
2042 <                        t.release(0);
2043 <                    }
2044 <                    if (!validated)
2045 <                        continue;
2046 <                }
2047 <            }
2048 <            else if ((fh & LOCKED) == 0 && f.casHash(fh, fh|LOCKED)) {
2049 <                boolean validated = false;
2050 <                try {              // split to lo and hi lists; copying as needed
2051 <                    if (tabAt(tab, i) == f) {
2052 <                        validated = true;
2053 <                        splitBin(nextTab, i, f);
2054 <                        setTabAt(tab, i, fwd);
2055 <                    }
2056 <                } finally {
2057 <                    if (!f.casHash(fh | LOCKED, fh)) {
2058 <                        f.hash = fh;
2059 <                        synchronized (f) { f.notifyAll(); };
2060 <                    }
2061 <                }
2062 <                if (!validated)
2063 <                    continue;
2064 <            }
2065 <            else {
2066 <                if (buffer == null) // initialize buffer for revisits
2067 <                    buffer = new int[TRANSFER_BUFFER_SIZE];
2068 <                if (bin < 0 && bufferIndex > 0) {
2069 <                    int j = buffer[--bufferIndex];
2070 <                    buffer[bufferIndex] = i;
2071 <                    i = j;         // swap with another bin
2072 <                    continue;
2073 <                }
2074 <                if (bin < 0 || nbuffered >= TRANSFER_BUFFER_SIZE) {
2075 <                    f.tryAwaitLock(tab, i);
2076 <                    continue;      // no other options -- block
2077 <                }
2078 <                if (rev == null)   // initialize reverse-forwarder
2079 <                    rev = new Node(MOVED, tab, null, null);
2080 <                if (tabAt(tab, i) != f || (f.hash & LOCKED) == 0)
2081 <                    continue;      // recheck before adding to list
2082 <                buffer[nbuffered++] = i;
2083 <                setTabAt(nextTab, i, rev);     // install place-holders
2084 <                setTabAt(nextTab, i + n, rev);
2085 <            }
2086 <
2087 <            if (bin > 0)
2088 <                i = --bin;
2089 <            else if (buffer != null && nbuffered > 0) {
2090 <                bin = -1;
2091 <                i = buffer[bufferIndex = --nbuffered];
2092 <            }
2093 <            else
2094 <                return nextTab;
2095 <        }
678 >     * Volatile access methods are used for table elements as well as
679 >     * elements of in-progress next table while resizing.  All uses of
680 >     * the tab arguments must be null checked by callers.  All callers
681 >     * also paranoically precheck that tab's length is not zero (or an
682 >     * equivalent check), thus ensuring that any index argument taking
683 >     * the form of a hash value anded with (length - 1) is a valid
684 >     * index.  Note that, to be correct wrt arbitrary concurrency
685 >     * errors by users, these checks must operate on local variables,
686 >     * which accounts for some odd-looking inline assignments below.
687 >     * Note that calls to setTabAt always occur within locked regions,
688 >     * and so do not need full volatile semantics, but still require
689 >     * ordering to maintain concurrent readability.
690 >     */
691 >
692 >    @SuppressWarnings("unchecked")
693 >    static final <K,V> Node<K,V> tabAt(Node<K,V>[] tab, int i) {
694 >        return (Node<K,V>)U.getObjectVolatile(tab, ((long)i << ASHIFT) + ABASE);
695 >    }
696 >
697 >    static final <K,V> boolean casTabAt(Node<K,V>[] tab, int i,
698 >                                        Node<K,V> c, Node<K,V> v) {
699 >        return U.compareAndSwapObject(tab, ((long)i << ASHIFT) + ABASE, c, v);
700      }
701  
702 <    /**
703 <     * Splits a normal bin with list headed by e into lo and hi parts;
2100 <     * installs in given table.
2101 <     */
2102 <    private static void splitBin(Node[] nextTab, int i, Node e) {
2103 <        int bit = nextTab.length >>> 1; // bit to split on
2104 <        int runBit = e.hash & bit;
2105 <        Node lastRun = e, lo = null, hi = null;
2106 <        for (Node p = e.next; p != null; p = p.next) {
2107 <            int b = p.hash & bit;
2108 <            if (b != runBit) {
2109 <                runBit = b;
2110 <                lastRun = p;
2111 <            }
2112 <        }
2113 <        if (runBit == 0)
2114 <            lo = lastRun;
2115 <        else
2116 <            hi = lastRun;
2117 <        for (Node p = e; p != lastRun; p = p.next) {
2118 <            int ph = p.hash & HASH_BITS;
2119 <            Object pk = p.key, pv = p.val;
2120 <            if ((ph & bit) == 0)
2121 <                lo = new Node(ph, pk, pv, lo);
2122 <            else
2123 <                hi = new Node(ph, pk, pv, hi);
2124 <        }
2125 <        setTabAt(nextTab, i, lo);
2126 <        setTabAt(nextTab, i + bit, hi);
702 >    static final <K,V> void setTabAt(Node<K,V>[] tab, int i, Node<K,V> v) {
703 >        U.putOrderedObject(tab, ((long)i << ASHIFT) + ABASE, v);
704      }
705  
706 +    /* ---------------- Fields -------------- */
707 +
708      /**
709 <     * Splits a tree bin into lo and hi parts; installs in given table.
709 >     * The array of bins. Lazily initialized upon first insertion.
710 >     * Size is always a power of two. Accessed directly by iterators.
711       */
712 <    private static void splitTreeBin(Node[] nextTab, int i, TreeBin t) {
2133 <        int bit = nextTab.length >>> 1;
2134 <        TreeBin lt = new TreeBin();
2135 <        TreeBin ht = new TreeBin();
2136 <        int lc = 0, hc = 0;
2137 <        for (Node e = t.first; e != null; e = e.next) {
2138 <            int h = e.hash & HASH_BITS;
2139 <            Object k = e.key, v = e.val;
2140 <            if ((h & bit) == 0) {
2141 <                ++lc;
2142 <                lt.putTreeNode(h, k, v);
2143 <            }
2144 <            else {
2145 <                ++hc;
2146 <                ht.putTreeNode(h, k, v);
2147 <            }
2148 <        }
2149 <        Node ln, hn; // throw away trees if too small
2150 <        if (lc <= (TREE_THRESHOLD >>> 1)) {
2151 <            ln = null;
2152 <            for (Node p = lt.first; p != null; p = p.next)
2153 <                ln = new Node(p.hash, p.key, p.val, ln);
2154 <        }
2155 <        else
2156 <            ln = new Node(MOVED, lt, null, null);
2157 <        setTabAt(nextTab, i, ln);
2158 <        if (hc <= (TREE_THRESHOLD >>> 1)) {
2159 <            hn = null;
2160 <            for (Node p = ht.first; p != null; p = p.next)
2161 <                hn = new Node(p.hash, p.key, p.val, hn);
2162 <        }
2163 <        else
2164 <            hn = new Node(MOVED, ht, null, null);
2165 <        setTabAt(nextTab, i + bit, hn);
2166 <    }
712 >    transient volatile Node<K,V>[] table;
713  
714      /**
715 <     * Implementation for clear. Steps through each bin, removing all
2170 <     * nodes.
715 >     * The next table to use; non-null only while resizing.
716       */
717 <    private final void internalClear() {
2173 <        long delta = 0L; // negative number of deletions
2174 <        int i = 0;
2175 <        Node[] tab = table;
2176 <        while (tab != null && i < tab.length) {
2177 <            int fh; Object fk;
2178 <            Node f = tabAt(tab, i);
2179 <            if (f == null)
2180 <                ++i;
2181 <            else if ((fh = f.hash) == MOVED) {
2182 <                if ((fk = f.key) instanceof TreeBin) {
2183 <                    TreeBin t = (TreeBin)fk;
2184 <                    t.acquire(0);
2185 <                    try {
2186 <                        if (tabAt(tab, i) == f) {
2187 <                            for (Node p = t.first; p != null; p = p.next) {
2188 <                                if (p.val != null) { // (currently always true)
2189 <                                    p.val = null;
2190 <                                    --delta;
2191 <                                }
2192 <                            }
2193 <                            t.first = null;
2194 <                            t.root = null;
2195 <                            ++i;
2196 <                        }
2197 <                    } finally {
2198 <                        t.release(0);
2199 <                    }
2200 <                }
2201 <                else
2202 <                    tab = (Node[])fk;
2203 <            }
2204 <            else if ((fh & LOCKED) != 0) {
2205 <                counter.add(delta); // opportunistically update count
2206 <                delta = 0L;
2207 <                f.tryAwaitLock(tab, i);
2208 <            }
2209 <            else if (f.casHash(fh, fh | LOCKED)) {
2210 <                try {
2211 <                    if (tabAt(tab, i) == f) {
2212 <                        for (Node e = f; e != null; e = e.next) {
2213 <                            if (e.val != null) {  // (currently always true)
2214 <                                e.val = null;
2215 <                                --delta;
2216 <                            }
2217 <                        }
2218 <                        setTabAt(tab, i, null);
2219 <                        ++i;
2220 <                    }
2221 <                } finally {
2222 <                    if (!f.casHash(fh | LOCKED, fh)) {
2223 <                        f.hash = fh;
2224 <                        synchronized (f) { f.notifyAll(); };
2225 <                    }
2226 <                }
2227 <            }
2228 <        }
2229 <        if (delta != 0)
2230 <            counter.add(delta);
2231 <    }
717 >    private transient volatile Node<K,V>[] nextTable;
718  
719 <    /* ----------------Table Traversal -------------- */
719 >    /**
720 >     * Base counter value, used mainly when there is no contention,
721 >     * but also as a fallback during table initialization
722 >     * races. Updated via CAS.
723 >     */
724 >    private transient volatile long baseCount;
725  
726      /**
727 <     * Encapsulates traversal for methods such as containsValue; also
728 <     * serves as a base class for other iterators and bulk tasks.
729 <     *
730 <     * At each step, the iterator snapshots the key ("nextKey") and
731 <     * value ("nextVal") of a valid node (i.e., one that, at point of
732 <     * snapshot, has a non-null user value). Because val fields can
733 <     * change (including to null, indicating deletion), field nextVal
734 <     * might not be accurate at point of use, but still maintains the
2244 <     * weak consistency property of holding a value that was once
2245 <     * valid.
2246 <     *
2247 <     * Internal traversals directly access these fields, as in:
2248 <     * {@code while (it.advance() != null) { process(it.nextKey); }}
2249 <     *
2250 <     * Exported iterators must track whether the iterator has advanced
2251 <     * (in hasNext vs next) (by setting/checking/nulling field
2252 <     * nextVal), and then extract key, value, or key-value pairs as
2253 <     * return values of next().
2254 <     *
2255 <     * The iterator visits once each still-valid node that was
2256 <     * reachable upon iterator construction. It might miss some that
2257 <     * were added to a bin after the bin was visited, which is OK wrt
2258 <     * consistency guarantees. Maintaining this property in the face
2259 <     * of possible ongoing resizes requires a fair amount of
2260 <     * bookkeeping state that is difficult to optimize away amidst
2261 <     * volatile accesses.  Even so, traversal maintains reasonable
2262 <     * throughput.
2263 <     *
2264 <     * Normally, iteration proceeds bin-by-bin traversing lists.
2265 <     * However, if the table has been resized, then all future steps
2266 <     * must traverse both the bin at the current index as well as at
2267 <     * (index + baseSize); and so on for further resizings. To
2268 <     * paranoically cope with potential sharing by users of iterators
2269 <     * across threads, iteration terminates if a bounds checks fails
2270 <     * for a table read.
2271 <     *
2272 <     * This class extends ForkJoinTask to streamline parallel
2273 <     * iteration in bulk operations (see BulkTask). This adds only an
2274 <     * int of space overhead, which is close enough to negligible in
2275 <     * cases where it is not needed to not worry about it.  Because
2276 <     * ForkJoinTask is Serializable, but iterators need not be, we
2277 <     * need to add warning suppressions.
2278 <     */
2279 <    @SuppressWarnings("serial") static class Traverser<K,V,R> extends ForkJoinTask<R> {
2280 <        final ConcurrentHashMap<K, V> map;
2281 <        Node next;           // the next entry to use
2282 <        Node last;           // the last entry used
2283 <        Object nextKey;      // cached key field of next
2284 <        Object nextVal;      // cached val field of next
2285 <        Node[] tab;          // current table; updated if resized
2286 <        int index;           // index of bin to use next
2287 <        int baseIndex;       // current index of initial table
2288 <        int baseLimit;       // index bound for initial table
2289 <        int baseSize;        // initial table size
727 >     * Table initialization and resizing control.  When negative, the
728 >     * table is being initialized or resized: -1 for initialization,
729 >     * else -(1 + the number of active resizing threads).  Otherwise,
730 >     * when table is null, holds the initial table size to use upon
731 >     * creation, or 0 for default. After initialization, holds the
732 >     * next element count value upon which to resize the table.
733 >     */
734 >    private transient volatile int sizeCtl;
735  
736 <        /** Creates iterator for all entries in the table. */
737 <        Traverser(ConcurrentHashMap<K, V> map) {
738 <            this.map = map;
739 <        }
736 >    /**
737 >     * The next table index (plus one) to split while resizing.
738 >     */
739 >    private transient volatile int transferIndex;
740  
741 <        /** Creates iterator for split() methods */
742 <        Traverser(Traverser<K,V,?> it) {
743 <            ConcurrentHashMap<K, V> m; Node[] t;
744 <            if ((m = this.map = it.map) == null)
2300 <                t = null;
2301 <            else if ((t = it.tab) == null && // force parent tab initialization
2302 <                     (t = it.tab = m.table) != null)
2303 <                it.baseLimit = it.baseSize = t.length;
2304 <            this.tab = t;
2305 <            this.baseSize = it.baseSize;
2306 <            it.baseLimit = this.index = this.baseIndex =
2307 <                ((this.baseLimit = it.baseLimit) + it.baseIndex + 1) >>> 1;
2308 <        }
741 >    /**
742 >     * The least available table index to split while resizing.
743 >     */
744 >    private transient volatile int transferOrigin;
745  
746 <        /**
747 <         * Advances next; returns nextVal or null if terminated.
748 <         * See above for explanation.
749 <         */
2314 <        final Object advance() {
2315 <            Node e = last = next;
2316 <            Object ev = null;
2317 <            outer: do {
2318 <                if (e != null)                  // advance past used/skipped node
2319 <                    e = e.next;
2320 <                while (e == null) {             // get to next non-null bin
2321 <                    ConcurrentHashMap<K, V> m;
2322 <                    Node[] t; int b, i, n; Object ek; // checks must use locals
2323 <                    if ((t = tab) != null)
2324 <                        n = t.length;
2325 <                    else if ((m = map) != null && (t = tab = m.table) != null)
2326 <                        n = baseLimit = baseSize = t.length;
2327 <                    else
2328 <                        break outer;
2329 <                    if ((b = baseIndex) >= baseLimit ||
2330 <                        (i = index) < 0 || i >= n)
2331 <                        break outer;
2332 <                    if ((e = tabAt(t, i)) != null && e.hash == MOVED) {
2333 <                        if ((ek = e.key) instanceof TreeBin)
2334 <                            e = ((TreeBin)ek).first;
2335 <                        else {
2336 <                            tab = (Node[])ek;
2337 <                            continue;           // restarts due to null val
2338 <                        }
2339 <                    }                           // visit upper slots if present
2340 <                    index = (i += baseSize) < n ? i : (baseIndex = b + 1);
2341 <                }
2342 <                nextKey = e.key;
2343 <            } while ((ev = e.val) == null);    // skip deleted or special nodes
2344 <            next = e;
2345 <            return nextVal = ev;
2346 <        }
746 >    /**
747 >     * Spinlock (locked via CAS) used when resizing and/or creating CounterCells.
748 >     */
749 >    private transient volatile int cellsBusy;
750  
751 <        public final void remove() {
752 <            if (nextVal == null && last == null)
753 <                advance();
754 <            Node e = last;
2352 <            if (e == null)
2353 <                throw new IllegalStateException();
2354 <            last = null;
2355 <            map.remove(e.key);
2356 <        }
751 >    /**
752 >     * Table of counter cells. When non-null, size is a power of 2.
753 >     */
754 >    private transient volatile CounterCell[] counterCells;
755  
756 <        public final boolean hasNext() {
757 <            return nextVal != null || advance() != null;
758 <        }
756 >    // views
757 >    private transient KeySetView<K,V> keySet;
758 >    private transient ValuesView<K,V> values;
759 >    private transient EntrySetView<K,V> entrySet;
760  
2362        public final boolean hasMoreElements() { return hasNext(); }
2363        public final void setRawResult(Object x) { }
2364        public R getRawResult() { return null; }
2365        public boolean exec() { return true; }
2366    }
761  
762      /* ---------------- Public operations -------------- */
763  
# Line 2371 | Line 765 | public class ConcurrentHashMap<K, V>
765       * Creates a new, empty map with the default initial table size (16).
766       */
767      public ConcurrentHashMap() {
2374        this.counter = new LongAdder();
768      }
769  
770      /**
# Line 2390 | Line 783 | public class ConcurrentHashMap<K, V>
783          int cap = ((initialCapacity >= (MAXIMUM_CAPACITY >>> 1)) ?
784                     MAXIMUM_CAPACITY :
785                     tableSizeFor(initialCapacity + (initialCapacity >>> 1) + 1));
2393        this.counter = new LongAdder();
786          this.sizeCtl = cap;
787      }
788  
# Line 2400 | Line 792 | public class ConcurrentHashMap<K, V>
792       * @param m the map
793       */
794      public ConcurrentHashMap(Map<? extends K, ? extends V> m) {
2403        this.counter = new LongAdder();
795          this.sizeCtl = DEFAULT_CAPACITY;
796 <        internalPutAll(m);
796 >        putAll(m);
797      }
798  
799      /**
# Line 2443 | Line 834 | public class ConcurrentHashMap<K, V>
834       * nonpositive
835       */
836      public ConcurrentHashMap(int initialCapacity,
837 <                               float loadFactor, int concurrencyLevel) {
837 >                             float loadFactor, int concurrencyLevel) {
838          if (!(loadFactor > 0.0f) || initialCapacity < 0 || concurrencyLevel <= 0)
839              throw new IllegalArgumentException();
840          if (initialCapacity < concurrencyLevel)   // Use at least as many bins
# Line 2451 | Line 842 | public class ConcurrentHashMap<K, V>
842          long size = (long)(1.0 + (long)initialCapacity / loadFactor);
843          int cap = (size >= (long)MAXIMUM_CAPACITY) ?
844              MAXIMUM_CAPACITY : tableSizeFor((int)size);
2454        this.counter = new LongAdder();
845          this.sizeCtl = cap;
846      }
847  
848 <    /**
2459 <     * {@inheritDoc}
2460 <     */
2461 <    public boolean isEmpty() {
2462 <        return counter.sum() <= 0L; // ignore transient negative values
2463 <    }
848 >    // Original (since JDK1.2) Map methods
849  
850      /**
851       * {@inheritDoc}
852       */
853      public int size() {
854 <        long n = counter.sum();
854 >        long n = sumCount();
855          return ((n < 0L) ? 0 :
856                  (n > (long)Integer.MAX_VALUE) ? Integer.MAX_VALUE :
857                  (int)n);
858      }
859  
860      /**
861 <     * Returns the number of mappings. This method should be used
2477 <     * instead of {@link #size} because a ConcurrentHashMap may
2478 <     * contain more mappings than can be represented as an int. The
2479 <     * value returned is a snapshot; the actual count may differ if
2480 <     * there are ongoing concurrent insertions or removals.
2481 <     *
2482 <     * @return the number of mappings
861 >     * {@inheritDoc}
862       */
863 <    public long mappingCount() {
864 <        long n = counter.sum();
2486 <        return (n < 0L) ? 0L : n; // ignore transient negative values
863 >    public boolean isEmpty() {
864 >        return sumCount() <= 0L; // ignore transient negative values
865      }
866  
867      /**
# Line 2497 | Line 875 | public class ConcurrentHashMap<K, V>
875       *
876       * @throws NullPointerException if the specified key is null
877       */
878 <    @SuppressWarnings("unchecked") public V get(Object key) {
879 <        if (key == null)
880 <            throw new NullPointerException();
881 <        return (V)internalGet(key);
882 <    }
883 <
884 <    /**
885 <     * Returns the value to which the specified key is mapped,
886 <     * or the given defaultValue if this map contains no mapping for the key.
887 <     *
888 <     * @param key the key
889 <     * @param defaultValue the value to return if this map contains
890 <     * no mapping for the given key
891 <     * @return the mapping for the key, if present; else the defaultValue
892 <     * @throws NullPointerException if the specified key is null
893 <     */
894 <    @SuppressWarnings("unchecked") public V getValueOrDefault(Object key, V defaultValue) {
895 <        if (key == null)
2518 <            throw new NullPointerException();
2519 <        V v = (V) internalGet(key);
2520 <        return v == null ? defaultValue : v;
878 >    public V get(Object key) {
879 >        Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek;
880 >        int h = spread(key.hashCode());
881 >        if ((tab = table) != null && (n = tab.length) > 0 &&
882 >            (e = tabAt(tab, (n - 1) & h)) != null) {
883 >            if ((eh = e.hash) == h) {
884 >                if ((ek = e.key) == key || (ek != null && key.equals(ek)))
885 >                    return e.val;
886 >            }
887 >            else if (eh < 0)
888 >                return (p = e.find(h, key)) != null ? p.val : null;
889 >            while ((e = e.next) != null) {
890 >                if (e.hash == h &&
891 >                    ((ek = e.key) == key || (ek != null && key.equals(ek))))
892 >                    return e.val;
893 >            }
894 >        }
895 >        return null;
896      }
897  
898      /**
899       * Tests if the specified object is a key in this table.
900       *
901 <     * @param  key   possible key
901 >     * @param  key possible key
902       * @return {@code true} if and only if the specified object
903       *         is a key in this table, as determined by the
904       *         {@code equals} method; {@code false} otherwise
905       * @throws NullPointerException if the specified key is null
906       */
907      public boolean containsKey(Object key) {
908 <        if (key == null)
2534 <            throw new NullPointerException();
2535 <        return internalGet(key) != null;
908 >        return get(key) != null;
909      }
910  
911      /**
# Line 2548 | Line 921 | public class ConcurrentHashMap<K, V>
921      public boolean containsValue(Object value) {
922          if (value == null)
923              throw new NullPointerException();
924 <        Object v;
925 <        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
926 <        while ((v = it.advance()) != null) {
927 <            if (v == value || value.equals(v))
928 <                return true;
924 >        Node<K,V>[] t;
925 >        if ((t = table) != null) {
926 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
927 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
928 >                V v;
929 >                if ((v = p.val) == value || (v != null && value.equals(v)))
930 >                    return true;
931 >            }
932          }
933          return false;
934      }
935  
936      /**
2561     * Legacy method testing if some key maps into the specified value
2562     * in this table.  This method is identical in functionality to
2563     * {@link #containsValue}, and exists solely to ensure
2564     * full compatibility with class {@link java.util.Hashtable},
2565     * which supported this method prior to introduction of the
2566     * Java Collections framework.
2567     *
2568     * @param  value a value to search for
2569     * @return {@code true} if and only if some key maps to the
2570     *         {@code value} argument in this table as
2571     *         determined by the {@code equals} method;
2572     *         {@code false} otherwise
2573     * @throws NullPointerException if the specified value is null
2574     */
2575    public boolean contains(Object value) {
2576        return containsValue(value);
2577    }
2578
2579    /**
937       * Maps the specified key to the specified value in this table.
938       * Neither the key nor the value can be null.
939       *
940 <     * <p> The value can be retrieved by calling the {@code get} method
940 >     * <p>The value can be retrieved by calling the {@code get} method
941       * with a key that is equal to the original key.
942       *
943       * @param key key with which the specified value is to be associated
# Line 2589 | Line 946 | public class ConcurrentHashMap<K, V>
946       *         {@code null} if there was no mapping for {@code key}
947       * @throws NullPointerException if the specified key or value is null
948       */
949 <    @SuppressWarnings("unchecked") public V put(K key, V value) {
950 <        if (key == null || value == null)
2594 <            throw new NullPointerException();
2595 <        return (V)internalPut(key, value);
949 >    public V put(K key, V value) {
950 >        return putVal(key, value, false);
951      }
952  
953 <    /**
954 <     * {@inheritDoc}
955 <     *
956 <     * @return the previous value associated with the specified key,
957 <     *         or {@code null} if there was no mapping for the key
958 <     * @throws NullPointerException if the specified key or value is null
959 <     */
960 <    @SuppressWarnings("unchecked") public V putIfAbsent(K key, V value) {
961 <        if (key == null || value == null)
962 <            throw new NullPointerException();
963 <        return (V)internalPutIfAbsent(key, value);
953 >    /** Implementation for put and putIfAbsent */
954 >    final V putVal(K key, V value, boolean onlyIfAbsent) {
955 >        if (key == null || value == null) throw new NullPointerException();
956 >        int hash = spread(key.hashCode());
957 >        int binCount = 0;
958 >        for (Node<K,V>[] tab = table;;) {
959 >            Node<K,V> f; int n, i, fh;
960 >            if (tab == null || (n = tab.length) == 0)
961 >                tab = initTable();
962 >            else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
963 >                if (casTabAt(tab, i, null,
964 >                             new Node<K,V>(hash, key, value, null)))
965 >                    break;                   // no lock when adding to empty bin
966 >            }
967 >            else if ((fh = f.hash) == MOVED)
968 >                tab = helpTransfer(tab, f);
969 >            else {
970 >                V oldVal = null;
971 >                synchronized (f) {
972 >                    if (tabAt(tab, i) == f) {
973 >                        if (fh >= 0) {
974 >                            binCount = 1;
975 >                            for (Node<K,V> e = f;; ++binCount) {
976 >                                K ek;
977 >                                if (e.hash == hash &&
978 >                                    ((ek = e.key) == key ||
979 >                                     (ek != null && key.equals(ek)))) {
980 >                                    oldVal = e.val;
981 >                                    if (!onlyIfAbsent)
982 >                                        e.val = value;
983 >                                    break;
984 >                                }
985 >                                Node<K,V> pred = e;
986 >                                if ((e = e.next) == null) {
987 >                                    pred.next = new Node<K,V>(hash, key,
988 >                                                              value, null);
989 >                                    break;
990 >                                }
991 >                            }
992 >                        }
993 >                        else if (f instanceof TreeBin) {
994 >                            Node<K,V> p;
995 >                            binCount = 2;
996 >                            if ((p = ((TreeBin<K,V>)f).putTreeVal(hash, key,
997 >                                                           value)) != null) {
998 >                                oldVal = p.val;
999 >                                if (!onlyIfAbsent)
1000 >                                    p.val = value;
1001 >                            }
1002 >                        }
1003 >                    }
1004 >                }
1005 >                if (binCount != 0) {
1006 >                    if (binCount >= TREEIFY_THRESHOLD)
1007 >                        treeifyBin(tab, i);
1008 >                    if (oldVal != null)
1009 >                        return oldVal;
1010 >                    break;
1011 >                }
1012 >            }
1013 >        }
1014 >        addCount(1L, binCount);
1015 >        return null;
1016      }
1017  
1018      /**
# Line 2616 | Line 1023 | public class ConcurrentHashMap<K, V>
1023       * @param m mappings to be stored in this map
1024       */
1025      public void putAll(Map<? extends K, ? extends V> m) {
1026 <        internalPutAll(m);
1027 <    }
1028 <
2622 <    /**
2623 <     * If the specified key is not already associated with a value,
2624 <     * computes its value using the given mappingFunction and enters
2625 <     * it into the map unless null.  This is equivalent to
2626 <     * <pre> {@code
2627 <     * if (map.containsKey(key))
2628 <     *   return map.get(key);
2629 <     * value = mappingFunction.apply(key);
2630 <     * if (value != null)
2631 <     *   map.put(key, value);
2632 <     * return value;}</pre>
2633 <     *
2634 <     * except that the action is performed atomically.  If the
2635 <     * function returns {@code null} no mapping is recorded. If the
2636 <     * function itself throws an (unchecked) exception, the exception
2637 <     * is rethrown to its caller, and no mapping is recorded.  Some
2638 <     * attempted update operations on this map by other threads may be
2639 <     * blocked while computation is in progress, so the computation
2640 <     * should be short and simple, and must not attempt to update any
2641 <     * other mappings of this Map. The most appropriate usage is to
2642 <     * construct a new object serving as an initial mapped value, or
2643 <     * memoized result, as in:
2644 <     *
2645 <     *  <pre> {@code
2646 <     * map.computeIfAbsent(key, new Fun<K, V>() {
2647 <     *   public V map(K k) { return new Value(f(k)); }});}</pre>
2648 <     *
2649 <     * @param key key with which the specified value is to be associated
2650 <     * @param mappingFunction the function to compute a value
2651 <     * @return the current (existing or computed) value associated with
2652 <     *         the specified key, or null if the computed value is null
2653 <     * @throws NullPointerException if the specified key or mappingFunction
2654 <     *         is null
2655 <     * @throws IllegalStateException if the computation detectably
2656 <     *         attempts a recursive update to this map that would
2657 <     *         otherwise never complete
2658 <     * @throws RuntimeException or Error if the mappingFunction does so,
2659 <     *         in which case the mapping is left unestablished
2660 <     */
2661 <    @SuppressWarnings("unchecked") public V computeIfAbsent
2662 <        (K key, Fun<? super K, ? extends V> mappingFunction) {
2663 <        if (key == null || mappingFunction == null)
2664 <            throw new NullPointerException();
2665 <        return (V)internalComputeIfAbsent(key, mappingFunction);
2666 <    }
2667 <
2668 <    /**
2669 <     * If the given key is present, computes a new mapping value given a key and
2670 <     * its current mapped value. This is equivalent to
2671 <     *  <pre> {@code
2672 <     *   if (map.containsKey(key)) {
2673 <     *     value = remappingFunction.apply(key, map.get(key));
2674 <     *     if (value != null)
2675 <     *       map.put(key, value);
2676 <     *     else
2677 <     *       map.remove(key);
2678 <     *   }
2679 <     * }</pre>
2680 <     *
2681 <     * except that the action is performed atomically.  If the
2682 <     * function returns {@code null}, the mapping is removed.  If the
2683 <     * function itself throws an (unchecked) exception, the exception
2684 <     * is rethrown to its caller, and the current mapping is left
2685 <     * unchanged.  Some attempted update operations on this map by
2686 <     * other threads may be blocked while computation is in progress,
2687 <     * so the computation should be short and simple, and must not
2688 <     * attempt to update any other mappings of this Map. For example,
2689 <     * to either create or append new messages to a value mapping:
2690 <     *
2691 <     * @param key key with which the specified value is to be associated
2692 <     * @param remappingFunction the function to compute a value
2693 <     * @return the new value associated with the specified key, or null if none
2694 <     * @throws NullPointerException if the specified key or remappingFunction
2695 <     *         is null
2696 <     * @throws IllegalStateException if the computation detectably
2697 <     *         attempts a recursive update to this map that would
2698 <     *         otherwise never complete
2699 <     * @throws RuntimeException or Error if the remappingFunction does so,
2700 <     *         in which case the mapping is unchanged
2701 <     */
2702 <    @SuppressWarnings("unchecked") public V computeIfPresent
2703 <        (K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
2704 <        if (key == null || remappingFunction == null)
2705 <            throw new NullPointerException();
2706 <        return (V)internalCompute(key, true, remappingFunction);
2707 <    }
2708 <
2709 <    /**
2710 <     * Computes a new mapping value given a key and
2711 <     * its current mapped value (or {@code null} if there is no current
2712 <     * mapping). This is equivalent to
2713 <     *  <pre> {@code
2714 <     *   value = remappingFunction.apply(key, map.get(key));
2715 <     *   if (value != null)
2716 <     *     map.put(key, value);
2717 <     *   else
2718 <     *     map.remove(key);
2719 <     * }</pre>
2720 <     *
2721 <     * except that the action is performed atomically.  If the
2722 <     * function returns {@code null}, the mapping is removed.  If the
2723 <     * function itself throws an (unchecked) exception, the exception
2724 <     * is rethrown to its caller, and the current mapping is left
2725 <     * unchanged.  Some attempted update operations on this map by
2726 <     * other threads may be blocked while computation is in progress,
2727 <     * so the computation should be short and simple, and must not
2728 <     * attempt to update any other mappings of this Map. For example,
2729 <     * to either create or append new messages to a value mapping:
2730 <     *
2731 <     * <pre> {@code
2732 <     * Map<Key, String> map = ...;
2733 <     * final String msg = ...;
2734 <     * map.compute(key, new BiFun<Key, String, String>() {
2735 <     *   public String apply(Key k, String v) {
2736 <     *    return (v == null) ? msg : v + msg;});}}</pre>
2737 <     *
2738 <     * @param key key with which the specified value is to be associated
2739 <     * @param remappingFunction the function to compute a value
2740 <     * @return the new value associated with the specified key, or null if none
2741 <     * @throws NullPointerException if the specified key or remappingFunction
2742 <     *         is null
2743 <     * @throws IllegalStateException if the computation detectably
2744 <     *         attempts a recursive update to this map that would
2745 <     *         otherwise never complete
2746 <     * @throws RuntimeException or Error if the remappingFunction does so,
2747 <     *         in which case the mapping is unchanged
2748 <     */
2749 <    @SuppressWarnings("unchecked") public V compute
2750 <        (K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
2751 <        if (key == null || remappingFunction == null)
2752 <            throw new NullPointerException();
2753 <        return (V)internalCompute(key, false, remappingFunction);
2754 <    }
2755 <
2756 <    /**
2757 <     * If the specified key is not already associated
2758 <     * with a value, associate it with the given value.
2759 <     * Otherwise, replace the value with the results of
2760 <     * the given remapping function. This is equivalent to:
2761 <     *  <pre> {@code
2762 <     *   if (!map.containsKey(key))
2763 <     *     map.put(value);
2764 <     *   else {
2765 <     *     newValue = remappingFunction.apply(map.get(key), value);
2766 <     *     if (value != null)
2767 <     *       map.put(key, value);
2768 <     *     else
2769 <     *       map.remove(key);
2770 <     *   }
2771 <     * }</pre>
2772 <     * except that the action is performed atomically.  If the
2773 <     * function returns {@code null}, the mapping is removed.  If the
2774 <     * function itself throws an (unchecked) exception, the exception
2775 <     * is rethrown to its caller, and the current mapping is left
2776 <     * unchanged.  Some attempted update operations on this map by
2777 <     * other threads may be blocked while computation is in progress,
2778 <     * so the computation should be short and simple, and must not
2779 <     * attempt to update any other mappings of this Map.
2780 <     */
2781 <    @SuppressWarnings("unchecked") public V merge
2782 <        (K key, V value, BiFun<? super V, ? super V, ? extends V> remappingFunction) {
2783 <        if (key == null || value == null || remappingFunction == null)
2784 <            throw new NullPointerException();
2785 <        return (V)internalMerge(key, value, remappingFunction);
1026 >        tryPresize(m.size());
1027 >        for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
1028 >            putVal(e.getKey(), e.getValue(), false);
1029      }
1030  
1031      /**
# Line 2794 | Line 1037 | public class ConcurrentHashMap<K, V>
1037       *         {@code null} if there was no mapping for {@code key}
1038       * @throws NullPointerException if the specified key is null
1039       */
1040 <    @SuppressWarnings("unchecked") public V remove(Object key) {
1041 <        if (key == null)
2799 <            throw new NullPointerException();
2800 <        return (V)internalReplace(key, null, null);
2801 <    }
2802 <
2803 <    /**
2804 <     * {@inheritDoc}
2805 <     *
2806 <     * @throws NullPointerException if the specified key is null
2807 <     */
2808 <    public boolean remove(Object key, Object value) {
2809 <        if (key == null)
2810 <            throw new NullPointerException();
2811 <        if (value == null)
2812 <            return false;
2813 <        return internalReplace(key, null, value) != null;
2814 <    }
2815 <
2816 <    /**
2817 <     * {@inheritDoc}
2818 <     *
2819 <     * @throws NullPointerException if any of the arguments are null
2820 <     */
2821 <    public boolean replace(K key, V oldValue, V newValue) {
2822 <        if (key == null || oldValue == null || newValue == null)
2823 <            throw new NullPointerException();
2824 <        return internalReplace(key, newValue, oldValue) != null;
1040 >    public V remove(Object key) {
1041 >        return replaceNode(key, null, null);
1042      }
1043  
1044      /**
1045 <     * {@inheritDoc}
1046 <     *
1047 <     * @return the previous value associated with the specified key,
2831 <     *         or {@code null} if there was no mapping for the key
2832 <     * @throws NullPointerException if the specified key or value is null
1045 >     * Implementation for the four public remove/replace methods:
1046 >     * Replaces node value with v, conditional upon match of cv if
1047 >     * non-null.  If resulting value is null, delete.
1048       */
1049 <    @SuppressWarnings("unchecked") public V replace(K key, V value) {
1050 <        if (key == null || value == null)
1051 <            throw new NullPointerException();
1052 <        return (V)internalReplace(key, value, null);
1049 >    final V replaceNode(Object key, V value, Object cv) {
1050 >        int hash = spread(key.hashCode());
1051 >        for (Node<K,V>[] tab = table;;) {
1052 >            Node<K,V> f; int n, i, fh;
1053 >            if (tab == null || (n = tab.length) == 0 ||
1054 >                (f = tabAt(tab, i = (n - 1) & hash)) == null)
1055 >                break;
1056 >            else if ((fh = f.hash) == MOVED)
1057 >                tab = helpTransfer(tab, f);
1058 >            else {
1059 >                V oldVal = null;
1060 >                boolean validated = false;
1061 >                synchronized (f) {
1062 >                    if (tabAt(tab, i) == f) {
1063 >                        if (fh >= 0) {
1064 >                            validated = true;
1065 >                            for (Node<K,V> e = f, pred = null;;) {
1066 >                                K ek;
1067 >                                if (e.hash == hash &&
1068 >                                    ((ek = e.key) == key ||
1069 >                                     (ek != null && key.equals(ek)))) {
1070 >                                    V ev = e.val;
1071 >                                    if (cv == null || cv == ev ||
1072 >                                        (ev != null && cv.equals(ev))) {
1073 >                                        oldVal = ev;
1074 >                                        if (value != null)
1075 >                                            e.val = value;
1076 >                                        else if (pred != null)
1077 >                                            pred.next = e.next;
1078 >                                        else
1079 >                                            setTabAt(tab, i, e.next);
1080 >                                    }
1081 >                                    break;
1082 >                                }
1083 >                                pred = e;
1084 >                                if ((e = e.next) == null)
1085 >                                    break;
1086 >                            }
1087 >                        }
1088 >                        else if (f instanceof TreeBin) {
1089 >                            validated = true;
1090 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
1091 >                            TreeNode<K,V> r, p;
1092 >                            if ((r = t.root) != null &&
1093 >                                (p = r.findTreeNode(hash, key, null)) != null) {
1094 >                                V pv = p.val;
1095 >                                if (cv == null || cv == pv ||
1096 >                                    (pv != null && cv.equals(pv))) {
1097 >                                    oldVal = pv;
1098 >                                    if (value != null)
1099 >                                        p.val = value;
1100 >                                    else if (t.removeTreeNode(p))
1101 >                                        setTabAt(tab, i, untreeify(t.first));
1102 >                                }
1103 >                            }
1104 >                        }
1105 >                    }
1106 >                }
1107 >                if (validated) {
1108 >                    if (oldVal != null) {
1109 >                        if (value == null)
1110 >                            addCount(-1L, -1);
1111 >                        return oldVal;
1112 >                    }
1113 >                    break;
1114 >                }
1115 >            }
1116 >        }
1117 >        return null;
1118      }
1119  
1120      /**
1121       * Removes all of the mappings from this map.
1122       */
1123      public void clear() {
1124 <        internalClear();
1124 >        long delta = 0L; // negative number of deletions
1125 >        int i = 0;
1126 >        Node<K,V>[] tab = table;
1127 >        while (tab != null && i < tab.length) {
1128 >            int fh;
1129 >            Node<K,V> f = tabAt(tab, i);
1130 >            if (f == null)
1131 >                ++i;
1132 >            else if ((fh = f.hash) == MOVED) {
1133 >                tab = helpTransfer(tab, f);
1134 >                i = 0; // restart
1135 >            }
1136 >            else {
1137 >                synchronized (f) {
1138 >                    if (tabAt(tab, i) == f) {
1139 >                        Node<K,V> p = (fh >= 0 ? f :
1140 >                                       (f instanceof TreeBin) ?
1141 >                                       ((TreeBin<K,V>)f).first : null);
1142 >                        while (p != null) {
1143 >                            --delta;
1144 >                            p = p.next;
1145 >                        }
1146 >                        setTabAt(tab, i++, null);
1147 >                    }
1148 >                }
1149 >            }
1150 >        }
1151 >        if (delta != 0L)
1152 >            addCount(delta, -1);
1153      }
1154  
1155      /**
1156       * Returns a {@link Set} view of the keys contained in this map.
1157       * The set is backed by the map, so changes to the map are
1158 <     * reflected in the set, and vice-versa.  The set supports element
1158 >     * reflected in the set, and vice-versa. The set supports element
1159       * removal, which removes the corresponding mapping from this map,
1160       * via the {@code Iterator.remove}, {@code Set.remove},
1161       * {@code removeAll}, {@code retainAll}, and {@code clear}
# Line 2859 | Line 1167 | public class ConcurrentHashMap<K, V>
1167       * and guarantees to traverse elements as they existed upon
1168       * construction of the iterator, and may (but is not guaranteed to)
1169       * reflect any modifications subsequent to construction.
1170 +     *
1171 +     * @return the set view
1172       */
1173 <    public Set<K> keySet() {
1174 <        KeySet<K,V> ks = keySet;
1175 <        return (ks != null) ? ks : (keySet = new KeySet<K,V>(this));
1173 >    public KeySetView<K,V> keySet() {
1174 >        KeySetView<K,V> ks;
1175 >        return (ks = keySet) != null ? ks : (keySet = new KeySetView<K,V>(this, null));
1176      }
1177  
1178      /**
# Line 2880 | Line 1190 | public class ConcurrentHashMap<K, V>
1190       * and guarantees to traverse elements as they existed upon
1191       * construction of the iterator, and may (but is not guaranteed to)
1192       * reflect any modifications subsequent to construction.
1193 +     *
1194 +     * @return the collection view
1195       */
1196      public Collection<V> values() {
1197 <        Values<K,V> vs = values;
1198 <        return (vs != null) ? vs : (values = new Values<K,V>(this));
1197 >        ValuesView<K,V> vs;
1198 >        return (vs = values) != null ? vs : (values = new ValuesView<K,V>(this));
1199      }
1200  
1201      /**
# Line 2893 | Line 1205 | public class ConcurrentHashMap<K, V>
1205       * removal, which removes the corresponding mapping from the map,
1206       * via the {@code Iterator.remove}, {@code Set.remove},
1207       * {@code removeAll}, {@code retainAll}, and {@code clear}
1208 <     * operations.  It does not support the {@code add} or
2897 <     * {@code addAll} operations.
1208 >     * operations.
1209       *
1210       * <p>The view's {@code iterator} is a "weakly consistent" iterator
1211       * that will never throw {@link ConcurrentModificationException},
1212       * and guarantees to traverse elements as they existed upon
1213       * construction of the iterator, and may (but is not guaranteed to)
1214       * reflect any modifications subsequent to construction.
2904     */
2905    public Set<Map.Entry<K,V>> entrySet() {
2906        EntrySet<K,V> es = entrySet;
2907        return (es != null) ? es : (entrySet = new EntrySet<K,V>(this));
2908    }
2909
2910    /**
2911     * Returns an enumeration of the keys in this table.
2912     *
2913     * @return an enumeration of the keys in this table
2914     * @see #keySet()
2915     */
2916    public Enumeration<K> keys() {
2917        return new KeyIterator<K,V>(this);
2918    }
2919
2920    /**
2921     * Returns an enumeration of the values in this table.
1215       *
1216 <     * @return an enumeration of the values in this table
2924 <     * @see #values()
1216 >     * @return the set view
1217       */
1218 <    public Enumeration<V> elements() {
1219 <        return new ValueIterator<K,V>(this);
1220 <    }
2929 <
2930 <    /**
2931 <     * Returns a partitionable iterator of the keys in this map.
2932 <     *
2933 <     * @return a partitionable iterator of the keys in this map
2934 <     */
2935 <    public Spliterator<K> keySpliterator() {
2936 <        return new KeyIterator<K,V>(this);
2937 <    }
2938 <
2939 <    /**
2940 <     * Returns a partitionable iterator of the values in this map.
2941 <     *
2942 <     * @return a partitionable iterator of the values in this map
2943 <     */
2944 <    public Spliterator<V> valueSpliterator() {
2945 <        return new ValueIterator<K,V>(this);
2946 <    }
2947 <
2948 <    /**
2949 <     * Returns a partitionable iterator of the entries in this map.
2950 <     *
2951 <     * @return a partitionable iterator of the entries in this map
2952 <     */
2953 <    public Spliterator<Map.Entry<K,V>> entrySpliterator() {
2954 <        return new EntryIterator<K,V>(this);
1218 >    public Set<Map.Entry<K,V>> entrySet() {
1219 >        EntrySetView<K,V> es;
1220 >        return (es = entrySet) != null ? es : (entrySet = new EntrySetView<K,V>(this));
1221      }
1222  
1223      /**
# Line 2963 | Line 1229 | public class ConcurrentHashMap<K, V>
1229       */
1230      public int hashCode() {
1231          int h = 0;
1232 <        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
1233 <        Object v;
1234 <        while ((v = it.advance()) != null) {
1235 <            h += it.nextKey.hashCode() ^ v.hashCode();
1232 >        Node<K,V>[] t;
1233 >        if ((t = table) != null) {
1234 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1235 >            for (Node<K,V> p; (p = it.advance()) != null; )
1236 >                h += p.key.hashCode() ^ p.val.hashCode();
1237          }
1238          return h;
1239      }
# Line 2983 | Line 1250 | public class ConcurrentHashMap<K, V>
1250       * @return a string representation of this map
1251       */
1252      public String toString() {
1253 <        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
1253 >        Node<K,V>[] t;
1254 >        int f = (t = table) == null ? 0 : t.length;
1255 >        Traverser<K,V> it = new Traverser<K,V>(t, f, 0, f);
1256          StringBuilder sb = new StringBuilder();
1257          sb.append('{');
1258 <        Object v;
1259 <        if ((v = it.advance()) != null) {
1258 >        Node<K,V> p;
1259 >        if ((p = it.advance()) != null) {
1260              for (;;) {
1261 <                Object k = it.nextKey;
1261 >                K k = p.key;
1262 >                V v = p.val;
1263                  sb.append(k == this ? "(this Map)" : k);
1264                  sb.append('=');
1265                  sb.append(v == this ? "(this Map)" : v);
1266 <                if ((v = it.advance()) == null)
1266 >                if ((p = it.advance()) == null)
1267                      break;
1268                  sb.append(',').append(' ');
1269              }
# Line 3016 | Line 1286 | public class ConcurrentHashMap<K, V>
1286              if (!(o instanceof Map))
1287                  return false;
1288              Map<?,?> m = (Map<?,?>) o;
1289 <            Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
1290 <            Object val;
1291 <            while ((val = it.advance()) != null) {
1292 <                Object v = m.get(it.nextKey);
1289 >            Node<K,V>[] t;
1290 >            int f = (t = table) == null ? 0 : t.length;
1291 >            Traverser<K,V> it = new Traverser<K,V>(t, f, 0, f);
1292 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
1293 >                V val = p.val;
1294 >                Object v = m.get(p.key);
1295                  if (v == null || (v != val && !v.equals(val)))
1296                      return false;
1297              }
# Line 3027 | Line 1299 | public class ConcurrentHashMap<K, V>
1299                  Object mk, mv, v;
1300                  if ((mk = e.getKey()) == null ||
1301                      (mv = e.getValue()) == null ||
1302 <                    (v = internalGet(mk)) == null ||
1302 >                    (v = get(mk)) == null ||
1303                      (mv != v && !mv.equals(v)))
1304                      return false;
1305              }
# Line 3035 | Line 1307 | public class ConcurrentHashMap<K, V>
1307          return true;
1308      }
1309  
1310 <    /* ----------------Iterators -------------- */
1310 >    /**
1311 >     * Stripped-down version of helper class used in previous version,
1312 >     * declared for the sake of serialization compatibility
1313 >     */
1314 >    static class Segment<K,V> extends ReentrantLock implements Serializable {
1315 >        private static final long serialVersionUID = 2249069246763182397L;
1316 >        final float loadFactor;
1317 >        Segment(float lf) { this.loadFactor = lf; }
1318 >    }
1319  
1320 <    @SuppressWarnings("serial") static final class KeyIterator<K,V> extends Traverser<K,V,Object>
1321 <        implements Spliterator<K>, Enumeration<K> {
1322 <        KeyIterator(ConcurrentHashMap<K, V> map) { super(map); }
1323 <        KeyIterator(Traverser<K,V,Object> it) {
1324 <            super(it);
1325 <        }
1326 <        public KeyIterator<K,V> split() {
1327 <            if (last != null || (next != null && nextVal == null))
1328 <                throw new IllegalStateException();
1329 <            return new KeyIterator<K,V>(this);
1330 <        }
1331 <        @SuppressWarnings("unchecked") public final K next() {
1332 <            if (nextVal == null && advance() == null)
1333 <                throw new NoSuchElementException();
1334 <            Object k = nextKey;
1335 <            nextVal = null;
1336 <            return (K) k;
1320 >    /**
1321 >     * Saves the state of the {@code ConcurrentHashMap} instance to a
1322 >     * stream (i.e., serializes it).
1323 >     * @param s the stream
1324 >     * @serialData
1325 >     * the key (Object) and value (Object)
1326 >     * for each key-value mapping, followed by a null pair.
1327 >     * The key-value mappings are emitted in no particular order.
1328 >     */
1329 >    private void writeObject(java.io.ObjectOutputStream s)
1330 >        throws java.io.IOException {
1331 >        // For serialization compatibility
1332 >        // Emulate segment calculation from previous version of this class
1333 >        int sshift = 0;
1334 >        int ssize = 1;
1335 >        while (ssize < DEFAULT_CONCURRENCY_LEVEL) {
1336 >            ++sshift;
1337 >            ssize <<= 1;
1338 >        }
1339 >        int segmentShift = 32 - sshift;
1340 >        int segmentMask = ssize - 1;
1341 >        @SuppressWarnings("unchecked") Segment<K,V>[] segments = (Segment<K,V>[])
1342 >            new Segment<?,?>[DEFAULT_CONCURRENCY_LEVEL];
1343 >        for (int i = 0; i < segments.length; ++i)
1344 >            segments[i] = new Segment<K,V>(LOAD_FACTOR);
1345 >        s.putFields().put("segments", segments);
1346 >        s.putFields().put("segmentShift", segmentShift);
1347 >        s.putFields().put("segmentMask", segmentMask);
1348 >        s.writeFields();
1349 >
1350 >        Node<K,V>[] t;
1351 >        if ((t = table) != null) {
1352 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1353 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
1354 >                s.writeObject(p.key);
1355 >                s.writeObject(p.val);
1356 >            }
1357          }
1358 <
1359 <        public final K nextElement() { return next(); }
1358 >        s.writeObject(null);
1359 >        s.writeObject(null);
1360 >        segments = null; // throw away
1361      }
1362  
1363 <    @SuppressWarnings("serial") static final class ValueIterator<K,V> extends Traverser<K,V,Object>
1364 <        implements Spliterator<V>, Enumeration<V> {
1365 <        ValueIterator(ConcurrentHashMap<K, V> map) { super(map); }
1366 <        ValueIterator(Traverser<K,V,Object> it) {
1367 <            super(it);
1368 <        }
1369 <        public ValueIterator<K,V> split() {
1370 <            if (last != null || (next != null && nextVal == null))
1371 <                throw new IllegalStateException();
1372 <            return new ValueIterator<K,V>(this);
1363 >    /**
1364 >     * Reconstitutes the instance from a stream (that is, deserializes it).
1365 >     * @param s the stream
1366 >     */
1367 >    private void readObject(java.io.ObjectInputStream s)
1368 >        throws java.io.IOException, ClassNotFoundException {
1369 >        /*
1370 >         * To improve performance in typical cases, we create nodes
1371 >         * while reading, then place in table once size is known.
1372 >         * However, we must also validate uniqueness and deal with
1373 >         * overpopulated bins while doing so, which requires
1374 >         * specialized versions of putVal mechanics.
1375 >         */
1376 >        sizeCtl = -1; // force exclusion for table construction
1377 >        s.defaultReadObject();
1378 >        long size = 0L;
1379 >        Node<K,V> p = null;
1380 >        for (;;) {
1381 >            @SuppressWarnings("unchecked") K k = (K) s.readObject();
1382 >            @SuppressWarnings("unchecked") V v = (V) s.readObject();
1383 >            if (k != null && v != null) {
1384 >                p = new Node<K,V>(spread(k.hashCode()), k, v, p);
1385 >                ++size;
1386 >            }
1387 >            else
1388 >                break;
1389          }
1390 <
1391 <        @SuppressWarnings("unchecked") public final V next() {
1392 <            Object v;
1393 <            if ((v = nextVal) == null && (v = advance()) == null)
1394 <                throw new NoSuchElementException();
1395 <            nextVal = null;
1396 <            return (V) v;
1390 >        if (size == 0L)
1391 >            sizeCtl = 0;
1392 >        else {
1393 >            int n;
1394 >            if (size >= (long)(MAXIMUM_CAPACITY >>> 1))
1395 >                n = MAXIMUM_CAPACITY;
1396 >            else {
1397 >                int sz = (int)size;
1398 >                n = tableSizeFor(sz + (sz >>> 1) + 1);
1399 >            }
1400 >            @SuppressWarnings({"rawtypes","unchecked"})
1401 >                Node<K,V>[] tab = (Node<K,V>[])new Node[n];
1402 >            int mask = n - 1;
1403 >            long added = 0L;
1404 >            while (p != null) {
1405 >                boolean insertAtFront;
1406 >                Node<K,V> next = p.next, first;
1407 >                int h = p.hash, j = h & mask;
1408 >                if ((first = tabAt(tab, j)) == null)
1409 >                    insertAtFront = true;
1410 >                else {
1411 >                    K k = p.key;
1412 >                    if (first.hash < 0) {
1413 >                        TreeBin<K,V> t = (TreeBin<K,V>)first;
1414 >                        if (t.putTreeVal(h, k, p.val) == null)
1415 >                            ++added;
1416 >                        insertAtFront = false;
1417 >                    }
1418 >                    else {
1419 >                        int binCount = 0;
1420 >                        insertAtFront = true;
1421 >                        Node<K,V> q; K qk;
1422 >                        for (q = first; q != null; q = q.next) {
1423 >                            if (q.hash == h &&
1424 >                                ((qk = q.key) == k ||
1425 >                                 (qk != null && k.equals(qk)))) {
1426 >                                insertAtFront = false;
1427 >                                break;
1428 >                            }
1429 >                            ++binCount;
1430 >                        }
1431 >                        if (insertAtFront && binCount >= TREEIFY_THRESHOLD) {
1432 >                            insertAtFront = false;
1433 >                            ++added;
1434 >                            p.next = first;
1435 >                            TreeNode<K,V> hd = null, tl = null;
1436 >                            for (q = p; q != null; q = q.next) {
1437 >                                TreeNode<K,V> t = new TreeNode<K,V>
1438 >                                    (q.hash, q.key, q.val, null, null);
1439 >                                if ((t.prev = tl) == null)
1440 >                                    hd = t;
1441 >                                else
1442 >                                    tl.next = t;
1443 >                                tl = t;
1444 >                            }
1445 >                            setTabAt(tab, j, new TreeBin<K,V>(hd));
1446 >                        }
1447 >                    }
1448 >                }
1449 >                if (insertAtFront) {
1450 >                    ++added;
1451 >                    p.next = first;
1452 >                    setTabAt(tab, j, p);
1453 >                }
1454 >                p = next;
1455 >            }
1456 >            table = tab;
1457 >            sizeCtl = n - (n >>> 2);
1458 >            baseCount = added;
1459          }
3081
3082        public final V nextElement() { return next(); }
1460      }
1461  
1462 <    @SuppressWarnings("serial") static final class EntryIterator<K,V> extends Traverser<K,V,Object>
3086 <        implements Spliterator<Map.Entry<K,V>> {
3087 <        EntryIterator(ConcurrentHashMap<K, V> map) { super(map); }
3088 <        EntryIterator(Traverser<K,V,Object> it) {
3089 <            super(it);
3090 <        }
3091 <        public EntryIterator<K,V> split() {
3092 <            if (last != null || (next != null && nextVal == null))
3093 <                throw new IllegalStateException();
3094 <            return new EntryIterator<K,V>(this);
3095 <        }
1462 >    // ConcurrentMap methods
1463  
1464 <        @SuppressWarnings("unchecked") public final Map.Entry<K,V> next() {
1465 <            Object v;
1466 <            if ((v = nextVal) == null && (v = advance()) == null)
1467 <                throw new NoSuchElementException();
1468 <            Object k = nextKey;
1469 <            nextVal = null;
1470 <            return new MapEntry<K,V>((K)k, (V)v, map);
1471 <        }
1464 >    /**
1465 >     * {@inheritDoc}
1466 >     *
1467 >     * @return the previous value associated with the specified key,
1468 >     *         or {@code null} if there was no mapping for the key
1469 >     * @throws NullPointerException if the specified key or value is null
1470 >     */
1471 >    public V putIfAbsent(K key, V value) {
1472 >        return putVal(key, value, true);
1473      }
1474  
1475      /**
1476 <     * Exported Entry for iterators
1476 >     * {@inheritDoc}
1477 >     *
1478 >     * @throws NullPointerException if the specified key is null
1479       */
1480 <    static final class MapEntry<K,V> implements Map.Entry<K, V> {
1481 <        final K key; // non-null
1482 <        V val;       // non-null
1483 <        final ConcurrentHashMap<K, V> map;
3114 <        MapEntry(K key, V val, ConcurrentHashMap<K, V> map) {
3115 <            this.key = key;
3116 <            this.val = val;
3117 <            this.map = map;
3118 <        }
3119 <        public final K getKey()       { return key; }
3120 <        public final V getValue()     { return val; }
3121 <        public final int hashCode()   { return key.hashCode() ^ val.hashCode(); }
3122 <        public final String toString(){ return key + "=" + val; }
3123 <
3124 <        public final boolean equals(Object o) {
3125 <            Object k, v; Map.Entry<?,?> e;
3126 <            return ((o instanceof Map.Entry) &&
3127 <                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3128 <                    (v = e.getValue()) != null &&
3129 <                    (k == key || k.equals(key)) &&
3130 <                    (v == val || v.equals(val)));
3131 <        }
3132 <
3133 <        /**
3134 <         * Sets our entry's value and writes through to the map. The
3135 <         * value to return is somewhat arbitrary here. Since we do not
3136 <         * necessarily track asynchronous changes, the most recent
3137 <         * "previous" value could be different from what we return (or
3138 <         * could even have been removed in which case the put will
3139 <         * re-establish). We do not and cannot guarantee more.
3140 <         */
3141 <        public final V setValue(V value) {
3142 <            if (value == null) throw new NullPointerException();
3143 <            V v = val;
3144 <            val = value;
3145 <            map.put(key, value);
3146 <            return v;
3147 <        }
1480 >    public boolean remove(Object key, Object value) {
1481 >        if (key == null)
1482 >            throw new NullPointerException();
1483 >        return value != null && replaceNode(key, null, value) != null;
1484      }
1485  
1486 <    /* ----------------Views -------------- */
1486 >    /**
1487 >     * {@inheritDoc}
1488 >     *
1489 >     * @throws NullPointerException if any of the arguments are null
1490 >     */
1491 >    public boolean replace(K key, V oldValue, V newValue) {
1492 >        if (key == null || oldValue == null || newValue == null)
1493 >            throw new NullPointerException();
1494 >        return replaceNode(key, newValue, oldValue) != null;
1495 >    }
1496  
1497      /**
1498 <     * Base class for views.
1498 >     * {@inheritDoc}
1499 >     *
1500 >     * @return the previous value associated with the specified key,
1501 >     *         or {@code null} if there was no mapping for the key
1502 >     * @throws NullPointerException if the specified key or value is null
1503       */
1504 <    static abstract class CHMView<K, V> {
1505 <        final ConcurrentHashMap<K, V> map;
1506 <        CHMView(ConcurrentHashMap<K, V> map)  { this.map = map; }
1507 <        public final int size()                 { return map.size(); }
1508 <        public final boolean isEmpty()          { return map.isEmpty(); }
3160 <        public final void clear()               { map.clear(); }
1504 >    public V replace(K key, V value) {
1505 >        if (key == null || value == null)
1506 >            throw new NullPointerException();
1507 >        return replaceNode(key, value, null);
1508 >    }
1509  
1510 <        // implementations below rely on concrete classes supplying these
3163 <        abstract public Iterator<?> iterator();
3164 <        abstract public boolean contains(Object o);
3165 <        abstract public boolean remove(Object o);
1510 >    // Overrides of JDK8+ Map extension method defaults
1511  
1512 <        private static final String oomeMsg = "Required array size too large";
1512 >    /**
1513 >     * Returns the value to which the specified key is mapped, or the
1514 >     * given default value if this map contains no mapping for the
1515 >     * key.
1516 >     *
1517 >     * @param key the key whose associated value is to be returned
1518 >     * @param defaultValue the value to return if this map contains
1519 >     * no mapping for the given key
1520 >     * @return the mapping for the key, if present; else the default value
1521 >     * @throws NullPointerException if the specified key is null
1522 >     */
1523 >    public V getOrDefault(Object key, V defaultValue) {
1524 >        V v;
1525 >        return (v = get(key)) == null ? defaultValue : v;
1526 >    }
1527 >
1528 >    public void forEach(BiConsumer<? super K, ? super V> action) {
1529 >        if (action == null) throw new NullPointerException();
1530 >        Node<K,V>[] t;
1531 >        if ((t = table) != null) {
1532 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1533 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
1534 >                action.accept(p.key, p.val);
1535 >            }
1536 >        }
1537 >    }
1538  
1539 <        public final Object[] toArray() {
1540 <            long sz = map.mappingCount();
1541 <            if (sz > (long)(MAX_ARRAY_SIZE))
1542 <                throw new OutOfMemoryError(oomeMsg);
1543 <            int n = (int)sz;
1544 <            Object[] r = new Object[n];
1545 <            int i = 0;
1546 <            Iterator<?> it = iterator();
1547 <            while (it.hasNext()) {
1548 <                if (i == n) {
1549 <                    if (n >= MAX_ARRAY_SIZE)
1550 <                        throw new OutOfMemoryError(oomeMsg);
1551 <                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
1552 <                        n = MAX_ARRAY_SIZE;
3183 <                    else
3184 <                        n += (n >>> 1) + 1;
3185 <                    r = Arrays.copyOf(r, n);
1539 >    public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
1540 >        if (function == null) throw new NullPointerException();
1541 >        Node<K,V>[] t;
1542 >        if ((t = table) != null) {
1543 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1544 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
1545 >                V oldValue = p.val;
1546 >                for (K key = p.key;;) {
1547 >                    V newValue = function.apply(key, oldValue);
1548 >                    if (newValue == null)
1549 >                        throw new NullPointerException();
1550 >                    if (replaceNode(key, newValue, oldValue) != null ||
1551 >                        (oldValue = get(key)) == null)
1552 >                        break;
1553                  }
3187                r[i++] = it.next();
1554              }
3189            return (i == n) ? r : Arrays.copyOf(r, i);
1555          }
1556 +    }
1557  
1558 <        @SuppressWarnings("unchecked") public final <T> T[] toArray(T[] a) {
1559 <            long sz = map.mappingCount();
1560 <            if (sz > (long)(MAX_ARRAY_SIZE))
1561 <                throw new OutOfMemoryError(oomeMsg);
1562 <            int m = (int)sz;
1563 <            T[] r = (a.length >= m) ? a :
1564 <                (T[])java.lang.reflect.Array
1565 <                .newInstance(a.getClass().getComponentType(), m);
1566 <            int n = r.length;
1567 <            int i = 0;
1568 <            Iterator<?> it = iterator();
1569 <            while (it.hasNext()) {
1570 <                if (i == n) {
1571 <                    if (n >= MAX_ARRAY_SIZE)
1572 <                        throw new OutOfMemoryError(oomeMsg);
1573 <                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
1574 <                        n = MAX_ARRAY_SIZE;
1575 <                    else
1576 <                        n += (n >>> 1) + 1;
1577 <                    r = Arrays.copyOf(r, n);
1558 >    /**
1559 >     * If the specified key is not already associated with a value,
1560 >     * attempts to compute its value using the given mapping function
1561 >     * and enters it into this map unless {@code null}.  The entire
1562 >     * method invocation is performed atomically, so the function is
1563 >     * applied at most once per key.  Some attempted update operations
1564 >     * on this map by other threads may be blocked while computation
1565 >     * is in progress, so the computation should be short and simple,
1566 >     * and must not attempt to update any other mappings of this map.
1567 >     *
1568 >     * @param key key with which the specified value is to be associated
1569 >     * @param mappingFunction the function to compute a value
1570 >     * @return the current (existing or computed) value associated with
1571 >     *         the specified key, or null if the computed value is null
1572 >     * @throws NullPointerException if the specified key or mappingFunction
1573 >     *         is null
1574 >     * @throws IllegalStateException if the computation detectably
1575 >     *         attempts a recursive update to this map that would
1576 >     *         otherwise never complete
1577 >     * @throws RuntimeException or Error if the mappingFunction does so,
1578 >     *         in which case the mapping is left unestablished
1579 >     */
1580 >    public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
1581 >        if (key == null || mappingFunction == null)
1582 >            throw new NullPointerException();
1583 >        int h = spread(key.hashCode());
1584 >        V val = null;
1585 >        int binCount = 0;
1586 >        for (Node<K,V>[] tab = table;;) {
1587 >            Node<K,V> f; int n, i, fh;
1588 >            if (tab == null || (n = tab.length) == 0)
1589 >                tab = initTable();
1590 >            else if ((f = tabAt(tab, i = (n - 1) & h)) == null) {
1591 >                Node<K,V> r = new ReservationNode<K,V>();
1592 >                synchronized (r) {
1593 >                    if (casTabAt(tab, i, null, r)) {
1594 >                        binCount = 1;
1595 >                        Node<K,V> node = null;
1596 >                        try {
1597 >                            if ((val = mappingFunction.apply(key)) != null)
1598 >                                node = new Node<K,V>(h, key, val, null);
1599 >                        } finally {
1600 >                            setTabAt(tab, i, node);
1601 >                        }
1602 >                    }
1603                  }
1604 <                r[i++] = (T)it.next();
1604 >                if (binCount != 0)
1605 >                    break;
1606              }
1607 <            if (a == r && i < n) {
1608 <                r[i] = null; // null-terminate
1609 <                return r;
1607 >            else if ((fh = f.hash) == MOVED)
1608 >                tab = helpTransfer(tab, f);
1609 >            else {
1610 >                boolean added = false;
1611 >                synchronized (f) {
1612 >                    if (tabAt(tab, i) == f) {
1613 >                        if (fh >= 0) {
1614 >                            binCount = 1;
1615 >                            for (Node<K,V> e = f;; ++binCount) {
1616 >                                K ek; V ev;
1617 >                                if (e.hash == h &&
1618 >                                    ((ek = e.key) == key ||
1619 >                                     (ek != null && key.equals(ek)))) {
1620 >                                    val = e.val;
1621 >                                    break;
1622 >                                }
1623 >                                Node<K,V> pred = e;
1624 >                                if ((e = e.next) == null) {
1625 >                                    if ((val = mappingFunction.apply(key)) != null) {
1626 >                                        added = true;
1627 >                                        pred.next = new Node<K,V>(h, key, val, null);
1628 >                                    }
1629 >                                    break;
1630 >                                }
1631 >                            }
1632 >                        }
1633 >                        else if (f instanceof TreeBin) {
1634 >                            binCount = 2;
1635 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
1636 >                            TreeNode<K,V> r, p;
1637 >                            if ((r = t.root) != null &&
1638 >                                (p = r.findTreeNode(h, key, null)) != null)
1639 >                                val = p.val;
1640 >                            else if ((val = mappingFunction.apply(key)) != null) {
1641 >                                added = true;
1642 >                                t.putTreeVal(h, key, val);
1643 >                            }
1644 >                        }
1645 >                    }
1646 >                }
1647 >                if (binCount != 0) {
1648 >                    if (binCount >= TREEIFY_THRESHOLD)
1649 >                        treeifyBin(tab, i);
1650 >                    if (!added)
1651 >                        return val;
1652 >                    break;
1653 >                }
1654              }
3219            return (i == n) ? r : Arrays.copyOf(r, i);
3220        }
3221
3222        public final int hashCode() {
3223            int h = 0;
3224            for (Iterator<?> it = iterator(); it.hasNext();)
3225                h += it.next().hashCode();
3226            return h;
1655          }
1656 +        if (val != null)
1657 +            addCount(1L, binCount);
1658 +        return val;
1659 +    }
1660  
1661 <        public final String toString() {
1662 <            StringBuilder sb = new StringBuilder();
1663 <            sb.append('[');
1664 <            Iterator<?> it = iterator();
1665 <            if (it.hasNext()) {
1666 <                for (;;) {
1667 <                    Object e = it.next();
1668 <                    sb.append(e == this ? "(this Collection)" : e);
1669 <                    if (!it.hasNext())
1670 <                        break;
1671 <                    sb.append(',').append(' ');
1661 >    /**
1662 >     * If the value for the specified key is present, attempts to
1663 >     * compute a new mapping given the key and its current mapped
1664 >     * value.  The entire method invocation is performed atomically.
1665 >     * Some attempted update operations on this map by other threads
1666 >     * may be blocked while computation is in progress, so the
1667 >     * computation should be short and simple, and must not attempt to
1668 >     * update any other mappings of this map.
1669 >     *
1670 >     * @param key key with which a value may be associated
1671 >     * @param remappingFunction the function to compute a value
1672 >     * @return the new value associated with the specified key, or null if none
1673 >     * @throws NullPointerException if the specified key or remappingFunction
1674 >     *         is null
1675 >     * @throws IllegalStateException if the computation detectably
1676 >     *         attempts a recursive update to this map that would
1677 >     *         otherwise never complete
1678 >     * @throws RuntimeException or Error if the remappingFunction does so,
1679 >     *         in which case the mapping is unchanged
1680 >     */
1681 >    public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
1682 >        if (key == null || remappingFunction == null)
1683 >            throw new NullPointerException();
1684 >        int h = spread(key.hashCode());
1685 >        V val = null;
1686 >        int delta = 0;
1687 >        int binCount = 0;
1688 >        for (Node<K,V>[] tab = table;;) {
1689 >            Node<K,V> f; int n, i, fh;
1690 >            if (tab == null || (n = tab.length) == 0)
1691 >                tab = initTable();
1692 >            else if ((f = tabAt(tab, i = (n - 1) & h)) == null)
1693 >                break;
1694 >            else if ((fh = f.hash) == MOVED)
1695 >                tab = helpTransfer(tab, f);
1696 >            else {
1697 >                synchronized (f) {
1698 >                    if (tabAt(tab, i) == f) {
1699 >                        if (fh >= 0) {
1700 >                            binCount = 1;
1701 >                            for (Node<K,V> e = f, pred = null;; ++binCount) {
1702 >                                K ek;
1703 >                                if (e.hash == h &&
1704 >                                    ((ek = e.key) == key ||
1705 >                                     (ek != null && key.equals(ek)))) {
1706 >                                    val = remappingFunction.apply(key, e.val);
1707 >                                    if (val != null)
1708 >                                        e.val = val;
1709 >                                    else {
1710 >                                        delta = -1;
1711 >                                        Node<K,V> en = e.next;
1712 >                                        if (pred != null)
1713 >                                            pred.next = en;
1714 >                                        else
1715 >                                            setTabAt(tab, i, en);
1716 >                                    }
1717 >                                    break;
1718 >                                }
1719 >                                pred = e;
1720 >                                if ((e = e.next) == null)
1721 >                                    break;
1722 >                            }
1723 >                        }
1724 >                        else if (f instanceof TreeBin) {
1725 >                            binCount = 2;
1726 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
1727 >                            TreeNode<K,V> r, p;
1728 >                            if ((r = t.root) != null &&
1729 >                                (p = r.findTreeNode(h, key, null)) != null) {
1730 >                                val = remappingFunction.apply(key, p.val);
1731 >                                if (val != null)
1732 >                                    p.val = val;
1733 >                                else {
1734 >                                    delta = -1;
1735 >                                    if (t.removeTreeNode(p))
1736 >                                        setTabAt(tab, i, untreeify(t.first));
1737 >                                }
1738 >                            }
1739 >                        }
1740 >                    }
1741                  }
1742 +                if (binCount != 0)
1743 +                    break;
1744              }
3242            return sb.append(']').toString();
1745          }
1746 +        if (delta != 0)
1747 +            addCount((long)delta, binCount);
1748 +        return val;
1749 +    }
1750  
1751 <        public final boolean containsAll(Collection<?> c) {
1752 <            if (c != this) {
1753 <                for (Iterator<?> it = c.iterator(); it.hasNext();) {
1754 <                    Object e = it.next();
1755 <                    if (e == null || !contains(e))
1756 <                        return false;
1751 >    /**
1752 >     * Attempts to compute a mapping for the specified key and its
1753 >     * current mapped value (or {@code null} if there is no current
1754 >     * mapping). The entire method invocation is performed atomically.
1755 >     * Some attempted update operations on this map by other threads
1756 >     * may be blocked while computation is in progress, so the
1757 >     * computation should be short and simple, and must not attempt to
1758 >     * update any other mappings of this Map.
1759 >     *
1760 >     * @param key key with which the specified value is to be associated
1761 >     * @param remappingFunction the function to compute a value
1762 >     * @return the new value associated with the specified key, or null if none
1763 >     * @throws NullPointerException if the specified key or remappingFunction
1764 >     *         is null
1765 >     * @throws IllegalStateException if the computation detectably
1766 >     *         attempts a recursive update to this map that would
1767 >     *         otherwise never complete
1768 >     * @throws RuntimeException or Error if the remappingFunction does so,
1769 >     *         in which case the mapping is unchanged
1770 >     */
1771 >    public V compute(K key,
1772 >                     BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
1773 >        if (key == null || remappingFunction == null)
1774 >            throw new NullPointerException();
1775 >        int h = spread(key.hashCode());
1776 >        V val = null;
1777 >        int delta = 0;
1778 >        int binCount = 0;
1779 >        for (Node<K,V>[] tab = table;;) {
1780 >            Node<K,V> f; int n, i, fh;
1781 >            if (tab == null || (n = tab.length) == 0)
1782 >                tab = initTable();
1783 >            else if ((f = tabAt(tab, i = (n - 1) & h)) == null) {
1784 >                Node<K,V> r = new ReservationNode<K,V>();
1785 >                synchronized (r) {
1786 >                    if (casTabAt(tab, i, null, r)) {
1787 >                        binCount = 1;
1788 >                        Node<K,V> node = null;
1789 >                        try {
1790 >                            if ((val = remappingFunction.apply(key, null)) != null) {
1791 >                                delta = 1;
1792 >                                node = new Node<K,V>(h, key, val, null);
1793 >                            }
1794 >                        } finally {
1795 >                            setTabAt(tab, i, node);
1796 >                        }
1797 >                    }
1798                  }
1799 +                if (binCount != 0)
1800 +                    break;
1801              }
1802 <            return true;
1803 <        }
1804 <
1805 <        public final boolean removeAll(Collection<?> c) {
1806 <            boolean modified = false;
1807 <            for (Iterator<?> it = iterator(); it.hasNext();) {
1808 <                if (c.contains(it.next())) {
1809 <                    it.remove();
1810 <                    modified = true;
1802 >            else if ((fh = f.hash) == MOVED)
1803 >                tab = helpTransfer(tab, f);
1804 >            else {
1805 >                synchronized (f) {
1806 >                    if (tabAt(tab, i) == f) {
1807 >                        if (fh >= 0) {
1808 >                            binCount = 1;
1809 >                            for (Node<K,V> e = f, pred = null;; ++binCount) {
1810 >                                K ek;
1811 >                                if (e.hash == h &&
1812 >                                    ((ek = e.key) == key ||
1813 >                                     (ek != null && key.equals(ek)))) {
1814 >                                    val = remappingFunction.apply(key, e.val);
1815 >                                    if (val != null)
1816 >                                        e.val = val;
1817 >                                    else {
1818 >                                        delta = -1;
1819 >                                        Node<K,V> en = e.next;
1820 >                                        if (pred != null)
1821 >                                            pred.next = en;
1822 >                                        else
1823 >                                            setTabAt(tab, i, en);
1824 >                                    }
1825 >                                    break;
1826 >                                }
1827 >                                pred = e;
1828 >                                if ((e = e.next) == null) {
1829 >                                    val = remappingFunction.apply(key, null);
1830 >                                    if (val != null) {
1831 >                                        delta = 1;
1832 >                                        pred.next =
1833 >                                            new Node<K,V>(h, key, val, null);
1834 >                                    }
1835 >                                    break;
1836 >                                }
1837 >                            }
1838 >                        }
1839 >                        else if (f instanceof TreeBin) {
1840 >                            binCount = 1;
1841 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
1842 >                            TreeNode<K,V> r, p;
1843 >                            if ((r = t.root) != null)
1844 >                                p = r.findTreeNode(h, key, null);
1845 >                            else
1846 >                                p = null;
1847 >                            V pv = (p == null) ? null : p.val;
1848 >                            val = remappingFunction.apply(key, pv);
1849 >                            if (val != null) {
1850 >                                if (p != null)
1851 >                                    p.val = val;
1852 >                                else {
1853 >                                    delta = 1;
1854 >                                    t.putTreeVal(h, key, val);
1855 >                                }
1856 >                            }
1857 >                            else if (p != null) {
1858 >                                delta = -1;
1859 >                                if (t.removeTreeNode(p))
1860 >                                    setTabAt(tab, i, untreeify(t.first));
1861 >                            }
1862 >                        }
1863 >                    }
1864 >                }
1865 >                if (binCount != 0) {
1866 >                    if (binCount >= TREEIFY_THRESHOLD)
1867 >                        treeifyBin(tab, i);
1868 >                    break;
1869                  }
1870              }
3264            return modified;
1871          }
1872 +        if (delta != 0)
1873 +            addCount((long)delta, binCount);
1874 +        return val;
1875 +    }
1876  
1877 <        public final boolean retainAll(Collection<?> c) {
1878 <            boolean modified = false;
1879 <            for (Iterator<?> it = iterator(); it.hasNext();) {
1880 <                if (!c.contains(it.next())) {
1881 <                    it.remove();
1882 <                    modified = true;
1877 >    /**
1878 >     * If the specified key is not already associated with a
1879 >     * (non-null) value, associates it with the given value.
1880 >     * Otherwise, replaces the value with the results of the given
1881 >     * remapping function, or removes if {@code null}. The entire
1882 >     * method invocation is performed atomically.  Some attempted
1883 >     * update operations on this map by other threads may be blocked
1884 >     * while computation is in progress, so the computation should be
1885 >     * short and simple, and must not attempt to update any other
1886 >     * mappings of this Map.
1887 >     *
1888 >     * @param key key with which the specified value is to be associated
1889 >     * @param value the value to use if absent
1890 >     * @param remappingFunction the function to recompute a value if present
1891 >     * @return the new value associated with the specified key, or null if none
1892 >     * @throws NullPointerException if the specified key or the
1893 >     *         remappingFunction is null
1894 >     * @throws RuntimeException or Error if the remappingFunction does so,
1895 >     *         in which case the mapping is unchanged
1896 >     */
1897 >    public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
1898 >        if (key == null || value == null || remappingFunction == null)
1899 >            throw new NullPointerException();
1900 >        int h = spread(key.hashCode());
1901 >        V val = null;
1902 >        int delta = 0;
1903 >        int binCount = 0;
1904 >        for (Node<K,V>[] tab = table;;) {
1905 >            Node<K,V> f; int n, i, fh;
1906 >            if (tab == null || (n = tab.length) == 0)
1907 >                tab = initTable();
1908 >            else if ((f = tabAt(tab, i = (n - 1) & h)) == null) {
1909 >                if (casTabAt(tab, i, null, new Node<K,V>(h, key, value, null))) {
1910 >                    delta = 1;
1911 >                    val = value;
1912 >                    break;
1913 >                }
1914 >            }
1915 >            else if ((fh = f.hash) == MOVED)
1916 >                tab = helpTransfer(tab, f);
1917 >            else {
1918 >                synchronized (f) {
1919 >                    if (tabAt(tab, i) == f) {
1920 >                        if (fh >= 0) {
1921 >                            binCount = 1;
1922 >                            for (Node<K,V> e = f, pred = null;; ++binCount) {
1923 >                                K ek;
1924 >                                if (e.hash == h &&
1925 >                                    ((ek = e.key) == key ||
1926 >                                     (ek != null && key.equals(ek)))) {
1927 >                                    val = remappingFunction.apply(e.val, value);
1928 >                                    if (val != null)
1929 >                                        e.val = val;
1930 >                                    else {
1931 >                                        delta = -1;
1932 >                                        Node<K,V> en = e.next;
1933 >                                        if (pred != null)
1934 >                                            pred.next = en;
1935 >                                        else
1936 >                                            setTabAt(tab, i, en);
1937 >                                    }
1938 >                                    break;
1939 >                                }
1940 >                                pred = e;
1941 >                                if ((e = e.next) == null) {
1942 >                                    delta = 1;
1943 >                                    val = value;
1944 >                                    pred.next =
1945 >                                        new Node<K,V>(h, key, val, null);
1946 >                                    break;
1947 >                                }
1948 >                            }
1949 >                        }
1950 >                        else if (f instanceof TreeBin) {
1951 >                            binCount = 2;
1952 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
1953 >                            TreeNode<K,V> r = t.root;
1954 >                            TreeNode<K,V> p = (r == null) ? null :
1955 >                                r.findTreeNode(h, key, null);
1956 >                            val = (p == null) ? value :
1957 >                                remappingFunction.apply(p.val, value);
1958 >                            if (val != null) {
1959 >                                if (p != null)
1960 >                                    p.val = val;
1961 >                                else {
1962 >                                    delta = 1;
1963 >                                    t.putTreeVal(h, key, val);
1964 >                                }
1965 >                            }
1966 >                            else if (p != null) {
1967 >                                delta = -1;
1968 >                                if (t.removeTreeNode(p))
1969 >                                    setTabAt(tab, i, untreeify(t.first));
1970 >                            }
1971 >                        }
1972 >                    }
1973 >                }
1974 >                if (binCount != 0) {
1975 >                    if (binCount >= TREEIFY_THRESHOLD)
1976 >                        treeifyBin(tab, i);
1977 >                    break;
1978                  }
1979              }
3275            return modified;
1980          }
1981 +        if (delta != 0)
1982 +            addCount((long)delta, binCount);
1983 +        return val;
1984 +    }
1985  
1986 +    // Hashtable legacy methods
1987 +
1988 +    /**
1989 +     * Legacy method testing if some key maps into the specified value
1990 +     * in this table.  This method is identical in functionality to
1991 +     * {@link #containsValue(Object)}, and exists solely to ensure
1992 +     * full compatibility with class {@link java.util.Hashtable},
1993 +     * which supported this method prior to introduction of the
1994 +     * Java Collections framework.
1995 +     *
1996 +     * @param  value a value to search for
1997 +     * @return {@code true} if and only if some key maps to the
1998 +     *         {@code value} argument in this table as
1999 +     *         determined by the {@code equals} method;
2000 +     *         {@code false} otherwise
2001 +     * @throws NullPointerException if the specified value is null
2002 +     */
2003 +    @Deprecated public boolean contains(Object value) {
2004 +        return containsValue(value);
2005      }
2006  
2007 <    static final class KeySet<K,V> extends CHMView<K,V> implements Set<K> {
2008 <        KeySet(ConcurrentHashMap<K, V> map)  {
2009 <            super(map);
2010 <        }
2011 <        public final boolean contains(Object o) { return map.containsKey(o); }
2012 <        public final boolean remove(Object o)   { return map.remove(o) != null; }
2013 <        public final Iterator<K> iterator() {
2014 <            return new KeyIterator<K,V>(map);
2015 <        }
2016 <        public final boolean add(K e) {
2017 <            throw new UnsupportedOperationException();
2007 >    /**
2008 >     * Returns an enumeration of the keys in this table.
2009 >     *
2010 >     * @return an enumeration of the keys in this table
2011 >     * @see #keySet()
2012 >     */
2013 >    public Enumeration<K> keys() {
2014 >        Node<K,V>[] t;
2015 >        int f = (t = table) == null ? 0 : t.length;
2016 >        return new KeyIterator<K,V>(t, f, 0, f, this);
2017 >    }
2018 >
2019 >    /**
2020 >     * Returns an enumeration of the values in this table.
2021 >     *
2022 >     * @return an enumeration of the values in this table
2023 >     * @see #values()
2024 >     */
2025 >    public Enumeration<V> elements() {
2026 >        Node<K,V>[] t;
2027 >        int f = (t = table) == null ? 0 : t.length;
2028 >        return new ValueIterator<K,V>(t, f, 0, f, this);
2029 >    }
2030 >
2031 >    // ConcurrentHashMap-only methods
2032 >
2033 >    /**
2034 >     * Returns the number of mappings. This method should be used
2035 >     * instead of {@link #size} because a ConcurrentHashMap may
2036 >     * contain more mappings than can be represented as an int. The
2037 >     * value returned is an estimate; the actual count may differ if
2038 >     * there are concurrent insertions or removals.
2039 >     *
2040 >     * @return the number of mappings
2041 >     * @since 1.8
2042 >     */
2043 >    public long mappingCount() {
2044 >        long n = sumCount();
2045 >        return (n < 0L) ? 0L : n; // ignore transient negative values
2046 >    }
2047 >
2048 >    /**
2049 >     * Creates a new {@link Set} backed by a ConcurrentHashMap
2050 >     * from the given type to {@code Boolean.TRUE}.
2051 >     *
2052 >     * @return the new set
2053 >     * @since 1.8
2054 >     */
2055 >    public static <K> KeySetView<K,Boolean> newKeySet() {
2056 >        return new KeySetView<K,Boolean>
2057 >            (new ConcurrentHashMap<K,Boolean>(), Boolean.TRUE);
2058 >    }
2059 >
2060 >    /**
2061 >     * Creates a new {@link Set} backed by a ConcurrentHashMap
2062 >     * from the given type to {@code Boolean.TRUE}.
2063 >     *
2064 >     * @param initialCapacity The implementation performs internal
2065 >     * sizing to accommodate this many elements.
2066 >     * @throws IllegalArgumentException if the initial capacity of
2067 >     * elements is negative
2068 >     * @return the new set
2069 >     * @since 1.8
2070 >     */
2071 >    public static <K> KeySetView<K,Boolean> newKeySet(int initialCapacity) {
2072 >        return new KeySetView<K,Boolean>
2073 >            (new ConcurrentHashMap<K,Boolean>(initialCapacity), Boolean.TRUE);
2074 >    }
2075 >
2076 >    /**
2077 >     * Returns a {@link Set} view of the keys in this map, using the
2078 >     * given common mapped value for any additions (i.e., {@link
2079 >     * Collection#add} and {@link Collection#addAll(Collection)}).
2080 >     * This is of course only appropriate if it is acceptable to use
2081 >     * the same value for all additions from this view.
2082 >     *
2083 >     * @param mappedValue the mapped value to use for any additions
2084 >     * @return the set view
2085 >     * @throws NullPointerException if the mappedValue is null
2086 >     */
2087 >    public KeySetView<K,V> keySet(V mappedValue) {
2088 >        if (mappedValue == null)
2089 >            throw new NullPointerException();
2090 >        return new KeySetView<K,V>(this, mappedValue);
2091 >    }
2092 >
2093 >    /* ---------------- Special Nodes -------------- */
2094 >
2095 >    /**
2096 >     * A node inserted at head of bins during transfer operations.
2097 >     */
2098 >    static final class ForwardingNode<K,V> extends Node<K,V> {
2099 >        final Node<K,V>[] nextTable;
2100 >        ForwardingNode(Node<K,V>[] tab) {
2101 >            super(MOVED, null, null, null);
2102 >            this.nextTable = tab;
2103 >        }
2104 >
2105 >        Node<K,V> find(int h, Object k) {
2106 >            Node<K,V> e; int n;
2107 >            Node<K,V>[] tab = nextTable;
2108 >            if (k != null && tab != null && (n = tab.length) > 0 &&
2109 >                (e = tabAt(tab, (n - 1) & h)) != null) {
2110 >                do {
2111 >                    int eh; K ek;
2112 >                    if ((eh = e.hash) == h &&
2113 >                        ((ek = e.key) == k || (ek != null && k.equals(ek))))
2114 >                        return e;
2115 >                    if (eh < 0)
2116 >                        return e.find(h, k);
2117 >                } while ((e = e.next) != null);
2118 >            }
2119 >            return null;
2120          }
2121 <        public final boolean addAll(Collection<? extends K> c) {
2122 <            throw new UnsupportedOperationException();
2121 >    }
2122 >
2123 >    /**
2124 >     * A place-holder node used in computeIfAbsent and compute
2125 >     */
2126 >    static final class ReservationNode<K,V> extends Node<K,V> {
2127 >        ReservationNode() {
2128 >            super(RESERVED, null, null, null);
2129          }
2130 <        public boolean equals(Object o) {
2131 <            Set<?> c;
2132 <            return ((o instanceof Set) &&
3298 <                    ((c = (Set<?>)o) == this ||
3299 <                     (containsAll(c) && c.containsAll(this))));
2130 >
2131 >        Node<K,V> find(int h, Object k) {
2132 >            return null;
2133          }
2134      }
2135  
2136 +    /* ---------------- Table Initialization and Resizing -------------- */
2137  
2138 <    static final class Values<K,V> extends CHMView<K,V>
2139 <        implements Collection<V> {
2140 <        Values(ConcurrentHashMap<K, V> map)   { super(map); }
2141 <        public final boolean contains(Object o) { return map.containsValue(o); }
2142 <        public final boolean remove(Object o) {
2143 <            if (o != null) {
2144 <                Iterator<V> it = new ValueIterator<K,V>(map);
2145 <                while (it.hasNext()) {
2146 <                    if (o.equals(it.next())) {
2147 <                        it.remove();
2148 <                        return true;
2138 >    /**
2139 >     * Initializes table, using the size recorded in sizeCtl.
2140 >     */
2141 >    private final Node<K,V>[] initTable() {
2142 >        Node<K,V>[] tab; int sc;
2143 >        while ((tab = table) == null || tab.length == 0) {
2144 >            if ((sc = sizeCtl) < 0)
2145 >                Thread.yield(); // lost initialization race; just spin
2146 >            else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
2147 >                try {
2148 >                    if ((tab = table) == null || tab.length == 0) {
2149 >                        int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
2150 >                        @SuppressWarnings({"rawtypes","unchecked"})
2151 >                            Node<K,V>[] nt = (Node<K,V>[])new Node[n];
2152 >                        table = tab = nt;
2153 >                        sc = n - (n >>> 2);
2154                      }
2155 +                } finally {
2156 +                    sizeCtl = sc;
2157                  }
2158 +                break;
2159              }
3318            return false;
2160          }
2161 <        public final Iterator<V> iterator() {
3321 <            return new ValueIterator<K,V>(map);
3322 <        }
3323 <        public final boolean add(V e) {
3324 <            throw new UnsupportedOperationException();
3325 <        }
3326 <        public final boolean addAll(Collection<? extends V> c) {
3327 <            throw new UnsupportedOperationException();
3328 <        }
3329 <
2161 >        return tab;
2162      }
2163  
2164 <    static final class EntrySet<K,V> extends CHMView<K,V>
2165 <        implements Set<Map.Entry<K,V>> {
2166 <        EntrySet(ConcurrentHashMap<K, V> map) { super(map); }
2167 <        public final boolean contains(Object o) {
2168 <            Object k, v, r; Map.Entry<?,?> e;
2169 <            return ((o instanceof Map.Entry) &&
2170 <                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
2171 <                    (r = map.get(k)) != null &&
2172 <                    (v = e.getValue()) != null &&
2173 <                    (v == r || v.equals(r)));
2174 <        }
2175 <        public final boolean remove(Object o) {
2176 <            Object k, v; Map.Entry<?,?> e;
2177 <            return ((o instanceof Map.Entry) &&
2178 <                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
2179 <                    (v = e.getValue()) != null &&
2180 <                    map.remove(k, v));
2181 <        }
2182 <        public final Iterator<Map.Entry<K,V>> iterator() {
2183 <            return new EntryIterator<K,V>(map);
2184 <        }
2185 <        public final boolean add(Entry<K,V> e) {
2186 <            throw new UnsupportedOperationException();
2187 <        }
2188 <        public final boolean addAll(Collection<? extends Entry<K,V>> c) {
2189 <            throw new UnsupportedOperationException();
2190 <        }
2191 <        public boolean equals(Object o) {
2192 <            Set<?> c;
2193 <            return ((o instanceof Set) &&
2194 <                    ((c = (Set<?>)o) == this ||
2195 <                     (containsAll(c) && c.containsAll(this))));
2164 >    /**
2165 >     * Adds to count, and if table is too small and not already
2166 >     * resizing, initiates transfer. If already resizing, helps
2167 >     * perform transfer if work is available.  Rechecks occupancy
2168 >     * after a transfer to see if another resize is already needed
2169 >     * because resizings are lagging additions.
2170 >     *
2171 >     * @param x the count to add
2172 >     * @param check if <0, don't check resize, if <= 1 only check if uncontended
2173 >     */
2174 >    private final void addCount(long x, int check) {
2175 >        CounterCell[] as; long b, s;
2176 >        if ((as = counterCells) != null ||
2177 >            !U.compareAndSwapLong(this, BASECOUNT, b = baseCount, s = b + x)) {
2178 >            CounterCell a; long v; int m;
2179 >            boolean uncontended = true;
2180 >            if (as == null || (m = as.length - 1) < 0 ||
2181 >                (a = as[ThreadLocalRandom.getProbe() & m]) == null ||
2182 >                !(uncontended =
2183 >                  U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))) {
2184 >                fullAddCount(x, uncontended);
2185 >                return;
2186 >            }
2187 >            if (check <= 1)
2188 >                return;
2189 >            s = sumCount();
2190 >        }
2191 >        if (check >= 0) {
2192 >            Node<K,V>[] tab, nt; int sc;
2193 >            while (s >= (long)(sc = sizeCtl) && (tab = table) != null &&
2194 >                   tab.length < MAXIMUM_CAPACITY) {
2195 >                if (sc < 0) {
2196 >                    if (sc == -1 || transferIndex <= transferOrigin ||
2197 >                        (nt = nextTable) == null)
2198 >                        break;
2199 >                    if (U.compareAndSwapInt(this, SIZECTL, sc, sc - 1))
2200 >                        transfer(tab, nt);
2201 >                }
2202 >                else if (U.compareAndSwapInt(this, SIZECTL, sc, -2))
2203 >                    transfer(tab, null);
2204 >                s = sumCount();
2205 >            }
2206          }
2207      }
2208  
2209 <    /* ---------------- Serialization Support -------------- */
2209 >    /**
2210 >     * Helps transfer if a resize is in progress.
2211 >     */
2212 >    final Node<K,V>[] helpTransfer(Node<K,V>[] tab, Node<K,V> f) {
2213 >        Node<K,V>[] nextTab; int sc;
2214 >        if ((f instanceof ForwardingNode) &&
2215 >            (nextTab = ((ForwardingNode<K,V>)f).nextTable) != null) {
2216 >            if (nextTab == nextTable && tab == table &&
2217 >                transferIndex > transferOrigin && (sc = sizeCtl) < -1 &&
2218 >                U.compareAndSwapInt(this, SIZECTL, sc, sc - 1))
2219 >                transfer(tab, nextTab);
2220 >            return nextTab;
2221 >        }
2222 >        return table;
2223 >    }
2224  
2225      /**
2226 <     * Stripped-down version of helper class used in previous version,
2227 <     * declared for the sake of serialization compatibility
2226 >     * Tries to presize table to accommodate the given number of elements.
2227 >     *
2228 >     * @param size number of elements (doesn't need to be perfectly accurate)
2229       */
2230 <    static class Segment<K,V> implements Serializable {
2231 <        private static final long serialVersionUID = 2249069246763182397L;
2232 <        final float loadFactor;
2233 <        Segment(float lf) { this.loadFactor = lf; }
2230 >    private final void tryPresize(int size) {
2231 >        int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
2232 >            tableSizeFor(size + (size >>> 1) + 1);
2233 >        int sc;
2234 >        while ((sc = sizeCtl) >= 0) {
2235 >            Node<K,V>[] tab = table; int n;
2236 >            if (tab == null || (n = tab.length) == 0) {
2237 >                n = (sc > c) ? sc : c;
2238 >                if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
2239 >                    try {
2240 >                        if (table == tab) {
2241 >                            @SuppressWarnings({"rawtypes","unchecked"})
2242 >                                Node<K,V>[] nt = (Node<K,V>[])new Node[n];
2243 >                            table = nt;
2244 >                            sc = n - (n >>> 2);
2245 >                        }
2246 >                    } finally {
2247 >                        sizeCtl = sc;
2248 >                    }
2249 >                }
2250 >            }
2251 >            else if (c <= sc || n >= MAXIMUM_CAPACITY)
2252 >                break;
2253 >            else if (tab == table &&
2254 >                     U.compareAndSwapInt(this, SIZECTL, sc, -2))
2255 >                transfer(tab, null);
2256 >        }
2257      }
2258  
2259      /**
2260 <     * Saves the state of the {@code ConcurrentHashMap} instance to a
2261 <     * stream (i.e., serializes it).
3382 <     * @param s the stream
3383 <     * @serialData
3384 <     * the key (Object) and value (Object)
3385 <     * for each key-value mapping, followed by a null pair.
3386 <     * The key-value mappings are emitted in no particular order.
2260 >     * Moves and/or copies the nodes in each bin to new table. See
2261 >     * above for explanation.
2262       */
2263 <    @SuppressWarnings("unchecked") private void writeObject(java.io.ObjectOutputStream s)
2264 <        throws java.io.IOException {
2265 <        if (segments == null) { // for serialization compatibility
2266 <            segments = (Segment<K,V>[])
2267 <                new Segment<?,?>[DEFAULT_CONCURRENCY_LEVEL];
2268 <            for (int i = 0; i < segments.length; ++i)
2269 <                segments[i] = new Segment<K,V>(LOAD_FACTOR);
2270 <        }
2271 <        s.defaultWriteObject();
2272 <        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
2273 <        Object v;
2274 <        while ((v = it.advance()) != null) {
2275 <            s.writeObject(it.nextKey);
2276 <            s.writeObject(v);
2263 >    private final void transfer(Node<K,V>[] tab, Node<K,V>[] nextTab) {
2264 >        int n = tab.length, stride;
2265 >        if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE)
2266 >            stride = MIN_TRANSFER_STRIDE; // subdivide range
2267 >        if (nextTab == null) {            // initiating
2268 >            try {
2269 >                @SuppressWarnings({"rawtypes","unchecked"})
2270 >                    Node<K,V>[] nt = (Node<K,V>[])new Node[n << 1];
2271 >                nextTab = nt;
2272 >            } catch (Throwable ex) {      // try to cope with OOME
2273 >                sizeCtl = Integer.MAX_VALUE;
2274 >                return;
2275 >            }
2276 >            nextTable = nextTab;
2277 >            transferOrigin = n;
2278 >            transferIndex = n;
2279 >            ForwardingNode<K,V> rev = new ForwardingNode<K,V>(tab);
2280 >            for (int k = n; k > 0;) {    // progressively reveal ready slots
2281 >                int nextk = (k > stride) ? k - stride : 0;
2282 >                for (int m = nextk; m < k; ++m)
2283 >                    nextTab[m] = rev;
2284 >                for (int m = n + nextk; m < n + k; ++m)
2285 >                    nextTab[m] = rev;
2286 >                U.putOrderedInt(this, TRANSFERORIGIN, k = nextk);
2287 >            }
2288 >        }
2289 >        int nextn = nextTab.length;
2290 >        ForwardingNode<K,V> fwd = new ForwardingNode<K,V>(nextTab);
2291 >        boolean advance = true;
2292 >        for (int i = 0, bound = 0;;) {
2293 >            int nextIndex, nextBound, fh; Node<K,V> f;
2294 >            while (advance) {
2295 >                if (--i >= bound)
2296 >                    advance = false;
2297 >                else if ((nextIndex = transferIndex) <= transferOrigin) {
2298 >                    i = -1;
2299 >                    advance = false;
2300 >                }
2301 >                else if (U.compareAndSwapInt
2302 >                         (this, TRANSFERINDEX, nextIndex,
2303 >                          nextBound = (nextIndex > stride ?
2304 >                                       nextIndex - stride : 0))) {
2305 >                    bound = nextBound;
2306 >                    i = nextIndex - 1;
2307 >                    advance = false;
2308 >                }
2309 >            }
2310 >            if (i < 0 || i >= n || i + n >= nextn) {
2311 >                for (int sc;;) {
2312 >                    if (U.compareAndSwapInt(this, SIZECTL, sc = sizeCtl, ++sc)) {
2313 >                        if (sc == -1) {
2314 >                            nextTable = null;
2315 >                            table = nextTab;
2316 >                            sizeCtl = (n << 1) - (n >>> 1);
2317 >                        }
2318 >                        return;
2319 >                    }
2320 >                }
2321 >            }
2322 >            else if ((f = tabAt(tab, i)) == null) {
2323 >                if (casTabAt(tab, i, null, fwd)) {
2324 >                    setTabAt(nextTab, i, null);
2325 >                    setTabAt(nextTab, i + n, null);
2326 >                    advance = true;
2327 >                }
2328 >            }
2329 >            else if ((fh = f.hash) == MOVED)
2330 >                advance = true; // already processed
2331 >            else {
2332 >                synchronized (f) {
2333 >                    if (tabAt(tab, i) == f) {
2334 >                        Node<K,V> ln, hn;
2335 >                        if (fh >= 0) {
2336 >                            int runBit = fh & n;
2337 >                            Node<K,V> lastRun = f;
2338 >                            for (Node<K,V> p = f.next; p != null; p = p.next) {
2339 >                                int b = p.hash & n;
2340 >                                if (b != runBit) {
2341 >                                    runBit = b;
2342 >                                    lastRun = p;
2343 >                                }
2344 >                            }
2345 >                            if (runBit == 0) {
2346 >                                ln = lastRun;
2347 >                                hn = null;
2348 >                            }
2349 >                            else {
2350 >                                hn = lastRun;
2351 >                                ln = null;
2352 >                            }
2353 >                            for (Node<K,V> p = f; p != lastRun; p = p.next) {
2354 >                                int ph = p.hash; K pk = p.key; V pv = p.val;
2355 >                                if ((ph & n) == 0)
2356 >                                    ln = new Node<K,V>(ph, pk, pv, ln);
2357 >                                else
2358 >                                    hn = new Node<K,V>(ph, pk, pv, hn);
2359 >                            }
2360 >                        }
2361 >                        else if (f instanceof TreeBin) {
2362 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
2363 >                            TreeNode<K,V> lo = null, loTail = null;
2364 >                            TreeNode<K,V> hi = null, hiTail = null;
2365 >                            int lc = 0, hc = 0;
2366 >                            for (Node<K,V> e = t.first; e != null; e = e.next) {
2367 >                                int h = e.hash;
2368 >                                TreeNode<K,V> p = new TreeNode<K,V>
2369 >                                    (h, e.key, e.val, null, null);
2370 >                                if ((h & n) == 0) {
2371 >                                    if ((p.prev = loTail) == null)
2372 >                                        lo = p;
2373 >                                    else
2374 >                                        loTail.next = p;
2375 >                                    loTail = p;
2376 >                                    ++lc;
2377 >                                }
2378 >                                else {
2379 >                                    if ((p.prev = hiTail) == null)
2380 >                                        hi = p;
2381 >                                    else
2382 >                                        hiTail.next = p;
2383 >                                    hiTail = p;
2384 >                                    ++hc;
2385 >                                }
2386 >                            }
2387 >                            ln = (lc <= UNTREEIFY_THRESHOLD) ? untreeify(lo) :
2388 >                                (hc != 0) ? new TreeBin<K,V>(lo) : t;
2389 >                            hn = (hc <= UNTREEIFY_THRESHOLD) ? untreeify(hi) :
2390 >                                (lc != 0) ? new TreeBin<K,V>(hi) : t;
2391 >                        }
2392 >                        else
2393 >                            ln = hn = null;
2394 >                        setTabAt(nextTab, i, ln);
2395 >                        setTabAt(nextTab, i + n, hn);
2396 >                        setTabAt(tab, i, fwd);
2397 >                        advance = true;
2398 >                    }
2399 >                }
2400 >            }
2401          }
3403        s.writeObject(null);
3404        s.writeObject(null);
3405        segments = null; // throw away
2402      }
2403  
2404 +    /* ---------------- Counter support -------------- */
2405 +
2406      /**
2407 <     * Reconstitutes the instance from a stream (that is, deserializes it).
2408 <     * @param s the stream
2407 >     * A padded cell for distributing counts.  Adapted from LongAdder
2408 >     * and Striped64.  See their internal docs for explanation.
2409       */
2410 <    @SuppressWarnings("unchecked") private void readObject(java.io.ObjectInputStream s)
2411 <        throws java.io.IOException, ClassNotFoundException {
2412 <        s.defaultReadObject();
2413 <        this.segments = null; // unneeded
3416 <        // initialize transient final field
3417 <        UNSAFE.putObjectVolatile(this, counterOffset, new LongAdder());
2410 >    @sun.misc.Contended static final class CounterCell {
2411 >        volatile long value;
2412 >        CounterCell(long x) { value = x; }
2413 >    }
2414  
2415 <        // Create all nodes, then place in table once size is known
2416 <        long size = 0L;
2417 <        Node p = null;
2418 <        for (;;) {
2419 <            K k = (K) s.readObject();
2420 <            V v = (V) s.readObject();
2421 <            if (k != null && v != null) {
3426 <                int h = spread(k.hashCode());
3427 <                p = new Node(h, k, v, p);
3428 <                ++size;
2415 >    final long sumCount() {
2416 >        CounterCell[] as = counterCells; CounterCell a;
2417 >        long sum = baseCount;
2418 >        if (as != null) {
2419 >            for (int i = 0; i < as.length; ++i) {
2420 >                if ((a = as[i]) != null)
2421 >                    sum += a.value;
2422              }
3430            else
3431                break;
2423          }
2424 <        if (p != null) {
2425 <            boolean init = false;
2426 <            int n;
2427 <            if (size >= (long)(MAXIMUM_CAPACITY >>> 1))
2428 <                n = MAXIMUM_CAPACITY;
2429 <            else {
2430 <                int sz = (int)size;
2431 <                n = tableSizeFor(sz + (sz >>> 1) + 1);
2432 <            }
2433 <            int sc = sizeCtl;
2434 <            boolean collide = false;
2435 <            if (n > sc &&
2436 <                UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
2437 <                try {
2438 <                    if (table == null) {
2439 <                        init = true;
2440 <                        Node[] tab = new Node[n];
2441 <                        int mask = n - 1;
2442 <                        while (p != null) {
2443 <                            int j = p.hash & mask;
2444 <                            Node next = p.next;
2445 <                            Node q = p.next = tabAt(tab, j);
2446 <                            setTabAt(tab, j, p);
2447 <                            if (!collide && q != null && q.hash == p.hash)
2448 <                                collide = true;
2449 <                            p = next;
2424 >        return sum;
2425 >    }
2426 >
2427 >    // See LongAdder version for explanation
2428 >    private final void fullAddCount(long x, boolean wasUncontended) {
2429 >        int h;
2430 >        if ((h = ThreadLocalRandom.getProbe()) == 0) {
2431 >            ThreadLocalRandom.localInit();      // force initialization
2432 >            h = ThreadLocalRandom.getProbe();
2433 >            wasUncontended = true;
2434 >        }
2435 >        boolean collide = false;                // True if last slot nonempty
2436 >        for (;;) {
2437 >            CounterCell[] as; CounterCell a; int n; long v;
2438 >            if ((as = counterCells) != null && (n = as.length) > 0) {
2439 >                if ((a = as[(n - 1) & h]) == null) {
2440 >                    if (cellsBusy == 0) {            // Try to attach new Cell
2441 >                        CounterCell r = new CounterCell(x); // Optimistic create
2442 >                        if (cellsBusy == 0 &&
2443 >                            U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
2444 >                            boolean created = false;
2445 >                            try {               // Recheck under lock
2446 >                                CounterCell[] rs; int m, j;
2447 >                                if ((rs = counterCells) != null &&
2448 >                                    (m = rs.length) > 0 &&
2449 >                                    rs[j = (m - 1) & h] == null) {
2450 >                                    rs[j] = r;
2451 >                                    created = true;
2452 >                                }
2453 >                            } finally {
2454 >                                cellsBusy = 0;
2455 >                            }
2456 >                            if (created)
2457 >                                break;
2458 >                            continue;           // Slot is now non-empty
2459                          }
3460                        table = tab;
3461                        counter.add(size);
3462                        sc = n - (n >>> 2);
2460                      }
2461 <                } finally {
3465 <                    sizeCtl = sc;
2461 >                    collide = false;
2462                  }
2463 <                if (collide) { // rescan and convert to TreeBins
2464 <                    Node[] tab = table;
2465 <                    for (int i = 0; i < tab.length; ++i) {
2466 <                        int c = 0;
2467 <                        for (Node e = tabAt(tab, i); e != null; e = e.next) {
2468 <                            if (++c > TREE_THRESHOLD &&
2469 <                                (e.key instanceof Comparable)) {
2470 <                                replaceWithTreeBin(tab, i, e.key);
2471 <                                break;
2472 <                            }
2463 >                else if (!wasUncontended)       // CAS already known to fail
2464 >                    wasUncontended = true;      // Continue after rehash
2465 >                else if (U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))
2466 >                    break;
2467 >                else if (counterCells != as || n >= NCPU)
2468 >                    collide = false;            // At max size or stale
2469 >                else if (!collide)
2470 >                    collide = true;
2471 >                else if (cellsBusy == 0 &&
2472 >                         U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
2473 >                    try {
2474 >                        if (counterCells == as) {// Expand table unless stale
2475 >                            CounterCell[] rs = new CounterCell[n << 1];
2476 >                            for (int i = 0; i < n; ++i)
2477 >                                rs[i] = as[i];
2478 >                            counterCells = rs;
2479                          }
2480 +                    } finally {
2481 +                        cellsBusy = 0;
2482                      }
2483 +                    collide = false;
2484 +                    continue;                   // Retry with expanded table
2485                  }
2486 +                h = ThreadLocalRandom.advanceProbe(h);
2487              }
2488 <            if (!init) { // Can only happen if unsafely published.
2489 <                while (p != null) {
2490 <                    internalPut(p.key, p.val);
2491 <                    p = p.next;
2488 >            else if (cellsBusy == 0 && counterCells == as &&
2489 >                     U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
2490 >                boolean init = false;
2491 >                try {                           // Initialize table
2492 >                    if (counterCells == as) {
2493 >                        CounterCell[] rs = new CounterCell[2];
2494 >                        rs[h & 1] = new CounterCell(x);
2495 >                        counterCells = rs;
2496 >                        init = true;
2497 >                    }
2498 >                } finally {
2499 >                    cellsBusy = 0;
2500                  }
2501 +                if (init)
2502 +                    break;
2503              }
2504 +            else if (U.compareAndSwapLong(this, BASECOUNT, v = baseCount, v + x))
2505 +                break;                          // Fall back on using base
2506          }
2507      }
2508  
2509 +    /* ---------------- Conversion from/to TreeBins -------------- */
2510  
2511 <    // -------------------------------------------------------
2512 <
2513 <    // Sams
2514 <    /** Interface describing a void action of one argument */
2515 <    public interface Action<A> { void apply(A a); }
2516 <    /** Interface describing a void action of two arguments */
2517 <    public interface BiAction<A,B> { void apply(A a, B b); }
2518 <    /** Interface describing a function of one argument */
2519 <    public interface Fun<A,T> { T apply(A a); }
2520 <    /** Interface describing a function of two arguments */
2521 <    public interface BiFun<A,B,T> { T apply(A a, B b); }
2522 <    /** Interface describing a function of no arguments */
2523 <    public interface Generator<T> { T apply(); }
2524 <    /** Interface describing a function mapping its argument to a double */
2525 <    public interface ObjectToDouble<A> { double apply(A a); }
2526 <    /** Interface describing a function mapping its argument to a long */
2527 <    public interface ObjectToLong<A> { long apply(A a); }
2528 <    /** Interface describing a function mapping its argument to an int */
2529 <    public interface ObjectToInt<A> {int apply(A a); }
2530 <    /** Interface describing a function mapping two arguments to a double */
2531 <    public interface ObjectByObjectToDouble<A,B> { double apply(A a, B b); }
2532 <    /** Interface describing a function mapping two arguments to a long */
2533 <    public interface ObjectByObjectToLong<A,B> { long apply(A a, B b); }
2534 <    /** Interface describing a function mapping two arguments to an int */
2535 <    public interface ObjectByObjectToInt<A,B> {int apply(A a, B b); }
2536 <    /** Interface describing a function mapping a double to a double */
2537 <    public interface DoubleToDouble { double apply(double a); }
2538 <    /** Interface describing a function mapping a long to a long */
2539 <    public interface LongToLong { long apply(long a); }
2540 <    /** Interface describing a function mapping an int to an int */
2541 <    public interface IntToInt { int apply(int a); }
2542 <    /** Interface describing a function mapping two doubles to a double */
3523 <    public interface DoubleByDoubleToDouble { double apply(double a, double b); }
3524 <    /** Interface describing a function mapping two longs to a long */
3525 <    public interface LongByLongToLong { long apply(long a, long b); }
3526 <    /** Interface describing a function mapping two ints to an int */
3527 <    public interface IntByIntToInt { int apply(int a, int b); }
3528 <
3529 <
3530 <    // -------------------------------------------------------
2511 >    /**
2512 >     * Replaces all linked nodes in bin at given index unless table is
2513 >     * too small, in which case resizes instead.
2514 >     */
2515 >    private final void treeifyBin(Node<K,V>[] tab, int index) {
2516 >        Node<K,V> b; int n, sc;
2517 >        if (tab != null) {
2518 >            if ((n = tab.length) < MIN_TREEIFY_CAPACITY) {
2519 >                if (tab == table && (sc = sizeCtl) >= 0 &&
2520 >                    U.compareAndSwapInt(this, SIZECTL, sc, -2))
2521 >                    transfer(tab, null);
2522 >            }
2523 >            else if ((b = tabAt(tab, index)) != null) {
2524 >                synchronized (b) {
2525 >                    if (tabAt(tab, index) == b) {
2526 >                        TreeNode<K,V> hd = null, tl = null;
2527 >                        for (Node<K,V> e = b; e != null; e = e.next) {
2528 >                            TreeNode<K,V> p =
2529 >                                new TreeNode<K,V>(e.hash, e.key, e.val,
2530 >                                                  null, null);
2531 >                            if ((p.prev = tl) == null)
2532 >                                hd = p;
2533 >                            else
2534 >                                tl.next = p;
2535 >                            tl = p;
2536 >                        }
2537 >                        setTabAt(tab, index, new TreeBin<K,V>(hd));
2538 >                    }
2539 >                }
2540 >            }
2541 >        }
2542 >    }
2543  
2544      /**
2545 <     * Returns an extended {@link Parallel} view of this map using the
3534 <     * given executor for bulk parallel operations.
3535 <     *
3536 <     * @param executor the executor
3537 <     * @return a parallel view
2545 >     * Returns a list on non-TreeNodes replacing those in given list
2546       */
2547 <    public Parallel parallel(ForkJoinPool executor)  {
2548 <        return new Parallel(executor);
2547 >    static <K,V> Node<K,V> untreeify(Node<K,V> b) {
2548 >        Node<K,V> hd = null, tl = null;
2549 >        for (Node<K,V> q = b; q != null; q = q.next) {
2550 >            Node<K,V> p = new Node<K,V>(q.hash, q.key, q.val, null);
2551 >            if (tl == null)
2552 >                hd = p;
2553 >            else
2554 >                tl.next = p;
2555 >            tl = p;
2556 >        }
2557 >        return hd;
2558      }
2559  
2560 +    /* ---------------- TreeNodes -------------- */
2561 +
2562      /**
2563 <     * An extended view of a ConcurrentHashMap supporting bulk
3545 <     * parallel operations. These operations are designed to be
3546 <     * safely, and often sensibly, applied even with maps that are
3547 <     * being concurrently updated by other threads; for example, when
3548 <     * computing a snapshot summary of the values in a shared
3549 <     * registry.  There are three kinds of operation, each with four
3550 <     * forms, accepting functions with Keys, Values, Entries, and
3551 <     * (Key, Value) arguments and/or return values. Because the
3552 <     * elements of a ConcurrentHashMap are not ordered in any
3553 <     * particular way, and may be processed in different orders in
3554 <     * different parallel executions, the correctness of supplied
3555 <     * functions should not depend on any ordering, or on any other
3556 <     * objects or values that may transiently change while computation
3557 <     * is in progress; and except for forEach actions, should ideally
3558 <     * be side-effect-free.
3559 <     *
3560 <     * <ul>
3561 <     * <li> forEach: Perform a given action on each element.
3562 <     * A variant form applies a given transformation on each element
3563 <     * before performing the action.</li>
3564 <     *
3565 <     * <li> search: Return the first available non-null result of
3566 <     * applying a given function on each element; skipping further
3567 <     * search when a result is found.</li>
3568 <     *
3569 <     * <li> reduce: Accumulate each element.  The supplied reduction
3570 <     * function cannot rely on ordering (more formally, it should be
3571 <     * both associative and commutative).  There are five variants:
3572 <     *
3573 <     * <ul>
3574 <     *
3575 <     * <li> Plain reductions. (There is not a form of this method for
3576 <     * (key, value) function arguments since there is no corresponding
3577 <     * return type.)</li>
3578 <     *
3579 <     * <li> Mapped reductions that accumulate the results of a given
3580 <     * function applied to each element.</li>
3581 <     *
3582 <     * <li> Reductions to scalar doubles, longs, and ints, using a
3583 <     * given basis value.</li>
3584 <     *
3585 <     * </li>
3586 <     * </ul>
3587 <     * </ul>
3588 <     *
3589 <     * <p>The concurrency properties of the bulk operations follow
3590 <     * from those of ConcurrentHashMap: Any non-null result returned
3591 <     * from {@code get(key)} and related access methods bears a
3592 <     * happens-before relation with the associated insertion or
3593 <     * update.  The result of any bulk operation reflects the
3594 <     * composition of these per-element relations (but is not
3595 <     * necessarily atomic with respect to the map as a whole unless it
3596 <     * is somehow known to be quiescent).  Conversely, because keys
3597 <     * and values in the map are never null, null serves as a reliable
3598 <     * atomic indicator of the current lack of any result.  To
3599 <     * maintain this property, null serves as an implicit basis for
3600 <     * all non-scalar reduction operations. For the double, long, and
3601 <     * int versions, the basis should be one that, when combined with
3602 <     * any other value, returns that other value (more formally, it
3603 <     * should be the identity element for the reduction). Most common
3604 <     * reductions have these properties; for example, computing a sum
3605 <     * with basis 0 or a minimum with basis MAX_VALUE.
3606 <     *
3607 <     * <p>Search and transformation functions provided as arguments
3608 <     * should similarly return null to indicate the lack of any result
3609 <     * (in which case it is not used). In the case of mapped
3610 <     * reductions, this also enables transformations to serve as
3611 <     * filters, returning null (or, in the case of primitive
3612 <     * specializations, the identity basis) if the element should not
3613 <     * be combined. You can create compound transformations and
3614 <     * filterings by composing them yourself under this "null means
3615 <     * there is nothing there now" rule before using them in search or
3616 <     * reduce operations.
3617 <     *
3618 <     * <p>Methods accepting and/or returning Entry arguments maintain
3619 <     * key-value associations. They may be useful for example when
3620 <     * finding the key for the greatest value. Note that "plain" Entry
3621 <     * arguments can be supplied using {@code new
3622 <     * AbstractMap.SimpleEntry(k,v)}.
3623 <     *
3624 <     * <p> Bulk operations may complete abruptly, throwing an
3625 <     * exception encountered in the application of a supplied
3626 <     * function. Bear in mind when handling such exceptions that other
3627 <     * concurrently executing functions could also have thrown
3628 <     * exceptions, or would have done so if the first exception had
3629 <     * not occurred.
3630 <     *
3631 <     * <p>Parallel speedups compared to sequential processing are
3632 <     * common but not guaranteed.  Operations involving brief
3633 <     * functions on small maps may execute more slowly than sequential
3634 <     * loops if the underlying work to parallelize the computation is
3635 <     * more expensive than the computation itself. Similarly,
3636 <     * parallelization may not lead to much actual parallelism if all
3637 <     * processors are busy performing unrelated tasks.
3638 <     *
3639 <     * <p> All arguments to all task methods must be non-null.
3640 <     *
3641 <     * <p><em>jsr166e note: During transition, this class
3642 <     * uses nested functional interfaces with different names but the
3643 <     * same forms as those expected for JDK8.<em>
2563 >     * Nodes for use in TreeBins
2564       */
2565 <    public class Parallel {
2566 <        final ForkJoinPool fjp;
2565 >    static final class TreeNode<K,V> extends Node<K,V> {
2566 >        TreeNode<K,V> parent;  // red-black tree links
2567 >        TreeNode<K,V> left;
2568 >        TreeNode<K,V> right;
2569 >        TreeNode<K,V> prev;    // needed to unlink next upon deletion
2570 >        boolean red;
2571  
2572 <        /**
2573 <         * Returns an extended view of this map using the given
2574 <         * executor for bulk parallel operations.
2575 <         *
3652 <         * @param executor the executor
3653 <         */
3654 <        public Parallel(ForkJoinPool executor)  {
3655 <            this.fjp = executor;
2572 >        TreeNode(int hash, K key, V val, Node<K,V> next,
2573 >                 TreeNode<K,V> parent) {
2574 >            super(hash, key, val, next);
2575 >            this.parent = parent;
2576          }
2577  
2578 <        /**
2579 <         * Performs the given action for each (key, value).
3660 <         *
3661 <         * @param action the action
3662 <         */
3663 <        public void forEach(BiAction<K,V> action) {
3664 <            fjp.invoke(ForkJoinTasks.forEach
3665 <                       (ConcurrentHashMap.this, action));
2578 >        Node<K,V> find(int h, Object k) {
2579 >            return findTreeNode(h, k, null);
2580          }
2581  
2582          /**
2583 <         * Performs the given action for each non-null transformation
2584 <         * of each (key, value).
3671 <         *
3672 <         * @param transformer a function returning the transformation
3673 <         * for an element, or null if there is no transformation (in
3674 <         * which case the action is not applied)
3675 <         * @param action the action
2583 >         * Returns the TreeNode (or null if not found) for the given key
2584 >         * starting at given root.
2585           */
2586 <        public <U> void forEach(BiFun<? super K, ? super V, ? extends U> transformer,
2587 <                                Action<U> action) {
2588 <            fjp.invoke(ForkJoinTasks.forEach
2589 <                       (ConcurrentHashMap.this, transformer, action));
2586 >        final TreeNode<K,V> findTreeNode(int h, Object k, Class<?> kc) {
2587 >            if (k != null) {
2588 >                TreeNode<K,V> p = this;
2589 >                do  {
2590 >                    int ph, dir; K pk; TreeNode<K,V> q;
2591 >                    TreeNode<K,V> pl = p.left, pr = p.right;
2592 >                    if ((ph = p.hash) > h)
2593 >                        p = pl;
2594 >                    else if (ph < h)
2595 >                        p = pr;
2596 >                    else if ((pk = p.key) == k || (pk != null && k.equals(pk)))
2597 >                        return p;
2598 >                    else if (pl == null && pr == null)
2599 >                        break;
2600 >                    else if ((kc != null ||
2601 >                              (kc = comparableClassFor(k)) != null) &&
2602 >                             (dir = compareComparables(kc, k, pk)) != 0)
2603 >                        p = (dir < 0) ? pl : pr;
2604 >                    else if (pl == null)
2605 >                        p = pr;
2606 >                    else if (pr == null ||
2607 >                             (q = pr.findTreeNode(h, k, kc)) == null)
2608 >                        p = pl;
2609 >                    else
2610 >                        return q;
2611 >                } while (p != null);
2612 >            }
2613 >            return null;
2614          }
2615 +    }
2616  
2617 <        /**
2618 <         * Returns a non-null result from applying the given search
2619 <         * function on each (key, value), or null if none.  Upon
2620 <         * success, further element processing is suppressed and the
2621 <         * results of any other parallel invocations of the search
2622 <         * function are ignored.
2623 <         *
2624 <         * @param searchFunction a function returning a non-null
2625 <         * result on success, else null
2626 <         * @return a non-null result from applying the given search
2627 <         * function on each (key, value), or null if none
2628 <         */
2629 <        public <U> U search(BiFun<? super K, ? super V, ? extends U> searchFunction) {
2630 <            return fjp.invoke(ForkJoinTasks.search
2631 <                              (ConcurrentHashMap.this, searchFunction));
2617 >    /* ---------------- TreeBins -------------- */
2618 >
2619 >    /**
2620 >     * TreeNodes used at the heads of bins. TreeBins do not hold user
2621 >     * keys or values, but instead point to list of TreeNodes and
2622 >     * their root. They also maintain a parasitic read-write lock
2623 >     * forcing writers (who hold bin lock) to wait for readers (who do
2624 >     * not) to complete before tree restructuring operations.
2625 >     */
2626 >    static final class TreeBin<K,V> extends Node<K,V> {
2627 >        TreeNode<K,V> root;
2628 >        volatile TreeNode<K,V> first;
2629 >        volatile Thread waiter;
2630 >        volatile int lockState;
2631 >        // values for lockState
2632 >        static final int WRITER = 1; // set while holding write lock
2633 >        static final int WAITER = 2; // set when waiting for write lock
2634 >        static final int READER = 4; // increment value for setting read lock
2635 >
2636 >        /**
2637 >         * Creates bin with initial set of nodes headed by b.
2638 >         */
2639 >        TreeBin(TreeNode<K,V> b) {
2640 >            super(TREEBIN, null, null, null);
2641 >            this.first = b;
2642 >            TreeNode<K,V> r = null;
2643 >            for (TreeNode<K,V> x = b, next; x != null; x = next) {
2644 >                next = (TreeNode<K,V>)x.next;
2645 >                x.left = x.right = null;
2646 >                if (r == null) {
2647 >                    x.parent = null;
2648 >                    x.red = false;
2649 >                    r = x;
2650 >                }
2651 >                else {
2652 >                    Object key = x.key;
2653 >                    int hash = x.hash;
2654 >                    Class<?> kc = null;
2655 >                    for (TreeNode<K,V> p = r;;) {
2656 >                        int dir, ph;
2657 >                        if ((ph = p.hash) > hash)
2658 >                            dir = -1;
2659 >                        else if (ph < hash)
2660 >                            dir = 1;
2661 >                        else if ((kc != null ||
2662 >                                  (kc = comparableClassFor(key)) != null))
2663 >                            dir = compareComparables(kc, key, p.key);
2664 >                        else
2665 >                            dir = 0;
2666 >                        TreeNode<K,V> xp = p;
2667 >                        if ((p = (dir <= 0) ? p.left : p.right) == null) {
2668 >                            x.parent = xp;
2669 >                            if (dir <= 0)
2670 >                                xp.left = x;
2671 >                            else
2672 >                                xp.right = x;
2673 >                            r = balanceInsertion(r, x);
2674 >                            break;
2675 >                        }
2676 >                    }
2677 >                }
2678 >            }
2679 >            this.root = r;
2680          }
2681  
2682          /**
2683 <         * Returns the result of accumulating the given transformation
3702 <         * of all (key, value) pairs using the given reducer to
3703 <         * combine values, or null if none.
3704 <         *
3705 <         * @param transformer a function returning the transformation
3706 <         * for an element, or null if there is no transformation (in
3707 <         * which case it is not combined)
3708 <         * @param reducer a commutative associative combining function
3709 <         * @return the result of accumulating the given transformation
3710 <         * of all (key, value) pairs
2683 >         * Acquires write lock for tree restructuring
2684           */
2685 <        public <U> U reduce(BiFun<? super K, ? super V, ? extends U> transformer,
2686 <                            BiFun<? super U, ? super U, ? extends U> reducer) {
2687 <            return fjp.invoke(ForkJoinTasks.reduce
3715 <                              (ConcurrentHashMap.this, transformer, reducer));
2685 >        private final void lockRoot() {
2686 >            if (!U.compareAndSwapInt(this, LOCKSTATE, 0, WRITER))
2687 >                contendedLock(); // offload to separate method
2688          }
2689  
2690          /**
2691 <         * Returns the result of accumulating the given transformation
3720 <         * of all (key, value) pairs using the given reducer to
3721 <         * combine values, and the given basis as an identity value.
3722 <         *
3723 <         * @param transformer a function returning the transformation
3724 <         * for an element
3725 <         * @param basis the identity (initial default value) for the reduction
3726 <         * @param reducer a commutative associative combining function
3727 <         * @return the result of accumulating the given transformation
3728 <         * of all (key, value) pairs
2691 >         * Releases write lock for tree restructuring
2692           */
2693 <        public double reduceToDouble(ObjectByObjectToDouble<? super K, ? super V> transformer,
2694 <                                     double basis,
3732 <                                     DoubleByDoubleToDouble reducer) {
3733 <            return fjp.invoke(ForkJoinTasks.reduceToDouble
3734 <                              (ConcurrentHashMap.this, transformer, basis, reducer));
2693 >        private final void unlockRoot() {
2694 >            lockState = 0;
2695          }
2696  
2697          /**
2698 <         * Returns the result of accumulating the given transformation
3739 <         * of all (key, value) pairs using the given reducer to
3740 <         * combine values, and the given basis as an identity value.
3741 <         *
3742 <         * @param transformer a function returning the transformation
3743 <         * for an element
3744 <         * @param basis the identity (initial default value) for the reduction
3745 <         * @param reducer a commutative associative combining function
3746 <         * @return the result of accumulating the given transformation
3747 <         * of all (key, value) pairs
2698 >         * Possibly blocks awaiting root lock
2699           */
2700 <        public long reduceToLong(ObjectByObjectToLong<? super K, ? super V> transformer,
2701 <                                 long basis,
2702 <                                 LongByLongToLong reducer) {
2703 <            return fjp.invoke(ForkJoinTasks.reduceToLong
2704 <                              (ConcurrentHashMap.this, transformer, basis, reducer));
2700 >        private final void contendedLock() {
2701 >            boolean waiting = false;
2702 >            for (int s;;) {
2703 >                if (((s = lockState) & WRITER) == 0) {
2704 >                    if (U.compareAndSwapInt(this, LOCKSTATE, s, WRITER)) {
2705 >                        if (waiting)
2706 >                            waiter = null;
2707 >                        return;
2708 >                    }
2709 >                }
2710 >                else if ((s | WAITER) == 0) {
2711 >                    if (U.compareAndSwapInt(this, LOCKSTATE, s, s | WAITER)) {
2712 >                        waiting = true;
2713 >                        waiter = Thread.currentThread();
2714 >                    }
2715 >                }
2716 >                else if (waiting)
2717 >                    LockSupport.park(this);
2718 >            }
2719          }
2720  
2721          /**
2722 <         * Returns the result of accumulating the given transformation
2723 <         * of all (key, value) pairs using the given reducer to
2724 <         * combine values, and the given basis as an identity value.
3760 <         *
3761 <         * @param transformer a function returning the transformation
3762 <         * for an element
3763 <         * @param basis the identity (initial default value) for the reduction
3764 <         * @param reducer a commutative associative combining function
3765 <         * @return the result of accumulating the given transformation
3766 <         * of all (key, value) pairs
2722 >         * Returns matching node or null if none. Tries to search
2723 >         * using tree compareisons from root, but continues linear
2724 >         * search when lock not available.
2725           */
2726 <        public int reduceToInt(ObjectByObjectToInt<? super K, ? super V> transformer,
2727 <                               int basis,
2728 <                               IntByIntToInt reducer) {
2729 <            return fjp.invoke(ForkJoinTasks.reduceToInt
2730 <                              (ConcurrentHashMap.this, transformer, basis, reducer));
2726 >        final Node<K,V> find(int h, Object k) {
2727 >            if (k != null) {
2728 >                for (Node<K,V> e = first; e != null; e = e.next) {
2729 >                    int s; K ek;
2730 >                    if (((s = lockState) & (WAITER|WRITER)) != 0) {
2731 >                        if (e.hash == h &&
2732 >                            ((ek = e.key) == k || (ek != null && k.equals(ek))))
2733 >                            return e;
2734 >                    }
2735 >                    else if (U.compareAndSwapInt(this, LOCKSTATE, s,
2736 >                                                 s + READER)) {
2737 >                        TreeNode<K,V> r, p;
2738 >                        try {
2739 >                            p = ((r = root) == null ? null :
2740 >                                 r.findTreeNode(h, k, null));
2741 >                        } finally {
2742 >                            Thread w;
2743 >                            if (U.getAndAddInt(this, LOCKSTATE, -READER) ==
2744 >                                (READER|WAITER) && (w = waiter) != null)
2745 >                                LockSupport.unpark(w);
2746 >                        }
2747 >                        return p;
2748 >                    }
2749 >                }
2750 >            }
2751 >            return null;
2752          }
2753  
2754          /**
2755 <         * Performs the given action for each key.
2756 <         *
3778 <         * @param action the action
2755 >         * Finds or adds a node.
2756 >         * @return null if added
2757           */
2758 <        public void forEachKey(Action<K> action) {
2759 <            fjp.invoke(ForkJoinTasks.forEachKey
2760 <                       (ConcurrentHashMap.this, action));
2758 >        final TreeNode<K,V> putTreeVal(int h, K k, V v) {
2759 >            Class<?> kc = null;
2760 >            for (TreeNode<K,V> p = root;;) {
2761 >                int dir, ph; K pk; TreeNode<K,V> q, pr;
2762 >                if (p == null) {
2763 >                    first = root = new TreeNode<K,V>(h, k, v, null, null);
2764 >                    break;
2765 >                }
2766 >                else if ((ph = p.hash) > h)
2767 >                    dir = -1;
2768 >                else if (ph < h)
2769 >                    dir = 1;
2770 >                else if ((pk = p.key) == k || (pk != null && k.equals(pk)))
2771 >                    return p;
2772 >                else if ((kc == null &&
2773 >                          (kc = comparableClassFor(k)) == null) ||
2774 >                         (dir = compareComparables(kc, k, pk)) == 0) {
2775 >                    if (p.left == null)
2776 >                        dir = 1;
2777 >                    else if ((pr = p.right) == null ||
2778 >                             (q = pr.findTreeNode(h, k, kc)) == null)
2779 >                        dir = -1;
2780 >                    else
2781 >                        return q;
2782 >                }
2783 >                TreeNode<K,V> xp = p;
2784 >                if ((p = (dir < 0) ? p.left : p.right) == null) {
2785 >                    TreeNode<K,V> x, f = first;
2786 >                    first = x = new TreeNode<K,V>(h, k, v, f, xp);
2787 >                    if (f != null)
2788 >                        f.prev = x;
2789 >                    if (dir < 0)
2790 >                        xp.left = x;
2791 >                    else
2792 >                        xp.right = x;
2793 >                    if (!xp.red)
2794 >                        x.red = true;
2795 >                    else {
2796 >                        lockRoot();
2797 >                        try {
2798 >                            root = balanceInsertion(root, x);
2799 >                        } finally {
2800 >                            unlockRoot();
2801 >                        }
2802 >                    }
2803 >                    break;
2804 >                }
2805 >            }
2806 >            assert checkInvariants(root);
2807 >            return null;
2808          }
2809  
2810          /**
2811 <         * Performs the given action for each non-null transformation
2812 <         * of each key.
2811 >         * Removes the given node, that must be present before this
2812 >         * call.  This is messier than typical red-black deletion code
2813 >         * because we cannot swap the contents of an interior node
2814 >         * with a leaf successor that is pinned by "next" pointers
2815 >         * that are accessible independently of lock. So instead we
2816 >         * swap the tree linkages.
2817           *
2818 <         * @param transformer a function returning the transformation
3790 <         * for an element, or null if there is no transformation (in
3791 <         * which case the action is not applied)
3792 <         * @param action the action
2818 >         * @return true if now too small so should be untreeified.
2819           */
2820 <        public <U> void forEachKey(Fun<? super K, ? extends U> transformer,
2821 <                                   Action<U> action) {
2822 <            fjp.invoke(ForkJoinTasks.forEachKey
2823 <                       (ConcurrentHashMap.this, transformer, action));
2820 >        final boolean removeTreeNode(TreeNode<K,V> p) {
2821 >            TreeNode<K,V> next = (TreeNode<K,V>)p.next;
2822 >            TreeNode<K,V> pred = p.prev;  // unlink traversal pointers
2823 >            TreeNode<K,V> r, rl;
2824 >            if (pred == null)
2825 >                first = next;
2826 >            else
2827 >                pred.next = next;
2828 >            if (next != null)
2829 >                next.prev = pred;
2830 >            if (first == null) {
2831 >                root = null;
2832 >                return true;
2833 >            }
2834 >            if ((r = root) == null || r.right == null || // too small
2835 >                (rl = r.left) == null || rl.left == null)
2836 >                return true;
2837 >            lockRoot();
2838 >            try {
2839 >                TreeNode<K,V> replacement;
2840 >                TreeNode<K,V> pl = p.left;
2841 >                TreeNode<K,V> pr = p.right;
2842 >                if (pl != null && pr != null) {
2843 >                    TreeNode<K,V> s = pr, sl;
2844 >                    while ((sl = s.left) != null) // find successor
2845 >                        s = sl;
2846 >                    boolean c = s.red; s.red = p.red; p.red = c; // swap colors
2847 >                    TreeNode<K,V> sr = s.right;
2848 >                    TreeNode<K,V> pp = p.parent;
2849 >                    if (s == pr) { // p was s's direct parent
2850 >                        p.parent = s;
2851 >                        s.right = p;
2852 >                    }
2853 >                    else {
2854 >                        TreeNode<K,V> sp = s.parent;
2855 >                        if ((p.parent = sp) != null) {
2856 >                            if (s == sp.left)
2857 >                                sp.left = p;
2858 >                            else
2859 >                                sp.right = p;
2860 >                        }
2861 >                        if ((s.right = pr) != null)
2862 >                            pr.parent = s;
2863 >                    }
2864 >                    p.left = null;
2865 >                    if ((p.right = sr) != null)
2866 >                        sr.parent = p;
2867 >                    if ((s.left = pl) != null)
2868 >                        pl.parent = s;
2869 >                    if ((s.parent = pp) == null)
2870 >                        r = s;
2871 >                    else if (p == pp.left)
2872 >                        pp.left = s;
2873 >                    else
2874 >                        pp.right = s;
2875 >                    if (sr != null)
2876 >                        replacement = sr;
2877 >                    else
2878 >                        replacement = p;
2879 >                }
2880 >                else if (pl != null)
2881 >                    replacement = pl;
2882 >                else if (pr != null)
2883 >                    replacement = pr;
2884 >                else
2885 >                    replacement = p;
2886 >                if (replacement != p) {
2887 >                    TreeNode<K,V> pp = replacement.parent = p.parent;
2888 >                    if (pp == null)
2889 >                        r = replacement;
2890 >                    else if (p == pp.left)
2891 >                        pp.left = replacement;
2892 >                    else
2893 >                        pp.right = replacement;
2894 >                    p.left = p.right = p.parent = null;
2895 >                }
2896 >
2897 >                root = (p.red) ? r : balanceDeletion(r, replacement);
2898 >
2899 >                if (p == replacement) {  // detach pointers
2900 >                    TreeNode<K,V> pp;
2901 >                    if ((pp = p.parent) != null) {
2902 >                        if (p == pp.left)
2903 >                            pp.left = null;
2904 >                        else if (p == pp.right)
2905 >                            pp.right = null;
2906 >                        p.parent = null;
2907 >                    }
2908 >                }
2909 >            } finally {
2910 >                unlockRoot();
2911 >            }
2912 >            assert checkInvariants(root);
2913 >            return false;
2914          }
2915  
2916 <        /**
2917 <         * Returns a non-null result from applying the given search
2918 <         * function on each key, or null if none. Upon success,
2919 <         * further element processing is suppressed and the results of
2920 <         * any other parallel invocations of the search function are
2921 <         * ignored.
2922 <         *
2923 <         * @param searchFunction a function returning a non-null
2924 <         * result on success, else null
2925 <         * @return a non-null result from applying the given search
2926 <         * function on each key, or null if none
2927 <         */
2928 <        public <U> U searchKeys(Fun<? super K, ? extends U> searchFunction) {
2929 <            return fjp.invoke(ForkJoinTasks.searchKeys
2930 <                              (ConcurrentHashMap.this, searchFunction));
2916 >        /* ------------------------------------------------------------ */
2917 >        // Red-black tree methods, all adapted from CLR
2918 >
2919 >        static <K,V> TreeNode<K,V> rotateLeft(TreeNode<K,V> root,
2920 >                                              TreeNode<K,V> p) {
2921 >            TreeNode<K,V> r, pp, rl;
2922 >            if (p != null && (r = p.right) != null) {
2923 >                if ((rl = p.right = r.left) != null)
2924 >                    rl.parent = p;
2925 >                if ((pp = r.parent = p.parent) == null)
2926 >                    (root = r).red = false;
2927 >                else if (pp.left == p)
2928 >                    pp.left = r;
2929 >                else
2930 >                    pp.right = r;
2931 >                r.left = p;
2932 >                p.parent = r;
2933 >            }
2934 >            return root;
2935          }
2936  
2937 <        /**
2938 <         * Returns the result of accumulating all keys using the given
2939 <         * reducer to combine values, or null if none.
2940 <         *
2941 <         * @param reducer a commutative associative combining function
2942 <         * @return the result of accumulating all keys using the given
2943 <         * reducer to combine values, or null if none
2944 <         */
2945 <        public K reduceKeys(BiFun<? super K, ? super K, ? extends K> reducer) {
2946 <            return fjp.invoke(ForkJoinTasks.reduceKeys
2947 <                              (ConcurrentHashMap.this, reducer));
2937 >        static <K,V> TreeNode<K,V> rotateRight(TreeNode<K,V> root,
2938 >                                               TreeNode<K,V> p) {
2939 >            TreeNode<K,V> l, pp, lr;
2940 >            if (p != null && (l = p.left) != null) {
2941 >                if ((lr = p.left = l.right) != null)
2942 >                    lr.parent = p;
2943 >                if ((pp = l.parent = p.parent) == null)
2944 >                    (root = l).red = false;
2945 >                else if (pp.right == p)
2946 >                    pp.right = l;
2947 >                else
2948 >                    pp.left = l;
2949 >                l.right = p;
2950 >                p.parent = l;
2951 >            }
2952 >            return root;
2953          }
2954  
2955 <        /**
2956 <         * Returns the result of accumulating the given transformation
2957 <         * of all keys using the given reducer to combine values, or
2958 <         * null if none.
2959 <         *
2960 <         * @param transformer a function returning the transformation
2961 <         * for an element, or null if there is no transformation (in
2962 <         * which case it is not combined)
2963 <         * @param reducer a commutative associative combining function
2964 <         * @return the result of accumulating the given transformation
2965 <         * of all keys
2966 <         */
2967 <        public <U> U reduceKeys(Fun<? super K, ? extends U> transformer,
2968 <                                BiFun<? super U, ? super U, ? extends U> reducer) {
2969 <            return fjp.invoke(ForkJoinTasks.reduceKeys
2970 <                              (ConcurrentHashMap.this, transformer, reducer));
2955 >        static <K,V> TreeNode<K,V> balanceInsertion(TreeNode<K,V> root,
2956 >                                                    TreeNode<K,V> x) {
2957 >            x.red = true;
2958 >            for (TreeNode<K,V> xp, xpp, xppl, xppr;;) {
2959 >                if ((xp = x.parent) == null) {
2960 >                    x.red = false;
2961 >                    return x;
2962 >                }
2963 >                else if (!xp.red || (xpp = xp.parent) == null)
2964 >                    return root;
2965 >                if (xp == (xppl = xpp.left)) {
2966 >                    if ((xppr = xpp.right) != null && xppr.red) {
2967 >                        xppr.red = false;
2968 >                        xp.red = false;
2969 >                        xpp.red = true;
2970 >                        x = xpp;
2971 >                    }
2972 >                    else {
2973 >                        if (x == xp.right) {
2974 >                            root = rotateLeft(root, x = xp);
2975 >                            xpp = (xp = x.parent) == null ? null : xp.parent;
2976 >                        }
2977 >                        if (xp != null) {
2978 >                            xp.red = false;
2979 >                            if (xpp != null) {
2980 >                                xpp.red = true;
2981 >                                root = rotateRight(root, xpp);
2982 >                            }
2983 >                        }
2984 >                    }
2985 >                }
2986 >                else {
2987 >                    if (xppl != null && xppl.red) {
2988 >                        xppl.red = false;
2989 >                        xp.red = false;
2990 >                        xpp.red = true;
2991 >                        x = xpp;
2992 >                    }
2993 >                    else {
2994 >                        if (x == xp.left) {
2995 >                            root = rotateRight(root, x = xp);
2996 >                            xpp = (xp = x.parent) == null ? null : xp.parent;
2997 >                        }
2998 >                        if (xp != null) {
2999 >                            xp.red = false;
3000 >                            if (xpp != null) {
3001 >                                xpp.red = true;
3002 >                                root = rotateLeft(root, xpp);
3003 >                            }
3004 >                        }
3005 >                    }
3006 >                }
3007 >            }
3008          }
3009  
3010 <        /**
3011 <         * Returns the result of accumulating the given transformation
3012 <         * of all keys using the given reducer to combine values, and
3013 <         * the given basis as an identity value.
3014 <         *
3015 <         * @param transformer a function returning the transformation
3016 <         * for an element
3017 <         * @param basis the identity (initial default value) for the reduction
3018 <         * @param reducer a commutative associative combining function
3019 <         * @return  the result of accumulating the given transformation
3020 <         * of all keys
3021 <         */
3022 <        public double reduceKeysToDouble(ObjectToDouble<? super K> transformer,
3023 <                                         double basis,
3024 <                                         DoubleByDoubleToDouble reducer) {
3025 <            return fjp.invoke(ForkJoinTasks.reduceKeysToDouble
3026 <                              (ConcurrentHashMap.this, transformer, basis, reducer));
3010 >        static <K,V> TreeNode<K,V> balanceDeletion(TreeNode<K,V> root,
3011 >                                                   TreeNode<K,V> x) {
3012 >            for (TreeNode<K,V> xp, xpl, xpr;;)  {
3013 >                if (x == null || x == root)
3014 >                    return root;
3015 >                else if ((xp = x.parent) == null) {
3016 >                    x.red = false;
3017 >                    return x;
3018 >                }
3019 >                else if (x.red) {
3020 >                    x.red = false;
3021 >                    return root;
3022 >                }
3023 >                else if ((xpl = xp.left) == x) {
3024 >                    if ((xpr = xp.right) != null && xpr.red) {
3025 >                        xpr.red = false;
3026 >                        xp.red = true;
3027 >                        root = rotateLeft(root, xp);
3028 >                        xpr = (xp = x.parent) == null ? null : xp.right;
3029 >                    }
3030 >                    if (xpr == null)
3031 >                        x = xp;
3032 >                    else {
3033 >                        TreeNode<K,V> sl = xpr.left, sr = xpr.right;
3034 >                        if ((sr == null || !sr.red) &&
3035 >                            (sl == null || !sl.red)) {
3036 >                            xpr.red = true;
3037 >                            x = xp;
3038 >                        }
3039 >                        else {
3040 >                            if (sr == null || !sr.red) {
3041 >                                if (sl != null)
3042 >                                    sl.red = false;
3043 >                                xpr.red = true;
3044 >                                root = rotateRight(root, xpr);
3045 >                                xpr = (xp = x.parent) == null ?
3046 >                                    null : xp.right;
3047 >                            }
3048 >                            if (xpr != null) {
3049 >                                xpr.red = (xp == null) ? false : xp.red;
3050 >                                if ((sr = xpr.right) != null)
3051 >                                    sr.red = false;
3052 >                            }
3053 >                            if (xp != null) {
3054 >                                xp.red = false;
3055 >                                root = rotateLeft(root, xp);
3056 >                            }
3057 >                            x = root;
3058 >                        }
3059 >                    }
3060 >                }
3061 >                else { // symmetric
3062 >                    if (xpl != null && xpl.red) {
3063 >                        xpl.red = false;
3064 >                        xp.red = true;
3065 >                        root = rotateRight(root, xp);
3066 >                        xpl = (xp = x.parent) == null ? null : xp.left;
3067 >                    }
3068 >                    if (xpl == null)
3069 >                        x = xp;
3070 >                    else {
3071 >                        TreeNode<K,V> sl = xpl.left, sr = xpl.right;
3072 >                        if ((sl == null || !sl.red) &&
3073 >                            (sr == null || !sr.red)) {
3074 >                            xpl.red = true;
3075 >                            x = xp;
3076 >                        }
3077 >                        else {
3078 >                            if (sl == null || !sl.red) {
3079 >                                if (sr != null)
3080 >                                    sr.red = false;
3081 >                                xpl.red = true;
3082 >                                root = rotateLeft(root, xpl);
3083 >                                xpl = (xp = x.parent) == null ?
3084 >                                    null : xp.left;
3085 >                            }
3086 >                            if (xpl != null) {
3087 >                                xpl.red = (xp == null) ? false : xp.red;
3088 >                                if ((sl = xpl.left) != null)
3089 >                                    sl.red = false;
3090 >                            }
3091 >                            if (xp != null) {
3092 >                                xp.red = false;
3093 >                                root = rotateRight(root, xp);
3094 >                            }
3095 >                            x = root;
3096 >                        }
3097 >                    }
3098 >                }
3099 >            }
3100          }
3101  
3102          /**
3103 <         * Returns the result of accumulating the given transformation
3869 <         * of all keys using the given reducer to combine values, and
3870 <         * the given basis as an identity value.
3871 <         *
3872 <         * @param transformer a function returning the transformation
3873 <         * for an element
3874 <         * @param basis the identity (initial default value) for the reduction
3875 <         * @param reducer a commutative associative combining function
3876 <         * @return the result of accumulating the given transformation
3877 <         * of all keys
3103 >         * Recursive invariant check
3104           */
3105 <        public long reduceKeysToLong(ObjectToLong<? super K> transformer,
3106 <                                     long basis,
3107 <                                     LongByLongToLong reducer) {
3108 <            return fjp.invoke(ForkJoinTasks.reduceKeysToLong
3109 <                              (ConcurrentHashMap.this, transformer, basis, reducer));
3105 >        static <K,V> boolean checkInvariants(TreeNode<K,V> t) {
3106 >            TreeNode<K,V> tp = t.parent, tl = t.left, tr = t.right,
3107 >                tb = t.prev, tn = (TreeNode<K,V>)t.next;
3108 >            if (tb != null && tb.next != t)
3109 >                return false;
3110 >            if (tn != null && tn.prev != t)
3111 >                return false;
3112 >            if (tp != null && t != tp.left && t != tp.right)
3113 >                return false;
3114 >            if (tl != null && (tl.parent != t || tl.hash > t.hash))
3115 >                return false;
3116 >            if (tr != null && (tr.parent != t || tr.hash < t.hash))
3117 >                return false;
3118 >            if (t.red && tl != null && tl.red && tr != null && tr.red)
3119 >                return false;
3120 >            if (tl != null && !checkInvariants(tl))
3121 >                return false;
3122 >            if (tr != null && !checkInvariants(tr))
3123 >                return false;
3124 >            return true;
3125          }
3126  
3127 <        /**
3128 <         * Returns the result of accumulating the given transformation
3129 <         * of all keys using the given reducer to combine values, and
3130 <         * the given basis as an identity value.
3131 <         *
3132 <         * @param transformer a function returning the transformation
3133 <         * for an element
3134 <         * @param basis the identity (initial default value) for the reduction
3135 <         * @param reducer a commutative associative combining function
3136 <         * @return the result of accumulating the given transformation
3137 <         * of all keys
3897 <         */
3898 <        public int reduceKeysToInt(ObjectToInt<? super K> transformer,
3899 <                                   int basis,
3900 <                                   IntByIntToInt reducer) {
3901 <            return fjp.invoke(ForkJoinTasks.reduceKeysToInt
3902 <                              (ConcurrentHashMap.this, transformer, basis, reducer));
3127 >        private static final sun.misc.Unsafe U;
3128 >        private static final long LOCKSTATE;
3129 >        static {
3130 >            try {
3131 >                U = sun.misc.Unsafe.getUnsafe();
3132 >                Class<?> k = TreeBin.class;
3133 >                LOCKSTATE = U.objectFieldOffset
3134 >                    (k.getDeclaredField("lockState"));
3135 >            } catch (Exception e) {
3136 >                throw new Error(e);
3137 >            }
3138          }
3139 +    }
3140  
3141 <        /**
3142 <         * Performs the given action for each value.
3143 <         *
3144 <         * @param action the action
3145 <         */
3146 <        public void forEachValue(Action<V> action) {
3147 <            fjp.invoke(ForkJoinTasks.forEachValue
3148 <                       (ConcurrentHashMap.this, action));
3141 >    /* ----------------Table Traversal -------------- */
3142 >
3143 >    /**
3144 >     * Encapsulates traversal for methods such as containsValue; also
3145 >     * serves as a base class for other iterators and spliterators.
3146 >     *
3147 >     * Method advance visits once each still-valid node that was
3148 >     * reachable upon iterator construction. It might miss some that
3149 >     * were added to a bin after the bin was visited, which is OK wrt
3150 >     * consistency guarantees. Maintaining this property in the face
3151 >     * of possible ongoing resizes requires a fair amount of
3152 >     * bookkeeping state that is difficult to optimize away amidst
3153 >     * volatile accesses.  Even so, traversal maintains reasonable
3154 >     * throughput.
3155 >     *
3156 >     * Normally, iteration proceeds bin-by-bin traversing lists.
3157 >     * However, if the table has been resized, then all future steps
3158 >     * must traverse both the bin at the current index as well as at
3159 >     * (index + baseSize); and so on for further resizings. To
3160 >     * paranoically cope with potential sharing by users of iterators
3161 >     * across threads, iteration terminates if a bounds checks fails
3162 >     * for a table read.
3163 >     */
3164 >    static class Traverser<K,V> {
3165 >        Node<K,V>[] tab;        // current table; updated if resized
3166 >        Node<K,V> next;         // the next entry to use
3167 >        int index;              // index of bin to use next
3168 >        int baseIndex;          // current index of initial table
3169 >        int baseLimit;          // index bound for initial table
3170 >        final int baseSize;     // initial table size
3171 >
3172 >        Traverser(Node<K,V>[] tab, int size, int index, int limit) {
3173 >            this.tab = tab;
3174 >            this.baseSize = size;
3175 >            this.baseIndex = this.index = index;
3176 >            this.baseLimit = limit;
3177 >            this.next = null;
3178          }
3179  
3180          /**
3181 <         * Performs the given action for each non-null transformation
3182 <         * of each value.
3183 <         *
3184 <         * @param transformer a function returning the transformation
3185 <         * for an element, or null if there is no transformation (in
3186 <         * which case the action is not applied)
3187 <         */
3188 <        public <U> void forEachValue(Fun<? super V, ? extends U> transformer,
3189 <                                     Action<U> action) {
3190 <            fjp.invoke(ForkJoinTasks.forEachValue
3191 <                       (ConcurrentHashMap.this, transformer, action));
3181 >         * Advances if possible, returning next valid node, or null if none.
3182 >         */
3183 >        final Node<K,V> advance() {
3184 >            Node<K,V> e;
3185 >            if ((e = next) != null)
3186 >                e = e.next;
3187 >            for (;;) {
3188 >                Node<K,V>[] t; int i, n; K ek;  // must use locals in checks
3189 >                if (e != null)
3190 >                    return next = e;
3191 >                if (baseIndex >= baseLimit || (t = tab) == null ||
3192 >                    (n = t.length) <= (i = index) || i < 0)
3193 >                    return next = null;
3194 >                if ((e = tabAt(t, index)) != null && e.hash < 0) {
3195 >                    if (e instanceof ForwardingNode) {
3196 >                        tab = ((ForwardingNode<K,V>)e).nextTable;
3197 >                        e = null;
3198 >                        continue;
3199 >                    }
3200 >                    else if (e instanceof TreeBin)
3201 >                        e = ((TreeBin<K,V>)e).first;
3202 >                    else
3203 >                        e = null;
3204 >                }
3205 >                if ((index += baseSize) >= n)
3206 >                    index = ++baseIndex;    // visit upper slots if present
3207 >            }
3208          }
3209 +    }
3210  
3211 <        /**
3212 <         * Returns a non-null result from applying the given search
3213 <         * function on each value, or null if none.  Upon success,
3214 <         * further element processing is suppressed and the results of
3215 <         * any other parallel invocations of the search function are
3216 <         * ignored.
3217 <         *
3218 <         * @param searchFunction a function returning a non-null
3219 <         * result on success, else null
3220 <         * @return a non-null result from applying the given search
3221 <         * function on each value, or null if none
3222 <         *
3941 <         */
3942 <        public <U> U searchValues(Fun<? super V, ? extends U> searchFunction) {
3943 <            return fjp.invoke(ForkJoinTasks.searchValues
3944 <                              (ConcurrentHashMap.this, searchFunction));
3211 >    /**
3212 >     * Base of key, value, and entry Iterators. Adds fields to
3213 >     * Traverser to support iterator.remove
3214 >     */
3215 >    static class BaseIterator<K,V> extends Traverser<K,V> {
3216 >        final ConcurrentHashMap<K,V> map;
3217 >        Node<K,V> lastReturned;
3218 >        BaseIterator(Node<K,V>[] tab, int size, int index, int limit,
3219 >                    ConcurrentHashMap<K,V> map) {
3220 >            super(tab, size, index, limit);
3221 >            this.map = map;
3222 >            advance();
3223          }
3224  
3225 <        /**
3226 <         * Returns the result of accumulating all values using the
3227 <         * given reducer to combine values, or null if none.
3228 <         *
3229 <         * @param reducer a commutative associative combining function
3230 <         * @return  the result of accumulating all values
3231 <         */
3232 <        public V reduceValues(BiFun<? super V, ? super V, ? extends V> reducer) {
3233 <            return fjp.invoke(ForkJoinTasks.reduceValues
3956 <                              (ConcurrentHashMap.this, reducer));
3225 >        public final boolean hasNext() { return next != null; }
3226 >        public final boolean hasMoreElements() { return next != null; }
3227 >
3228 >        public final void remove() {
3229 >            Node<K,V> p;
3230 >            if ((p = lastReturned) == null)
3231 >                throw new IllegalStateException();
3232 >            lastReturned = null;
3233 >            map.replaceNode(p.key, null, null);
3234          }
3235 +    }
3236  
3237 <        /**
3238 <         * Returns the result of accumulating the given transformation
3239 <         * of all values using the given reducer to combine values, or
3240 <         * null if none.
3241 <         *
3964 <         * @param transformer a function returning the transformation
3965 <         * for an element, or null if there is no transformation (in
3966 <         * which case it is not combined)
3967 <         * @param reducer a commutative associative combining function
3968 <         * @return the result of accumulating the given transformation
3969 <         * of all values
3970 <         */
3971 <        public <U> U reduceValues(Fun<? super V, ? extends U> transformer,
3972 <                                  BiFun<? super U, ? super U, ? extends U> reducer) {
3973 <            return fjp.invoke(ForkJoinTasks.reduceValues
3974 <                              (ConcurrentHashMap.this, transformer, reducer));
3237 >    static final class KeyIterator<K,V> extends BaseIterator<K,V>
3238 >        implements Iterator<K>, Enumeration<K> {
3239 >        KeyIterator(Node<K,V>[] tab, int index, int size, int limit,
3240 >                    ConcurrentHashMap<K,V> map) {
3241 >            super(tab, index, size, limit, map);
3242          }
3243  
3244 <        /**
3245 <         * Returns the result of accumulating the given transformation
3246 <         * of all values using the given reducer to combine values,
3247 <         * and the given basis as an identity value.
3248 <         *
3249 <         * @param transformer a function returning the transformation
3250 <         * for an element
3251 <         * @param basis the identity (initial default value) for the reduction
3985 <         * @param reducer a commutative associative combining function
3986 <         * @return the result of accumulating the given transformation
3987 <         * of all values
3988 <         */
3989 <        public double reduceValuesToDouble(ObjectToDouble<? super V> transformer,
3990 <                                           double basis,
3991 <                                           DoubleByDoubleToDouble reducer) {
3992 <            return fjp.invoke(ForkJoinTasks.reduceValuesToDouble
3993 <                              (ConcurrentHashMap.this, transformer, basis, reducer));
3244 >        public final K next() {
3245 >            Node<K,V> p;
3246 >            if ((p = next) == null)
3247 >                throw new NoSuchElementException();
3248 >            K k = p.key;
3249 >            lastReturned = p;
3250 >            advance();
3251 >            return k;
3252          }
3253  
3254 <        /**
3255 <         * Returns the result of accumulating the given transformation
3256 <         * of all values using the given reducer to combine values,
3257 <         * and the given basis as an identity value.
3258 <         *
3259 <         * @param transformer a function returning the transformation
3260 <         * for an element
3261 <         * @param basis the identity (initial default value) for the reduction
4004 <         * @param reducer a commutative associative combining function
4005 <         * @return the result of accumulating the given transformation
4006 <         * of all values
4007 <         */
4008 <        public long reduceValuesToLong(ObjectToLong<? super V> transformer,
4009 <                                       long basis,
4010 <                                       LongByLongToLong reducer) {
4011 <            return fjp.invoke(ForkJoinTasks.reduceValuesToLong
4012 <                              (ConcurrentHashMap.this, transformer, basis, reducer));
3254 >        public final K nextElement() { return next(); }
3255 >    }
3256 >
3257 >    static final class ValueIterator<K,V> extends BaseIterator<K,V>
3258 >        implements Iterator<V>, Enumeration<V> {
3259 >        ValueIterator(Node<K,V>[] tab, int index, int size, int limit,
3260 >                      ConcurrentHashMap<K,V> map) {
3261 >            super(tab, index, size, limit, map);
3262          }
3263  
3264 <        /**
3265 <         * Returns the result of accumulating the given transformation
3266 <         * of all values using the given reducer to combine values,
3267 <         * and the given basis as an identity value.
3268 <         *
3269 <         * @param transformer a function returning the transformation
3270 <         * for an element
3271 <         * @param basis the identity (initial default value) for the reduction
4023 <         * @param reducer a commutative associative combining function
4024 <         * @return the result of accumulating the given transformation
4025 <         * of all values
4026 <         */
4027 <        public int reduceValuesToInt(ObjectToInt<? super V> transformer,
4028 <                                     int basis,
4029 <                                     IntByIntToInt reducer) {
4030 <            return fjp.invoke(ForkJoinTasks.reduceValuesToInt
4031 <                              (ConcurrentHashMap.this, transformer, basis, reducer));
3264 >        public final V next() {
3265 >            Node<K,V> p;
3266 >            if ((p = next) == null)
3267 >                throw new NoSuchElementException();
3268 >            V v = p.val;
3269 >            lastReturned = p;
3270 >            advance();
3271 >            return v;
3272          }
3273  
3274 <        /**
3275 <         * Performs the given action for each entry.
3276 <         *
3277 <         * @param action the action
3278 <         */
3279 <        public void forEachEntry(Action<Map.Entry<K,V>> action) {
3280 <            fjp.invoke(ForkJoinTasks.forEachEntry
3281 <                       (ConcurrentHashMap.this, action));
3274 >        public final V nextElement() { return next(); }
3275 >    }
3276 >
3277 >    static final class EntryIterator<K,V> extends BaseIterator<K,V>
3278 >        implements Iterator<Map.Entry<K,V>> {
3279 >        EntryIterator(Node<K,V>[] tab, int index, int size, int limit,
3280 >                      ConcurrentHashMap<K,V> map) {
3281 >            super(tab, index, size, limit, map);
3282          }
3283  
3284 <        /**
3285 <         * Performs the given action for each non-null transformation
3286 <         * of each entry.
3287 <         *
3288 <         * @param transformer a function returning the transformation
3289 <         * for an element, or null if there is no transformation (in
3290 <         * which case the action is not applied)
3291 <         * @param action the action
3292 <         */
4053 <        public <U> void forEachEntry(Fun<Map.Entry<K,V>, ? extends U> transformer,
4054 <                                     Action<U> action) {
4055 <            fjp.invoke(ForkJoinTasks.forEachEntry
4056 <                       (ConcurrentHashMap.this, transformer, action));
3284 >        public final Map.Entry<K,V> next() {
3285 >            Node<K,V> p;
3286 >            if ((p = next) == null)
3287 >                throw new NoSuchElementException();
3288 >            K k = p.key;
3289 >            V v = p.val;
3290 >            lastReturned = p;
3291 >            advance();
3292 >            return new MapEntry<K,V>(k, v, map);
3293          }
3294 +    }
3295  
3296 <        /**
3297 <         * Returns a non-null result from applying the given search
3298 <         * function on each entry, or null if none.  Upon success,
3299 <         * further element processing is suppressed and the results of
3300 <         * any other parallel invocations of the search function are
3301 <         * ignored.
3302 <         *
3303 <         * @param searchFunction a function returning a non-null
3304 <         * result on success, else null
3305 <         * @return a non-null result from applying the given search
3306 <         * function on each entry, or null if none
4070 <         */
4071 <        public <U> U searchEntries(Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
4072 <            return fjp.invoke(ForkJoinTasks.searchEntries
4073 <                              (ConcurrentHashMap.this, searchFunction));
3296 >    /**
3297 >     * Exported Entry for EntryIterator
3298 >     */
3299 >    static final class MapEntry<K,V> implements Map.Entry<K,V> {
3300 >        final K key; // non-null
3301 >        V val;       // non-null
3302 >        final ConcurrentHashMap<K,V> map;
3303 >        MapEntry(K key, V val, ConcurrentHashMap<K,V> map) {
3304 >            this.key = key;
3305 >            this.val = val;
3306 >            this.map = map;
3307          }
3308 +        public K getKey()        { return key; }
3309 +        public V getValue()      { return val; }
3310 +        public int hashCode()    { return key.hashCode() ^ val.hashCode(); }
3311 +        public String toString() { return key + "=" + val; }
3312  
3313 <        /**
3314 <         * Returns the result of accumulating all entries using the
3315 <         * given reducer to combine values, or null if none.
3316 <         *
3317 <         * @param reducer a commutative associative combining function
3318 <         * @return the result of accumulating all entries
3319 <         */
4083 <        public Map.Entry<K,V> reduceEntries(BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4084 <            return fjp.invoke(ForkJoinTasks.reduceEntries
4085 <                              (ConcurrentHashMap.this, reducer));
3313 >        public boolean equals(Object o) {
3314 >            Object k, v; Map.Entry<?,?> e;
3315 >            return ((o instanceof Map.Entry) &&
3316 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3317 >                    (v = e.getValue()) != null &&
3318 >                    (k == key || k.equals(key)) &&
3319 >                    (v == val || v.equals(val)));
3320          }
3321  
3322          /**
3323 <         * Returns the result of accumulating the given transformation
3324 <         * of all entries using the given reducer to combine values,
3325 <         * or null if none.
3326 <         *
3327 <         * @param transformer a function returning the transformation
3328 <         * for an element, or null if there is no transformation (in
4095 <         * which case it is not combined).
4096 <         * @param reducer a commutative associative combining function
4097 <         * @return the result of accumulating the given transformation
4098 <         * of all entries
3323 >         * Sets our entry's value and writes through to the map. The
3324 >         * value to return is somewhat arbitrary here. Since we do not
3325 >         * necessarily track asynchronous changes, the most recent
3326 >         * "previous" value could be different from what we return (or
3327 >         * could even have been removed, in which case the put will
3328 >         * re-establish). We do not and cannot guarantee more.
3329           */
3330 <        public <U> U reduceEntries(Fun<Map.Entry<K,V>, ? extends U> transformer,
3331 <                                   BiFun<? super U, ? super U, ? extends U> reducer) {
3332 <            return fjp.invoke(ForkJoinTasks.reduceEntries
3333 <                              (ConcurrentHashMap.this, transformer, reducer));
3330 >        public V setValue(V value) {
3331 >            if (value == null) throw new NullPointerException();
3332 >            V v = val;
3333 >            val = value;
3334 >            map.put(key, value);
3335 >            return v;
3336          }
3337 +    }
3338  
3339 <        /**
3340 <         * Returns the result of accumulating the given transformation
3341 <         * of all entries using the given reducer to combine values,
3342 <         * and the given basis as an identity value.
3343 <         *
3344 <         * @param transformer a function returning the transformation
3345 <         * for an element
3346 <         * @param basis the identity (initial default value) for the reduction
3347 <         * @param reducer a commutative associative combining function
3348 <         * @return the result of accumulating the given transformation
3349 <         * of all entries
3350 <         */
3351 <        public double reduceEntriesToDouble(ObjectToDouble<Map.Entry<K,V>> transformer,
3352 <                                            double basis,
4120 <                                            DoubleByDoubleToDouble reducer) {
4121 <            return fjp.invoke(ForkJoinTasks.reduceEntriesToDouble
4122 <                              (ConcurrentHashMap.this, transformer, basis, reducer));
3339 >    static final class KeySpliterator<K,V> extends Traverser<K,V>
3340 >        implements Spliterator<K> {
3341 >        long est;               // size estimate
3342 >        KeySpliterator(Node<K,V>[] tab, int size, int index, int limit,
3343 >                       long est) {
3344 >            super(tab, size, index, limit);
3345 >            this.est = est;
3346 >        }
3347 >
3348 >        public Spliterator<K> trySplit() {
3349 >            int i, f, h;
3350 >            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
3351 >                new KeySpliterator<K,V>(tab, baseSize, baseLimit = h,
3352 >                                        f, est >>>= 1);
3353          }
3354  
3355 <        /**
3356 <         * Returns the result of accumulating the given transformation
3357 <         * of all entries using the given reducer to combine values,
3358 <         * and the given basis as an identity value.
4129 <         *
4130 <         * @param transformer a function returning the transformation
4131 <         * for an element
4132 <         * @param basis the identity (initial default value) for the reduction
4133 <         * @param reducer a commutative associative combining function
4134 <         * @return  the result of accumulating the given transformation
4135 <         * of all entries
4136 <         */
4137 <        public long reduceEntriesToLong(ObjectToLong<Map.Entry<K,V>> transformer,
4138 <                                        long basis,
4139 <                                        LongByLongToLong reducer) {
4140 <            return fjp.invoke(ForkJoinTasks.reduceEntriesToLong
4141 <                              (ConcurrentHashMap.this, transformer, basis, reducer));
3355 >        public void forEachRemaining(Consumer<? super K> action) {
3356 >            if (action == null) throw new NullPointerException();
3357 >            for (Node<K,V> p; (p = advance()) != null;)
3358 >                action.accept(p.key);
3359          }
3360  
3361 <        /**
3362 <         * Returns the result of accumulating the given transformation
3363 <         * of all entries using the given reducer to combine values,
3364 <         * and the given basis as an identity value.
3365 <         *
3366 <         * @param transformer a function returning the transformation
3367 <         * for an element
3368 <         * @param basis the identity (initial default value) for the reduction
3369 <         * @param reducer a commutative associative combining function
3370 <         * @return the result of accumulating the given transformation
3371 <         * of all entries
3372 <         */
3373 <        public int reduceEntriesToInt(ObjectToInt<Map.Entry<K,V>> transformer,
3374 <                                      int basis,
4158 <                                      IntByIntToInt reducer) {
4159 <            return fjp.invoke(ForkJoinTasks.reduceEntriesToInt
4160 <                              (ConcurrentHashMap.this, transformer, basis, reducer));
3361 >        public boolean tryAdvance(Consumer<? super K> action) {
3362 >            if (action == null) throw new NullPointerException();
3363 >            Node<K,V> p;
3364 >            if ((p = advance()) == null)
3365 >                return false;
3366 >            action.accept(p.key);
3367 >            return true;
3368 >        }
3369 >
3370 >        public long estimateSize() { return est; }
3371 >
3372 >        public int characteristics() {
3373 >            return Spliterator.DISTINCT | Spliterator.CONCURRENT |
3374 >                Spliterator.NONNULL;
3375          }
3376      }
3377  
3378 <    // ---------------------------------------------------------------------
3378 >    static final class ValueSpliterator<K,V> extends Traverser<K,V>
3379 >        implements Spliterator<V> {
3380 >        long est;               // size estimate
3381 >        ValueSpliterator(Node<K,V>[] tab, int size, int index, int limit,
3382 >                         long est) {
3383 >            super(tab, size, index, limit);
3384 >            this.est = est;
3385 >        }
3386  
3387 <    /**
3388 <     * Predefined tasks for performing bulk parallel operations on
3389 <     * ConcurrentHashMaps. These tasks follow the forms and rules used
3390 <     * in class {@link Parallel}. Each method has the same name, but
3391 <     * returns a task rather than invoking it. These methods may be
3392 <     * useful in custom applications such as submitting a task without
4172 <     * waiting for completion, or combining with other tasks.
4173 <     */
4174 <    public static class ForkJoinTasks {
4175 <        private ForkJoinTasks() {}
3387 >        public Spliterator<V> trySplit() {
3388 >            int i, f, h;
3389 >            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
3390 >                new ValueSpliterator<K,V>(tab, baseSize, baseLimit = h,
3391 >                                          f, est >>>= 1);
3392 >        }
3393  
3394 <        /**
4178 <         * Returns a task that when invoked, performs the given
4179 <         * action for each (key, value)
4180 <         *
4181 <         * @param map the map
4182 <         * @param action the action
4183 <         * @return the task
4184 <         */
4185 <        public static <K,V> ForkJoinTask<Void> forEach
4186 <            (ConcurrentHashMap<K,V> map,
4187 <             BiAction<K,V> action) {
3394 >        public void forEachRemaining(Consumer<? super V> action) {
3395              if (action == null) throw new NullPointerException();
3396 <            return new ForEachMappingTask<K,V>(map, null, -1, action);
3396 >            for (Node<K,V> p; (p = advance()) != null;)
3397 >                action.accept(p.val);
3398          }
3399  
3400 <        /**
3401 <         * Returns a task that when invoked, performs the given
3402 <         * action for each non-null transformation of each (key, value)
3403 <         *
3404 <         * @param map the map
3405 <         * @param transformer a function returning the transformation
3406 <         * for an element, or null if there is no transformation (in
4199 <         * which case the action is not applied)
4200 <         * @param action the action
4201 <         * @return the task
4202 <         */
4203 <        public static <K,V,U> ForkJoinTask<Void> forEach
4204 <            (ConcurrentHashMap<K,V> map,
4205 <             BiFun<? super K, ? super V, ? extends U> transformer,
4206 <             Action<U> action) {
4207 <            if (transformer == null || action == null)
4208 <                throw new NullPointerException();
4209 <            return new ForEachTransformedMappingTask<K,V,U>
4210 <                (map, null, -1, transformer, action);
3400 >        public boolean tryAdvance(Consumer<? super V> action) {
3401 >            if (action == null) throw new NullPointerException();
3402 >            Node<K,V> p;
3403 >            if ((p = advance()) == null)
3404 >                return false;
3405 >            action.accept(p.val);
3406 >            return true;
3407          }
3408  
3409 <        /**
4214 <         * Returns a task that when invoked, returns a non-null result
4215 <         * from applying the given search function on each (key,
4216 <         * value), or null if none. Upon success, further element
4217 <         * processing is suppressed and the results of any other
4218 <         * parallel invocations of the search function are ignored.
4219 <         *
4220 <         * @param map the map
4221 <         * @param searchFunction a function returning a non-null
4222 <         * result on success, else null
4223 <         * @return the task
4224 <         */
4225 <        public static <K,V,U> ForkJoinTask<U> search
4226 <            (ConcurrentHashMap<K,V> map,
4227 <             BiFun<? super K, ? super V, ? extends U> searchFunction) {
4228 <            if (searchFunction == null) throw new NullPointerException();
4229 <            return new SearchMappingsTask<K,V,U>
4230 <                (map, null, -1, searchFunction,
4231 <                 new AtomicReference<U>());
4232 <        }
3409 >        public long estimateSize() { return est; }
3410  
3411 <        /**
3412 <         * Returns a task that when invoked, returns the result of
4236 <         * accumulating the given transformation of all (key, value) pairs
4237 <         * using the given reducer to combine values, or null if none.
4238 <         *
4239 <         * @param map the map
4240 <         * @param transformer a function returning the transformation
4241 <         * for an element, or null if there is no transformation (in
4242 <         * which case it is not combined).
4243 <         * @param reducer a commutative associative combining function
4244 <         * @return the task
4245 <         */
4246 <        public static <K,V,U> ForkJoinTask<U> reduce
4247 <            (ConcurrentHashMap<K,V> map,
4248 <             BiFun<? super K, ? super V, ? extends U> transformer,
4249 <             BiFun<? super U, ? super U, ? extends U> reducer) {
4250 <            if (transformer == null || reducer == null)
4251 <                throw new NullPointerException();
4252 <            return new MapReduceMappingsTask<K,V,U>
4253 <                (map, null, -1, null, transformer, reducer);
3411 >        public int characteristics() {
3412 >            return Spliterator.CONCURRENT | Spliterator.NONNULL;
3413          }
3414 +    }
3415  
3416 <        /**
3417 <         * Returns a task that when invoked, returns the result of
3418 <         * accumulating the given transformation of all (key, value) pairs
3419 <         * using the given reducer to combine values, and the given
3420 <         * basis as an identity value.
3421 <         *
3422 <         * @param map the map
3423 <         * @param transformer a function returning the transformation
3424 <         * for an element
4265 <         * @param basis the identity (initial default value) for the reduction
4266 <         * @param reducer a commutative associative combining function
4267 <         * @return the task
4268 <         */
4269 <        public static <K,V> ForkJoinTask<Double> reduceToDouble
4270 <            (ConcurrentHashMap<K,V> map,
4271 <             ObjectByObjectToDouble<? super K, ? super V> transformer,
4272 <             double basis,
4273 <             DoubleByDoubleToDouble reducer) {
4274 <            if (transformer == null || reducer == null)
4275 <                throw new NullPointerException();
4276 <            return new MapReduceMappingsToDoubleTask<K,V>
4277 <                (map, null, -1, null, transformer, basis, reducer);
3416 >    static final class EntrySpliterator<K,V> extends Traverser<K,V>
3417 >        implements Spliterator<Map.Entry<K,V>> {
3418 >        final ConcurrentHashMap<K,V> map; // To export MapEntry
3419 >        long est;               // size estimate
3420 >        EntrySpliterator(Node<K,V>[] tab, int size, int index, int limit,
3421 >                         long est, ConcurrentHashMap<K,V> map) {
3422 >            super(tab, size, index, limit);
3423 >            this.map = map;
3424 >            this.est = est;
3425          }
3426  
3427 <        /**
3428 <         * Returns a task that when invoked, returns the result of
3429 <         * accumulating the given transformation of all (key, value) pairs
3430 <         * using the given reducer to combine values, and the given
3431 <         * basis as an identity value.
4285 <         *
4286 <         * @param map the map
4287 <         * @param transformer a function returning the transformation
4288 <         * for an element
4289 <         * @param basis the identity (initial default value) for the reduction
4290 <         * @param reducer a commutative associative combining function
4291 <         * @return the task
4292 <         */
4293 <        public static <K,V> ForkJoinTask<Long> reduceToLong
4294 <            (ConcurrentHashMap<K,V> map,
4295 <             ObjectByObjectToLong<? super K, ? super V> transformer,
4296 <             long basis,
4297 <             LongByLongToLong reducer) {
4298 <            if (transformer == null || reducer == null)
4299 <                throw new NullPointerException();
4300 <            return new MapReduceMappingsToLongTask<K,V>
4301 <                (map, null, -1, null, transformer, basis, reducer);
3427 >        public Spliterator<Map.Entry<K,V>> trySplit() {
3428 >            int i, f, h;
3429 >            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
3430 >                new EntrySpliterator<K,V>(tab, baseSize, baseLimit = h,
3431 >                                          f, est >>>= 1, map);
3432          }
3433  
3434 <        /**
3435 <         * Returns a task that when invoked, returns the result of
3436 <         * accumulating the given transformation of all (key, value) pairs
3437 <         * using the given reducer to combine values, and the given
4308 <         * basis as an identity value.
4309 <         *
4310 <         * @param transformer a function returning the transformation
4311 <         * for an element
4312 <         * @param basis the identity (initial default value) for the reduction
4313 <         * @param reducer a commutative associative combining function
4314 <         * @return the task
4315 <         */
4316 <        public static <K,V> ForkJoinTask<Integer> reduceToInt
4317 <            (ConcurrentHashMap<K,V> map,
4318 <             ObjectByObjectToInt<? super K, ? super V> transformer,
4319 <             int basis,
4320 <             IntByIntToInt reducer) {
4321 <            if (transformer == null || reducer == null)
4322 <                throw new NullPointerException();
4323 <            return new MapReduceMappingsToIntTask<K,V>
4324 <                (map, null, -1, null, transformer, basis, reducer);
3434 >        public void forEachRemaining(Consumer<? super Map.Entry<K,V>> action) {
3435 >            if (action == null) throw new NullPointerException();
3436 >            for (Node<K,V> p; (p = advance()) != null; )
3437 >                action.accept(new MapEntry<K,V>(p.key, p.val, map));
3438          }
3439  
3440 <        /**
4328 <         * Returns a task that when invoked, performs the given action
4329 <         * for each key.
4330 <         *
4331 <         * @param map the map
4332 <         * @param action the action
4333 <         * @return the task
4334 <         */
4335 <        public static <K,V> ForkJoinTask<Void> forEachKey
4336 <            (ConcurrentHashMap<K,V> map,
4337 <             Action<K> action) {
3440 >        public boolean tryAdvance(Consumer<? super Map.Entry<K,V>> action) {
3441              if (action == null) throw new NullPointerException();
3442 <            return new ForEachKeyTask<K,V>(map, null, -1, action);
3442 >            Node<K,V> p;
3443 >            if ((p = advance()) == null)
3444 >                return false;
3445 >            action.accept(new MapEntry<K,V>(p.key, p.val, map));
3446 >            return true;
3447          }
3448  
3449 <        /**
3450 <         * Returns a task that when invoked, performs the given action
3451 <         * for each non-null transformation of each key.
3452 <         *
3453 <         * @param map the map
4347 <         * @param transformer a function returning the transformation
4348 <         * for an element, or null if there is no transformation (in
4349 <         * which case the action is not applied)
4350 <         * @param action the action
4351 <         * @return the task
4352 <         */
4353 <        public static <K,V,U> ForkJoinTask<Void> forEachKey
4354 <            (ConcurrentHashMap<K,V> map,
4355 <             Fun<? super K, ? extends U> transformer,
4356 <             Action<U> action) {
4357 <            if (transformer == null || action == null)
4358 <                throw new NullPointerException();
4359 <            return new ForEachTransformedKeyTask<K,V,U>
4360 <                (map, null, -1, transformer, action);
3449 >        public long estimateSize() { return est; }
3450 >
3451 >        public int characteristics() {
3452 >            return Spliterator.DISTINCT | Spliterator.CONCURRENT |
3453 >                Spliterator.NONNULL;
3454          }
3455 +    }
3456 +
3457 +    // Parallel bulk operations
3458 +
3459 +    /**
3460 +     * Computes initial batch value for bulk tasks. The returned value
3461 +     * is approximately exp2 of the number of times (minus one) to
3462 +     * split task by two before executing leaf action. This value is
3463 +     * faster to compute and more convenient to use as a guide to
3464 +     * splitting than is the depth, since it is used while dividing by
3465 +     * two anyway.
3466 +     */
3467 +    final int batchFor(long b) {
3468 +        long n;
3469 +        if (b == Long.MAX_VALUE || (n = sumCount()) <= 1L || n < b)
3470 +            return 0;
3471 +        int sp = ForkJoinPool.getCommonPoolParallelism() << 2; // slack of 4
3472 +        return (b <= 0L || (n /= b) >= sp) ? sp : (int)n;
3473 +    }
3474 +
3475 +    /**
3476 +     * Performs the given action for each (key, value).
3477 +     *
3478 +     * @param parallelismThreshold the (estimated) number of elements
3479 +     * needed for this operation to be executed in parallel
3480 +     * @param action the action
3481 +     * @since 1.8
3482 +     */
3483 +    public void forEach(long parallelismThreshold,
3484 +                        BiConsumer<? super K,? super V> action) {
3485 +        if (action == null) throw new NullPointerException();
3486 +        new ForEachMappingTask<K,V>
3487 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3488 +             action).invoke();
3489 +    }
3490 +
3491 +    /**
3492 +     * Performs the given action for each non-null transformation
3493 +     * of each (key, value).
3494 +     *
3495 +     * @param parallelismThreshold the (estimated) number of elements
3496 +     * needed for this operation to be executed in parallel
3497 +     * @param transformer a function returning the transformation
3498 +     * for an element, or null if there is no transformation (in
3499 +     * which case the action is not applied)
3500 +     * @param action the action
3501 +     * @since 1.8
3502 +     */
3503 +    public <U> void forEach(long parallelismThreshold,
3504 +                            BiFunction<? super K, ? super V, ? extends U> transformer,
3505 +                            Consumer<? super U> action) {
3506 +        if (transformer == null || action == null)
3507 +            throw new NullPointerException();
3508 +        new ForEachTransformedMappingTask<K,V,U>
3509 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3510 +             transformer, action).invoke();
3511 +    }
3512 +
3513 +    /**
3514 +     * Returns a non-null result from applying the given search
3515 +     * function on each (key, value), or null if none.  Upon
3516 +     * success, further element processing is suppressed and the
3517 +     * results of any other parallel invocations of the search
3518 +     * function are ignored.
3519 +     *
3520 +     * @param parallelismThreshold the (estimated) number of elements
3521 +     * needed for this operation to be executed in parallel
3522 +     * @param searchFunction a function returning a non-null
3523 +     * result on success, else null
3524 +     * @return a non-null result from applying the given search
3525 +     * function on each (key, value), or null if none
3526 +     * @since 1.8
3527 +     */
3528 +    public <U> U search(long parallelismThreshold,
3529 +                        BiFunction<? super K, ? super V, ? extends U> searchFunction) {
3530 +        if (searchFunction == null) throw new NullPointerException();
3531 +        return new SearchMappingsTask<K,V,U>
3532 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3533 +             searchFunction, new AtomicReference<U>()).invoke();
3534 +    }
3535 +
3536 +    /**
3537 +     * Returns the result of accumulating the given transformation
3538 +     * of all (key, value) pairs using the given reducer to
3539 +     * combine values, or null if none.
3540 +     *
3541 +     * @param parallelismThreshold the (estimated) number of elements
3542 +     * needed for this operation to be executed in parallel
3543 +     * @param transformer a function returning the transformation
3544 +     * for an element, or null if there is no transformation (in
3545 +     * which case it is not combined)
3546 +     * @param reducer a commutative associative combining function
3547 +     * @return the result of accumulating the given transformation
3548 +     * of all (key, value) pairs
3549 +     * @since 1.8
3550 +     */
3551 +    public <U> U reduce(long parallelismThreshold,
3552 +                        BiFunction<? super K, ? super V, ? extends U> transformer,
3553 +                        BiFunction<? super U, ? super U, ? extends U> reducer) {
3554 +        if (transformer == null || reducer == null)
3555 +            throw new NullPointerException();
3556 +        return new MapReduceMappingsTask<K,V,U>
3557 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3558 +             null, transformer, reducer).invoke();
3559 +    }
3560 +
3561 +    /**
3562 +     * Returns the result of accumulating the given transformation
3563 +     * of all (key, value) pairs using the given reducer to
3564 +     * combine values, and the given basis as an identity value.
3565 +     *
3566 +     * @param parallelismThreshold the (estimated) number of elements
3567 +     * needed for this operation to be executed in parallel
3568 +     * @param transformer a function returning the transformation
3569 +     * for an element
3570 +     * @param basis the identity (initial default value) for the reduction
3571 +     * @param reducer a commutative associative combining function
3572 +     * @return the result of accumulating the given transformation
3573 +     * of all (key, value) pairs
3574 +     * @since 1.8
3575 +     */
3576 +    public double reduceToDoubleIn(long parallelismThreshold,
3577 +                                   ToDoubleBiFunction<? super K, ? super V> transformer,
3578 +                                   double basis,
3579 +                                   DoubleBinaryOperator reducer) {
3580 +        if (transformer == null || reducer == null)
3581 +            throw new NullPointerException();
3582 +        return new MapReduceMappingsToDoubleTask<K,V>
3583 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3584 +             null, transformer, basis, reducer).invoke();
3585 +    }
3586 +
3587 +    /**
3588 +     * Returns the result of accumulating the given transformation
3589 +     * of all (key, value) pairs using the given reducer to
3590 +     * combine values, and the given basis as an identity value.
3591 +     *
3592 +     * @param parallelismThreshold the (estimated) number of elements
3593 +     * needed for this operation to be executed in parallel
3594 +     * @param transformer a function returning the transformation
3595 +     * for an element
3596 +     * @param basis the identity (initial default value) for the reduction
3597 +     * @param reducer a commutative associative combining function
3598 +     * @return the result of accumulating the given transformation
3599 +     * of all (key, value) pairs
3600 +     * @since 1.8
3601 +     */
3602 +    public long reduceToLong(long parallelismThreshold,
3603 +                             ToLongBiFunction<? super K, ? super V> transformer,
3604 +                             long basis,
3605 +                             LongBinaryOperator reducer) {
3606 +        if (transformer == null || reducer == null)
3607 +            throw new NullPointerException();
3608 +        return new MapReduceMappingsToLongTask<K,V>
3609 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3610 +             null, transformer, basis, reducer).invoke();
3611 +    }
3612 +
3613 +    /**
3614 +     * Returns the result of accumulating the given transformation
3615 +     * of all (key, value) pairs using the given reducer to
3616 +     * combine values, and the given basis as an identity value.
3617 +     *
3618 +     * @param parallelismThreshold the (estimated) number of elements
3619 +     * needed for this operation to be executed in parallel
3620 +     * @param transformer a function returning the transformation
3621 +     * for an element
3622 +     * @param basis the identity (initial default value) for the reduction
3623 +     * @param reducer a commutative associative combining function
3624 +     * @return the result of accumulating the given transformation
3625 +     * of all (key, value) pairs
3626 +     * @since 1.8
3627 +     */
3628 +    public int reduceToInt(long parallelismThreshold,
3629 +                           ToIntBiFunction<? super K, ? super V> transformer,
3630 +                           int basis,
3631 +                           IntBinaryOperator reducer) {
3632 +        if (transformer == null || reducer == null)
3633 +            throw new NullPointerException();
3634 +        return new MapReduceMappingsToIntTask<K,V>
3635 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3636 +             null, transformer, basis, reducer).invoke();
3637 +    }
3638 +
3639 +    /**
3640 +     * Performs the given action for each key.
3641 +     *
3642 +     * @param parallelismThreshold the (estimated) number of elements
3643 +     * needed for this operation to be executed in parallel
3644 +     * @param action the action
3645 +     * @since 1.8
3646 +     */
3647 +    public void forEachKey(long parallelismThreshold,
3648 +                           Consumer<? super K> action) {
3649 +        if (action == null) throw new NullPointerException();
3650 +        new ForEachKeyTask<K,V>
3651 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3652 +             action).invoke();
3653 +    }
3654 +
3655 +    /**
3656 +     * Performs the given action for each non-null transformation
3657 +     * of each key.
3658 +     *
3659 +     * @param parallelismThreshold the (estimated) number of elements
3660 +     * needed for this operation to be executed in parallel
3661 +     * @param transformer a function returning the transformation
3662 +     * for an element, or null if there is no transformation (in
3663 +     * which case the action is not applied)
3664 +     * @param action the action
3665 +     * @since 1.8
3666 +     */
3667 +    public <U> void forEachKey(long parallelismThreshold,
3668 +                               Function<? super K, ? extends U> transformer,
3669 +                               Consumer<? super U> action) {
3670 +        if (transformer == null || action == null)
3671 +            throw new NullPointerException();
3672 +        new ForEachTransformedKeyTask<K,V,U>
3673 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3674 +             transformer, action).invoke();
3675 +    }
3676 +
3677 +    /**
3678 +     * Returns a non-null result from applying the given search
3679 +     * function on each key, or null if none. Upon success,
3680 +     * further element processing is suppressed and the results of
3681 +     * any other parallel invocations of the search function are
3682 +     * ignored.
3683 +     *
3684 +     * @param parallelismThreshold the (estimated) number of elements
3685 +     * needed for this operation to be executed in parallel
3686 +     * @param searchFunction a function returning a non-null
3687 +     * result on success, else null
3688 +     * @return a non-null result from applying the given search
3689 +     * function on each key, or null if none
3690 +     * @since 1.8
3691 +     */
3692 +    public <U> U searchKeys(long parallelismThreshold,
3693 +                            Function<? super K, ? extends U> searchFunction) {
3694 +        if (searchFunction == null) throw new NullPointerException();
3695 +        return new SearchKeysTask<K,V,U>
3696 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3697 +             searchFunction, new AtomicReference<U>()).invoke();
3698 +    }
3699 +
3700 +    /**
3701 +     * Returns the result of accumulating all keys using the given
3702 +     * reducer to combine values, or null if none.
3703 +     *
3704 +     * @param parallelismThreshold the (estimated) number of elements
3705 +     * needed for this operation to be executed in parallel
3706 +     * @param reducer a commutative associative combining function
3707 +     * @return the result of accumulating all keys using the given
3708 +     * reducer to combine values, or null if none
3709 +     * @since 1.8
3710 +     */
3711 +    public K reduceKeys(long parallelismThreshold,
3712 +                        BiFunction<? super K, ? super K, ? extends K> reducer) {
3713 +        if (reducer == null) throw new NullPointerException();
3714 +        return new ReduceKeysTask<K,V>
3715 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3716 +             null, reducer).invoke();
3717 +    }
3718 +
3719 +    /**
3720 +     * Returns the result of accumulating the given transformation
3721 +     * of all keys using the given reducer to combine values, or
3722 +     * null if none.
3723 +     *
3724 +     * @param parallelismThreshold the (estimated) number of elements
3725 +     * needed for this operation to be executed in parallel
3726 +     * @param transformer a function returning the transformation
3727 +     * for an element, or null if there is no transformation (in
3728 +     * which case it is not combined)
3729 +     * @param reducer a commutative associative combining function
3730 +     * @return the result of accumulating the given transformation
3731 +     * of all keys
3732 +     * @since 1.8
3733 +     */
3734 +    public <U> U reduceKeys(long parallelismThreshold,
3735 +                            Function<? super K, ? extends U> transformer,
3736 +         BiFunction<? super U, ? super U, ? extends U> reducer) {
3737 +        if (transformer == null || reducer == null)
3738 +            throw new NullPointerException();
3739 +        return new MapReduceKeysTask<K,V,U>
3740 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3741 +             null, transformer, reducer).invoke();
3742 +    }
3743 +
3744 +    /**
3745 +     * Returns the result of accumulating the given transformation
3746 +     * of all keys using the given reducer to combine values, and
3747 +     * the given basis as an identity value.
3748 +     *
3749 +     * @param parallelismThreshold the (estimated) number of elements
3750 +     * needed for this operation to be executed in parallel
3751 +     * @param transformer a function returning the transformation
3752 +     * for an element
3753 +     * @param basis the identity (initial default value) for the reduction
3754 +     * @param reducer a commutative associative combining function
3755 +     * @return the result of accumulating the given transformation
3756 +     * of all keys
3757 +     * @since 1.8
3758 +     */
3759 +    public double reduceKeysToDouble(long parallelismThreshold,
3760 +                                     ToDoubleFunction<? super K> transformer,
3761 +                                     double basis,
3762 +                                     DoubleBinaryOperator reducer) {
3763 +        if (transformer == null || reducer == null)
3764 +            throw new NullPointerException();
3765 +        return new MapReduceKeysToDoubleTask<K,V>
3766 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3767 +             null, transformer, basis, reducer).invoke();
3768 +    }
3769 +
3770 +    /**
3771 +     * Returns the result of accumulating the given transformation
3772 +     * of all keys using the given reducer to combine values, and
3773 +     * the given basis as an identity value.
3774 +     *
3775 +     * @param parallelismThreshold the (estimated) number of elements
3776 +     * needed for this operation to be executed in parallel
3777 +     * @param transformer a function returning the transformation
3778 +     * for an element
3779 +     * @param basis the identity (initial default value) for the reduction
3780 +     * @param reducer a commutative associative combining function
3781 +     * @return the result of accumulating the given transformation
3782 +     * of all keys
3783 +     * @since 1.8
3784 +     */
3785 +    public long reduceKeysToLong(long parallelismThreshold,
3786 +                                 ToLongFunction<? super K> transformer,
3787 +                                 long basis,
3788 +                                 LongBinaryOperator reducer) {
3789 +        if (transformer == null || reducer == null)
3790 +            throw new NullPointerException();
3791 +        return new MapReduceKeysToLongTask<K,V>
3792 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3793 +             null, transformer, basis, reducer).invoke();
3794 +    }
3795 +
3796 +    /**
3797 +     * Returns the result of accumulating the given transformation
3798 +     * of all keys using the given reducer to combine values, and
3799 +     * the given basis as an identity value.
3800 +     *
3801 +     * @param parallelismThreshold the (estimated) number of elements
3802 +     * needed for this operation to be executed in parallel
3803 +     * @param transformer a function returning the transformation
3804 +     * for an element
3805 +     * @param basis the identity (initial default value) for the reduction
3806 +     * @param reducer a commutative associative combining function
3807 +     * @return the result of accumulating the given transformation
3808 +     * of all keys
3809 +     * @since 1.8
3810 +     */
3811 +    public int reduceKeysToInt(long parallelismThreshold,
3812 +                               ToIntFunction<? super K> transformer,
3813 +                               int basis,
3814 +                               IntBinaryOperator reducer) {
3815 +        if (transformer == null || reducer == null)
3816 +            throw new NullPointerException();
3817 +        return new MapReduceKeysToIntTask<K,V>
3818 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3819 +             null, transformer, basis, reducer).invoke();
3820 +    }
3821 +
3822 +    /**
3823 +     * Performs the given action for each value.
3824 +     *
3825 +     * @param parallelismThreshold the (estimated) number of elements
3826 +     * needed for this operation to be executed in parallel
3827 +     * @param action the action
3828 +     * @since 1.8
3829 +     */
3830 +    public void forEachValue(long parallelismThreshold,
3831 +                             Consumer<? super V> action) {
3832 +        if (action == null)
3833 +            throw new NullPointerException();
3834 +        new ForEachValueTask<K,V>
3835 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3836 +             action).invoke();
3837 +    }
3838 +
3839 +    /**
3840 +     * Performs the given action for each non-null transformation
3841 +     * of each value.
3842 +     *
3843 +     * @param parallelismThreshold the (estimated) number of elements
3844 +     * needed for this operation to be executed in parallel
3845 +     * @param transformer a function returning the transformation
3846 +     * for an element, or null if there is no transformation (in
3847 +     * which case the action is not applied)
3848 +     * @param action the action
3849 +     * @since 1.8
3850 +     */
3851 +    public <U> void forEachValue(long parallelismThreshold,
3852 +                                 Function<? super V, ? extends U> transformer,
3853 +                                 Consumer<? super U> action) {
3854 +        if (transformer == null || action == null)
3855 +            throw new NullPointerException();
3856 +        new ForEachTransformedValueTask<K,V,U>
3857 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3858 +             transformer, action).invoke();
3859 +    }
3860 +
3861 +    /**
3862 +     * Returns a non-null result from applying the given search
3863 +     * function on each value, or null if none.  Upon success,
3864 +     * further element processing is suppressed and the results of
3865 +     * any other parallel invocations of the search function are
3866 +     * ignored.
3867 +     *
3868 +     * @param parallelismThreshold the (estimated) number of elements
3869 +     * needed for this operation to be executed in parallel
3870 +     * @param searchFunction a function returning a non-null
3871 +     * result on success, else null
3872 +     * @return a non-null result from applying the given search
3873 +     * function on each value, or null if none
3874 +     * @since 1.8
3875 +     */
3876 +    public <U> U searchValues(long parallelismThreshold,
3877 +                              Function<? super V, ? extends U> searchFunction) {
3878 +        if (searchFunction == null) throw new NullPointerException();
3879 +        return new SearchValuesTask<K,V,U>
3880 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3881 +             searchFunction, new AtomicReference<U>()).invoke();
3882 +    }
3883 +
3884 +    /**
3885 +     * Returns the result of accumulating all values using the
3886 +     * given reducer to combine values, or null if none.
3887 +     *
3888 +     * @param parallelismThreshold the (estimated) number of elements
3889 +     * needed for this operation to be executed in parallel
3890 +     * @param reducer a commutative associative combining function
3891 +     * @return the result of accumulating all values
3892 +     * @since 1.8
3893 +     */
3894 +    public V reduceValues(long parallelismThreshold,
3895 +                          BiFunction<? super V, ? super V, ? extends V> reducer) {
3896 +        if (reducer == null) throw new NullPointerException();
3897 +        return new ReduceValuesTask<K,V>
3898 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3899 +             null, reducer).invoke();
3900 +    }
3901 +
3902 +    /**
3903 +     * Returns the result of accumulating the given transformation
3904 +     * of all values using the given reducer to combine values, or
3905 +     * null if none.
3906 +     *
3907 +     * @param parallelismThreshold the (estimated) number of elements
3908 +     * needed for this operation to be executed in parallel
3909 +     * @param transformer a function returning the transformation
3910 +     * for an element, or null if there is no transformation (in
3911 +     * which case it is not combined)
3912 +     * @param reducer a commutative associative combining function
3913 +     * @return the result of accumulating the given transformation
3914 +     * of all values
3915 +     * @since 1.8
3916 +     */
3917 +    public <U> U reduceValues(long parallelismThreshold,
3918 +                              Function<? super V, ? extends U> transformer,
3919 +                              BiFunction<? super U, ? super U, ? extends U> reducer) {
3920 +        if (transformer == null || reducer == null)
3921 +            throw new NullPointerException();
3922 +        return new MapReduceValuesTask<K,V,U>
3923 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3924 +             null, transformer, reducer).invoke();
3925 +    }
3926 +
3927 +    /**
3928 +     * Returns the result of accumulating the given transformation
3929 +     * of all values using the given reducer to combine values,
3930 +     * and the given basis as an identity value.
3931 +     *
3932 +     * @param parallelismThreshold the (estimated) number of elements
3933 +     * needed for this operation to be executed in parallel
3934 +     * @param transformer a function returning the transformation
3935 +     * for an element
3936 +     * @param basis the identity (initial default value) for the reduction
3937 +     * @param reducer a commutative associative combining function
3938 +     * @return the result of accumulating the given transformation
3939 +     * of all values
3940 +     * @since 1.8
3941 +     */
3942 +    public double reduceValuesToDouble(long parallelismThreshold,
3943 +                                       ToDoubleFunction<? super V> transformer,
3944 +                                       double basis,
3945 +                                       DoubleBinaryOperator reducer) {
3946 +        if (transformer == null || reducer == null)
3947 +            throw new NullPointerException();
3948 +        return new MapReduceValuesToDoubleTask<K,V>
3949 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3950 +             null, transformer, basis, reducer).invoke();
3951 +    }
3952 +
3953 +    /**
3954 +     * Returns the result of accumulating the given transformation
3955 +     * of all values using the given reducer to combine values,
3956 +     * and the given basis as an identity value.
3957 +     *
3958 +     * @param parallelismThreshold the (estimated) number of elements
3959 +     * needed for this operation to be executed in parallel
3960 +     * @param transformer a function returning the transformation
3961 +     * for an element
3962 +     * @param basis the identity (initial default value) for the reduction
3963 +     * @param reducer a commutative associative combining function
3964 +     * @return the result of accumulating the given transformation
3965 +     * of all values
3966 +     * @since 1.8
3967 +     */
3968 +    public long reduceValuesToLong(long parallelismThreshold,
3969 +                                   ToLongFunction<? super V> transformer,
3970 +                                   long basis,
3971 +                                   LongBinaryOperator reducer) {
3972 +        if (transformer == null || reducer == null)
3973 +            throw new NullPointerException();
3974 +        return new MapReduceValuesToLongTask<K,V>
3975 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3976 +             null, transformer, basis, reducer).invoke();
3977 +    }
3978 +
3979 +    /**
3980 +     * Returns the result of accumulating the given transformation
3981 +     * of all values using the given reducer to combine values,
3982 +     * and the given basis as an identity value.
3983 +     *
3984 +     * @param parallelismThreshold the (estimated) number of elements
3985 +     * needed for this operation to be executed in parallel
3986 +     * @param transformer a function returning the transformation
3987 +     * for an element
3988 +     * @param basis the identity (initial default value) for the reduction
3989 +     * @param reducer a commutative associative combining function
3990 +     * @return the result of accumulating the given transformation
3991 +     * of all values
3992 +     * @since 1.8
3993 +     */
3994 +    public int reduceValuesToInt(long parallelismThreshold,
3995 +                                 ToIntFunction<? super V> transformer,
3996 +                                 int basis,
3997 +                                 IntBinaryOperator reducer) {
3998 +        if (transformer == null || reducer == null)
3999 +            throw new NullPointerException();
4000 +        return new MapReduceValuesToIntTask<K,V>
4001 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4002 +             null, transformer, basis, reducer).invoke();
4003 +    }
4004 +
4005 +    /**
4006 +     * Performs the given action for each entry.
4007 +     *
4008 +     * @param parallelismThreshold the (estimated) number of elements
4009 +     * needed for this operation to be executed in parallel
4010 +     * @param action the action
4011 +     * @since 1.8
4012 +     */
4013 +    public void forEachEntry(long parallelismThreshold,
4014 +                             Consumer<? super Map.Entry<K,V>> action) {
4015 +        if (action == null) throw new NullPointerException();
4016 +        new ForEachEntryTask<K,V>(null, batchFor(parallelismThreshold), 0, 0, table,
4017 +                                  action).invoke();
4018 +    }
4019 +
4020 +    /**
4021 +     * Performs the given action for each non-null transformation
4022 +     * of each entry.
4023 +     *
4024 +     * @param parallelismThreshold the (estimated) number of elements
4025 +     * needed for this operation to be executed in parallel
4026 +     * @param transformer a function returning the transformation
4027 +     * for an element, or null if there is no transformation (in
4028 +     * which case the action is not applied)
4029 +     * @param action the action
4030 +     * @since 1.8
4031 +     */
4032 +    public <U> void forEachEntry(long parallelismThreshold,
4033 +                                 Function<Map.Entry<K,V>, ? extends U> transformer,
4034 +                                 Consumer<? super U> action) {
4035 +        if (transformer == null || action == null)
4036 +            throw new NullPointerException();
4037 +        new ForEachTransformedEntryTask<K,V,U>
4038 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4039 +             transformer, action).invoke();
4040 +    }
4041 +
4042 +    /**
4043 +     * Returns a non-null result from applying the given search
4044 +     * function on each entry, or null if none.  Upon success,
4045 +     * further element processing is suppressed and the results of
4046 +     * any other parallel invocations of the search function are
4047 +     * ignored.
4048 +     *
4049 +     * @param parallelismThreshold the (estimated) number of elements
4050 +     * needed for this operation to be executed in parallel
4051 +     * @param searchFunction a function returning a non-null
4052 +     * result on success, else null
4053 +     * @return a non-null result from applying the given search
4054 +     * function on each entry, or null if none
4055 +     * @since 1.8
4056 +     */
4057 +    public <U> U searchEntries(long parallelismThreshold,
4058 +                               Function<Map.Entry<K,V>, ? extends U> searchFunction) {
4059 +        if (searchFunction == null) throw new NullPointerException();
4060 +        return new SearchEntriesTask<K,V,U>
4061 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4062 +             searchFunction, new AtomicReference<U>()).invoke();
4063 +    }
4064 +
4065 +    /**
4066 +     * Returns the result of accumulating all entries using the
4067 +     * given reducer to combine values, or null if none.
4068 +     *
4069 +     * @param parallelismThreshold the (estimated) number of elements
4070 +     * needed for this operation to be executed in parallel
4071 +     * @param reducer a commutative associative combining function
4072 +     * @return the result of accumulating all entries
4073 +     * @since 1.8
4074 +     */
4075 +    public Map.Entry<K,V> reduceEntries(long parallelismThreshold,
4076 +                                        BiFunction<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4077 +        if (reducer == null) throw new NullPointerException();
4078 +        return new ReduceEntriesTask<K,V>
4079 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4080 +             null, reducer).invoke();
4081 +    }
4082 +
4083 +    /**
4084 +     * Returns the result of accumulating the given transformation
4085 +     * of all entries using the given reducer to combine values,
4086 +     * or null if none.
4087 +     *
4088 +     * @param parallelismThreshold the (estimated) number of elements
4089 +     * needed for this operation to be executed in parallel
4090 +     * @param transformer a function returning the transformation
4091 +     * for an element, or null if there is no transformation (in
4092 +     * which case it is not combined)
4093 +     * @param reducer a commutative associative combining function
4094 +     * @return the result of accumulating the given transformation
4095 +     * of all entries
4096 +     * @since 1.8
4097 +     */
4098 +    public <U> U reduceEntries(long parallelismThreshold,
4099 +                               Function<Map.Entry<K,V>, ? extends U> transformer,
4100 +                               BiFunction<? super U, ? super U, ? extends U> reducer) {
4101 +        if (transformer == null || reducer == null)
4102 +            throw new NullPointerException();
4103 +        return new MapReduceEntriesTask<K,V,U>
4104 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4105 +             null, transformer, reducer).invoke();
4106 +    }
4107 +
4108 +    /**
4109 +     * Returns the result of accumulating the given transformation
4110 +     * of all entries using the given reducer to combine values,
4111 +     * and the given basis as an identity value.
4112 +     *
4113 +     * @param parallelismThreshold the (estimated) number of elements
4114 +     * needed for this operation to be executed in parallel
4115 +     * @param transformer a function returning the transformation
4116 +     * for an element
4117 +     * @param basis the identity (initial default value) for the reduction
4118 +     * @param reducer a commutative associative combining function
4119 +     * @return the result of accumulating the given transformation
4120 +     * of all entries
4121 +     * @since 1.8
4122 +     */
4123 +    public double reduceEntriesToDouble(long parallelismThreshold,
4124 +                                        ToDoubleFunction<Map.Entry<K,V>> transformer,
4125 +                                        double basis,
4126 +                                        DoubleBinaryOperator reducer) {
4127 +        if (transformer == null || reducer == null)
4128 +            throw new NullPointerException();
4129 +        return new MapReduceEntriesToDoubleTask<K,V>
4130 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4131 +             null, transformer, basis, reducer).invoke();
4132 +    }
4133 +
4134 +    /**
4135 +     * Returns the result of accumulating the given transformation
4136 +     * of all entries using the given reducer to combine values,
4137 +     * and the given basis as an identity value.
4138 +     *
4139 +     * @param parallelismThreshold the (estimated) number of elements
4140 +     * needed for this operation to be executed in parallel
4141 +     * @param transformer a function returning the transformation
4142 +     * for an element
4143 +     * @param basis the identity (initial default value) for the reduction
4144 +     * @param reducer a commutative associative combining function
4145 +     * @return the result of accumulating the given transformation
4146 +     * of all entries
4147 +     * @since 1.8
4148 +     */
4149 +    public long reduceEntriesToLong(long parallelismThreshold,
4150 +                                    ToLongFunction<Map.Entry<K,V>> transformer,
4151 +                                    long basis,
4152 +                                    LongBinaryOperator reducer) {
4153 +        if (transformer == null || reducer == null)
4154 +            throw new NullPointerException();
4155 +        return new MapReduceEntriesToLongTask<K,V>
4156 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4157 +             null, transformer, basis, reducer).invoke();
4158 +    }
4159 +
4160 +    /**
4161 +     * Returns the result of accumulating the given transformation
4162 +     * of all entries using the given reducer to combine values,
4163 +     * and the given basis as an identity value.
4164 +     *
4165 +     * @param parallelismThreshold the (estimated) number of elements
4166 +     * needed for this operation to be executed in parallel
4167 +     * @param transformer a function returning the transformation
4168 +     * for an element
4169 +     * @param basis the identity (initial default value) for the reduction
4170 +     * @param reducer a commutative associative combining function
4171 +     * @return the result of accumulating the given transformation
4172 +     * of all entries
4173 +     * @since 1.8
4174 +     */
4175 +    public int reduceEntriesToInt(long parallelismThreshold,
4176 +                                  ToIntFunction<Map.Entry<K,V>> transformer,
4177 +                                  int basis,
4178 +                                  IntBinaryOperator reducer) {
4179 +        if (transformer == null || reducer == null)
4180 +            throw new NullPointerException();
4181 +        return new MapReduceEntriesToIntTask<K,V>
4182 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4183 +             null, transformer, basis, reducer).invoke();
4184 +    }
4185 +
4186 +
4187 +    /* ----------------Views -------------- */
4188 +
4189 +    /**
4190 +     * Base class for views.
4191 +     */
4192 +    abstract static class CollectionView<K,V,E>
4193 +        implements Collection<E>, java.io.Serializable {
4194 +        private static final long serialVersionUID = 7249069246763182397L;
4195 +        final ConcurrentHashMap<K,V> map;
4196 +        CollectionView(ConcurrentHashMap<K,V> map)  { this.map = map; }
4197  
4198          /**
4199 <         * Returns a task that when invoked, returns a non-null result
4365 <         * from applying the given search function on each key, or
4366 <         * null if none.  Upon success, further element processing is
4367 <         * suppressed and the results of any other parallel
4368 <         * invocations of the search function are ignored.
4199 >         * Returns the map backing this view.
4200           *
4201 <         * @param map the map
4371 <         * @param searchFunction a function returning a non-null
4372 <         * result on success, else null
4373 <         * @return the task
4201 >         * @return the map backing this view
4202           */
4203 <        public static <K,V,U> ForkJoinTask<U> searchKeys
4376 <            (ConcurrentHashMap<K,V> map,
4377 <             Fun<? super K, ? extends U> searchFunction) {
4378 <            if (searchFunction == null) throw new NullPointerException();
4379 <            return new SearchKeysTask<K,V,U>
4380 <                (map, null, -1, searchFunction,
4381 <                 new AtomicReference<U>());
4382 <        }
4203 >        public ConcurrentHashMap<K,V> getMap() { return map; }
4204  
4205          /**
4206 <         * Returns a task that when invoked, returns the result of
4207 <         * accumulating all keys using the given reducer to combine
4387 <         * values, or null if none.
4388 <         *
4389 <         * @param map the map
4390 <         * @param reducer a commutative associative combining function
4391 <         * @return the task
4206 >         * Removes all of the elements from this view, by removing all
4207 >         * the mappings from the map backing this view.
4208           */
4209 <        public static <K,V> ForkJoinTask<K> reduceKeys
4210 <            (ConcurrentHashMap<K,V> map,
4211 <             BiFun<? super K, ? super K, ? extends K> reducer) {
4396 <            if (reducer == null) throw new NullPointerException();
4397 <            return new ReduceKeysTask<K,V>
4398 <                (map, null, -1, null, reducer);
4399 <        }
4209 >        public final void clear()      { map.clear(); }
4210 >        public final int size()        { return map.size(); }
4211 >        public final boolean isEmpty() { return map.isEmpty(); }
4212  
4213 +        // implementations below rely on concrete classes supplying these
4214 +        // abstract methods
4215          /**
4216 <         * Returns a task that when invoked, returns the result of
4217 <         * accumulating the given transformation of all keys using the given
4218 <         * reducer to combine values, or null if none.
4219 <         *
4220 <         * @param map the map
4221 <         * @param transformer a function returning the transformation
4222 <         * for an element, or null if there is no transformation (in
4223 <         * which case it is not combined).
4224 <         * @param reducer a commutative associative combining function
4225 <         * @return the task
4226 <         */
4227 <        public static <K,V,U> ForkJoinTask<U> reduceKeys
4228 <            (ConcurrentHashMap<K,V> map,
4229 <             Fun<? super K, ? extends U> transformer,
4230 <             BiFun<? super U, ? super U, ? extends U> reducer) {
4231 <            if (transformer == null || reducer == null)
4232 <                throw new NullPointerException();
4233 <            return new MapReduceKeysTask<K,V,U>
4234 <                (map, null, -1, null, transformer, reducer);
4216 >         * Returns a "weakly consistent" iterator that will never
4217 >         * throw {@link ConcurrentModificationException}, and
4218 >         * guarantees to traverse elements as they existed upon
4219 >         * construction of the iterator, and may (but is not
4220 >         * guaranteed to) reflect any modifications subsequent to
4221 >         * construction.
4222 >         */
4223 >        public abstract Iterator<E> iterator();
4224 >        public abstract boolean contains(Object o);
4225 >        public abstract boolean remove(Object o);
4226 >
4227 >        private static final String oomeMsg = "Required array size too large";
4228 >
4229 >        public final Object[] toArray() {
4230 >            long sz = map.mappingCount();
4231 >            if (sz > MAX_ARRAY_SIZE)
4232 >                throw new OutOfMemoryError(oomeMsg);
4233 >            int n = (int)sz;
4234 >            Object[] r = new Object[n];
4235 >            int i = 0;
4236 >            for (E e : this) {
4237 >                if (i == n) {
4238 >                    if (n >= MAX_ARRAY_SIZE)
4239 >                        throw new OutOfMemoryError(oomeMsg);
4240 >                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
4241 >                        n = MAX_ARRAY_SIZE;
4242 >                    else
4243 >                        n += (n >>> 1) + 1;
4244 >                    r = Arrays.copyOf(r, n);
4245 >                }
4246 >                r[i++] = e;
4247 >            }
4248 >            return (i == n) ? r : Arrays.copyOf(r, i);
4249          }
4250  
4251 <        /**
4252 <         * Returns a task that when invoked, returns the result of
4253 <         * accumulating the given transformation of all keys using the given
4254 <         * reducer to combine values, and the given basis as an
4255 <         * identity value.
4256 <         *
4257 <         * @param map the map
4258 <         * @param transformer a function returning the transformation
4259 <         * for an element
4260 <         * @param basis the identity (initial default value) for the reduction
4261 <         * @param reducer a commutative associative combining function
4262 <         * @return the task
4263 <         */
4264 <        public static <K,V> ForkJoinTask<Double> reduceKeysToDouble
4265 <            (ConcurrentHashMap<K,V> map,
4266 <             ObjectToDouble<? super K> transformer,
4267 <             double basis,
4268 <             DoubleByDoubleToDouble reducer) {
4269 <            if (transformer == null || reducer == null)
4270 <                throw new NullPointerException();
4271 <            return new MapReduceKeysToDoubleTask<K,V>
4272 <                (map, null, -1, null, transformer, basis, reducer);
4251 >        @SuppressWarnings("unchecked")
4252 >        public final <T> T[] toArray(T[] a) {
4253 >            long sz = map.mappingCount();
4254 >            if (sz > MAX_ARRAY_SIZE)
4255 >                throw new OutOfMemoryError(oomeMsg);
4256 >            int m = (int)sz;
4257 >            T[] r = (a.length >= m) ? a :
4258 >                (T[])java.lang.reflect.Array
4259 >                .newInstance(a.getClass().getComponentType(), m);
4260 >            int n = r.length;
4261 >            int i = 0;
4262 >            for (E e : this) {
4263 >                if (i == n) {
4264 >                    if (n >= MAX_ARRAY_SIZE)
4265 >                        throw new OutOfMemoryError(oomeMsg);
4266 >                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
4267 >                        n = MAX_ARRAY_SIZE;
4268 >                    else
4269 >                        n += (n >>> 1) + 1;
4270 >                    r = Arrays.copyOf(r, n);
4271 >                }
4272 >                r[i++] = (T)e;
4273 >            }
4274 >            if (a == r && i < n) {
4275 >                r[i] = null; // null-terminate
4276 >                return r;
4277 >            }
4278 >            return (i == n) ? r : Arrays.copyOf(r, i);
4279          }
4280  
4281          /**
4282 <         * Returns a task that when invoked, returns the result of
4283 <         * accumulating the given transformation of all keys using the given
4284 <         * reducer to combine values, and the given basis as an
4285 <         * identity value.
4282 >         * Returns a string representation of this collection.
4283 >         * The string representation consists of the string representations
4284 >         * of the collection's elements in the order they are returned by
4285 >         * its iterator, enclosed in square brackets ({@code "[]"}).
4286 >         * Adjacent elements are separated by the characters {@code ", "}
4287 >         * (comma and space).  Elements are converted to strings as by
4288 >         * {@link String#valueOf(Object)}.
4289           *
4290 <         * @param map the map
4454 <         * @param transformer a function returning the transformation
4455 <         * for an element
4456 <         * @param basis the identity (initial default value) for the reduction
4457 <         * @param reducer a commutative associative combining function
4458 <         * @return the task
4290 >         * @return a string representation of this collection
4291           */
4292 <        public static <K,V> ForkJoinTask<Long> reduceKeysToLong
4293 <            (ConcurrentHashMap<K,V> map,
4294 <             ObjectToLong<? super K> transformer,
4295 <             long basis,
4296 <             LongByLongToLong reducer) {
4297 <            if (transformer == null || reducer == null)
4298 <                throw new NullPointerException();
4299 <            return new MapReduceKeysToLongTask<K,V>
4300 <                (map, null, -1, null, transformer, basis, reducer);
4292 >        public final String toString() {
4293 >            StringBuilder sb = new StringBuilder();
4294 >            sb.append('[');
4295 >            Iterator<E> it = iterator();
4296 >            if (it.hasNext()) {
4297 >                for (;;) {
4298 >                    Object e = it.next();
4299 >                    sb.append(e == this ? "(this Collection)" : e);
4300 >                    if (!it.hasNext())
4301 >                        break;
4302 >                    sb.append(',').append(' ');
4303 >                }
4304 >            }
4305 >            return sb.append(']').toString();
4306          }
4307  
4308 <        /**
4309 <         * Returns a task that when invoked, returns the result of
4310 <         * accumulating the given transformation of all keys using the given
4311 <         * reducer to combine values, and the given basis as an
4312 <         * identity value.
4313 <         *
4314 <         * @param map the map
4315 <         * @param transformer a function returning the transformation
4479 <         * for an element
4480 <         * @param basis the identity (initial default value) for the reduction
4481 <         * @param reducer a commutative associative combining function
4482 <         * @return the task
4483 <         */
4484 <        public static <K,V> ForkJoinTask<Integer> reduceKeysToInt
4485 <            (ConcurrentHashMap<K,V> map,
4486 <             ObjectToInt<? super K> transformer,
4487 <             int basis,
4488 <             IntByIntToInt reducer) {
4489 <            if (transformer == null || reducer == null)
4490 <                throw new NullPointerException();
4491 <            return new MapReduceKeysToIntTask<K,V>
4492 <                (map, null, -1, null, transformer, basis, reducer);
4308 >        public final boolean containsAll(Collection<?> c) {
4309 >            if (c != this) {
4310 >                for (Object e : c) {
4311 >                    if (e == null || !contains(e))
4312 >                        return false;
4313 >                }
4314 >            }
4315 >            return true;
4316          }
4317  
4318 <        /**
4319 <         * Returns a task that when invoked, performs the given action
4320 <         * for each value.
4321 <         *
4322 <         * @param map the map
4323 <         * @param action the action
4324 <         */
4325 <        public static <K,V> ForkJoinTask<Void> forEachValue
4326 <            (ConcurrentHashMap<K,V> map,
4504 <             Action<V> action) {
4505 <            if (action == null) throw new NullPointerException();
4506 <            return new ForEachValueTask<K,V>(map, null, -1, action);
4318 >        public final boolean removeAll(Collection<?> c) {
4319 >            boolean modified = false;
4320 >            for (Iterator<E> it = iterator(); it.hasNext();) {
4321 >                if (c.contains(it.next())) {
4322 >                    it.remove();
4323 >                    modified = true;
4324 >                }
4325 >            }
4326 >            return modified;
4327          }
4328  
4329 <        /**
4330 <         * Returns a task that when invoked, performs the given action
4331 <         * for each non-null transformation of each value.
4332 <         *
4333 <         * @param map the map
4334 <         * @param transformer a function returning the transformation
4335 <         * for an element, or null if there is no transformation (in
4336 <         * which case the action is not applied)
4337 <         * @param action the action
4518 <         */
4519 <        public static <K,V,U> ForkJoinTask<Void> forEachValue
4520 <            (ConcurrentHashMap<K,V> map,
4521 <             Fun<? super V, ? extends U> transformer,
4522 <             Action<U> action) {
4523 <            if (transformer == null || action == null)
4524 <                throw new NullPointerException();
4525 <            return new ForEachTransformedValueTask<K,V,U>
4526 <                (map, null, -1, transformer, action);
4329 >        public final boolean retainAll(Collection<?> c) {
4330 >            boolean modified = false;
4331 >            for (Iterator<E> it = iterator(); it.hasNext();) {
4332 >                if (!c.contains(it.next())) {
4333 >                    it.remove();
4334 >                    modified = true;
4335 >                }
4336 >            }
4337 >            return modified;
4338          }
4339  
4340 <        /**
4341 <         * Returns a task that when invoked, returns a non-null result
4342 <         * from applying the given search function on each value, or
4343 <         * null if none.  Upon success, further element processing is
4344 <         * suppressed and the results of any other parallel
4345 <         * invocations of the search function are ignored.
4346 <         *
4347 <         * @param map the map
4348 <         * @param searchFunction a function returning a non-null
4349 <         * result on success, else null
4350 <         * @return the task
4351 <         *
4352 <         */
4353 <        public static <K,V,U> ForkJoinTask<U> searchValues
4354 <            (ConcurrentHashMap<K,V> map,
4355 <             Fun<? super V, ? extends U> searchFunction) {
4356 <            if (searchFunction == null) throw new NullPointerException();
4357 <            return new SearchValuesTask<K,V,U>
4358 <                (map, null, -1, searchFunction,
4359 <                 new AtomicReference<U>());
4340 >    }
4341 >
4342 >    /**
4343 >     * A view of a ConcurrentHashMap as a {@link Set} of keys, in
4344 >     * which additions may optionally be enabled by mapping to a
4345 >     * common value.  This class cannot be directly instantiated.
4346 >     * See {@link #keySet() keySet()},
4347 >     * {@link #keySet(Object) keySet(V)},
4348 >     * {@link #newKeySet() newKeySet()},
4349 >     * {@link #newKeySet(int) newKeySet(int)}.
4350 >     *
4351 >     * @since 1.8
4352 >     */
4353 >    public static class KeySetView<K,V> extends CollectionView<K,V,K>
4354 >        implements Set<K>, java.io.Serializable {
4355 >        private static final long serialVersionUID = 7249069246763182397L;
4356 >        private final V value;
4357 >        KeySetView(ConcurrentHashMap<K,V> map, V value) {  // non-public
4358 >            super(map);
4359 >            this.value = value;
4360          }
4361  
4362          /**
4363 <         * Returns a task that when invoked, returns the result of
4364 <         * accumulating all values using the given reducer to combine
4554 <         * values, or null if none.
4363 >         * Returns the default mapped value for additions,
4364 >         * or {@code null} if additions are not supported.
4365           *
4366 <         * @param map the map
4367 <         * @param reducer a commutative associative combining function
4558 <         * @return the task
4366 >         * @return the default mapped value for additions, or {@code null}
4367 >         * if not supported
4368           */
4369 <        public static <K,V> ForkJoinTask<V> reduceValues
4561 <            (ConcurrentHashMap<K,V> map,
4562 <             BiFun<? super V, ? super V, ? extends V> reducer) {
4563 <            if (reducer == null) throw new NullPointerException();
4564 <            return new ReduceValuesTask<K,V>
4565 <                (map, null, -1, null, reducer);
4566 <        }
4369 >        public V getMappedValue() { return value; }
4370  
4371          /**
4372 <         * Returns a task that when invoked, returns the result of
4373 <         * accumulating the given transformation of all values using the
4571 <         * given reducer to combine values, or null if none.
4572 <         *
4573 <         * @param map the map
4574 <         * @param transformer a function returning the transformation
4575 <         * for an element, or null if there is no transformation (in
4576 <         * which case it is not combined).
4577 <         * @param reducer a commutative associative combining function
4578 <         * @return the task
4372 >         * {@inheritDoc}
4373 >         * @throws NullPointerException if the specified key is null
4374           */
4375 <        public static <K,V,U> ForkJoinTask<U> reduceValues
4581 <            (ConcurrentHashMap<K,V> map,
4582 <             Fun<? super V, ? extends U> transformer,
4583 <             BiFun<? super U, ? super U, ? extends U> reducer) {
4584 <            if (transformer == null || reducer == null)
4585 <                throw new NullPointerException();
4586 <            return new MapReduceValuesTask<K,V,U>
4587 <                (map, null, -1, null, transformer, reducer);
4588 <        }
4375 >        public boolean contains(Object o) { return map.containsKey(o); }
4376  
4377          /**
4378 <         * Returns a task that when invoked, returns the result of
4379 <         * accumulating the given transformation of all values using the
4380 <         * given reducer to combine values, and the given basis as an
4594 <         * identity value.
4378 >         * Removes the key from this map view, by removing the key (and its
4379 >         * corresponding value) from the backing map.  This method does
4380 >         * nothing if the key is not in the map.
4381           *
4382 <         * @param map the map
4383 <         * @param transformer a function returning the transformation
4384 <         * for an element
4599 <         * @param basis the identity (initial default value) for the reduction
4600 <         * @param reducer a commutative associative combining function
4601 <         * @return the task
4382 >         * @param  o the key to be removed from the backing map
4383 >         * @return {@code true} if the backing map contained the specified key
4384 >         * @throws NullPointerException if the specified key is null
4385           */
4386 <        public static <K,V> ForkJoinTask<Double> reduceValuesToDouble
4604 <            (ConcurrentHashMap<K,V> map,
4605 <             ObjectToDouble<? super V> transformer,
4606 <             double basis,
4607 <             DoubleByDoubleToDouble reducer) {
4608 <            if (transformer == null || reducer == null)
4609 <                throw new NullPointerException();
4610 <            return new MapReduceValuesToDoubleTask<K,V>
4611 <                (map, null, -1, null, transformer, basis, reducer);
4612 <        }
4386 >        public boolean remove(Object o) { return map.remove(o) != null; }
4387  
4388          /**
4389 <         * Returns a task that when invoked, returns the result of
4616 <         * accumulating the given transformation of all values using the
4617 <         * given reducer to combine values, and the given basis as an
4618 <         * identity value.
4619 <         *
4620 <         * @param map the map
4621 <         * @param transformer a function returning the transformation
4622 <         * for an element
4623 <         * @param basis the identity (initial default value) for the reduction
4624 <         * @param reducer a commutative associative combining function
4625 <         * @return the task
4389 >         * @return an iterator over the keys of the backing map
4390           */
4391 <        public static <K,V> ForkJoinTask<Long> reduceValuesToLong
4392 <            (ConcurrentHashMap<K,V> map,
4393 <             ObjectToLong<? super V> transformer,
4394 <             long basis,
4395 <             LongByLongToLong reducer) {
4632 <            if (transformer == null || reducer == null)
4633 <                throw new NullPointerException();
4634 <            return new MapReduceValuesToLongTask<K,V>
4635 <                (map, null, -1, null, transformer, basis, reducer);
4391 >        public Iterator<K> iterator() {
4392 >            Node<K,V>[] t;
4393 >            ConcurrentHashMap<K,V> m = map;
4394 >            int f = (t = m.table) == null ? 0 : t.length;
4395 >            return new KeyIterator<K,V>(t, f, 0, f, m);
4396          }
4397  
4398          /**
4399 <         * Returns a task that when invoked, returns the result of
4400 <         * accumulating the given transformation of all values using the
4641 <         * given reducer to combine values, and the given basis as an
4642 <         * identity value.
4399 >         * Adds the specified key to this set view by mapping the key to
4400 >         * the default mapped value in the backing map, if defined.
4401           *
4402 <         * @param map the map
4403 <         * @param transformer a function returning the transformation
4404 <         * for an element
4405 <         * @param basis the identity (initial default value) for the reduction
4406 <         * @param reducer a commutative associative combining function
4649 <         * @return the task
4402 >         * @param e key to be added
4403 >         * @return {@code true} if this set changed as a result of the call
4404 >         * @throws NullPointerException if the specified key is null
4405 >         * @throws UnsupportedOperationException if no default mapped value
4406 >         * for additions was provided
4407           */
4408 <        public static <K,V> ForkJoinTask<Integer> reduceValuesToInt
4409 <            (ConcurrentHashMap<K,V> map,
4410 <             ObjectToInt<? super V> transformer,
4411 <             int basis,
4412 <             IntByIntToInt reducer) {
4656 <            if (transformer == null || reducer == null)
4657 <                throw new NullPointerException();
4658 <            return new MapReduceValuesToIntTask<K,V>
4659 <                (map, null, -1, null, transformer, basis, reducer);
4408 >        public boolean add(K e) {
4409 >            V v;
4410 >            if ((v = value) == null)
4411 >                throw new UnsupportedOperationException();
4412 >            return map.putVal(e, v, true) == null;
4413          }
4414  
4415          /**
4416 <         * Returns a task that when invoked, perform the given action
4417 <         * for each entry.
4416 >         * Adds all of the elements in the specified collection to this set,
4417 >         * as if by calling {@link #add} on each one.
4418           *
4419 <         * @param map the map
4420 <         * @param action the action
4419 >         * @param c the elements to be inserted into this set
4420 >         * @return {@code true} if this set changed as a result of the call
4421 >         * @throws NullPointerException if the collection or any of its
4422 >         * elements are {@code null}
4423 >         * @throws UnsupportedOperationException if no default mapped value
4424 >         * for additions was provided
4425           */
4426 <        public static <K,V> ForkJoinTask<Void> forEachEntry
4427 <            (ConcurrentHashMap<K,V> map,
4428 <             Action<Map.Entry<K,V>> action) {
4429 <            if (action == null) throw new NullPointerException();
4430 <            return new ForEachEntryTask<K,V>(map, null, -1, action);
4426 >        public boolean addAll(Collection<? extends K> c) {
4427 >            boolean added = false;
4428 >            V v;
4429 >            if ((v = value) == null)
4430 >                throw new UnsupportedOperationException();
4431 >            for (K e : c) {
4432 >                if (map.putVal(e, v, true) == null)
4433 >                    added = true;
4434 >            }
4435 >            return added;
4436          }
4437  
4438 <        /**
4439 <         * Returns a task that when invoked, perform the given action
4440 <         * for each non-null transformation of each entry.
4441 <         *
4442 <         * @param map the map
4681 <         * @param transformer a function returning the transformation
4682 <         * for an element, or null if there is no transformation (in
4683 <         * which case the action is not applied)
4684 <         * @param action the action
4685 <         */
4686 <        public static <K,V,U> ForkJoinTask<Void> forEachEntry
4687 <            (ConcurrentHashMap<K,V> map,
4688 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
4689 <             Action<U> action) {
4690 <            if (transformer == null || action == null)
4691 <                throw new NullPointerException();
4692 <            return new ForEachTransformedEntryTask<K,V,U>
4693 <                (map, null, -1, transformer, action);
4438 >        public int hashCode() {
4439 >            int h = 0;
4440 >            for (K e : this)
4441 >                h += e.hashCode();
4442 >            return h;
4443          }
4444  
4445 <        /**
4446 <         * Returns a task that when invoked, returns a non-null result
4447 <         * from applying the given search function on each entry, or
4448 <         * null if none.  Upon success, further element processing is
4449 <         * suppressed and the results of any other parallel
4701 <         * invocations of the search function are ignored.
4702 <         *
4703 <         * @param map the map
4704 <         * @param searchFunction a function returning a non-null
4705 <         * result on success, else null
4706 <         * @return the task
4707 <         *
4708 <         */
4709 <        public static <K,V,U> ForkJoinTask<U> searchEntries
4710 <            (ConcurrentHashMap<K,V> map,
4711 <             Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
4712 <            if (searchFunction == null) throw new NullPointerException();
4713 <            return new SearchEntriesTask<K,V,U>
4714 <                (map, null, -1, searchFunction,
4715 <                 new AtomicReference<U>());
4445 >        public boolean equals(Object o) {
4446 >            Set<?> c;
4447 >            return ((o instanceof Set) &&
4448 >                    ((c = (Set<?>)o) == this ||
4449 >                     (containsAll(c) && c.containsAll(this))));
4450          }
4451  
4452 <        /**
4453 <         * Returns a task that when invoked, returns the result of
4454 <         * accumulating all entries using the given reducer to combine
4455 <         * values, or null if none.
4456 <         *
4457 <         * @param map the map
4724 <         * @param reducer a commutative associative combining function
4725 <         * @return the task
4726 <         */
4727 <        public static <K,V> ForkJoinTask<Map.Entry<K,V>> reduceEntries
4728 <            (ConcurrentHashMap<K,V> map,
4729 <             BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4730 <            if (reducer == null) throw new NullPointerException();
4731 <            return new ReduceEntriesTask<K,V>
4732 <                (map, null, -1, null, reducer);
4452 >        public Spliterator<K> spliterator() {
4453 >            Node<K,V>[] t;
4454 >            ConcurrentHashMap<K,V> m = map;
4455 >            long n = m.sumCount();
4456 >            int f = (t = m.table) == null ? 0 : t.length;
4457 >            return new KeySpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n);
4458          }
4459  
4460 <        /**
4461 <         * Returns a task that when invoked, returns the result of
4462 <         * accumulating the given transformation of all entries using the
4463 <         * given reducer to combine values, or null if none.
4464 <         *
4465 <         * @param map the map
4466 <         * @param transformer a function returning the transformation
4467 <         * for an element, or null if there is no transformation (in
4743 <         * which case it is not combined).
4744 <         * @param reducer a commutative associative combining function
4745 <         * @return the task
4746 <         */
4747 <        public static <K,V,U> ForkJoinTask<U> reduceEntries
4748 <            (ConcurrentHashMap<K,V> map,
4749 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
4750 <             BiFun<? super U, ? super U, ? extends U> reducer) {
4751 <            if (transformer == null || reducer == null)
4752 <                throw new NullPointerException();
4753 <            return new MapReduceEntriesTask<K,V,U>
4754 <                (map, null, -1, null, transformer, reducer);
4460 >        public void forEach(Consumer<? super K> action) {
4461 >            if (action == null) throw new NullPointerException();
4462 >            Node<K,V>[] t;
4463 >            if ((t = map.table) != null) {
4464 >                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
4465 >                for (Node<K,V> p; (p = it.advance()) != null; )
4466 >                    action.accept(p.key);
4467 >            }
4468          }
4469 +    }
4470  
4471 <        /**
4472 <         * Returns a task that when invoked, returns the result of
4473 <         * accumulating the given transformation of all entries using the
4474 <         * given reducer to combine values, and the given basis as an
4475 <         * identity value.
4476 <         *
4477 <         * @param map the map
4478 <         * @param transformer a function returning the transformation
4479 <         * for an element
4480 <         * @param basis the identity (initial default value) for the reduction
4481 <         * @param reducer a commutative associative combining function
4768 <         * @return the task
4769 <         */
4770 <        public static <K,V> ForkJoinTask<Double> reduceEntriesToDouble
4771 <            (ConcurrentHashMap<K,V> map,
4772 <             ObjectToDouble<Map.Entry<K,V>> transformer,
4773 <             double basis,
4774 <             DoubleByDoubleToDouble reducer) {
4775 <            if (transformer == null || reducer == null)
4776 <                throw new NullPointerException();
4777 <            return new MapReduceEntriesToDoubleTask<K,V>
4778 <                (map, null, -1, null, transformer, basis, reducer);
4471 >    /**
4472 >     * A view of a ConcurrentHashMap as a {@link Collection} of
4473 >     * values, in which additions are disabled. This class cannot be
4474 >     * directly instantiated. See {@link #values()}.
4475 >     */
4476 >    static final class ValuesView<K,V> extends CollectionView<K,V,V>
4477 >        implements Collection<V>, java.io.Serializable {
4478 >        private static final long serialVersionUID = 2249069246763182397L;
4479 >        ValuesView(ConcurrentHashMap<K,V> map) { super(map); }
4480 >        public final boolean contains(Object o) {
4481 >            return map.containsValue(o);
4482          }
4483  
4484 <        /**
4485 <         * Returns a task that when invoked, returns the result of
4486 <         * accumulating the given transformation of all entries using the
4487 <         * given reducer to combine values, and the given basis as an
4488 <         * identity value.
4489 <         *
4490 <         * @param map the map
4491 <         * @param transformer a function returning the transformation
4492 <         * for an element
4493 <         * @param basis the identity (initial default value) for the reduction
4791 <         * @param reducer a commutative associative combining function
4792 <         * @return the task
4793 <         */
4794 <        public static <K,V> ForkJoinTask<Long> reduceEntriesToLong
4795 <            (ConcurrentHashMap<K,V> map,
4796 <             ObjectToLong<Map.Entry<K,V>> transformer,
4797 <             long basis,
4798 <             LongByLongToLong reducer) {
4799 <            if (transformer == null || reducer == null)
4800 <                throw new NullPointerException();
4801 <            return new MapReduceEntriesToLongTask<K,V>
4802 <                (map, null, -1, null, transformer, basis, reducer);
4484 >        public final boolean remove(Object o) {
4485 >            if (o != null) {
4486 >                for (Iterator<V> it = iterator(); it.hasNext();) {
4487 >                    if (o.equals(it.next())) {
4488 >                        it.remove();
4489 >                        return true;
4490 >                    }
4491 >                }
4492 >            }
4493 >            return false;
4494          }
4495  
4496 <        /**
4497 <         * Returns a task that when invoked, returns the result of
4498 <         * accumulating the given transformation of all entries using the
4499 <         * given reducer to combine values, and the given basis as an
4500 <         * identity value.
4810 <         *
4811 <         * @param map the map
4812 <         * @param transformer a function returning the transformation
4813 <         * for an element
4814 <         * @param basis the identity (initial default value) for the reduction
4815 <         * @param reducer a commutative associative combining function
4816 <         * @return the task
4817 <         */
4818 <        public static <K,V> ForkJoinTask<Integer> reduceEntriesToInt
4819 <            (ConcurrentHashMap<K,V> map,
4820 <             ObjectToInt<Map.Entry<K,V>> transformer,
4821 <             int basis,
4822 <             IntByIntToInt reducer) {
4823 <            if (transformer == null || reducer == null)
4824 <                throw new NullPointerException();
4825 <            return new MapReduceEntriesToIntTask<K,V>
4826 <                (map, null, -1, null, transformer, basis, reducer);
4496 >        public final Iterator<V> iterator() {
4497 >            ConcurrentHashMap<K,V> m = map;
4498 >            Node<K,V>[] t;
4499 >            int f = (t = m.table) == null ? 0 : t.length;
4500 >            return new ValueIterator<K,V>(t, f, 0, f, m);
4501          }
4828    }
4502  
4503 <    // -------------------------------------------------------
4503 >        public final boolean add(V e) {
4504 >            throw new UnsupportedOperationException();
4505 >        }
4506 >        public final boolean addAll(Collection<? extends V> c) {
4507 >            throw new UnsupportedOperationException();
4508 >        }
4509  
4510 <    /**
4511 <     * Base for FJ tasks for bulk operations. This adds a variant of
4512 <     * CountedCompleters and some split and merge bookkeeping to
4513 <     * iterator functionality. The forEach and reduce methods are
4514 <     * similar to those illustrated in CountedCompleter documentation,
4515 <     * except that bottom-up reduction completions perform them within
4516 <     * their compute methods. The search methods are like forEach
4839 <     * except they continually poll for success and exit early.  Also,
4840 <     * exceptions are handled in a simpler manner, by just trying to
4841 <     * complete root task exceptionally.
4842 <     */
4843 <    @SuppressWarnings("serial") static abstract class BulkTask<K,V,R> extends Traverser<K,V,R> {
4844 <        final BulkTask<K,V,?> parent;  // completion target
4845 <        int batch;                     // split control; -1 for unknown
4846 <        int pending;                   // completion control
4510 >        public Spliterator<V> spliterator() {
4511 >            Node<K,V>[] t;
4512 >            ConcurrentHashMap<K,V> m = map;
4513 >            long n = m.sumCount();
4514 >            int f = (t = m.table) == null ? 0 : t.length;
4515 >            return new ValueSpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n);
4516 >        }
4517  
4518 <        BulkTask(ConcurrentHashMap<K,V> map, BulkTask<K,V,?> parent,
4519 <                 int batch) {
4520 <            super(map);
4521 <            this.parent = parent;
4522 <            this.batch = batch;
4523 <            if (parent != null && map != null) { // split parent
4524 <                Node[] t;
4855 <                if ((t = parent.tab) == null &&
4856 <                    (t = parent.tab = map.table) != null)
4857 <                    parent.baseLimit = parent.baseSize = t.length;
4858 <                this.tab = t;
4859 <                this.baseSize = parent.baseSize;
4860 <                int hi = this.baseLimit = parent.baseLimit;
4861 <                parent.baseLimit = this.index = this.baseIndex =
4862 <                    (hi + parent.baseIndex + 1) >>> 1;
4518 >        public void forEach(Consumer<? super V> action) {
4519 >            if (action == null) throw new NullPointerException();
4520 >            Node<K,V>[] t;
4521 >            if ((t = map.table) != null) {
4522 >                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
4523 >                for (Node<K,V> p; (p = it.advance()) != null; )
4524 >                    action.accept(p.val);
4525              }
4526          }
4527 +    }
4528  
4529 <        // FJ methods
4529 >    /**
4530 >     * A view of a ConcurrentHashMap as a {@link Set} of (key, value)
4531 >     * entries.  This class cannot be directly instantiated. See
4532 >     * {@link #entrySet()}.
4533 >     */
4534 >    static final class EntrySetView<K,V> extends CollectionView<K,V,Map.Entry<K,V>>
4535 >        implements Set<Map.Entry<K,V>>, java.io.Serializable {
4536 >        private static final long serialVersionUID = 2249069246763182397L;
4537 >        EntrySetView(ConcurrentHashMap<K,V> map) { super(map); }
4538 >
4539 >        public boolean contains(Object o) {
4540 >            Object k, v, r; Map.Entry<?,?> e;
4541 >            return ((o instanceof Map.Entry) &&
4542 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
4543 >                    (r = map.get(k)) != null &&
4544 >                    (v = e.getValue()) != null &&
4545 >                    (v == r || v.equals(r)));
4546 >        }
4547 >
4548 >        public boolean remove(Object o) {
4549 >            Object k, v; Map.Entry<?,?> e;
4550 >            return ((o instanceof Map.Entry) &&
4551 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
4552 >                    (v = e.getValue()) != null &&
4553 >                    map.remove(k, v));
4554 >        }
4555  
4556          /**
4557 <         * Propagates completion. Note that all reduce actions
4870 <         * bypass this method to combine while completing.
4557 >         * @return an iterator over the entries of the backing map
4558           */
4559 <        final void tryComplete() {
4560 <            BulkTask<K,V,?> a = this, s = a;
4561 <            for (int c;;) {
4562 <                if ((c = a.pending) == 0) {
4563 <                    if ((a = (s = a).parent) == null) {
4564 <                        s.quietlyComplete();
4565 <                        break;
4566 <                    }
4567 <                }
4568 <                else if (U.compareAndSwapInt(a, PENDING, c, c - 1))
4569 <                    break;
4559 >        public Iterator<Map.Entry<K,V>> iterator() {
4560 >            ConcurrentHashMap<K,V> m = map;
4561 >            Node<K,V>[] t;
4562 >            int f = (t = m.table) == null ? 0 : t.length;
4563 >            return new EntryIterator<K,V>(t, f, 0, f, m);
4564 >        }
4565 >
4566 >        public boolean add(Entry<K,V> e) {
4567 >            return map.putVal(e.getKey(), e.getValue(), false) == null;
4568 >        }
4569 >
4570 >        public boolean addAll(Collection<? extends Entry<K,V>> c) {
4571 >            boolean added = false;
4572 >            for (Entry<K,V> e : c) {
4573 >                if (add(e))
4574 >                    added = true;
4575              }
4576 +            return added;
4577          }
4578  
4579 <        /**
4580 <         * Forces root task to complete.
4581 <         * @param ex if null, complete normally, else exceptionally
4582 <         * @return false to simplify use
4583 <         */
4584 <        final boolean tryCompleteComputation(Throwable ex) {
4585 <            for (BulkTask<K,V,?> a = this;;) {
4893 <                BulkTask<K,V,?> p = a.parent;
4894 <                if (p == null) {
4895 <                    if (ex != null)
4896 <                        a.completeExceptionally(ex);
4897 <                    else
4898 <                        a.quietlyComplete();
4899 <                    return false;
4579 >        public final int hashCode() {
4580 >            int h = 0;
4581 >            Node<K,V>[] t;
4582 >            if ((t = map.table) != null) {
4583 >                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
4584 >                for (Node<K,V> p; (p = it.advance()) != null; ) {
4585 >                    h += p.hashCode();
4586                  }
4901                a = p;
4587              }
4588 +            return h;
4589          }
4590  
4591 <        /**
4592 <         * Version of tryCompleteComputation for function screening checks
4593 <         */
4594 <        final boolean abortOnNullFunction() {
4595 <            return tryCompleteComputation(new Error("Unexpected null function"));
4591 >        public final boolean equals(Object o) {
4592 >            Set<?> c;
4593 >            return ((o instanceof Set) &&
4594 >                    ((c = (Set<?>)o) == this ||
4595 >                     (containsAll(c) && c.containsAll(this))));
4596          }
4597  
4598 <        // utilities
4598 >        public Spliterator<Map.Entry<K,V>> spliterator() {
4599 >            Node<K,V>[] t;
4600 >            ConcurrentHashMap<K,V> m = map;
4601 >            long n = m.sumCount();
4602 >            int f = (t = m.table) == null ? 0 : t.length;
4603 >            return new EntrySpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n, m);
4604 >        }
4605  
4606 <        /** CompareAndSet pending count */
4607 <        final boolean casPending(int cmp, int val) {
4608 <            return U.compareAndSwapInt(this, PENDING, cmp, val);
4606 >        public void forEach(Consumer<? super Map.Entry<K,V>> action) {
4607 >            if (action == null) throw new NullPointerException();
4608 >            Node<K,V>[] t;
4609 >            if ((t = map.table) != null) {
4610 >                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
4611 >                for (Node<K,V> p; (p = it.advance()) != null; )
4612 >                    action.accept(new MapEntry<K,V>(p.key, p.val, map));
4613 >            }
4614          }
4615  
4616 <        /**
4617 <         * Returns approx exp2 of the number of times (minus one) to
4618 <         * split task by two before executing leaf action. This value
4619 <         * is faster to compute and more convenient to use as a guide
4620 <         * to splitting than is the depth, since it is used while
4621 <         * dividing by two anyway.
4622 <         */
4623 <        final int batch() {
4624 <            ConcurrentHashMap<K, V> m; int b; Node[] t;
4625 <            if ((b = batch) < 0 && (m = map) != null) { // force initialization
4626 <                if ((t = tab) == null && (t = tab = m.table) != null)
4627 <                    baseLimit = baseSize = t.length;
4628 <                if (t != null) {
4629 <                    long n = m.counter.sum();
4630 <                    int sp = getPool().getParallelism() << 3; // slack of 8
4631 <                    b = batch = (n <= 0L) ? 0 : (n < (long)sp) ? (int)n : sp;
4632 <                }
4616 >    }
4617 >
4618 >    // -------------------------------------------------------
4619 >
4620 >    /**
4621 >     * Base class for bulk tasks. Repeats some fields and code from
4622 >     * class Traverser, because we need to subclass CountedCompleter.
4623 >     */
4624 >    abstract static class BulkTask<K,V,R> extends CountedCompleter<R> {
4625 >        Node<K,V>[] tab;        // same as Traverser
4626 >        Node<K,V> next;
4627 >        int index;
4628 >        int baseIndex;
4629 >        int baseLimit;
4630 >        final int baseSize;
4631 >        int batch;              // split control
4632 >
4633 >        BulkTask(BulkTask<K,V,?> par, int b, int i, int f, Node<K,V>[] t) {
4634 >            super(par);
4635 >            this.batch = b;
4636 >            this.index = this.baseIndex = i;
4637 >            if ((this.tab = t) == null)
4638 >                this.baseSize = this.baseLimit = 0;
4639 >            else if (par == null)
4640 >                this.baseSize = this.baseLimit = t.length;
4641 >            else {
4642 >                this.baseLimit = f;
4643 >                this.baseSize = par.baseSize;
4644              }
4937            return b;
4645          }
4646  
4647          /**
4648 <         * Returns exportable snapshot entry.
4648 >         * Same as Traverser version
4649           */
4650 <        static <K,V> AbstractMap.SimpleEntry<K,V> entryFor(K k, V v) {
4651 <            return new AbstractMap.SimpleEntry<K,V>(k, v);
4652 <        }
4653 <
4654 <        // Unsafe mechanics
4655 <        private static final sun.misc.Unsafe U;
4656 <        private static final long PENDING;
4657 <        static {
4658 <            try {
4659 <                U = sun.misc.Unsafe.getUnsafe();
4660 <                PENDING = U.objectFieldOffset
4661 <                    (BulkTask.class.getDeclaredField("pending"));
4662 <            } catch (Exception e) {
4663 <                throw new Error(e);
4650 >        final Node<K,V> advance() {
4651 >            Node<K,V> e;
4652 >            if ((e = next) != null)
4653 >                e = e.next;
4654 >            for (;;) {
4655 >                Node<K,V>[] t; int i, n; K ek;  // must use locals in checks
4656 >                if (e != null)
4657 >                    return next = e;
4658 >                if (baseIndex >= baseLimit || (t = tab) == null ||
4659 >                    (n = t.length) <= (i = index) || i < 0)
4660 >                    return next = null;
4661 >                if ((e = tabAt(t, index)) != null && e.hash < 0) {
4662 >                    if (e instanceof ForwardingNode) {
4663 >                        tab = ((ForwardingNode<K,V>)e).nextTable;
4664 >                        e = null;
4665 >                        continue;
4666 >                    }
4667 >                    else if (e instanceof TreeBin)
4668 >                        e = ((TreeBin<K,V>)e).first;
4669 >                    else
4670 >                        e = null;
4671 >                }
4672 >                if ((index += baseSize) >= n)
4673 >                    index = ++baseIndex;    // visit upper slots if present
4674              }
4675          }
4676      }
# Line 4961 | Line 4678 | public class ConcurrentHashMap<K, V>
4678      /*
4679       * Task classes. Coded in a regular but ugly format/style to
4680       * simplify checks that each variant differs in the right way from
4681 <     * others.
4681 >     * others. The null screenings exist because compilers cannot tell
4682 >     * that we've already null-checked task arguments, so we force
4683 >     * simplest hoisted bypass to help avoid convoluted traps.
4684       */
4685 <
4686 <    @SuppressWarnings("serial") static final class ForEachKeyTask<K,V>
4685 >    @SuppressWarnings("serial")
4686 >    static final class ForEachKeyTask<K,V>
4687          extends BulkTask<K,V,Void> {
4688 <        final Action<K> action;
4688 >        final Consumer<? super K> action;
4689          ForEachKeyTask
4690 <            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
4691 <             Action<K> action) {
4692 <            super(m, p, b);
4690 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4691 >             Consumer<? super K> action) {
4692 >            super(p, b, i, f, t);
4693              this.action = action;
4694          }
4695 <        @SuppressWarnings("unchecked") public final boolean exec() {
4696 <            final Action<K> action = this.action;
4697 <            if (action == null)
4698 <                return abortOnNullFunction();
4699 <            try {
4700 <                int b = batch(), c;
4701 <                while (b > 1 && baseIndex != baseLimit) {
4702 <                    do {} while (!casPending(c = pending, c+1));
4703 <                    new ForEachKeyTask<K,V>(map, this, b >>>= 1, action).fork();
4704 <                }
4705 <                while (advance() != null)
4706 <                    action.apply((K)nextKey);
4707 <                tryComplete();
4989 <            } catch (Throwable ex) {
4990 <                return tryCompleteComputation(ex);
4695 >        public final void compute() {
4696 >            final Consumer<? super K> action;
4697 >            if ((action = this.action) != null) {
4698 >                for (int i = baseIndex, f, h; batch > 0 &&
4699 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4700 >                    addToPendingCount(1);
4701 >                    new ForEachKeyTask<K,V>
4702 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4703 >                         action).fork();
4704 >                }
4705 >                for (Node<K,V> p; (p = advance()) != null;)
4706 >                    action.accept(p.key);
4707 >                propagateCompletion();
4708              }
4992            return false;
4709          }
4710      }
4711  
4712 <    @SuppressWarnings("serial") static final class ForEachValueTask<K,V>
4712 >    @SuppressWarnings("serial")
4713 >    static final class ForEachValueTask<K,V>
4714          extends BulkTask<K,V,Void> {
4715 <        final Action<V> action;
4715 >        final Consumer<? super V> action;
4716          ForEachValueTask
4717 <            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
4718 <             Action<V> action) {
4719 <            super(m, p, b);
4717 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4718 >             Consumer<? super V> action) {
4719 >            super(p, b, i, f, t);
4720              this.action = action;
4721          }
4722 <        @SuppressWarnings("unchecked") public final boolean exec() {
4723 <            final Action<V> action = this.action;
4724 <            if (action == null)
4725 <                return abortOnNullFunction();
4726 <            try {
4727 <                int b = batch(), c;
4728 <                while (b > 1 && baseIndex != baseLimit) {
4729 <                    do {} while (!casPending(c = pending, c+1));
4730 <                    new ForEachValueTask<K,V>(map, this, b >>>= 1, action).fork();
4731 <                }
4732 <                Object v;
4733 <                while ((v = advance()) != null)
4734 <                    action.apply((V)v);
5018 <                tryComplete();
5019 <            } catch (Throwable ex) {
5020 <                return tryCompleteComputation(ex);
4722 >        public final void compute() {
4723 >            final Consumer<? super V> action;
4724 >            if ((action = this.action) != null) {
4725 >                for (int i = baseIndex, f, h; batch > 0 &&
4726 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4727 >                    addToPendingCount(1);
4728 >                    new ForEachValueTask<K,V>
4729 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4730 >                         action).fork();
4731 >                }
4732 >                for (Node<K,V> p; (p = advance()) != null;)
4733 >                    action.accept(p.val);
4734 >                propagateCompletion();
4735              }
5022            return false;
4736          }
4737      }
4738  
4739 <    @SuppressWarnings("serial") static final class ForEachEntryTask<K,V>
4739 >    @SuppressWarnings("serial")
4740 >    static final class ForEachEntryTask<K,V>
4741          extends BulkTask<K,V,Void> {
4742 <        final Action<Entry<K,V>> action;
4742 >        final Consumer<? super Entry<K,V>> action;
4743          ForEachEntryTask
4744 <            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
4745 <             Action<Entry<K,V>> action) {
4746 <            super(m, p, b);
4744 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4745 >             Consumer<? super Entry<K,V>> action) {
4746 >            super(p, b, i, f, t);
4747              this.action = action;
4748          }
4749 <        @SuppressWarnings("unchecked") public final boolean exec() {
4750 <            final Action<Entry<K,V>> action = this.action;
4751 <            if (action == null)
4752 <                return abortOnNullFunction();
4753 <            try {
4754 <                int b = batch(), c;
4755 <                while (b > 1 && baseIndex != baseLimit) {
4756 <                    do {} while (!casPending(c = pending, c+1));
4757 <                    new ForEachEntryTask<K,V>(map, this, b >>>= 1, action).fork();
4758 <                }
4759 <                Object v;
4760 <                while ((v = advance()) != null)
4761 <                    action.apply(entryFor((K)nextKey, (V)v));
5048 <                tryComplete();
5049 <            } catch (Throwable ex) {
5050 <                return tryCompleteComputation(ex);
4749 >        public final void compute() {
4750 >            final Consumer<? super Entry<K,V>> action;
4751 >            if ((action = this.action) != null) {
4752 >                for (int i = baseIndex, f, h; batch > 0 &&
4753 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4754 >                    addToPendingCount(1);
4755 >                    new ForEachEntryTask<K,V>
4756 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4757 >                         action).fork();
4758 >                }
4759 >                for (Node<K,V> p; (p = advance()) != null; )
4760 >                    action.accept(p);
4761 >                propagateCompletion();
4762              }
5052            return false;
4763          }
4764      }
4765  
4766 <    @SuppressWarnings("serial") static final class ForEachMappingTask<K,V>
4766 >    @SuppressWarnings("serial")
4767 >    static final class ForEachMappingTask<K,V>
4768          extends BulkTask<K,V,Void> {
4769 <        final BiAction<K,V> action;
4769 >        final BiConsumer<? super K, ? super V> action;
4770          ForEachMappingTask
4771 <            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
4772 <             BiAction<K,V> action) {
4773 <            super(m, p, b);
4771 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4772 >             BiConsumer<? super K,? super V> action) {
4773 >            super(p, b, i, f, t);
4774              this.action = action;
4775          }
4776 <        @SuppressWarnings("unchecked") public final boolean exec() {
4777 <            final BiAction<K,V> action = this.action;
4778 <            if (action == null)
4779 <                return abortOnNullFunction();
4780 <            try {
4781 <                int b = batch(), c;
4782 <                while (b > 1 && baseIndex != baseLimit) {
4783 <                    do {} while (!casPending(c = pending, c+1));
4784 <                    new ForEachMappingTask<K,V>(map, this, b >>>= 1,
4785 <                                                action).fork();
4786 <                }
4787 <                Object v;
4788 <                while ((v = advance()) != null)
5078 <                    action.apply((K)nextKey, (V)v);
5079 <                tryComplete();
5080 <            } catch (Throwable ex) {
5081 <                return tryCompleteComputation(ex);
4776 >        public final void compute() {
4777 >            final BiConsumer<? super K, ? super V> action;
4778 >            if ((action = this.action) != null) {
4779 >                for (int i = baseIndex, f, h; batch > 0 &&
4780 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4781 >                    addToPendingCount(1);
4782 >                    new ForEachMappingTask<K,V>
4783 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4784 >                         action).fork();
4785 >                }
4786 >                for (Node<K,V> p; (p = advance()) != null; )
4787 >                    action.accept(p.key, p.val);
4788 >                propagateCompletion();
4789              }
5083            return false;
4790          }
4791      }
4792  
4793 <    @SuppressWarnings("serial") static final class ForEachTransformedKeyTask<K,V,U>
4793 >    @SuppressWarnings("serial")
4794 >    static final class ForEachTransformedKeyTask<K,V,U>
4795          extends BulkTask<K,V,Void> {
4796 <        final Fun<? super K, ? extends U> transformer;
4797 <        final Action<U> action;
4796 >        final Function<? super K, ? extends U> transformer;
4797 >        final Consumer<? super U> action;
4798          ForEachTransformedKeyTask
4799 <            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
4800 <             Fun<? super K, ? extends U> transformer,
4801 <             Action<U> action) {
4802 <            super(m, p, b);
4803 <            this.transformer = transformer;
4804 <            this.action = action;
4805 <
4806 <        }
4807 <        @SuppressWarnings("unchecked") public final boolean exec() {
4808 <            final Fun<? super K, ? extends U> transformer =
4809 <                this.transformer;
4810 <            final Action<U> action = this.action;
4811 <            if (transformer == null || action == null)
5105 <                return abortOnNullFunction();
5106 <            try {
5107 <                int b = batch(), c;
5108 <                while (b > 1 && baseIndex != baseLimit) {
5109 <                    do {} while (!casPending(c = pending, c+1));
4799 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4800 >             Function<? super K, ? extends U> transformer, Consumer<? super U> action) {
4801 >            super(p, b, i, f, t);
4802 >            this.transformer = transformer; this.action = action;
4803 >        }
4804 >        public final void compute() {
4805 >            final Function<? super K, ? extends U> transformer;
4806 >            final Consumer<? super U> action;
4807 >            if ((transformer = this.transformer) != null &&
4808 >                (action = this.action) != null) {
4809 >                for (int i = baseIndex, f, h; batch > 0 &&
4810 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4811 >                    addToPendingCount(1);
4812                      new ForEachTransformedKeyTask<K,V,U>
4813 <                        (map, this, b >>>= 1, transformer, action).fork();
4813 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4814 >                         transformer, action).fork();
4815                  }
4816 <                U u;
4817 <                while (advance() != null) {
4818 <                    if ((u = transformer.apply((K)nextKey)) != null)
4819 <                        action.apply(u);
4820 <                }
4821 <                tryComplete();
5119 <            } catch (Throwable ex) {
5120 <                return tryCompleteComputation(ex);
4816 >                for (Node<K,V> p; (p = advance()) != null; ) {
4817 >                    U u;
4818 >                    if ((u = transformer.apply(p.key)) != null)
4819 >                        action.accept(u);
4820 >                }
4821 >                propagateCompletion();
4822              }
5122            return false;
4823          }
4824      }
4825  
4826 <    @SuppressWarnings("serial") static final class ForEachTransformedValueTask<K,V,U>
4826 >    @SuppressWarnings("serial")
4827 >    static final class ForEachTransformedValueTask<K,V,U>
4828          extends BulkTask<K,V,Void> {
4829 <        final Fun<? super V, ? extends U> transformer;
4830 <        final Action<U> action;
4829 >        final Function<? super V, ? extends U> transformer;
4830 >        final Consumer<? super U> action;
4831          ForEachTransformedValueTask
4832 <            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
4833 <             Fun<? super V, ? extends U> transformer,
4834 <             Action<U> action) {
4835 <            super(m, p, b);
4836 <            this.transformer = transformer;
4837 <            this.action = action;
4838 <
4839 <        }
4840 <        @SuppressWarnings("unchecked") public final boolean exec() {
4841 <            final Fun<? super V, ? extends U> transformer =
4842 <                this.transformer;
4843 <            final Action<U> action = this.action;
4844 <            if (transformer == null || action == null)
5144 <                return abortOnNullFunction();
5145 <            try {
5146 <                int b = batch(), c;
5147 <                while (b > 1 && baseIndex != baseLimit) {
5148 <                    do {} while (!casPending(c = pending, c+1));
4832 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4833 >             Function<? super V, ? extends U> transformer, Consumer<? super U> action) {
4834 >            super(p, b, i, f, t);
4835 >            this.transformer = transformer; this.action = action;
4836 >        }
4837 >        public final void compute() {
4838 >            final Function<? super V, ? extends U> transformer;
4839 >            final Consumer<? super U> action;
4840 >            if ((transformer = this.transformer) != null &&
4841 >                (action = this.action) != null) {
4842 >                for (int i = baseIndex, f, h; batch > 0 &&
4843 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4844 >                    addToPendingCount(1);
4845                      new ForEachTransformedValueTask<K,V,U>
4846 <                        (map, this, b >>>= 1, transformer, action).fork();
4846 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4847 >                         transformer, action).fork();
4848                  }
4849 <                Object v; U u;
4850 <                while ((v = advance()) != null) {
4851 <                    if ((u = transformer.apply((V)v)) != null)
4852 <                        action.apply(u);
4853 <                }
4854 <                tryComplete();
5158 <            } catch (Throwable ex) {
5159 <                return tryCompleteComputation(ex);
4849 >                for (Node<K,V> p; (p = advance()) != null; ) {
4850 >                    U u;
4851 >                    if ((u = transformer.apply(p.val)) != null)
4852 >                        action.accept(u);
4853 >                }
4854 >                propagateCompletion();
4855              }
5161            return false;
4856          }
4857      }
4858  
4859 <    @SuppressWarnings("serial") static final class ForEachTransformedEntryTask<K,V,U>
4859 >    @SuppressWarnings("serial")
4860 >    static final class ForEachTransformedEntryTask<K,V,U>
4861          extends BulkTask<K,V,Void> {
4862 <        final Fun<Map.Entry<K,V>, ? extends U> transformer;
4863 <        final Action<U> action;
4862 >        final Function<Map.Entry<K,V>, ? extends U> transformer;
4863 >        final Consumer<? super U> action;
4864          ForEachTransformedEntryTask
4865 <            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
4866 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
4867 <             Action<U> action) {
4868 <            super(m, p, b);
4869 <            this.transformer = transformer;
4870 <            this.action = action;
4871 <
4872 <        }
4873 <        @SuppressWarnings("unchecked") public final boolean exec() {
4874 <            final Fun<Map.Entry<K,V>, ? extends U> transformer =
4875 <                this.transformer;
4876 <            final Action<U> action = this.action;
4877 <            if (transformer == null || action == null)
5183 <                return abortOnNullFunction();
5184 <            try {
5185 <                int b = batch(), c;
5186 <                while (b > 1 && baseIndex != baseLimit) {
5187 <                    do {} while (!casPending(c = pending, c+1));
4865 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4866 >             Function<Map.Entry<K,V>, ? extends U> transformer, Consumer<? super U> action) {
4867 >            super(p, b, i, f, t);
4868 >            this.transformer = transformer; this.action = action;
4869 >        }
4870 >        public final void compute() {
4871 >            final Function<Map.Entry<K,V>, ? extends U> transformer;
4872 >            final Consumer<? super U> action;
4873 >            if ((transformer = this.transformer) != null &&
4874 >                (action = this.action) != null) {
4875 >                for (int i = baseIndex, f, h; batch > 0 &&
4876 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4877 >                    addToPendingCount(1);
4878                      new ForEachTransformedEntryTask<K,V,U>
4879 <                        (map, this, b >>>= 1, transformer, action).fork();
4879 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4880 >                         transformer, action).fork();
4881 >                }
4882 >                for (Node<K,V> p; (p = advance()) != null; ) {
4883 >                    U u;
4884 >                    if ((u = transformer.apply(p)) != null)
4885 >                        action.accept(u);
4886                  }
4887 <                Object v; U u;
5192 <                while ((v = advance()) != null) {
5193 <                    if ((u = transformer.apply(entryFor((K)nextKey, (V)v))) != null)
5194 <                        action.apply(u);
5195 <                }
5196 <                tryComplete();
5197 <            } catch (Throwable ex) {
5198 <                return tryCompleteComputation(ex);
4887 >                propagateCompletion();
4888              }
5200            return false;
4889          }
4890      }
4891  
4892 <    @SuppressWarnings("serial") static final class ForEachTransformedMappingTask<K,V,U>
4892 >    @SuppressWarnings("serial")
4893 >    static final class ForEachTransformedMappingTask<K,V,U>
4894          extends BulkTask<K,V,Void> {
4895 <        final BiFun<? super K, ? super V, ? extends U> transformer;
4896 <        final Action<U> action;
4895 >        final BiFunction<? super K, ? super V, ? extends U> transformer;
4896 >        final Consumer<? super U> action;
4897          ForEachTransformedMappingTask
4898 <            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
4899 <             BiFun<? super K, ? super V, ? extends U> transformer,
4900 <             Action<U> action) {
4901 <            super(m, p, b);
4902 <            this.transformer = transformer;
4903 <            this.action = action;
4904 <
4905 <        }
4906 <        @SuppressWarnings("unchecked") public final boolean exec() {
4907 <            final BiFun<? super K, ? super V, ? extends U> transformer =
4908 <                this.transformer;
4909 <            final Action<U> action = this.action;
4910 <            if (transformer == null || action == null)
4911 <                return abortOnNullFunction();
5223 <            try {
5224 <                int b = batch(), c;
5225 <                while (b > 1 && baseIndex != baseLimit) {
5226 <                    do {} while (!casPending(c = pending, c+1));
4898 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4899 >             BiFunction<? super K, ? super V, ? extends U> transformer,
4900 >             Consumer<? super U> action) {
4901 >            super(p, b, i, f, t);
4902 >            this.transformer = transformer; this.action = action;
4903 >        }
4904 >        public final void compute() {
4905 >            final BiFunction<? super K, ? super V, ? extends U> transformer;
4906 >            final Consumer<? super U> action;
4907 >            if ((transformer = this.transformer) != null &&
4908 >                (action = this.action) != null) {
4909 >                for (int i = baseIndex, f, h; batch > 0 &&
4910 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4911 >                    addToPendingCount(1);
4912                      new ForEachTransformedMappingTask<K,V,U>
4913 <                        (map, this, b >>>= 1, transformer, action).fork();
4913 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4914 >                         transformer, action).fork();
4915                  }
4916 <                Object v; U u;
4917 <                while ((v = advance()) != null) {
4918 <                    if ((u = transformer.apply((K)nextKey, (V)v)) != null)
4919 <                        action.apply(u);
4920 <                }
4921 <                tryComplete();
5236 <            } catch (Throwable ex) {
5237 <                return tryCompleteComputation(ex);
4916 >                for (Node<K,V> p; (p = advance()) != null; ) {
4917 >                    U u;
4918 >                    if ((u = transformer.apply(p.key, p.val)) != null)
4919 >                        action.accept(u);
4920 >                }
4921 >                propagateCompletion();
4922              }
5239            return false;
4923          }
4924      }
4925  
4926 <    @SuppressWarnings("serial") static final class SearchKeysTask<K,V,U>
4926 >    @SuppressWarnings("serial")
4927 >    static final class SearchKeysTask<K,V,U>
4928          extends BulkTask<K,V,U> {
4929 <        final Fun<? super K, ? extends U> searchFunction;
4929 >        final Function<? super K, ? extends U> searchFunction;
4930          final AtomicReference<U> result;
4931          SearchKeysTask
4932 <            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
4933 <             Fun<? super K, ? extends U> searchFunction,
4932 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4933 >             Function<? super K, ? extends U> searchFunction,
4934               AtomicReference<U> result) {
4935 <            super(m, p, b);
4935 >            super(p, b, i, f, t);
4936              this.searchFunction = searchFunction; this.result = result;
4937          }
4938 <        @SuppressWarnings("unchecked") public final boolean exec() {
4939 <            AtomicReference<U> result = this.result;
4940 <            final Fun<? super K, ? extends U> searchFunction =
4941 <                this.searchFunction;
4942 <            if (searchFunction == null || result == null)
4943 <                return abortOnNullFunction();
4944 <            try {
4945 <                int b = batch(), c;
4946 <                while (b > 1 && baseIndex != baseLimit && result.get() == null) {
4947 <                    do {} while (!casPending(c = pending, c+1));
4948 <                    new SearchKeysTask<K,V,U>(map, this, b >>>= 1,
4949 <                                              searchFunction, result).fork();
4950 <                }
4951 <                U u;
4952 <                while (result.get() == null && advance() != null) {
4953 <                    if ((u = searchFunction.apply((K)nextKey)) != null) {
4938 >        public final U getRawResult() { return result.get(); }
4939 >        public final void compute() {
4940 >            final Function<? super K, ? extends U> searchFunction;
4941 >            final AtomicReference<U> result;
4942 >            if ((searchFunction = this.searchFunction) != null &&
4943 >                (result = this.result) != null) {
4944 >                for (int i = baseIndex, f, h; batch > 0 &&
4945 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4946 >                    if (result.get() != null)
4947 >                        return;
4948 >                    addToPendingCount(1);
4949 >                    new SearchKeysTask<K,V,U>
4950 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4951 >                         searchFunction, result).fork();
4952 >                }
4953 >                while (result.get() == null) {
4954 >                    U u;
4955 >                    Node<K,V> p;
4956 >                    if ((p = advance()) == null) {
4957 >                        propagateCompletion();
4958 >                        break;
4959 >                    }
4960 >                    if ((u = searchFunction.apply(p.key)) != null) {
4961                          if (result.compareAndSet(null, u))
4962 <                            tryCompleteComputation(null);
4962 >                            quietlyCompleteRoot();
4963                          break;
4964                      }
4965                  }
5275                tryComplete();
5276            } catch (Throwable ex) {
5277                return tryCompleteComputation(ex);
4966              }
5279            return false;
4967          }
5281        public final U getRawResult() { return result.get(); }
4968      }
4969  
4970 <    @SuppressWarnings("serial") static final class SearchValuesTask<K,V,U>
4970 >    @SuppressWarnings("serial")
4971 >    static final class SearchValuesTask<K,V,U>
4972          extends BulkTask<K,V,U> {
4973 <        final Fun<? super V, ? extends U> searchFunction;
4973 >        final Function<? super V, ? extends U> searchFunction;
4974          final AtomicReference<U> result;
4975          SearchValuesTask
4976 <            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
4977 <             Fun<? super V, ? extends U> searchFunction,
4976 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4977 >             Function<? super V, ? extends U> searchFunction,
4978               AtomicReference<U> result) {
4979 <            super(m, p, b);
4979 >            super(p, b, i, f, t);
4980              this.searchFunction = searchFunction; this.result = result;
4981          }
4982 <        @SuppressWarnings("unchecked") public final boolean exec() {
4983 <            AtomicReference<U> result = this.result;
4984 <            final Fun<? super V, ? extends U> searchFunction =
4985 <                this.searchFunction;
4986 <            if (searchFunction == null || result == null)
4987 <                return abortOnNullFunction();
4988 <            try {
4989 <                int b = batch(), c;
4990 <                while (b > 1 && baseIndex != baseLimit && result.get() == null) {
4991 <                    do {} while (!casPending(c = pending, c+1));
4992 <                    new SearchValuesTask<K,V,U>(map, this, b >>>= 1,
4993 <                                                searchFunction, result).fork();
4994 <                }
4995 <                Object v; U u;
4996 <                while (result.get() == null && (v = advance()) != null) {
4997 <                    if ((u = searchFunction.apply((V)v)) != null) {
4982 >        public final U getRawResult() { return result.get(); }
4983 >        public final void compute() {
4984 >            final Function<? super V, ? extends U> searchFunction;
4985 >            final AtomicReference<U> result;
4986 >            if ((searchFunction = this.searchFunction) != null &&
4987 >                (result = this.result) != null) {
4988 >                for (int i = baseIndex, f, h; batch > 0 &&
4989 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4990 >                    if (result.get() != null)
4991 >                        return;
4992 >                    addToPendingCount(1);
4993 >                    new SearchValuesTask<K,V,U>
4994 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4995 >                         searchFunction, result).fork();
4996 >                }
4997 >                while (result.get() == null) {
4998 >                    U u;
4999 >                    Node<K,V> p;
5000 >                    if ((p = advance()) == null) {
5001 >                        propagateCompletion();
5002 >                        break;
5003 >                    }
5004 >                    if ((u = searchFunction.apply(p.val)) != null) {
5005                          if (result.compareAndSet(null, u))
5006 <                            tryCompleteComputation(null);
5006 >                            quietlyCompleteRoot();
5007                          break;
5008                      }
5009                  }
5316                tryComplete();
5317            } catch (Throwable ex) {
5318                return tryCompleteComputation(ex);
5010              }
5320            return false;
5011          }
5322        public final U getRawResult() { return result.get(); }
5012      }
5013  
5014 <    @SuppressWarnings("serial") static final class SearchEntriesTask<K,V,U>
5014 >    @SuppressWarnings("serial")
5015 >    static final class SearchEntriesTask<K,V,U>
5016          extends BulkTask<K,V,U> {
5017 <        final Fun<Entry<K,V>, ? extends U> searchFunction;
5017 >        final Function<Entry<K,V>, ? extends U> searchFunction;
5018          final AtomicReference<U> result;
5019          SearchEntriesTask
5020 <            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5021 <             Fun<Entry<K,V>, ? extends U> searchFunction,
5020 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5021 >             Function<Entry<K,V>, ? extends U> searchFunction,
5022               AtomicReference<U> result) {
5023 <            super(m, p, b);
5023 >            super(p, b, i, f, t);
5024              this.searchFunction = searchFunction; this.result = result;
5025          }
5026 <        @SuppressWarnings("unchecked") public final boolean exec() {
5027 <            AtomicReference<U> result = this.result;
5028 <            final Fun<Entry<K,V>, ? extends U> searchFunction =
5029 <                this.searchFunction;
5030 <            if (searchFunction == null || result == null)
5031 <                return abortOnNullFunction();
5032 <            try {
5033 <                int b = batch(), c;
5034 <                while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5035 <                    do {} while (!casPending(c = pending, c+1));
5036 <                    new SearchEntriesTask<K,V,U>(map, this, b >>>= 1,
5037 <                                                 searchFunction, result).fork();
5038 <                }
5039 <                Object v; U u;
5040 <                while (result.get() == null && (v = advance()) != null) {
5041 <                    if ((u = searchFunction.apply(entryFor((K)nextKey, (V)v))) != null) {
5042 <                        if (result.compareAndSet(null, u))
5043 <                            tryCompleteComputation(null);
5026 >        public final U getRawResult() { return result.get(); }
5027 >        public final void compute() {
5028 >            final Function<Entry<K,V>, ? extends U> searchFunction;
5029 >            final AtomicReference<U> result;
5030 >            if ((searchFunction = this.searchFunction) != null &&
5031 >                (result = this.result) != null) {
5032 >                for (int i = baseIndex, f, h; batch > 0 &&
5033 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5034 >                    if (result.get() != null)
5035 >                        return;
5036 >                    addToPendingCount(1);
5037 >                    new SearchEntriesTask<K,V,U>
5038 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
5039 >                         searchFunction, result).fork();
5040 >                }
5041 >                while (result.get() == null) {
5042 >                    U u;
5043 >                    Node<K,V> p;
5044 >                    if ((p = advance()) == null) {
5045 >                        propagateCompletion();
5046                          break;
5047                      }
5048 +                    if ((u = searchFunction.apply(p)) != null) {
5049 +                        if (result.compareAndSet(null, u))
5050 +                            quietlyCompleteRoot();
5051 +                        return;
5052 +                    }
5053                  }
5357                tryComplete();
5358            } catch (Throwable ex) {
5359                return tryCompleteComputation(ex);
5054              }
5361            return false;
5055          }
5363        public final U getRawResult() { return result.get(); }
5056      }
5057  
5058 <    @SuppressWarnings("serial") static final class SearchMappingsTask<K,V,U>
5058 >    @SuppressWarnings("serial")
5059 >    static final class SearchMappingsTask<K,V,U>
5060          extends BulkTask<K,V,U> {
5061 <        final BiFun<? super K, ? super V, ? extends U> searchFunction;
5061 >        final BiFunction<? super K, ? super V, ? extends U> searchFunction;
5062          final AtomicReference<U> result;
5063          SearchMappingsTask
5064 <            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5065 <             BiFun<? super K, ? super V, ? extends U> searchFunction,
5064 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5065 >             BiFunction<? super K, ? super V, ? extends U> searchFunction,
5066               AtomicReference<U> result) {
5067 <            super(m, p, b);
5067 >            super(p, b, i, f, t);
5068              this.searchFunction = searchFunction; this.result = result;
5069          }
5070 <        @SuppressWarnings("unchecked") public final boolean exec() {
5071 <            AtomicReference<U> result = this.result;
5072 <            final BiFun<? super K, ? super V, ? extends U> searchFunction =
5073 <                this.searchFunction;
5074 <            if (searchFunction == null || result == null)
5075 <                return abortOnNullFunction();
5076 <            try {
5077 <                int b = batch(), c;
5078 <                while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5079 <                    do {} while (!casPending(c = pending, c+1));
5080 <                    new SearchMappingsTask<K,V,U>(map, this, b >>>= 1,
5081 <                                                  searchFunction, result).fork();
5082 <                }
5083 <                Object v; U u;
5084 <                while (result.get() == null && (v = advance()) != null) {
5085 <                    if ((u = searchFunction.apply((K)nextKey, (V)v)) != null) {
5070 >        public final U getRawResult() { return result.get(); }
5071 >        public final void compute() {
5072 >            final BiFunction<? super K, ? super V, ? extends U> searchFunction;
5073 >            final AtomicReference<U> result;
5074 >            if ((searchFunction = this.searchFunction) != null &&
5075 >                (result = this.result) != null) {
5076 >                for (int i = baseIndex, f, h; batch > 0 &&
5077 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5078 >                    if (result.get() != null)
5079 >                        return;
5080 >                    addToPendingCount(1);
5081 >                    new SearchMappingsTask<K,V,U>
5082 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
5083 >                         searchFunction, result).fork();
5084 >                }
5085 >                while (result.get() == null) {
5086 >                    U u;
5087 >                    Node<K,V> p;
5088 >                    if ((p = advance()) == null) {
5089 >                        propagateCompletion();
5090 >                        break;
5091 >                    }
5092 >                    if ((u = searchFunction.apply(p.key, p.val)) != null) {
5093                          if (result.compareAndSet(null, u))
5094 <                            tryCompleteComputation(null);
5094 >                            quietlyCompleteRoot();
5095                          break;
5096                      }
5097                  }
5398                tryComplete();
5399            } catch (Throwable ex) {
5400                return tryCompleteComputation(ex);
5098              }
5402            return false;
5099          }
5404        public final U getRawResult() { return result.get(); }
5100      }
5101  
5102 <    @SuppressWarnings("serial") static final class ReduceKeysTask<K,V>
5102 >    @SuppressWarnings("serial")
5103 >    static final class ReduceKeysTask<K,V>
5104          extends BulkTask<K,V,K> {
5105 <        final BiFun<? super K, ? super K, ? extends K> reducer;
5105 >        final BiFunction<? super K, ? super K, ? extends K> reducer;
5106          K result;
5107          ReduceKeysTask<K,V> rights, nextRight;
5108          ReduceKeysTask
5109 <            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5109 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5110               ReduceKeysTask<K,V> nextRight,
5111 <             BiFun<? super K, ? super K, ? extends K> reducer) {
5112 <            super(m, p, b); this.nextRight = nextRight;
5111 >             BiFunction<? super K, ? super K, ? extends K> reducer) {
5112 >            super(p, b, i, f, t); this.nextRight = nextRight;
5113              this.reducer = reducer;
5114          }
5115 <        @SuppressWarnings("unchecked") public final boolean exec() {
5116 <            final BiFun<? super K, ? super K, ? extends K> reducer =
5117 <                this.reducer;
5118 <            if (reducer == null)
5119 <                return abortOnNullFunction();
5120 <            try {
5121 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5426 <                    do {} while (!casPending(c = pending, c+1));
5115 >        public final K getRawResult() { return result; }
5116 >        public final void compute() {
5117 >            final BiFunction<? super K, ? super K, ? extends K> reducer;
5118 >            if ((reducer = this.reducer) != null) {
5119 >                for (int i = baseIndex, f, h; batch > 0 &&
5120 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5121 >                    addToPendingCount(1);
5122                      (rights = new ReduceKeysTask<K,V>
5123 <                     (map, this, b >>>= 1, rights, reducer)).fork();
5123 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5124 >                      rights, reducer)).fork();
5125                  }
5126                  K r = null;
5127 <                while (advance() != null) {
5128 <                    K u = (K)nextKey;
5129 <                    r = (r == null) ? u : reducer.apply(r, u);
5127 >                for (Node<K,V> p; (p = advance()) != null; ) {
5128 >                    K u = p.key;
5129 >                    r = (r == null) ? u : u == null ? r : reducer.apply(r, u);
5130                  }
5131                  result = r;
5132 <                for (ReduceKeysTask<K,V> t = this, s;;) {
5133 <                    int c; BulkTask<K,V,?> par; K tr, sr;
5134 <                    if ((c = t.pending) == 0) {
5135 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5136 <                            if ((sr = s.result) != null)
5137 <                                t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
5138 <                        }
5139 <                        if ((par = t.parent) == null ||
5140 <                            !(par instanceof ReduceKeysTask)) {
5141 <                            t.quietlyComplete();
5142 <                            break;
5447 <                        }
5448 <                        t = (ReduceKeysTask<K,V>)par;
5132 >                CountedCompleter<?> c;
5133 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5134 >                    @SuppressWarnings("unchecked") ReduceKeysTask<K,V>
5135 >                        t = (ReduceKeysTask<K,V>)c,
5136 >                        s = t.rights;
5137 >                    while (s != null) {
5138 >                        K tr, sr;
5139 >                        if ((sr = s.result) != null)
5140 >                            t.result = (((tr = t.result) == null) ? sr :
5141 >                                        reducer.apply(tr, sr));
5142 >                        s = t.rights = s.nextRight;
5143                      }
5450                    else if (t.casPending(c, c - 1))
5451                        break;
5144                  }
5453            } catch (Throwable ex) {
5454                return tryCompleteComputation(ex);
5145              }
5456            return false;
5146          }
5458        public final K getRawResult() { return result; }
5147      }
5148  
5149 <    @SuppressWarnings("serial") static final class ReduceValuesTask<K,V>
5149 >    @SuppressWarnings("serial")
5150 >    static final class ReduceValuesTask<K,V>
5151          extends BulkTask<K,V,V> {
5152 <        final BiFun<? super V, ? super V, ? extends V> reducer;
5152 >        final BiFunction<? super V, ? super V, ? extends V> reducer;
5153          V result;
5154          ReduceValuesTask<K,V> rights, nextRight;
5155          ReduceValuesTask
5156 <            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5156 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5157               ReduceValuesTask<K,V> nextRight,
5158 <             BiFun<? super V, ? super V, ? extends V> reducer) {
5159 <            super(m, p, b); this.nextRight = nextRight;
5158 >             BiFunction<? super V, ? super V, ? extends V> reducer) {
5159 >            super(p, b, i, f, t); this.nextRight = nextRight;
5160              this.reducer = reducer;
5161          }
5162 <        @SuppressWarnings("unchecked") public final boolean exec() {
5163 <            final BiFun<? super V, ? super V, ? extends V> reducer =
5164 <                this.reducer;
5165 <            if (reducer == null)
5166 <                return abortOnNullFunction();
5167 <            try {
5168 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5480 <                    do {} while (!casPending(c = pending, c+1));
5162 >        public final V getRawResult() { return result; }
5163 >        public final void compute() {
5164 >            final BiFunction<? super V, ? super V, ? extends V> reducer;
5165 >            if ((reducer = this.reducer) != null) {
5166 >                for (int i = baseIndex, f, h; batch > 0 &&
5167 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5168 >                    addToPendingCount(1);
5169                      (rights = new ReduceValuesTask<K,V>
5170 <                     (map, this, b >>>= 1, rights, reducer)).fork();
5170 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5171 >                      rights, reducer)).fork();
5172                  }
5173                  V r = null;
5174 <                Object v;
5175 <                while ((v = advance()) != null) {
5176 <                    V u = (V)v;
5488 <                    r = (r == null) ? u : reducer.apply(r, u);
5174 >                for (Node<K,V> p; (p = advance()) != null; ) {
5175 >                    V v = p.val;
5176 >                    r = (r == null) ? v : reducer.apply(r, v);
5177                  }
5178                  result = r;
5179 <                for (ReduceValuesTask<K,V> t = this, s;;) {
5180 <                    int c; BulkTask<K,V,?> par; V tr, sr;
5181 <                    if ((c = t.pending) == 0) {
5182 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5183 <                            if ((sr = s.result) != null)
5184 <                                t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
5185 <                        }
5186 <                        if ((par = t.parent) == null ||
5187 <                            !(par instanceof ReduceValuesTask)) {
5188 <                            t.quietlyComplete();
5189 <                            break;
5502 <                        }
5503 <                        t = (ReduceValuesTask<K,V>)par;
5179 >                CountedCompleter<?> c;
5180 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5181 >                    @SuppressWarnings("unchecked") ReduceValuesTask<K,V>
5182 >                        t = (ReduceValuesTask<K,V>)c,
5183 >                        s = t.rights;
5184 >                    while (s != null) {
5185 >                        V tr, sr;
5186 >                        if ((sr = s.result) != null)
5187 >                            t.result = (((tr = t.result) == null) ? sr :
5188 >                                        reducer.apply(tr, sr));
5189 >                        s = t.rights = s.nextRight;
5190                      }
5505                    else if (t.casPending(c, c - 1))
5506                        break;
5191                  }
5508            } catch (Throwable ex) {
5509                return tryCompleteComputation(ex);
5192              }
5511            return false;
5193          }
5513        public final V getRawResult() { return result; }
5194      }
5195  
5196 <    @SuppressWarnings("serial") static final class ReduceEntriesTask<K,V>
5196 >    @SuppressWarnings("serial")
5197 >    static final class ReduceEntriesTask<K,V>
5198          extends BulkTask<K,V,Map.Entry<K,V>> {
5199 <        final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
5199 >        final BiFunction<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
5200          Map.Entry<K,V> result;
5201          ReduceEntriesTask<K,V> rights, nextRight;
5202          ReduceEntriesTask
5203 <            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5203 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5204               ReduceEntriesTask<K,V> nextRight,
5205 <             BiFun<Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
5206 <            super(m, p, b); this.nextRight = nextRight;
5205 >             BiFunction<Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
5206 >            super(p, b, i, f, t); this.nextRight = nextRight;
5207              this.reducer = reducer;
5208          }
5209 <        @SuppressWarnings("unchecked") public final boolean exec() {
5210 <            final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer =
5211 <                this.reducer;
5212 <            if (reducer == null)
5213 <                return abortOnNullFunction();
5214 <            try {
5215 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5535 <                    do {} while (!casPending(c = pending, c+1));
5209 >        public final Map.Entry<K,V> getRawResult() { return result; }
5210 >        public final void compute() {
5211 >            final BiFunction<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
5212 >            if ((reducer = this.reducer) != null) {
5213 >                for (int i = baseIndex, f, h; batch > 0 &&
5214 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5215 >                    addToPendingCount(1);
5216                      (rights = new ReduceEntriesTask<K,V>
5217 <                     (map, this, b >>>= 1, rights, reducer)).fork();
5217 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5218 >                      rights, reducer)).fork();
5219                  }
5220                  Map.Entry<K,V> r = null;
5221 <                Object v;
5222 <                while ((v = advance()) != null) {
5542 <                    Map.Entry<K,V> u = entryFor((K)nextKey, (V)v);
5543 <                    r = (r == null) ? u : reducer.apply(r, u);
5544 <                }
5221 >                for (Node<K,V> p; (p = advance()) != null; )
5222 >                    r = (r == null) ? p : reducer.apply(r, p);
5223                  result = r;
5224 <                for (ReduceEntriesTask<K,V> t = this, s;;) {
5225 <                    int c; BulkTask<K,V,?> par; Map.Entry<K,V> tr, sr;
5226 <                    if ((c = t.pending) == 0) {
5227 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5228 <                            if ((sr = s.result) != null)
5229 <                                t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
5230 <                        }
5231 <                        if ((par = t.parent) == null ||
5232 <                            !(par instanceof ReduceEntriesTask)) {
5233 <                            t.quietlyComplete();
5234 <                            break;
5557 <                        }
5558 <                        t = (ReduceEntriesTask<K,V>)par;
5224 >                CountedCompleter<?> c;
5225 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5226 >                    @SuppressWarnings("unchecked") ReduceEntriesTask<K,V>
5227 >                        t = (ReduceEntriesTask<K,V>)c,
5228 >                        s = t.rights;
5229 >                    while (s != null) {
5230 >                        Map.Entry<K,V> tr, sr;
5231 >                        if ((sr = s.result) != null)
5232 >                            t.result = (((tr = t.result) == null) ? sr :
5233 >                                        reducer.apply(tr, sr));
5234 >                        s = t.rights = s.nextRight;
5235                      }
5560                    else if (t.casPending(c, c - 1))
5561                        break;
5236                  }
5563            } catch (Throwable ex) {
5564                return tryCompleteComputation(ex);
5237              }
5566            return false;
5238          }
5568        public final Map.Entry<K,V> getRawResult() { return result; }
5239      }
5240  
5241 <    @SuppressWarnings("serial") static final class MapReduceKeysTask<K,V,U>
5241 >    @SuppressWarnings("serial")
5242 >    static final class MapReduceKeysTask<K,V,U>
5243          extends BulkTask<K,V,U> {
5244 <        final Fun<? super K, ? extends U> transformer;
5245 <        final BiFun<? super U, ? super U, ? extends U> reducer;
5244 >        final Function<? super K, ? extends U> transformer;
5245 >        final BiFunction<? super U, ? super U, ? extends U> reducer;
5246          U result;
5247          MapReduceKeysTask<K,V,U> rights, nextRight;
5248          MapReduceKeysTask
5249 <            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5249 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5250               MapReduceKeysTask<K,V,U> nextRight,
5251 <             Fun<? super K, ? extends U> transformer,
5252 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5253 <            super(m, p, b); this.nextRight = nextRight;
5251 >             Function<? super K, ? extends U> transformer,
5252 >             BiFunction<? super U, ? super U, ? extends U> reducer) {
5253 >            super(p, b, i, f, t); this.nextRight = nextRight;
5254              this.transformer = transformer;
5255              this.reducer = reducer;
5256          }
5257 <        @SuppressWarnings("unchecked") public final boolean exec() {
5258 <            final Fun<? super K, ? extends U> transformer =
5259 <                this.transformer;
5260 <            final BiFun<? super U, ? super U, ? extends U> reducer =
5261 <                this.reducer;
5262 <            if (transformer == null || reducer == null)
5263 <                return abortOnNullFunction();
5264 <            try {
5265 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5595 <                    do {} while (!casPending(c = pending, c+1));
5257 >        public final U getRawResult() { return result; }
5258 >        public final void compute() {
5259 >            final Function<? super K, ? extends U> transformer;
5260 >            final BiFunction<? super U, ? super U, ? extends U> reducer;
5261 >            if ((transformer = this.transformer) != null &&
5262 >                (reducer = this.reducer) != null) {
5263 >                for (int i = baseIndex, f, h; batch > 0 &&
5264 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5265 >                    addToPendingCount(1);
5266                      (rights = new MapReduceKeysTask<K,V,U>
5267 <                     (map, this, b >>>= 1, rights, transformer, reducer)).fork();
5267 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5268 >                      rights, transformer, reducer)).fork();
5269                  }
5270 <                U r = null, u;
5271 <                while (advance() != null) {
5272 <                    if ((u = transformer.apply((K)nextKey)) != null)
5270 >                U r = null;
5271 >                for (Node<K,V> p; (p = advance()) != null; ) {
5272 >                    U u;
5273 >                    if ((u = transformer.apply(p.key)) != null)
5274                          r = (r == null) ? u : reducer.apply(r, u);
5275                  }
5276                  result = r;
5277 <                for (MapReduceKeysTask<K,V,U> t = this, s;;) {
5278 <                    int c; BulkTask<K,V,?> par; U tr, sr;
5279 <                    if ((c = t.pending) == 0) {
5280 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5281 <                            if ((sr = s.result) != null)
5282 <                                t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
5283 <                        }
5284 <                        if ((par = t.parent) == null ||
5285 <                            !(par instanceof MapReduceKeysTask)) {
5286 <                            t.quietlyComplete();
5287 <                            break;
5616 <                        }
5617 <                        t = (MapReduceKeysTask<K,V,U>)par;
5277 >                CountedCompleter<?> c;
5278 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5279 >                    @SuppressWarnings("unchecked") MapReduceKeysTask<K,V,U>
5280 >                        t = (MapReduceKeysTask<K,V,U>)c,
5281 >                        s = t.rights;
5282 >                    while (s != null) {
5283 >                        U tr, sr;
5284 >                        if ((sr = s.result) != null)
5285 >                            t.result = (((tr = t.result) == null) ? sr :
5286 >                                        reducer.apply(tr, sr));
5287 >                        s = t.rights = s.nextRight;
5288                      }
5619                    else if (t.casPending(c, c - 1))
5620                        break;
5289                  }
5622            } catch (Throwable ex) {
5623                return tryCompleteComputation(ex);
5290              }
5625            return false;
5291          }
5627        public final U getRawResult() { return result; }
5292      }
5293  
5294 <    @SuppressWarnings("serial") static final class MapReduceValuesTask<K,V,U>
5294 >    @SuppressWarnings("serial")
5295 >    static final class MapReduceValuesTask<K,V,U>
5296          extends BulkTask<K,V,U> {
5297 <        final Fun<? super V, ? extends U> transformer;
5298 <        final BiFun<? super U, ? super U, ? extends U> reducer;
5297 >        final Function<? super V, ? extends U> transformer;
5298 >        final BiFunction<? super U, ? super U, ? extends U> reducer;
5299          U result;
5300          MapReduceValuesTask<K,V,U> rights, nextRight;
5301          MapReduceValuesTask
5302 <            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5302 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5303               MapReduceValuesTask<K,V,U> nextRight,
5304 <             Fun<? super V, ? extends U> transformer,
5305 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5306 <            super(m, p, b); this.nextRight = nextRight;
5304 >             Function<? super V, ? extends U> transformer,
5305 >             BiFunction<? super U, ? super U, ? extends U> reducer) {
5306 >            super(p, b, i, f, t); this.nextRight = nextRight;
5307              this.transformer = transformer;
5308              this.reducer = reducer;
5309          }
5310 <        @SuppressWarnings("unchecked") public final boolean exec() {
5311 <            final Fun<? super V, ? extends U> transformer =
5312 <                this.transformer;
5313 <            final BiFun<? super U, ? super U, ? extends U> reducer =
5314 <                this.reducer;
5315 <            if (transformer == null || reducer == null)
5316 <                return abortOnNullFunction();
5317 <            try {
5318 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5654 <                    do {} while (!casPending(c = pending, c+1));
5310 >        public final U getRawResult() { return result; }
5311 >        public final void compute() {
5312 >            final Function<? super V, ? extends U> transformer;
5313 >            final BiFunction<? super U, ? super U, ? extends U> reducer;
5314 >            if ((transformer = this.transformer) != null &&
5315 >                (reducer = this.reducer) != null) {
5316 >                for (int i = baseIndex, f, h; batch > 0 &&
5317 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5318 >                    addToPendingCount(1);
5319                      (rights = new MapReduceValuesTask<K,V,U>
5320 <                     (map, this, b >>>= 1, rights, transformer, reducer)).fork();
5320 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5321 >                      rights, transformer, reducer)).fork();
5322                  }
5323 <                U r = null, u;
5324 <                Object v;
5325 <                while ((v = advance()) != null) {
5326 <                    if ((u = transformer.apply((V)v)) != null)
5323 >                U r = null;
5324 >                for (Node<K,V> p; (p = advance()) != null; ) {
5325 >                    U u;
5326 >                    if ((u = transformer.apply(p.val)) != null)
5327                          r = (r == null) ? u : reducer.apply(r, u);
5328                  }
5329                  result = r;
5330 <                for (MapReduceValuesTask<K,V,U> t = this, s;;) {
5331 <                    int c; BulkTask<K,V,?> par; U tr, sr;
5332 <                    if ((c = t.pending) == 0) {
5333 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5334 <                            if ((sr = s.result) != null)
5335 <                                t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
5336 <                        }
5337 <                        if ((par = t.parent) == null ||
5338 <                            !(par instanceof MapReduceValuesTask)) {
5339 <                            t.quietlyComplete();
5340 <                            break;
5676 <                        }
5677 <                        t = (MapReduceValuesTask<K,V,U>)par;
5330 >                CountedCompleter<?> c;
5331 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5332 >                    @SuppressWarnings("unchecked") MapReduceValuesTask<K,V,U>
5333 >                        t = (MapReduceValuesTask<K,V,U>)c,
5334 >                        s = t.rights;
5335 >                    while (s != null) {
5336 >                        U tr, sr;
5337 >                        if ((sr = s.result) != null)
5338 >                            t.result = (((tr = t.result) == null) ? sr :
5339 >                                        reducer.apply(tr, sr));
5340 >                        s = t.rights = s.nextRight;
5341                      }
5679                    else if (t.casPending(c, c - 1))
5680                        break;
5342                  }
5682            } catch (Throwable ex) {
5683                return tryCompleteComputation(ex);
5343              }
5685            return false;
5344          }
5687        public final U getRawResult() { return result; }
5345      }
5346  
5347 <    @SuppressWarnings("serial") static final class MapReduceEntriesTask<K,V,U>
5347 >    @SuppressWarnings("serial")
5348 >    static final class MapReduceEntriesTask<K,V,U>
5349          extends BulkTask<K,V,U> {
5350 <        final Fun<Map.Entry<K,V>, ? extends U> transformer;
5351 <        final BiFun<? super U, ? super U, ? extends U> reducer;
5350 >        final Function<Map.Entry<K,V>, ? extends U> transformer;
5351 >        final BiFunction<? super U, ? super U, ? extends U> reducer;
5352          U result;
5353          MapReduceEntriesTask<K,V,U> rights, nextRight;
5354          MapReduceEntriesTask
5355 <            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5355 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5356               MapReduceEntriesTask<K,V,U> nextRight,
5357 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
5358 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5359 <            super(m, p, b); this.nextRight = nextRight;
5357 >             Function<Map.Entry<K,V>, ? extends U> transformer,
5358 >             BiFunction<? super U, ? super U, ? extends U> reducer) {
5359 >            super(p, b, i, f, t); this.nextRight = nextRight;
5360              this.transformer = transformer;
5361              this.reducer = reducer;
5362          }
5363 <        @SuppressWarnings("unchecked") public final boolean exec() {
5364 <            final Fun<Map.Entry<K,V>, ? extends U> transformer =
5365 <                this.transformer;
5366 <            final BiFun<? super U, ? super U, ? extends U> reducer =
5367 <                this.reducer;
5368 <            if (transformer == null || reducer == null)
5369 <                return abortOnNullFunction();
5370 <            try {
5371 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5714 <                    do {} while (!casPending(c = pending, c+1));
5363 >        public final U getRawResult() { return result; }
5364 >        public final void compute() {
5365 >            final Function<Map.Entry<K,V>, ? extends U> transformer;
5366 >            final BiFunction<? super U, ? super U, ? extends U> reducer;
5367 >            if ((transformer = this.transformer) != null &&
5368 >                (reducer = this.reducer) != null) {
5369 >                for (int i = baseIndex, f, h; batch > 0 &&
5370 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5371 >                    addToPendingCount(1);
5372                      (rights = new MapReduceEntriesTask<K,V,U>
5373 <                     (map, this, b >>>= 1, rights, transformer, reducer)).fork();
5373 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5374 >                      rights, transformer, reducer)).fork();
5375                  }
5376 <                U r = null, u;
5377 <                Object v;
5378 <                while ((v = advance()) != null) {
5379 <                    if ((u = transformer.apply(entryFor((K)nextKey, (V)v))) != null)
5376 >                U r = null;
5377 >                for (Node<K,V> p; (p = advance()) != null; ) {
5378 >                    U u;
5379 >                    if ((u = transformer.apply(p)) != null)
5380                          r = (r == null) ? u : reducer.apply(r, u);
5381                  }
5382                  result = r;
5383 <                for (MapReduceEntriesTask<K,V,U> t = this, s;;) {
5384 <                    int c; BulkTask<K,V,?> par; U tr, sr;
5385 <                    if ((c = t.pending) == 0) {
5386 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5387 <                            if ((sr = s.result) != null)
5388 <                                t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
5389 <                        }
5390 <                        if ((par = t.parent) == null ||
5391 <                            !(par instanceof MapReduceEntriesTask)) {
5392 <                            t.quietlyComplete();
5393 <                            break;
5736 <                        }
5737 <                        t = (MapReduceEntriesTask<K,V,U>)par;
5383 >                CountedCompleter<?> c;
5384 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5385 >                    @SuppressWarnings("unchecked") MapReduceEntriesTask<K,V,U>
5386 >                        t = (MapReduceEntriesTask<K,V,U>)c,
5387 >                        s = t.rights;
5388 >                    while (s != null) {
5389 >                        U tr, sr;
5390 >                        if ((sr = s.result) != null)
5391 >                            t.result = (((tr = t.result) == null) ? sr :
5392 >                                        reducer.apply(tr, sr));
5393 >                        s = t.rights = s.nextRight;
5394                      }
5739                    else if (t.casPending(c, c - 1))
5740                        break;
5395                  }
5742            } catch (Throwable ex) {
5743                return tryCompleteComputation(ex);
5396              }
5745            return false;
5397          }
5747        public final U getRawResult() { return result; }
5398      }
5399  
5400 <    @SuppressWarnings("serial") static final class MapReduceMappingsTask<K,V,U>
5400 >    @SuppressWarnings("serial")
5401 >    static final class MapReduceMappingsTask<K,V,U>
5402          extends BulkTask<K,V,U> {
5403 <        final BiFun<? super K, ? super V, ? extends U> transformer;
5404 <        final BiFun<? super U, ? super U, ? extends U> reducer;
5403 >        final BiFunction<? super K, ? super V, ? extends U> transformer;
5404 >        final BiFunction<? super U, ? super U, ? extends U> reducer;
5405          U result;
5406          MapReduceMappingsTask<K,V,U> rights, nextRight;
5407          MapReduceMappingsTask
5408 <            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5408 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5409               MapReduceMappingsTask<K,V,U> nextRight,
5410 <             BiFun<? super K, ? super V, ? extends U> transformer,
5411 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5412 <            super(m, p, b); this.nextRight = nextRight;
5410 >             BiFunction<? super K, ? super V, ? extends U> transformer,
5411 >             BiFunction<? super U, ? super U, ? extends U> reducer) {
5412 >            super(p, b, i, f, t); this.nextRight = nextRight;
5413              this.transformer = transformer;
5414              this.reducer = reducer;
5415          }
5416 <        @SuppressWarnings("unchecked") public final boolean exec() {
5417 <            final BiFun<? super K, ? super V, ? extends U> transformer =
5418 <                this.transformer;
5419 <            final BiFun<? super U, ? super U, ? extends U> reducer =
5420 <                this.reducer;
5421 <            if (transformer == null || reducer == null)
5422 <                return abortOnNullFunction();
5423 <            try {
5424 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5774 <                    do {} while (!casPending(c = pending, c+1));
5416 >        public final U getRawResult() { return result; }
5417 >        public final void compute() {
5418 >            final BiFunction<? super K, ? super V, ? extends U> transformer;
5419 >            final BiFunction<? super U, ? super U, ? extends U> reducer;
5420 >            if ((transformer = this.transformer) != null &&
5421 >                (reducer = this.reducer) != null) {
5422 >                for (int i = baseIndex, f, h; batch > 0 &&
5423 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5424 >                    addToPendingCount(1);
5425                      (rights = new MapReduceMappingsTask<K,V,U>
5426 <                     (map, this, b >>>= 1, rights, transformer, reducer)).fork();
5426 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5427 >                      rights, transformer, reducer)).fork();
5428                  }
5429 <                U r = null, u;
5430 <                Object v;
5431 <                while ((v = advance()) != null) {
5432 <                    if ((u = transformer.apply((K)nextKey, (V)v)) != null)
5429 >                U r = null;
5430 >                for (Node<K,V> p; (p = advance()) != null; ) {
5431 >                    U u;
5432 >                    if ((u = transformer.apply(p.key, p.val)) != null)
5433                          r = (r == null) ? u : reducer.apply(r, u);
5434                  }
5435                  result = r;
5436 <                for (MapReduceMappingsTask<K,V,U> t = this, s;;) {
5437 <                    int c; BulkTask<K,V,?> par; U tr, sr;
5438 <                    if ((c = t.pending) == 0) {
5439 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5440 <                            if ((sr = s.result) != null)
5441 <                                t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
5442 <                        }
5443 <                        if ((par = t.parent) == null ||
5444 <                            !(par instanceof MapReduceMappingsTask)) {
5445 <                            t.quietlyComplete();
5446 <                            break;
5796 <                        }
5797 <                        t = (MapReduceMappingsTask<K,V,U>)par;
5436 >                CountedCompleter<?> c;
5437 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5438 >                    @SuppressWarnings("unchecked") MapReduceMappingsTask<K,V,U>
5439 >                        t = (MapReduceMappingsTask<K,V,U>)c,
5440 >                        s = t.rights;
5441 >                    while (s != null) {
5442 >                        U tr, sr;
5443 >                        if ((sr = s.result) != null)
5444 >                            t.result = (((tr = t.result) == null) ? sr :
5445 >                                        reducer.apply(tr, sr));
5446 >                        s = t.rights = s.nextRight;
5447                      }
5799                    else if (t.casPending(c, c - 1))
5800                        break;
5448                  }
5802            } catch (Throwable ex) {
5803                return tryCompleteComputation(ex);
5449              }
5805            return false;
5450          }
5807        public final U getRawResult() { return result; }
5451      }
5452  
5453 <    @SuppressWarnings("serial") static final class MapReduceKeysToDoubleTask<K,V>
5453 >    @SuppressWarnings("serial")
5454 >    static final class MapReduceKeysToDoubleTask<K,V>
5455          extends BulkTask<K,V,Double> {
5456 <        final ObjectToDouble<? super K> transformer;
5457 <        final DoubleByDoubleToDouble reducer;
5456 >        final ToDoubleFunction<? super K> transformer;
5457 >        final DoubleBinaryOperator reducer;
5458          final double basis;
5459          double result;
5460          MapReduceKeysToDoubleTask<K,V> rights, nextRight;
5461          MapReduceKeysToDoubleTask
5462 <            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5462 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5463               MapReduceKeysToDoubleTask<K,V> nextRight,
5464 <             ObjectToDouble<? super K> transformer,
5464 >             ToDoubleFunction<? super K> transformer,
5465               double basis,
5466 <             DoubleByDoubleToDouble reducer) {
5467 <            super(m, p, b); this.nextRight = nextRight;
5466 >             DoubleBinaryOperator reducer) {
5467 >            super(p, b, i, f, t); this.nextRight = nextRight;
5468              this.transformer = transformer;
5469              this.basis = basis; this.reducer = reducer;
5470          }
5471 <        @SuppressWarnings("unchecked") public final boolean exec() {
5472 <            final ObjectToDouble<? super K> transformer =
5473 <                this.transformer;
5474 <            final DoubleByDoubleToDouble reducer = this.reducer;
5475 <            if (transformer == null || reducer == null)
5476 <                return abortOnNullFunction();
5477 <            try {
5478 <                final double id = this.basis;
5479 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5480 <                    do {} while (!casPending(c = pending, c+1));
5471 >        public final Double getRawResult() { return result; }
5472 >        public final void compute() {
5473 >            final ToDoubleFunction<? super K> transformer;
5474 >            final DoubleBinaryOperator reducer;
5475 >            if ((transformer = this.transformer) != null &&
5476 >                (reducer = this.reducer) != null) {
5477 >                double r = this.basis;
5478 >                for (int i = baseIndex, f, h; batch > 0 &&
5479 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5480 >                    addToPendingCount(1);
5481                      (rights = new MapReduceKeysToDoubleTask<K,V>
5482 <                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
5482 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5483 >                      rights, transformer, r, reducer)).fork();
5484                  }
5485 <                double r = id;
5486 <                while (advance() != null)
5842 <                    r = reducer.apply(r, transformer.apply((K)nextKey));
5485 >                for (Node<K,V> p; (p = advance()) != null; )
5486 >                    r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.key));
5487                  result = r;
5488 <                for (MapReduceKeysToDoubleTask<K,V> t = this, s;;) {
5489 <                    int c; BulkTask<K,V,?> par;
5490 <                    if ((c = t.pending) == 0) {
5491 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5492 <                            t.result = reducer.apply(t.result, s.result);
5493 <                        }
5494 <                        if ((par = t.parent) == null ||
5495 <                            !(par instanceof MapReduceKeysToDoubleTask)) {
5852 <                            t.quietlyComplete();
5853 <                            break;
5854 <                        }
5855 <                        t = (MapReduceKeysToDoubleTask<K,V>)par;
5488 >                CountedCompleter<?> c;
5489 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5490 >                    @SuppressWarnings("unchecked") MapReduceKeysToDoubleTask<K,V>
5491 >                        t = (MapReduceKeysToDoubleTask<K,V>)c,
5492 >                        s = t.rights;
5493 >                    while (s != null) {
5494 >                        t.result = reducer.applyAsDouble(t.result, s.result);
5495 >                        s = t.rights = s.nextRight;
5496                      }
5857                    else if (t.casPending(c, c - 1))
5858                        break;
5497                  }
5860            } catch (Throwable ex) {
5861                return tryCompleteComputation(ex);
5498              }
5863            return false;
5499          }
5865        public final Double getRawResult() { return result; }
5500      }
5501  
5502 <    @SuppressWarnings("serial") static final class MapReduceValuesToDoubleTask<K,V>
5502 >    @SuppressWarnings("serial")
5503 >    static final class MapReduceValuesToDoubleTask<K,V>
5504          extends BulkTask<K,V,Double> {
5505 <        final ObjectToDouble<? super V> transformer;
5506 <        final DoubleByDoubleToDouble reducer;
5505 >        final ToDoubleFunction<? super V> transformer;
5506 >        final DoubleBinaryOperator reducer;
5507          final double basis;
5508          double result;
5509          MapReduceValuesToDoubleTask<K,V> rights, nextRight;
5510          MapReduceValuesToDoubleTask
5511 <            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5511 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5512               MapReduceValuesToDoubleTask<K,V> nextRight,
5513 <             ObjectToDouble<? super V> transformer,
5513 >             ToDoubleFunction<? super V> transformer,
5514               double basis,
5515 <             DoubleByDoubleToDouble reducer) {
5516 <            super(m, p, b); this.nextRight = nextRight;
5515 >             DoubleBinaryOperator reducer) {
5516 >            super(p, b, i, f, t); this.nextRight = nextRight;
5517              this.transformer = transformer;
5518              this.basis = basis; this.reducer = reducer;
5519          }
5520 <        @SuppressWarnings("unchecked") public final boolean exec() {
5521 <            final ObjectToDouble<? super V> transformer =
5522 <                this.transformer;
5523 <            final DoubleByDoubleToDouble reducer = this.reducer;
5524 <            if (transformer == null || reducer == null)
5525 <                return abortOnNullFunction();
5526 <            try {
5527 <                final double id = this.basis;
5528 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5529 <                    do {} while (!casPending(c = pending, c+1));
5520 >        public final Double getRawResult() { return result; }
5521 >        public final void compute() {
5522 >            final ToDoubleFunction<? super V> transformer;
5523 >            final DoubleBinaryOperator reducer;
5524 >            if ((transformer = this.transformer) != null &&
5525 >                (reducer = this.reducer) != null) {
5526 >                double r = this.basis;
5527 >                for (int i = baseIndex, f, h; batch > 0 &&
5528 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5529 >                    addToPendingCount(1);
5530                      (rights = new MapReduceValuesToDoubleTask<K,V>
5531 <                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
5531 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5532 >                      rights, transformer, r, reducer)).fork();
5533                  }
5534 <                double r = id;
5535 <                Object v;
5900 <                while ((v = advance()) != null)
5901 <                    r = reducer.apply(r, transformer.apply((V)v));
5534 >                for (Node<K,V> p; (p = advance()) != null; )
5535 >                    r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.val));
5536                  result = r;
5537 <                for (MapReduceValuesToDoubleTask<K,V> t = this, s;;) {
5538 <                    int c; BulkTask<K,V,?> par;
5539 <                    if ((c = t.pending) == 0) {
5540 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5541 <                            t.result = reducer.apply(t.result, s.result);
5542 <                        }
5543 <                        if ((par = t.parent) == null ||
5544 <                            !(par instanceof MapReduceValuesToDoubleTask)) {
5911 <                            t.quietlyComplete();
5912 <                            break;
5913 <                        }
5914 <                        t = (MapReduceValuesToDoubleTask<K,V>)par;
5537 >                CountedCompleter<?> c;
5538 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5539 >                    @SuppressWarnings("unchecked") MapReduceValuesToDoubleTask<K,V>
5540 >                        t = (MapReduceValuesToDoubleTask<K,V>)c,
5541 >                        s = t.rights;
5542 >                    while (s != null) {
5543 >                        t.result = reducer.applyAsDouble(t.result, s.result);
5544 >                        s = t.rights = s.nextRight;
5545                      }
5916                    else if (t.casPending(c, c - 1))
5917                        break;
5546                  }
5919            } catch (Throwable ex) {
5920                return tryCompleteComputation(ex);
5547              }
5922            return false;
5548          }
5924        public final Double getRawResult() { return result; }
5549      }
5550  
5551 <    @SuppressWarnings("serial") static final class MapReduceEntriesToDoubleTask<K,V>
5551 >    @SuppressWarnings("serial")
5552 >    static final class MapReduceEntriesToDoubleTask<K,V>
5553          extends BulkTask<K,V,Double> {
5554 <        final ObjectToDouble<Map.Entry<K,V>> transformer;
5555 <        final DoubleByDoubleToDouble reducer;
5554 >        final ToDoubleFunction<Map.Entry<K,V>> transformer;
5555 >        final DoubleBinaryOperator reducer;
5556          final double basis;
5557          double result;
5558          MapReduceEntriesToDoubleTask<K,V> rights, nextRight;
5559          MapReduceEntriesToDoubleTask
5560 <            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5560 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5561               MapReduceEntriesToDoubleTask<K,V> nextRight,
5562 <             ObjectToDouble<Map.Entry<K,V>> transformer,
5562 >             ToDoubleFunction<Map.Entry<K,V>> transformer,
5563               double basis,
5564 <             DoubleByDoubleToDouble reducer) {
5565 <            super(m, p, b); this.nextRight = nextRight;
5564 >             DoubleBinaryOperator reducer) {
5565 >            super(p, b, i, f, t); this.nextRight = nextRight;
5566              this.transformer = transformer;
5567              this.basis = basis; this.reducer = reducer;
5568          }
5569 <        @SuppressWarnings("unchecked") public final boolean exec() {
5570 <            final ObjectToDouble<Map.Entry<K,V>> transformer =
5571 <                this.transformer;
5572 <            final DoubleByDoubleToDouble reducer = this.reducer;
5573 <            if (transformer == null || reducer == null)
5574 <                return abortOnNullFunction();
5575 <            try {
5576 <                final double id = this.basis;
5577 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5578 <                    do {} while (!casPending(c = pending, c+1));
5569 >        public final Double getRawResult() { return result; }
5570 >        public final void compute() {
5571 >            final ToDoubleFunction<Map.Entry<K,V>> transformer;
5572 >            final DoubleBinaryOperator reducer;
5573 >            if ((transformer = this.transformer) != null &&
5574 >                (reducer = this.reducer) != null) {
5575 >                double r = this.basis;
5576 >                for (int i = baseIndex, f, h; batch > 0 &&
5577 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5578 >                    addToPendingCount(1);
5579                      (rights = new MapReduceEntriesToDoubleTask<K,V>
5580 <                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
5580 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5581 >                      rights, transformer, r, reducer)).fork();
5582                  }
5583 <                double r = id;
5584 <                Object v;
5959 <                while ((v = advance()) != null)
5960 <                    r = reducer.apply(r, transformer.apply(entryFor((K)nextKey, (V)v)));
5583 >                for (Node<K,V> p; (p = advance()) != null; )
5584 >                    r = reducer.applyAsDouble(r, transformer.applyAsDouble(p));
5585                  result = r;
5586 <                for (MapReduceEntriesToDoubleTask<K,V> t = this, s;;) {
5587 <                    int c; BulkTask<K,V,?> par;
5588 <                    if ((c = t.pending) == 0) {
5589 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5590 <                            t.result = reducer.apply(t.result, s.result);
5591 <                        }
5592 <                        if ((par = t.parent) == null ||
5593 <                            !(par instanceof MapReduceEntriesToDoubleTask)) {
5970 <                            t.quietlyComplete();
5971 <                            break;
5972 <                        }
5973 <                        t = (MapReduceEntriesToDoubleTask<K,V>)par;
5586 >                CountedCompleter<?> c;
5587 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5588 >                    @SuppressWarnings("unchecked") MapReduceEntriesToDoubleTask<K,V>
5589 >                        t = (MapReduceEntriesToDoubleTask<K,V>)c,
5590 >                        s = t.rights;
5591 >                    while (s != null) {
5592 >                        t.result = reducer.applyAsDouble(t.result, s.result);
5593 >                        s = t.rights = s.nextRight;
5594                      }
5975                    else if (t.casPending(c, c - 1))
5976                        break;
5595                  }
5978            } catch (Throwable ex) {
5979                return tryCompleteComputation(ex);
5596              }
5981            return false;
5597          }
5983        public final Double getRawResult() { return result; }
5598      }
5599  
5600 <    @SuppressWarnings("serial") static final class MapReduceMappingsToDoubleTask<K,V>
5600 >    @SuppressWarnings("serial")
5601 >    static final class MapReduceMappingsToDoubleTask<K,V>
5602          extends BulkTask<K,V,Double> {
5603 <        final ObjectByObjectToDouble<? super K, ? super V> transformer;
5604 <        final DoubleByDoubleToDouble reducer;
5603 >        final ToDoubleBiFunction<? super K, ? super V> transformer;
5604 >        final DoubleBinaryOperator reducer;
5605          final double basis;
5606          double result;
5607          MapReduceMappingsToDoubleTask<K,V> rights, nextRight;
5608          MapReduceMappingsToDoubleTask
5609 <            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5609 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5610               MapReduceMappingsToDoubleTask<K,V> nextRight,
5611 <             ObjectByObjectToDouble<? super K, ? super V> transformer,
5611 >             ToDoubleBiFunction<? super K, ? super V> transformer,
5612               double basis,
5613 <             DoubleByDoubleToDouble reducer) {
5614 <            super(m, p, b); this.nextRight = nextRight;
5613 >             DoubleBinaryOperator reducer) {
5614 >            super(p, b, i, f, t); this.nextRight = nextRight;
5615              this.transformer = transformer;
5616              this.basis = basis; this.reducer = reducer;
5617          }
5618 <        @SuppressWarnings("unchecked") public final boolean exec() {
5619 <            final ObjectByObjectToDouble<? super K, ? super V> transformer =
5620 <                this.transformer;
5621 <            final DoubleByDoubleToDouble reducer = this.reducer;
5622 <            if (transformer == null || reducer == null)
5623 <                return abortOnNullFunction();
5624 <            try {
5625 <                final double id = this.basis;
5626 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5627 <                    do {} while (!casPending(c = pending, c+1));
5618 >        public final Double getRawResult() { return result; }
5619 >        public final void compute() {
5620 >            final ToDoubleBiFunction<? super K, ? super V> transformer;
5621 >            final DoubleBinaryOperator reducer;
5622 >            if ((transformer = this.transformer) != null &&
5623 >                (reducer = this.reducer) != null) {
5624 >                double r = this.basis;
5625 >                for (int i = baseIndex, f, h; batch > 0 &&
5626 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5627 >                    addToPendingCount(1);
5628                      (rights = new MapReduceMappingsToDoubleTask<K,V>
5629 <                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
5629 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5630 >                      rights, transformer, r, reducer)).fork();
5631                  }
5632 <                double r = id;
5633 <                Object v;
6018 <                while ((v = advance()) != null)
6019 <                    r = reducer.apply(r, transformer.apply((K)nextKey, (V)v));
5632 >                for (Node<K,V> p; (p = advance()) != null; )
5633 >                    r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.key, p.val));
5634                  result = r;
5635 <                for (MapReduceMappingsToDoubleTask<K,V> t = this, s;;) {
5636 <                    int c; BulkTask<K,V,?> par;
5637 <                    if ((c = t.pending) == 0) {
5638 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5639 <                            t.result = reducer.apply(t.result, s.result);
5640 <                        }
5641 <                        if ((par = t.parent) == null ||
5642 <                            !(par instanceof MapReduceMappingsToDoubleTask)) {
6029 <                            t.quietlyComplete();
6030 <                            break;
6031 <                        }
6032 <                        t = (MapReduceMappingsToDoubleTask<K,V>)par;
5635 >                CountedCompleter<?> c;
5636 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5637 >                    @SuppressWarnings("unchecked") MapReduceMappingsToDoubleTask<K,V>
5638 >                        t = (MapReduceMappingsToDoubleTask<K,V>)c,
5639 >                        s = t.rights;
5640 >                    while (s != null) {
5641 >                        t.result = reducer.applyAsDouble(t.result, s.result);
5642 >                        s = t.rights = s.nextRight;
5643                      }
6034                    else if (t.casPending(c, c - 1))
6035                        break;
5644                  }
6037            } catch (Throwable ex) {
6038                return tryCompleteComputation(ex);
5645              }
6040            return false;
5646          }
6042        public final Double getRawResult() { return result; }
5647      }
5648  
5649 <    @SuppressWarnings("serial") static final class MapReduceKeysToLongTask<K,V>
5649 >    @SuppressWarnings("serial")
5650 >    static final class MapReduceKeysToLongTask<K,V>
5651          extends BulkTask<K,V,Long> {
5652 <        final ObjectToLong<? super K> transformer;
5653 <        final LongByLongToLong reducer;
5652 >        final ToLongFunction<? super K> transformer;
5653 >        final LongBinaryOperator reducer;
5654          final long basis;
5655          long result;
5656          MapReduceKeysToLongTask<K,V> rights, nextRight;
5657          MapReduceKeysToLongTask
5658 <            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5658 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5659               MapReduceKeysToLongTask<K,V> nextRight,
5660 <             ObjectToLong<? super K> transformer,
5660 >             ToLongFunction<? super K> transformer,
5661               long basis,
5662 <             LongByLongToLong reducer) {
5663 <            super(m, p, b); this.nextRight = nextRight;
5662 >             LongBinaryOperator reducer) {
5663 >            super(p, b, i, f, t); this.nextRight = nextRight;
5664              this.transformer = transformer;
5665              this.basis = basis; this.reducer = reducer;
5666          }
5667 <        @SuppressWarnings("unchecked") public final boolean exec() {
5668 <            final ObjectToLong<? super K> transformer =
5669 <                this.transformer;
5670 <            final LongByLongToLong reducer = this.reducer;
5671 <            if (transformer == null || reducer == null)
5672 <                return abortOnNullFunction();
5673 <            try {
5674 <                final long id = this.basis;
5675 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5676 <                    do {} while (!casPending(c = pending, c+1));
5667 >        public final Long getRawResult() { return result; }
5668 >        public final void compute() {
5669 >            final ToLongFunction<? super K> transformer;
5670 >            final LongBinaryOperator reducer;
5671 >            if ((transformer = this.transformer) != null &&
5672 >                (reducer = this.reducer) != null) {
5673 >                long r = this.basis;
5674 >                for (int i = baseIndex, f, h; batch > 0 &&
5675 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5676 >                    addToPendingCount(1);
5677                      (rights = new MapReduceKeysToLongTask<K,V>
5678 <                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
5678 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5679 >                      rights, transformer, r, reducer)).fork();
5680                  }
5681 <                long r = id;
5682 <                while (advance() != null)
6077 <                    r = reducer.apply(r, transformer.apply((K)nextKey));
5681 >                for (Node<K,V> p; (p = advance()) != null; )
5682 >                    r = reducer.applyAsLong(r, transformer.applyAsLong(p.key));
5683                  result = r;
5684 <                for (MapReduceKeysToLongTask<K,V> t = this, s;;) {
5685 <                    int c; BulkTask<K,V,?> par;
5686 <                    if ((c = t.pending) == 0) {
5687 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5688 <                            t.result = reducer.apply(t.result, s.result);
5689 <                        }
5690 <                        if ((par = t.parent) == null ||
5691 <                            !(par instanceof MapReduceKeysToLongTask)) {
6087 <                            t.quietlyComplete();
6088 <                            break;
6089 <                        }
6090 <                        t = (MapReduceKeysToLongTask<K,V>)par;
5684 >                CountedCompleter<?> c;
5685 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5686 >                    @SuppressWarnings("unchecked") MapReduceKeysToLongTask<K,V>
5687 >                        t = (MapReduceKeysToLongTask<K,V>)c,
5688 >                        s = t.rights;
5689 >                    while (s != null) {
5690 >                        t.result = reducer.applyAsLong(t.result, s.result);
5691 >                        s = t.rights = s.nextRight;
5692                      }
6092                    else if (t.casPending(c, c - 1))
6093                        break;
5693                  }
6095            } catch (Throwable ex) {
6096                return tryCompleteComputation(ex);
5694              }
6098            return false;
5695          }
6100        public final Long getRawResult() { return result; }
5696      }
5697  
5698 <    @SuppressWarnings("serial") static final class MapReduceValuesToLongTask<K,V>
5698 >    @SuppressWarnings("serial")
5699 >    static final class MapReduceValuesToLongTask<K,V>
5700          extends BulkTask<K,V,Long> {
5701 <        final ObjectToLong<? super V> transformer;
5702 <        final LongByLongToLong reducer;
5701 >        final ToLongFunction<? super V> transformer;
5702 >        final LongBinaryOperator reducer;
5703          final long basis;
5704          long result;
5705          MapReduceValuesToLongTask<K,V> rights, nextRight;
5706          MapReduceValuesToLongTask
5707 <            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5707 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5708               MapReduceValuesToLongTask<K,V> nextRight,
5709 <             ObjectToLong<? super V> transformer,
5709 >             ToLongFunction<? super V> transformer,
5710               long basis,
5711 <             LongByLongToLong reducer) {
5712 <            super(m, p, b); this.nextRight = nextRight;
5711 >             LongBinaryOperator reducer) {
5712 >            super(p, b, i, f, t); this.nextRight = nextRight;
5713              this.transformer = transformer;
5714              this.basis = basis; this.reducer = reducer;
5715          }
5716 <        @SuppressWarnings("unchecked") public final boolean exec() {
5717 <            final ObjectToLong<? super V> transformer =
5718 <                this.transformer;
5719 <            final LongByLongToLong reducer = this.reducer;
5720 <            if (transformer == null || reducer == null)
5721 <                return abortOnNullFunction();
5722 <            try {
5723 <                final long id = this.basis;
5724 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5725 <                    do {} while (!casPending(c = pending, c+1));
5716 >        public final Long getRawResult() { return result; }
5717 >        public final void compute() {
5718 >            final ToLongFunction<? super V> transformer;
5719 >            final LongBinaryOperator reducer;
5720 >            if ((transformer = this.transformer) != null &&
5721 >                (reducer = this.reducer) != null) {
5722 >                long r = this.basis;
5723 >                for (int i = baseIndex, f, h; batch > 0 &&
5724 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5725 >                    addToPendingCount(1);
5726                      (rights = new MapReduceValuesToLongTask<K,V>
5727 <                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
5727 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5728 >                      rights, transformer, r, reducer)).fork();
5729                  }
5730 <                long r = id;
5731 <                Object v;
6135 <                while ((v = advance()) != null)
6136 <                    r = reducer.apply(r, transformer.apply((V)v));
5730 >                for (Node<K,V> p; (p = advance()) != null; )
5731 >                    r = reducer.applyAsLong(r, transformer.applyAsLong(p.val));
5732                  result = r;
5733 <                for (MapReduceValuesToLongTask<K,V> t = this, s;;) {
5734 <                    int c; BulkTask<K,V,?> par;
5735 <                    if ((c = t.pending) == 0) {
5736 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5737 <                            t.result = reducer.apply(t.result, s.result);
5738 <                        }
5739 <                        if ((par = t.parent) == null ||
5740 <                            !(par instanceof MapReduceValuesToLongTask)) {
6146 <                            t.quietlyComplete();
6147 <                            break;
6148 <                        }
6149 <                        t = (MapReduceValuesToLongTask<K,V>)par;
5733 >                CountedCompleter<?> c;
5734 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5735 >                    @SuppressWarnings("unchecked") MapReduceValuesToLongTask<K,V>
5736 >                        t = (MapReduceValuesToLongTask<K,V>)c,
5737 >                        s = t.rights;
5738 >                    while (s != null) {
5739 >                        t.result = reducer.applyAsLong(t.result, s.result);
5740 >                        s = t.rights = s.nextRight;
5741                      }
6151                    else if (t.casPending(c, c - 1))
6152                        break;
5742                  }
6154            } catch (Throwable ex) {
6155                return tryCompleteComputation(ex);
5743              }
6157            return false;
5744          }
6159        public final Long getRawResult() { return result; }
5745      }
5746  
5747 <    @SuppressWarnings("serial") static final class MapReduceEntriesToLongTask<K,V>
5747 >    @SuppressWarnings("serial")
5748 >    static final class MapReduceEntriesToLongTask<K,V>
5749          extends BulkTask<K,V,Long> {
5750 <        final ObjectToLong<Map.Entry<K,V>> transformer;
5751 <        final LongByLongToLong reducer;
5750 >        final ToLongFunction<Map.Entry<K,V>> transformer;
5751 >        final LongBinaryOperator reducer;
5752          final long basis;
5753          long result;
5754          MapReduceEntriesToLongTask<K,V> rights, nextRight;
5755          MapReduceEntriesToLongTask
5756 <            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5756 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5757               MapReduceEntriesToLongTask<K,V> nextRight,
5758 <             ObjectToLong<Map.Entry<K,V>> transformer,
5758 >             ToLongFunction<Map.Entry<K,V>> transformer,
5759               long basis,
5760 <             LongByLongToLong reducer) {
5761 <            super(m, p, b); this.nextRight = nextRight;
5760 >             LongBinaryOperator reducer) {
5761 >            super(p, b, i, f, t); this.nextRight = nextRight;
5762              this.transformer = transformer;
5763              this.basis = basis; this.reducer = reducer;
5764          }
5765 <        @SuppressWarnings("unchecked") public final boolean exec() {
5766 <            final ObjectToLong<Map.Entry<K,V>> transformer =
5767 <                this.transformer;
5768 <            final LongByLongToLong reducer = this.reducer;
5769 <            if (transformer == null || reducer == null)
5770 <                return abortOnNullFunction();
5771 <            try {
5772 <                final long id = this.basis;
5773 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5774 <                    do {} while (!casPending(c = pending, c+1));
5765 >        public final Long getRawResult() { return result; }
5766 >        public final void compute() {
5767 >            final ToLongFunction<Map.Entry<K,V>> transformer;
5768 >            final LongBinaryOperator reducer;
5769 >            if ((transformer = this.transformer) != null &&
5770 >                (reducer = this.reducer) != null) {
5771 >                long r = this.basis;
5772 >                for (int i = baseIndex, f, h; batch > 0 &&
5773 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5774 >                    addToPendingCount(1);
5775                      (rights = new MapReduceEntriesToLongTask<K,V>
5776 <                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
5776 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5777 >                      rights, transformer, r, reducer)).fork();
5778                  }
5779 <                long r = id;
5780 <                Object v;
6194 <                while ((v = advance()) != null)
6195 <                    r = reducer.apply(r, transformer.apply(entryFor((K)nextKey, (V)v)));
5779 >                for (Node<K,V> p; (p = advance()) != null; )
5780 >                    r = reducer.applyAsLong(r, transformer.applyAsLong(p));
5781                  result = r;
5782 <                for (MapReduceEntriesToLongTask<K,V> t = this, s;;) {
5783 <                    int c; BulkTask<K,V,?> par;
5784 <                    if ((c = t.pending) == 0) {
5785 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5786 <                            t.result = reducer.apply(t.result, s.result);
5787 <                        }
5788 <                        if ((par = t.parent) == null ||
5789 <                            !(par instanceof MapReduceEntriesToLongTask)) {
6205 <                            t.quietlyComplete();
6206 <                            break;
6207 <                        }
6208 <                        t = (MapReduceEntriesToLongTask<K,V>)par;
5782 >                CountedCompleter<?> c;
5783 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5784 >                    @SuppressWarnings("unchecked") MapReduceEntriesToLongTask<K,V>
5785 >                        t = (MapReduceEntriesToLongTask<K,V>)c,
5786 >                        s = t.rights;
5787 >                    while (s != null) {
5788 >                        t.result = reducer.applyAsLong(t.result, s.result);
5789 >                        s = t.rights = s.nextRight;
5790                      }
6210                    else if (t.casPending(c, c - 1))
6211                        break;
5791                  }
6213            } catch (Throwable ex) {
6214                return tryCompleteComputation(ex);
5792              }
6216            return false;
5793          }
6218        public final Long getRawResult() { return result; }
5794      }
5795  
5796 <    @SuppressWarnings("serial") static final class MapReduceMappingsToLongTask<K,V>
5796 >    @SuppressWarnings("serial")
5797 >    static final class MapReduceMappingsToLongTask<K,V>
5798          extends BulkTask<K,V,Long> {
5799 <        final ObjectByObjectToLong<? super K, ? super V> transformer;
5800 <        final LongByLongToLong reducer;
5799 >        final ToLongBiFunction<? super K, ? super V> transformer;
5800 >        final LongBinaryOperator reducer;
5801          final long basis;
5802          long result;
5803          MapReduceMappingsToLongTask<K,V> rights, nextRight;
5804          MapReduceMappingsToLongTask
5805 <            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5805 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5806               MapReduceMappingsToLongTask<K,V> nextRight,
5807 <             ObjectByObjectToLong<? super K, ? super V> transformer,
5807 >             ToLongBiFunction<? super K, ? super V> transformer,
5808               long basis,
5809 <             LongByLongToLong reducer) {
5810 <            super(m, p, b); this.nextRight = nextRight;
5809 >             LongBinaryOperator reducer) {
5810 >            super(p, b, i, f, t); this.nextRight = nextRight;
5811              this.transformer = transformer;
5812              this.basis = basis; this.reducer = reducer;
5813          }
5814 <        @SuppressWarnings("unchecked") public final boolean exec() {
5815 <            final ObjectByObjectToLong<? super K, ? super V> transformer =
5816 <                this.transformer;
5817 <            final LongByLongToLong reducer = this.reducer;
5818 <            if (transformer == null || reducer == null)
5819 <                return abortOnNullFunction();
5820 <            try {
5821 <                final long id = this.basis;
5822 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5823 <                    do {} while (!casPending(c = pending, c+1));
5814 >        public final Long getRawResult() { return result; }
5815 >        public final void compute() {
5816 >            final ToLongBiFunction<? super K, ? super V> transformer;
5817 >            final LongBinaryOperator reducer;
5818 >            if ((transformer = this.transformer) != null &&
5819 >                (reducer = this.reducer) != null) {
5820 >                long r = this.basis;
5821 >                for (int i = baseIndex, f, h; batch > 0 &&
5822 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5823 >                    addToPendingCount(1);
5824                      (rights = new MapReduceMappingsToLongTask<K,V>
5825 <                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
5825 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5826 >                      rights, transformer, r, reducer)).fork();
5827                  }
5828 <                long r = id;
5829 <                Object v;
6253 <                while ((v = advance()) != null)
6254 <                    r = reducer.apply(r, transformer.apply((K)nextKey, (V)v));
5828 >                for (Node<K,V> p; (p = advance()) != null; )
5829 >                    r = reducer.applyAsLong(r, transformer.applyAsLong(p.key, p.val));
5830                  result = r;
5831 <                for (MapReduceMappingsToLongTask<K,V> t = this, s;;) {
5832 <                    int c; BulkTask<K,V,?> par;
5833 <                    if ((c = t.pending) == 0) {
5834 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5835 <                            t.result = reducer.apply(t.result, s.result);
5836 <                        }
5837 <                        if ((par = t.parent) == null ||
5838 <                            !(par instanceof MapReduceMappingsToLongTask)) {
6264 <                            t.quietlyComplete();
6265 <                            break;
6266 <                        }
6267 <                        t = (MapReduceMappingsToLongTask<K,V>)par;
5831 >                CountedCompleter<?> c;
5832 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5833 >                    @SuppressWarnings("unchecked") MapReduceMappingsToLongTask<K,V>
5834 >                        t = (MapReduceMappingsToLongTask<K,V>)c,
5835 >                        s = t.rights;
5836 >                    while (s != null) {
5837 >                        t.result = reducer.applyAsLong(t.result, s.result);
5838 >                        s = t.rights = s.nextRight;
5839                      }
6269                    else if (t.casPending(c, c - 1))
6270                        break;
5840                  }
6272            } catch (Throwable ex) {
6273                return tryCompleteComputation(ex);
5841              }
6275            return false;
5842          }
6277        public final Long getRawResult() { return result; }
5843      }
5844  
5845 <    @SuppressWarnings("serial") static final class MapReduceKeysToIntTask<K,V>
5845 >    @SuppressWarnings("serial")
5846 >    static final class MapReduceKeysToIntTask<K,V>
5847          extends BulkTask<K,V,Integer> {
5848 <        final ObjectToInt<? super K> transformer;
5849 <        final IntByIntToInt reducer;
5848 >        final ToIntFunction<? super K> transformer;
5849 >        final IntBinaryOperator reducer;
5850          final int basis;
5851          int result;
5852          MapReduceKeysToIntTask<K,V> rights, nextRight;
5853          MapReduceKeysToIntTask
5854 <            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5854 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5855               MapReduceKeysToIntTask<K,V> nextRight,
5856 <             ObjectToInt<? super K> transformer,
5856 >             ToIntFunction<? super K> transformer,
5857               int basis,
5858 <             IntByIntToInt reducer) {
5859 <            super(m, p, b); this.nextRight = nextRight;
5858 >             IntBinaryOperator reducer) {
5859 >            super(p, b, i, f, t); this.nextRight = nextRight;
5860              this.transformer = transformer;
5861              this.basis = basis; this.reducer = reducer;
5862          }
5863 <        @SuppressWarnings("unchecked") public final boolean exec() {
5864 <            final ObjectToInt<? super K> transformer =
5865 <                this.transformer;
5866 <            final IntByIntToInt reducer = this.reducer;
5867 <            if (transformer == null || reducer == null)
5868 <                return abortOnNullFunction();
5869 <            try {
5870 <                final int id = this.basis;
5871 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5872 <                    do {} while (!casPending(c = pending, c+1));
5863 >        public final Integer getRawResult() { return result; }
5864 >        public final void compute() {
5865 >            final ToIntFunction<? super K> transformer;
5866 >            final IntBinaryOperator reducer;
5867 >            if ((transformer = this.transformer) != null &&
5868 >                (reducer = this.reducer) != null) {
5869 >                int r = this.basis;
5870 >                for (int i = baseIndex, f, h; batch > 0 &&
5871 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5872 >                    addToPendingCount(1);
5873                      (rights = new MapReduceKeysToIntTask<K,V>
5874 <                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
5874 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5875 >                      rights, transformer, r, reducer)).fork();
5876                  }
5877 <                int r = id;
5878 <                while (advance() != null)
6312 <                    r = reducer.apply(r, transformer.apply((K)nextKey));
5877 >                for (Node<K,V> p; (p = advance()) != null; )
5878 >                    r = reducer.applyAsInt(r, transformer.applyAsInt(p.key));
5879                  result = r;
5880 <                for (MapReduceKeysToIntTask<K,V> t = this, s;;) {
5881 <                    int c; BulkTask<K,V,?> par;
5882 <                    if ((c = t.pending) == 0) {
5883 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5884 <                            t.result = reducer.apply(t.result, s.result);
5885 <                        }
5886 <                        if ((par = t.parent) == null ||
5887 <                            !(par instanceof MapReduceKeysToIntTask)) {
6322 <                            t.quietlyComplete();
6323 <                            break;
6324 <                        }
6325 <                        t = (MapReduceKeysToIntTask<K,V>)par;
5880 >                CountedCompleter<?> c;
5881 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5882 >                    @SuppressWarnings("unchecked") MapReduceKeysToIntTask<K,V>
5883 >                        t = (MapReduceKeysToIntTask<K,V>)c,
5884 >                        s = t.rights;
5885 >                    while (s != null) {
5886 >                        t.result = reducer.applyAsInt(t.result, s.result);
5887 >                        s = t.rights = s.nextRight;
5888                      }
6327                    else if (t.casPending(c, c - 1))
6328                        break;
5889                  }
6330            } catch (Throwable ex) {
6331                return tryCompleteComputation(ex);
5890              }
6333            return false;
5891          }
6335        public final Integer getRawResult() { return result; }
5892      }
5893  
5894 <    @SuppressWarnings("serial") static final class MapReduceValuesToIntTask<K,V>
5894 >    @SuppressWarnings("serial")
5895 >    static final class MapReduceValuesToIntTask<K,V>
5896          extends BulkTask<K,V,Integer> {
5897 <        final ObjectToInt<? super V> transformer;
5898 <        final IntByIntToInt reducer;
5897 >        final ToIntFunction<? super V> transformer;
5898 >        final IntBinaryOperator reducer;
5899          final int basis;
5900          int result;
5901          MapReduceValuesToIntTask<K,V> rights, nextRight;
5902          MapReduceValuesToIntTask
5903 <            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5903 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5904               MapReduceValuesToIntTask<K,V> nextRight,
5905 <             ObjectToInt<? super V> transformer,
5905 >             ToIntFunction<? super V> transformer,
5906               int basis,
5907 <             IntByIntToInt reducer) {
5908 <            super(m, p, b); this.nextRight = nextRight;
5907 >             IntBinaryOperator reducer) {
5908 >            super(p, b, i, f, t); this.nextRight = nextRight;
5909              this.transformer = transformer;
5910              this.basis = basis; this.reducer = reducer;
5911          }
5912 <        @SuppressWarnings("unchecked") public final boolean exec() {
5913 <            final ObjectToInt<? super V> transformer =
5914 <                this.transformer;
5915 <            final IntByIntToInt reducer = this.reducer;
5916 <            if (transformer == null || reducer == null)
5917 <                return abortOnNullFunction();
5918 <            try {
5919 <                final int id = this.basis;
5920 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5921 <                    do {} while (!casPending(c = pending, c+1));
5912 >        public final Integer getRawResult() { return result; }
5913 >        public final void compute() {
5914 >            final ToIntFunction<? super V> transformer;
5915 >            final IntBinaryOperator reducer;
5916 >            if ((transformer = this.transformer) != null &&
5917 >                (reducer = this.reducer) != null) {
5918 >                int r = this.basis;
5919 >                for (int i = baseIndex, f, h; batch > 0 &&
5920 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5921 >                    addToPendingCount(1);
5922                      (rights = new MapReduceValuesToIntTask<K,V>
5923 <                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
5923 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5924 >                      rights, transformer, r, reducer)).fork();
5925                  }
5926 <                int r = id;
5927 <                Object v;
6370 <                while ((v = advance()) != null)
6371 <                    r = reducer.apply(r, transformer.apply((V)v));
5926 >                for (Node<K,V> p; (p = advance()) != null; )
5927 >                    r = reducer.applyAsInt(r, transformer.applyAsInt(p.val));
5928                  result = r;
5929 <                for (MapReduceValuesToIntTask<K,V> t = this, s;;) {
5930 <                    int c; BulkTask<K,V,?> par;
5931 <                    if ((c = t.pending) == 0) {
5932 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5933 <                            t.result = reducer.apply(t.result, s.result);
5934 <                        }
5935 <                        if ((par = t.parent) == null ||
5936 <                            !(par instanceof MapReduceValuesToIntTask)) {
6381 <                            t.quietlyComplete();
6382 <                            break;
6383 <                        }
6384 <                        t = (MapReduceValuesToIntTask<K,V>)par;
5929 >                CountedCompleter<?> c;
5930 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5931 >                    @SuppressWarnings("unchecked") MapReduceValuesToIntTask<K,V>
5932 >                        t = (MapReduceValuesToIntTask<K,V>)c,
5933 >                        s = t.rights;
5934 >                    while (s != null) {
5935 >                        t.result = reducer.applyAsInt(t.result, s.result);
5936 >                        s = t.rights = s.nextRight;
5937                      }
6386                    else if (t.casPending(c, c - 1))
6387                        break;
5938                  }
6389            } catch (Throwable ex) {
6390                return tryCompleteComputation(ex);
5939              }
6392            return false;
5940          }
6394        public final Integer getRawResult() { return result; }
5941      }
5942  
5943 <    @SuppressWarnings("serial") static final class MapReduceEntriesToIntTask<K,V>
5943 >    @SuppressWarnings("serial")
5944 >    static final class MapReduceEntriesToIntTask<K,V>
5945          extends BulkTask<K,V,Integer> {
5946 <        final ObjectToInt<Map.Entry<K,V>> transformer;
5947 <        final IntByIntToInt reducer;
5946 >        final ToIntFunction<Map.Entry<K,V>> transformer;
5947 >        final IntBinaryOperator reducer;
5948          final int basis;
5949          int result;
5950          MapReduceEntriesToIntTask<K,V> rights, nextRight;
5951          MapReduceEntriesToIntTask
5952 <            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
5952 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5953               MapReduceEntriesToIntTask<K,V> nextRight,
5954 <             ObjectToInt<Map.Entry<K,V>> transformer,
5954 >             ToIntFunction<Map.Entry<K,V>> transformer,
5955               int basis,
5956 <             IntByIntToInt reducer) {
5957 <            super(m, p, b); this.nextRight = nextRight;
5956 >             IntBinaryOperator reducer) {
5957 >            super(p, b, i, f, t); this.nextRight = nextRight;
5958              this.transformer = transformer;
5959              this.basis = basis; this.reducer = reducer;
5960          }
5961 <        @SuppressWarnings("unchecked") public final boolean exec() {
5962 <            final ObjectToInt<Map.Entry<K,V>> transformer =
5963 <                this.transformer;
5964 <            final IntByIntToInt reducer = this.reducer;
5965 <            if (transformer == null || reducer == null)
5966 <                return abortOnNullFunction();
5967 <            try {
5968 <                final int id = this.basis;
5969 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5970 <                    do {} while (!casPending(c = pending, c+1));
5961 >        public final Integer getRawResult() { return result; }
5962 >        public final void compute() {
5963 >            final ToIntFunction<Map.Entry<K,V>> transformer;
5964 >            final IntBinaryOperator reducer;
5965 >            if ((transformer = this.transformer) != null &&
5966 >                (reducer = this.reducer) != null) {
5967 >                int r = this.basis;
5968 >                for (int i = baseIndex, f, h; batch > 0 &&
5969 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5970 >                    addToPendingCount(1);
5971                      (rights = new MapReduceEntriesToIntTask<K,V>
5972 <                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
5972 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5973 >                      rights, transformer, r, reducer)).fork();
5974                  }
5975 <                int r = id;
5976 <                Object v;
6429 <                while ((v = advance()) != null)
6430 <                    r = reducer.apply(r, transformer.apply(entryFor((K)nextKey, (V)v)));
5975 >                for (Node<K,V> p; (p = advance()) != null; )
5976 >                    r = reducer.applyAsInt(r, transformer.applyAsInt(p));
5977                  result = r;
5978 <                for (MapReduceEntriesToIntTask<K,V> t = this, s;;) {
5979 <                    int c; BulkTask<K,V,?> par;
5980 <                    if ((c = t.pending) == 0) {
5981 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5982 <                            t.result = reducer.apply(t.result, s.result);
5983 <                        }
5984 <                        if ((par = t.parent) == null ||
5985 <                            !(par instanceof MapReduceEntriesToIntTask)) {
6440 <                            t.quietlyComplete();
6441 <                            break;
6442 <                        }
6443 <                        t = (MapReduceEntriesToIntTask<K,V>)par;
5978 >                CountedCompleter<?> c;
5979 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5980 >                    @SuppressWarnings("unchecked") MapReduceEntriesToIntTask<K,V>
5981 >                        t = (MapReduceEntriesToIntTask<K,V>)c,
5982 >                        s = t.rights;
5983 >                    while (s != null) {
5984 >                        t.result = reducer.applyAsInt(t.result, s.result);
5985 >                        s = t.rights = s.nextRight;
5986                      }
6445                    else if (t.casPending(c, c - 1))
6446                        break;
5987                  }
6448            } catch (Throwable ex) {
6449                return tryCompleteComputation(ex);
5988              }
6451            return false;
5989          }
6453        public final Integer getRawResult() { return result; }
5990      }
5991  
5992 <    @SuppressWarnings("serial") static final class MapReduceMappingsToIntTask<K,V>
5992 >    @SuppressWarnings("serial")
5993 >    static final class MapReduceMappingsToIntTask<K,V>
5994          extends BulkTask<K,V,Integer> {
5995 <        final ObjectByObjectToInt<? super K, ? super V> transformer;
5996 <        final IntByIntToInt reducer;
5995 >        final ToIntBiFunction<? super K, ? super V> transformer;
5996 >        final IntBinaryOperator reducer;
5997          final int basis;
5998          int result;
5999          MapReduceMappingsToIntTask<K,V> rights, nextRight;
6000          MapReduceMappingsToIntTask
6001 <            (ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b,
6002 <             MapReduceMappingsToIntTask<K,V> rights,
6003 <             ObjectByObjectToInt<? super K, ? super V> transformer,
6001 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
6002 >             MapReduceMappingsToIntTask<K,V> nextRight,
6003 >             ToIntBiFunction<? super K, ? super V> transformer,
6004               int basis,
6005 <             IntByIntToInt reducer) {
6006 <            super(m, p, b); this.nextRight = nextRight;
6005 >             IntBinaryOperator reducer) {
6006 >            super(p, b, i, f, t); this.nextRight = nextRight;
6007              this.transformer = transformer;
6008              this.basis = basis; this.reducer = reducer;
6009          }
6010 <        @SuppressWarnings("unchecked") public final boolean exec() {
6011 <            final ObjectByObjectToInt<? super K, ? super V> transformer =
6012 <                this.transformer;
6013 <            final IntByIntToInt reducer = this.reducer;
6014 <            if (transformer == null || reducer == null)
6015 <                return abortOnNullFunction();
6016 <            try {
6017 <                final int id = this.basis;
6018 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6019 <                    do {} while (!casPending(c = pending, c+1));
6010 >        public final Integer getRawResult() { return result; }
6011 >        public final void compute() {
6012 >            final ToIntBiFunction<? super K, ? super V> transformer;
6013 >            final IntBinaryOperator reducer;
6014 >            if ((transformer = this.transformer) != null &&
6015 >                (reducer = this.reducer) != null) {
6016 >                int r = this.basis;
6017 >                for (int i = baseIndex, f, h; batch > 0 &&
6018 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
6019 >                    addToPendingCount(1);
6020                      (rights = new MapReduceMappingsToIntTask<K,V>
6021 <                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6021 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
6022 >                      rights, transformer, r, reducer)).fork();
6023                  }
6024 <                int r = id;
6025 <                Object v;
6488 <                while ((v = advance()) != null)
6489 <                    r = reducer.apply(r, transformer.apply((K)nextKey, (V)v));
6024 >                for (Node<K,V> p; (p = advance()) != null; )
6025 >                    r = reducer.applyAsInt(r, transformer.applyAsInt(p.key, p.val));
6026                  result = r;
6027 <                for (MapReduceMappingsToIntTask<K,V> t = this, s;;) {
6028 <                    int c; BulkTask<K,V,?> par;
6029 <                    if ((c = t.pending) == 0) {
6030 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6031 <                            t.result = reducer.apply(t.result, s.result);
6032 <                        }
6033 <                        if ((par = t.parent) == null ||
6034 <                            !(par instanceof MapReduceMappingsToIntTask)) {
6499 <                            t.quietlyComplete();
6500 <                            break;
6501 <                        }
6502 <                        t = (MapReduceMappingsToIntTask<K,V>)par;
6027 >                CountedCompleter<?> c;
6028 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6029 >                    @SuppressWarnings("unchecked") MapReduceMappingsToIntTask<K,V>
6030 >                        t = (MapReduceMappingsToIntTask<K,V>)c,
6031 >                        s = t.rights;
6032 >                    while (s != null) {
6033 >                        t.result = reducer.applyAsInt(t.result, s.result);
6034 >                        s = t.rights = s.nextRight;
6035                      }
6504                    else if (t.casPending(c, c - 1))
6505                        break;
6036                  }
6507            } catch (Throwable ex) {
6508                return tryCompleteComputation(ex);
6037              }
6510            return false;
6038          }
6512        public final Integer getRawResult() { return result; }
6039      }
6040  
6515
6041      // Unsafe mechanics
6042 <    private static final sun.misc.Unsafe UNSAFE;
6043 <    private static final long counterOffset;
6044 <    private static final long sizeCtlOffset;
6042 >    private static final sun.misc.Unsafe U;
6043 >    private static final long SIZECTL;
6044 >    private static final long TRANSFERINDEX;
6045 >    private static final long TRANSFERORIGIN;
6046 >    private static final long BASECOUNT;
6047 >    private static final long CELLSBUSY;
6048 >    private static final long CELLVALUE;
6049      private static final long ABASE;
6050      private static final int ASHIFT;
6051  
6052      static {
6524        int ss;
6053          try {
6054 <            UNSAFE = sun.misc.Unsafe.getUnsafe();
6054 >            U = sun.misc.Unsafe.getUnsafe();
6055              Class<?> k = ConcurrentHashMap.class;
6056 <            counterOffset = UNSAFE.objectFieldOffset
6529 <                (k.getDeclaredField("counter"));
6530 <            sizeCtlOffset = UNSAFE.objectFieldOffset
6056 >            SIZECTL = U.objectFieldOffset
6057                  (k.getDeclaredField("sizeCtl"));
6058 <            Class<?> sc = Node[].class;
6059 <            ABASE = UNSAFE.arrayBaseOffset(sc);
6060 <            ss = UNSAFE.arrayIndexScale(sc);
6058 >            TRANSFERINDEX = U.objectFieldOffset
6059 >                (k.getDeclaredField("transferIndex"));
6060 >            TRANSFERORIGIN = U.objectFieldOffset
6061 >                (k.getDeclaredField("transferOrigin"));
6062 >            BASECOUNT = U.objectFieldOffset
6063 >                (k.getDeclaredField("baseCount"));
6064 >            CELLSBUSY = U.objectFieldOffset
6065 >                (k.getDeclaredField("cellsBusy"));
6066 >            Class<?> ck = CounterCell.class;
6067 >            CELLVALUE = U.objectFieldOffset
6068 >                (ck.getDeclaredField("value"));
6069 >            Class<?> ak = Node[].class;
6070 >            ABASE = U.arrayBaseOffset(ak);
6071 >            int scale = U.arrayIndexScale(ak);
6072 >            if ((scale & (scale - 1)) != 0)
6073 >                throw new Error("data type scale not a power of two");
6074 >            ASHIFT = 31 - Integer.numberOfLeadingZeros(scale);
6075          } catch (Exception e) {
6076              throw new Error(e);
6077          }
6538        if ((ss & (ss-1)) != 0)
6539            throw new Error("data type scale not a power of two");
6540        ASHIFT = 31 - Integer.numberOfLeadingZeros(ss);
6078      }
6079   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines