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.112 by jsr166, Fri Jun 3 02:28:05 2011 UTC vs.
Revision 1.126 by dl, Tue Aug 14 13:16:54 2012 UTC

# Line 5 | Line 5
5   */
6  
7   package java.util.concurrent;
8 < import java.util.concurrent.locks.*;
9 < import java.util.*;
8 > import java.util.concurrent.atomic.LongAdder;
9 > import java.util.concurrent.ForkJoinPool;
10 > import java.util.concurrent.ForkJoinTask;
11 >
12 > import java.util.Comparator;
13 > import java.util.Arrays;
14 > import java.util.Map;
15 > import java.util.Set;
16 > import java.util.Collection;
17 > import java.util.AbstractMap;
18 > import java.util.AbstractSet;
19 > import java.util.AbstractCollection;
20 > import java.util.Hashtable;
21 > import java.util.HashMap;
22 > import java.util.Iterator;
23 > import java.util.Enumeration;
24 > import java.util.ConcurrentModificationException;
25 > import java.util.NoSuchElementException;
26 > import java.util.concurrent.ConcurrentMap;
27 > import java.util.concurrent.ThreadLocalRandom;
28 > import java.util.concurrent.locks.LockSupport;
29 > import java.util.concurrent.locks.AbstractQueuedSynchronizer;
30 > import java.util.concurrent.atomic.AtomicReference;
31 >
32   import java.io.Serializable;
11 import java.io.IOException;
12 import java.io.ObjectInputStream;
13 import java.io.ObjectOutputStream;
33  
34   /**
35   * A hash table supporting full concurrency of retrievals and
36 < * adjustable expected concurrency for updates. This class obeys the
36 > * high expected concurrency for updates. This class obeys the
37   * same functional specification as {@link java.util.Hashtable}, and
38   * includes versions of methods corresponding to each method of
39 < * <tt>Hashtable</tt>. However, even though all operations are
39 > * {@code Hashtable}. However, even though all operations are
40   * thread-safe, retrieval operations do <em>not</em> entail locking,
41   * and there is <em>not</em> any support for locking the entire table
42   * in a way that prevents all access.  This class is fully
43 < * interoperable with <tt>Hashtable</tt> in programs that rely on its
43 > * interoperable with {@code Hashtable} in programs that rely on its
44   * thread safety but not on its synchronization details.
45   *
46 < * <p> Retrieval operations (including <tt>get</tt>) generally do not
47 < * block, so may overlap with update operations (including
48 < * <tt>put</tt> and <tt>remove</tt>). Retrievals reflect the results
49 < * of the most recently <em>completed</em> update operations holding
50 < * upon their onset.  For aggregate operations such as <tt>putAll</tt>
51 < * and <tt>clear</tt>, concurrent retrievals may reflect insertion or
52 < * removal of only some entries.  Similarly, Iterators and
53 < * Enumerations return elements reflecting the state of the hash table
54 < * at some point at or since the creation of the iterator/enumeration.
55 < * They do <em>not</em> throw {@link ConcurrentModificationException}.
56 < * However, iterators are designed to be used by only one thread at a time.
46 > * <p> Retrieval operations (including {@code get}) generally do not
47 > * block, so may overlap with update operations (including {@code put}
48 > * and {@code remove}). Retrievals reflect the results of the most
49 > * recently <em>completed</em> update operations holding upon their
50 > * onset. (More formally, an update operation for a given key bears a
51 > * <em>happens-before</em> relation with any (non-null) retrieval for
52 > * that key reporting the updated value.)  For aggregate operations
53 > * such as {@code putAll} and {@code clear}, concurrent retrievals may
54 > * reflect insertion or removal of only some entries.  Similarly,
55 > * Iterators and Enumerations return elements reflecting the state of
56 > * the hash table at some point at or since the creation of the
57 > * iterator/enumeration.  They do <em>not</em> throw {@link
58 > * ConcurrentModificationException}.  However, iterators are designed
59 > * to be used by only one thread at a time.  Bear in mind that the
60 > * results of aggregate status methods including {@code size}, {@code
61 > * isEmpty}, and {@code containsValue} are typically useful only when
62 > * a map is not undergoing concurrent updates in other threads.
63 > * Otherwise the results of these methods reflect transient states
64 > * that may be adequate for monitoring or estimation purposes, but not
65 > * for program control.
66   *
67 < * <p> The allowed concurrency among update operations is guided by
68 < * the optional <tt>concurrencyLevel</tt> constructor argument
69 < * (default <tt>16</tt>), which is used as a hint for internal sizing.  The
70 < * table is internally partitioned to try to permit the indicated
71 < * number of concurrent updates without contention. Because placement
72 < * in hash tables is essentially random, the actual concurrency will
73 < * vary.  Ideally, you should choose a value to accommodate as many
74 < * threads as will ever concurrently modify the table. Using a
75 < * significantly higher value than you need can waste space and time,
76 < * and a significantly lower value can lead to thread contention. But
77 < * overestimates and underestimates within an order of magnitude do
78 < * not usually have much noticeable impact. A value of one is
79 < * appropriate when it is known that only one thread will modify and
80 < * all others will only read. Also, resizing this or any other kind of
81 < * hash table is a relatively slow operation, so, when possible, it is
82 < * a good idea to provide estimates of expected table sizes in
83 < * constructors.
67 > * <p> The table is dynamically expanded when there are too many
68 > * collisions (i.e., keys that have distinct hash codes but fall into
69 > * the same slot modulo the table size), with the expected average
70 > * effect of maintaining roughly two bins per mapping (corresponding
71 > * to a 0.75 load factor threshold for resizing). There may be much
72 > * variance around this average as mappings are added and removed, but
73 > * overall, this maintains a commonly accepted time/space tradeoff for
74 > * hash tables.  However, resizing this or any other kind of hash
75 > * table may be a relatively slow operation. When possible, it is a
76 > * good idea to provide a size estimate as an optional {@code
77 > * initialCapacity} constructor argument. An additional optional
78 > * {@code loadFactor} constructor argument provides a further means of
79 > * customizing initial table capacity by specifying the table density
80 > * to be used in calculating the amount of space to allocate for the
81 > * given number of elements.  Also, for compatibility with previous
82 > * versions of this class, constructors may optionally specify an
83 > * expected {@code concurrencyLevel} as an additional hint for
84 > * internal sizing.  Note that using many keys with exactly the same
85 > * {@code hashCode()} is a sure way to slow down performance of any
86 > * hash table.
87   *
88   * <p>This class and its views and iterators implement all of the
89   * <em>optional</em> methods of the {@link Map} and {@link Iterator}
90   * interfaces.
91   *
92   * <p> Like {@link Hashtable} but unlike {@link HashMap}, this class
93 < * does <em>not</em> allow <tt>null</tt> to be used as a key or value.
93 > * does <em>not</em> allow {@code null} to be used as a key or value.
94   *
95   * <p>This class is a member of the
96   * <a href="{@docRoot}/../technotes/guides/collections/index.html">
97   * Java Collections Framework</a>.
98   *
99 + * <p><em>jsr166e note: This class is a candidate replacement for
100 + * java.util.concurrent.ConcurrentHashMap.  During transition, this
101 + * class declares and uses nested functional interfaces with different
102 + * names but the same forms as those expected for JDK8.<em>
103 + *
104   * @since 1.5
105   * @author Doug Lea
106   * @param <K> the type of keys maintained by this map
107   * @param <V> the type of mapped values
108   */
109 < public class ConcurrentHashMap<K, V> extends AbstractMap<K, V>
110 <        implements ConcurrentMap<K, V>, Serializable {
109 > public class ConcurrentHashMap<K, V>
110 >    implements ConcurrentMap<K, V>, Serializable {
111      private static final long serialVersionUID = 7249069246763182397L;
112  
113 +    /**
114 +     * A partitionable iterator. A Spliterator can be traversed
115 +     * directly, but can also be partitioned (before traversal) by
116 +     * creating another Spliterator that covers a non-overlapping
117 +     * portion of the elements, and so may be amenable to parallel
118 +     * execution.
119 +     *
120 +     * <p> This interface exports a subset of expected JDK8
121 +     * functionality.
122 +     *
123 +     * <p>Sample usage: Here is one (of the several) ways to compute
124 +     * the sum of the values held in a map using the ForkJoin
125 +     * framework. As illustrated here, Spliterators are well suited to
126 +     * designs in which a task repeatedly splits off half its work
127 +     * into forked subtasks until small enough to process directly,
128 +     * and then joins these subtasks. Variants of this style can also
129 +     * be used in completion-based designs.
130 +     *
131 +     * <pre>
132 +     * {@code ConcurrentHashMap<String, Long> m = ...
133 +     * // split as if have 8 * parallelism, for load balance
134 +     * int n = m.size();
135 +     * int p = aForkJoinPool.getParallelism() * 8;
136 +     * int split = (n < p)? n : p;
137 +     * long sum = aForkJoinPool.invoke(new SumValues(m.valueSpliterator(), split, null));
138 +     * // ...
139 +     * static class SumValues extends RecursiveTask<Long> {
140 +     *   final Spliterator<Long> s;
141 +     *   final int split;             // split while > 1
142 +     *   final SumValues nextJoin;    // records forked subtasks to join
143 +     *   SumValues(Spliterator<Long> s, int depth, SumValues nextJoin) {
144 +     *     this.s = s; this.depth = depth; this.nextJoin = nextJoin;
145 +     *   }
146 +     *   public Long compute() {
147 +     *     long sum = 0;
148 +     *     SumValues subtasks = null; // fork subtasks
149 +     *     for (int s = split >>> 1; s > 0; s >>>= 1)
150 +     *       (subtasks = new SumValues(s.split(), s, subtasks)).fork();
151 +     *     while (s.hasNext())        // directly process remaining elements
152 +     *       sum += s.next();
153 +     *     for (SumValues t = subtasks; t != null; t = t.nextJoin)
154 +     *       sum += t.join();         // collect subtask results
155 +     *     return sum;
156 +     *   }
157 +     * }
158 +     * }</pre>
159 +     */
160 +    public static interface Spliterator<T> extends Iterator<T> {
161 +        /**
162 +         * Returns a Spliterator covering approximately half of the
163 +         * elements, guaranteed not to overlap with those subsequently
164 +         * returned by this Spliterator.  After invoking this method,
165 +         * the current Spliterator will <em>not</em> produce any of
166 +         * the elements of the returned Spliterator, but the two
167 +         * Spliterators together will produce all of the elements that
168 +         * would have been produced by this Spliterator had this
169 +         * method not been called. The exact number of elements
170 +         * produced by the returned Spliterator is not guaranteed, and
171 +         * may be zero (i.e., with {@code hasNext()} reporting {@code
172 +         * false}) if this Spliterator cannot be further split.
173 +         *
174 +         * @return a Spliterator covering approximately half of the
175 +         * elements
176 +         * @throws IllegalStateException if this Spliterator has
177 +         * already commenced traversing elements
178 +         */
179 +        Spliterator<T> split();
180 +    }
181 +
182      /*
183 <     * The basic strategy is to subdivide the table among Segments,
184 <     * each of which itself is a concurrently readable hash table.  To
185 <     * reduce footprint, all but one segments are constructed only
186 <     * when first needed (see ensureSegment). To maintain visibility
187 <     * in the presence of lazy construction, accesses to segments as
188 <     * well as elements of segment's table must use volatile access,
189 <     * which is done via Unsafe within methods segmentAt etc
190 <     * below. These provide the functionality of AtomicReferenceArrays
191 <     * but reduce the levels of indirection. Additionally,
192 <     * volatile-writes of table elements and entry "next" fields
193 <     * within locked operations use the cheaper "lazySet" forms of
194 <     * writes (via putOrderedObject) because these writes are always
195 <     * followed by lock releases that maintain sequential consistency
196 <     * of table updates.
197 <     *
198 <     * Historical note: The previous version of this class relied
199 <     * heavily on "final" fields, which avoided some volatile reads at
200 <     * the expense of a large initial footprint.  Some remnants of
201 <     * that design (including forced construction of segment 0) exist
202 <     * to ensure serialization compatibility.
183 >     * Overview:
184 >     *
185 >     * The primary design goal of this hash table is to maintain
186 >     * concurrent readability (typically method get(), but also
187 >     * iterators and related methods) while minimizing update
188 >     * contention. Secondary goals are to keep space consumption about
189 >     * the same or better than java.util.HashMap, and to support high
190 >     * initial insertion rates on an empty table by many threads.
191 >     *
192 >     * Each key-value mapping is held in a Node.  Because Node fields
193 >     * can contain special values, they are defined using plain Object
194 >     * types. Similarly in turn, all internal methods that use them
195 >     * work off Object types. And similarly, so do the internal
196 >     * methods of auxiliary iterator and view classes.  All public
197 >     * generic typed methods relay in/out of these internal methods,
198 >     * supplying null-checks and casts as needed. This also allows
199 >     * many of the public methods to be factored into a smaller number
200 >     * of internal methods (although sadly not so for the five
201 >     * variants of put-related operations). The validation-based
202 >     * approach explained below leads to a lot of code sprawl because
203 >     * retry-control precludes factoring into smaller methods.
204 >     *
205 >     * The table is lazily initialized to a power-of-two size upon the
206 >     * first insertion.  Each bin in the table normally contains a
207 >     * list of Nodes (most often, the list has only zero or one Node).
208 >     * Table accesses require volatile/atomic reads, writes, and
209 >     * CASes.  Because there is no other way to arrange this without
210 >     * adding further indirections, we use intrinsics
211 >     * (sun.misc.Unsafe) operations.  The lists of nodes within bins
212 >     * are always accurately traversable under volatile reads, so long
213 >     * as lookups check hash code and non-nullness of value before
214 >     * checking key equality.
215 >     *
216 >     * We use the top two bits of Node hash fields for control
217 >     * purposes -- they are available anyway because of addressing
218 >     * constraints.  As explained further below, these top bits are
219 >     * used as follows:
220 >     *  00 - Normal
221 >     *  01 - Locked
222 >     *  11 - Locked and may have a thread waiting for lock
223 >     *  10 - Node is a forwarding node
224 >     *
225 >     * The lower 30 bits of each Node's hash field contain a
226 >     * transformation of the key's hash code, except for forwarding
227 >     * nodes, for which the lower bits are zero (and so always have
228 >     * hash field == MOVED).
229 >     *
230 >     * Insertion (via put or its variants) of the first node in an
231 >     * empty bin is performed by just CASing it to the bin.  This is
232 >     * by far the most common case for put operations under most
233 >     * key/hash distributions.  Other update operations (insert,
234 >     * delete, and replace) require locks.  We do not want to waste
235 >     * the space required to associate a distinct lock object with
236 >     * each bin, so instead use the first node of a bin list itself as
237 >     * a lock. Blocking support for these locks relies on the builtin
238 >     * "synchronized" monitors.  However, we also need a tryLock
239 >     * construction, so we overlay these by using bits of the Node
240 >     * hash field for lock control (see above), and so normally use
241 >     * builtin monitors only for blocking and signalling using
242 >     * wait/notifyAll constructions. See Node.tryAwaitLock.
243 >     *
244 >     * Using the first node of a list as a lock does not by itself
245 >     * suffice though: When a node is locked, any update must first
246 >     * validate that it is still the first node after locking it, and
247 >     * retry if not. Because new nodes are always appended to lists,
248 >     * once a node is first in a bin, it remains first until deleted
249 >     * or the bin becomes invalidated (upon resizing).  However,
250 >     * operations that only conditionally update may inspect nodes
251 >     * until the point of update. This is a converse of sorts to the
252 >     * lazy locking technique described by Herlihy & Shavit.
253 >     *
254 >     * The main disadvantage of per-bin locks is that other update
255 >     * operations on other nodes in a bin list protected by the same
256 >     * lock can stall, for example when user equals() or mapping
257 >     * functions take a long time.  However, statistically, under
258 >     * random hash codes, this is not a common problem.  Ideally, the
259 >     * frequency of nodes in bins follows a Poisson distribution
260 >     * (http://en.wikipedia.org/wiki/Poisson_distribution) with a
261 >     * parameter of about 0.5 on average, given the resizing threshold
262 >     * of 0.75, although with a large variance because of resizing
263 >     * granularity. Ignoring variance, the expected occurrences of
264 >     * list size k are (exp(-0.5) * pow(0.5, k) / factorial(k)). The
265 >     * first values are:
266 >     *
267 >     * 0:    0.60653066
268 >     * 1:    0.30326533
269 >     * 2:    0.07581633
270 >     * 3:    0.01263606
271 >     * 4:    0.00157952
272 >     * 5:    0.00015795
273 >     * 6:    0.00001316
274 >     * 7:    0.00000094
275 >     * 8:    0.00000006
276 >     * more: less than 1 in ten million
277 >     *
278 >     * Lock contention probability for two threads accessing distinct
279 >     * elements is roughly 1 / (8 * #elements) under random hashes.
280 >     *
281 >     * Actual hash code distributions encountered in practice
282 >     * sometimes deviate significantly from uniform randomness.  This
283 >     * includes the case when N > (1<<30), so some keys MUST collide.
284 >     * Similarly for dumb or hostile usages in which multiple keys are
285 >     * designed to have identical hash codes. Also, although we guard
286 >     * against the worst effects of this (see method spread), sets of
287 >     * hashes may differ only in bits that do not impact their bin
288 >     * index for a given power-of-two mask.  So we use a secondary
289 >     * strategy that applies when the number of nodes in a bin exceeds
290 >     * a threshold, and at least one of the keys implements
291 >     * Comparable.  These TreeBins use a balanced tree to hold nodes
292 >     * (a specialized form of red-black trees), bounding search time
293 >     * to O(log N).  Each search step in a TreeBin is around twice as
294 >     * slow as in a regular list, but given that N cannot exceed
295 >     * (1<<64) (before running out of addresses) this bounds search
296 >     * steps, lock hold times, etc, to reasonable constants (roughly
297 >     * 100 nodes inspected per operation worst case) so long as keys
298 >     * are Comparable (which is very common -- String, Long, etc).
299 >     * TreeBin nodes (TreeNodes) also maintain the same "next"
300 >     * traversal pointers as regular nodes, so can be traversed in
301 >     * iterators in the same way.
302 >     *
303 >     * The table is resized when occupancy exceeds a percentage
304 >     * threshold (nominally, 0.75, but see below).  Only a single
305 >     * thread performs the resize (using field "sizeCtl", to arrange
306 >     * exclusion), but the table otherwise remains usable for reads
307 >     * and updates. Resizing proceeds by transferring bins, one by
308 >     * one, from the table to the next table.  Because we are using
309 >     * power-of-two expansion, the elements from each bin must either
310 >     * stay at same index, or move with a power of two offset. We
311 >     * eliminate unnecessary node creation by catching cases where old
312 >     * nodes can be reused because their next fields won't change.  On
313 >     * average, only about one-sixth of them need cloning when a table
314 >     * doubles. The nodes they replace will be garbage collectable as
315 >     * soon as they are no longer referenced by any reader thread that
316 >     * may be in the midst of concurrently traversing table.  Upon
317 >     * transfer, the old table bin contains only a special forwarding
318 >     * node (with hash field "MOVED") that contains the next table as
319 >     * its key. On encountering a forwarding node, access and update
320 >     * operations restart, using the new table.
321 >     *
322 >     * Each bin transfer requires its bin lock. However, unlike other
323 >     * cases, a transfer can skip a bin if it fails to acquire its
324 >     * lock, and revisit it later (unless it is a TreeBin). Method
325 >     * rebuild maintains a buffer of TRANSFER_BUFFER_SIZE bins that
326 >     * have been skipped because of failure to acquire a lock, and
327 >     * blocks only if none are available (i.e., only very rarely).
328 >     * The transfer operation must also ensure that all accessible
329 >     * bins in both the old and new table are usable by any traversal.
330 >     * When there are no lock acquisition failures, this is arranged
331 >     * simply by proceeding from the last bin (table.length - 1) up
332 >     * towards the first.  Upon seeing a forwarding node, traversals
333 >     * (see class Iter) arrange to move to the new table
334 >     * without revisiting nodes.  However, when any node is skipped
335 >     * during a transfer, all earlier table bins may have become
336 >     * visible, so are initialized with a reverse-forwarding node back
337 >     * to the old table until the new ones are established. (This
338 >     * sometimes requires transiently locking a forwarding node, which
339 >     * is possible under the above encoding.) These more expensive
340 >     * mechanics trigger only when necessary.
341 >     *
342 >     * The traversal scheme also applies to partial traversals of
343 >     * ranges of bins (via an alternate Traverser constructor)
344 >     * to support partitioned aggregate operations.  Also, read-only
345 >     * operations give up if ever forwarded to a null table, which
346 >     * provides support for shutdown-style clearing, which is also not
347 >     * currently implemented.
348 >     *
349 >     * Lazy table initialization minimizes footprint until first use,
350 >     * and also avoids resizings when the first operation is from a
351 >     * putAll, constructor with map argument, or deserialization.
352 >     * These cases attempt to override the initial capacity settings,
353 >     * but harmlessly fail to take effect in cases of races.
354 >     *
355 >     * The element count is maintained using a LongAdder, which avoids
356 >     * contention on updates but can encounter cache thrashing if read
357 >     * too frequently during concurrent access. To avoid reading so
358 >     * often, resizing is attempted either when a bin lock is
359 >     * contended, or upon adding to a bin already holding two or more
360 >     * nodes (checked before adding in the xIfAbsent methods, after
361 >     * adding in others). Under uniform hash distributions, the
362 >     * probability of this occurring at threshold is around 13%,
363 >     * meaning that only about 1 in 8 puts check threshold (and after
364 >     * resizing, many fewer do so). But this approximation has high
365 >     * variance for small table sizes, so we check on any collision
366 >     * for sizes <= 64. The bulk putAll operation further reduces
367 >     * contention by only committing count updates upon these size
368 >     * checks.
369 >     *
370 >     * Maintaining API and serialization compatibility with previous
371 >     * versions of this class introduces several oddities. Mainly: We
372 >     * leave untouched but unused constructor arguments refering to
373 >     * concurrencyLevel. We accept a loadFactor constructor argument,
374 >     * but apply it only to initial table capacity (which is the only
375 >     * time that we can guarantee to honor it.) We also declare an
376 >     * unused "Segment" class that is instantiated in minimal form
377 >     * only when serializing.
378       */
379  
380      /* ---------------- Constants -------------- */
381  
382      /**
383 <     * The default initial capacity for this table,
384 <     * used when not otherwise specified in a constructor.
383 >     * The largest possible table capacity.  This value must be
384 >     * exactly 1<<30 to stay within Java array allocation and indexing
385 >     * bounds for power of two table sizes, and is further required
386 >     * because the top two bits of 32bit hash fields are used for
387 >     * control purposes.
388       */
389 <    static final int DEFAULT_INITIAL_CAPACITY = 16;
389 >    private static final int MAXIMUM_CAPACITY = 1 << 30;
390  
391      /**
392 <     * The default load factor for this table, used when not
393 <     * otherwise specified in a constructor.
392 >     * The default initial table capacity.  Must be a power of 2
393 >     * (i.e., at least 1) and at most MAXIMUM_CAPACITY.
394       */
395 <    static final float DEFAULT_LOAD_FACTOR = 0.75f;
395 >    private static final int DEFAULT_CAPACITY = 16;
396  
397      /**
398 <     * The default concurrency level for this table, used when not
399 <     * otherwise specified in a constructor.
398 >     * The largest possible (non-power of two) array size.
399 >     * Needed by toArray and related methods.
400       */
401 <    static final int DEFAULT_CONCURRENCY_LEVEL = 16;
401 >    static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
402  
403      /**
404 <     * The maximum capacity, used if a higher value is implicitly
405 <     * specified by either of the constructors with arguments.  MUST
123 <     * be a power of two <= 1<<30 to ensure that entries are indexable
124 <     * using ints.
404 >     * The default concurrency level for this table. Unused but
405 >     * defined for compatibility with previous versions of this class.
406       */
407 <    static final int MAXIMUM_CAPACITY = 1 << 30;
407 >    private static final int DEFAULT_CONCURRENCY_LEVEL = 16;
408  
409      /**
410 <     * The minimum capacity for per-segment tables.  Must be a power
411 <     * of two, at least two to avoid immediate resizing on next use
412 <     * after lazy construction.
410 >     * The load factor for this table. Overrides of this value in
411 >     * constructors affect only the initial table capacity.  The
412 >     * actual floating point value isn't normally used -- it is
413 >     * simpler to use expressions such as {@code n - (n >>> 2)} for
414 >     * the associated resizing threshold.
415       */
416 <    static final int MIN_SEGMENT_TABLE_CAPACITY = 2;
416 >    private static final float LOAD_FACTOR = 0.75f;
417  
418      /**
419 <     * The maximum number of segments to allow; used to bound
420 <     * constructor arguments. Must be power of two less than 1 << 24.
419 >     * The buffer size for skipped bins during transfers. The
420 >     * value is arbitrary but should be large enough to avoid
421 >     * most locking stalls during resizes.
422       */
423 <    static final int MAX_SEGMENTS = 1 << 16; // slightly conservative
423 >    private static final int TRANSFER_BUFFER_SIZE = 32;
424  
425      /**
426 <     * Number of unsynchronized retries in size and containsValue
427 <     * methods before resorting to locking. This is used to avoid
428 <     * unbounded retries if tables undergo continuous modification
145 <     * which would make it impossible to obtain an accurate result.
426 >     * The bin count threshold for using a tree rather than list for a
427 >     * bin.  The value reflects the approximate break-even point for
428 >     * using tree-based operations.
429       */
430 <    static final int RETRIES_BEFORE_LOCK = 2;
430 >    private static final int TREE_THRESHOLD = 8;
431 >
432 >    /*
433 >     * Encodings for special uses of Node hash fields. See above for
434 >     * explanation.
435 >     */
436 >    static final int MOVED     = 0x80000000; // hash field for forwarding nodes
437 >    static final int LOCKED    = 0x40000000; // set/tested only as a bit
438 >    static final int WAITING   = 0xc0000000; // both bits set/tested together
439 >    static final int HASH_BITS = 0x3fffffff; // usable bits of normal node hash
440  
441      /* ---------------- Fields -------------- */
442  
443      /**
444 <     * Mask value for indexing into segments. The upper bits of a
445 <     * key's hash code are used to choose the segment.
444 >     * The array of bins. Lazily initialized upon first insertion.
445 >     * Size is always a power of two. Accessed directly by iterators.
446       */
447 <    final int segmentMask;
447 >    transient volatile Node[] table;
448  
449      /**
450 <     * Shift value for indexing within segments.
450 >     * The counter maintaining number of elements.
451       */
452 <    final int segmentShift;
452 >    private transient final LongAdder counter;
453  
454      /**
455 <     * The segments, each of which is a specialized hash table.
455 >     * Table initialization and resizing control.  When negative, the
456 >     * table is being initialized or resized. Otherwise, when table is
457 >     * null, holds the initial table size to use upon creation, or 0
458 >     * for default. After initialization, holds the next element count
459 >     * value upon which to resize the table.
460       */
461 <    final Segment<K,V>[] segments;
461 >    private transient volatile int sizeCtl;
462 >
463 >    // views
464 >    private transient KeySet<K,V> keySet;
465 >    private transient Values<K,V> values;
466 >    private transient EntrySet<K,V> entrySet;
467  
468 <    transient Set<K> keySet;
469 <    transient Set<Map.Entry<K,V>> entrySet;
470 <    transient Collection<V> values;
468 >    /** For serialization compatibility. Null unless serialized; see below */
469 >    private Segment<K,V>[] segments;
470 >
471 >    /* ---------------- Table element access -------------- */
472 >
473 >    /*
474 >     * Volatile access methods are used for table elements as well as
475 >     * elements of in-progress next table while resizing.  Uses are
476 >     * null checked by callers, and implicitly bounds-checked, relying
477 >     * on the invariants that tab arrays have non-zero size, and all
478 >     * indices are masked with (tab.length - 1) which is never
479 >     * negative and always less than length. Note that, to be correct
480 >     * wrt arbitrary concurrency errors by users, bounds checks must
481 >     * operate on local variables, which accounts for some odd-looking
482 >     * inline assignments below.
483 >     */
484 >
485 >    static final Node tabAt(Node[] tab, int i) { // used by Iter
486 >        return (Node)UNSAFE.getObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE);
487 >    }
488 >
489 >    private static final boolean casTabAt(Node[] tab, int i, Node c, Node v) {
490 >        return UNSAFE.compareAndSwapObject(tab, ((long)i<<ASHIFT)+ABASE, c, v);
491 >    }
492 >
493 >    private static final void setTabAt(Node[] tab, int i, Node v) {
494 >        UNSAFE.putObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE, v);
495 >    }
496 >
497 >    /* ---------------- Nodes -------------- */
498  
499      /**
500 <     * ConcurrentHashMap list entry. Note that this is never exported
501 <     * out as a user-visible Map.Entry.
500 >     * Key-value entry. Note that this is never exported out as a
501 >     * user-visible Map.Entry (see MapEntry below). Nodes with a hash
502 >     * field of MOVED are special, and do not contain user keys or
503 >     * values.  Otherwise, keys are never null, and null val fields
504 >     * indicate that a node is in the process of being deleted or
505 >     * created. For purposes of read-only access, a key may be read
506 >     * before a val, but can only be used after checking val to be
507 >     * non-null.
508       */
509 <    static final class HashEntry<K,V> {
510 <        final int hash;
511 <        final K key;
512 <        volatile V value;
513 <        volatile HashEntry<K,V> next;
509 >    static class Node {
510 >        volatile int hash;
511 >        final Object key;
512 >        volatile Object val;
513 >        volatile Node next;
514  
515 <        HashEntry(int hash, K key, V value, HashEntry<K,V> next) {
515 >        Node(int hash, Object key, Object val, Node next) {
516              this.hash = hash;
517              this.key = key;
518 <            this.value = value;
518 >            this.val = val;
519              this.next = next;
520          }
521  
522 +        /** CompareAndSet the hash field */
523 +        final boolean casHash(int cmp, int val) {
524 +            return UNSAFE.compareAndSwapInt(this, hashOffset, cmp, val);
525 +        }
526 +
527 +        /** The number of spins before blocking for a lock */
528 +        static final int MAX_SPINS =
529 +            Runtime.getRuntime().availableProcessors() > 1 ? 64 : 1;
530 +
531          /**
532 <         * Sets next field with volatile write semantics.  (See above
533 <         * about use of putOrderedObject.)
532 >         * Spins a while if LOCKED bit set and this node is the first
533 >         * of its bin, and then sets WAITING bits on hash field and
534 >         * blocks (once) if they are still set.  It is OK for this
535 >         * method to return even if lock is not available upon exit,
536 >         * which enables these simple single-wait mechanics.
537 >         *
538 >         * The corresponding signalling operation is performed within
539 >         * callers: Upon detecting that WAITING has been set when
540 >         * unlocking lock (via a failed CAS from non-waiting LOCKED
541 >         * state), unlockers acquire the sync lock and perform a
542 >         * notifyAll.
543           */
544 <        final void setNext(HashEntry<K,V> n) {
545 <            UNSAFE.putOrderedObject(this, nextOffset, n);
544 >        final void tryAwaitLock(Node[] tab, int i) {
545 >            if (tab != null && i >= 0 && i < tab.length) { // bounds check
546 >                int r = ThreadLocalRandom.current().nextInt(); // randomize spins
547 >                int spins = MAX_SPINS, h;
548 >                while (tabAt(tab, i) == this && ((h = hash) & LOCKED) != 0) {
549 >                    if (spins >= 0) {
550 >                        r ^= r << 1; r ^= r >>> 3; r ^= r << 10; // xorshift
551 >                        if (r >= 0 && --spins == 0)
552 >                            Thread.yield();  // yield before block
553 >                    }
554 >                    else if (casHash(h, h | WAITING)) {
555 >                        synchronized (this) {
556 >                            if (tabAt(tab, i) == this &&
557 >                                (hash & WAITING) == WAITING) {
558 >                                try {
559 >                                    wait();
560 >                                } catch (InterruptedException ie) {
561 >                                    Thread.currentThread().interrupt();
562 >                                }
563 >                            }
564 >                            else
565 >                                notifyAll(); // possibly won race vs signaller
566 >                        }
567 >                        break;
568 >                    }
569 >                }
570 >            }
571          }
572  
573 <        // Unsafe mechanics
574 <        static final sun.misc.Unsafe UNSAFE;
575 <        static final long nextOffset;
573 >        // Unsafe mechanics for casHash
574 >        private static final sun.misc.Unsafe UNSAFE;
575 >        private static final long hashOffset;
576 >
577          static {
578              try {
579                  UNSAFE = sun.misc.Unsafe.getUnsafe();
580 <                Class<?> k = HashEntry.class;
581 <                nextOffset = UNSAFE.objectFieldOffset
582 <                    (k.getDeclaredField("next"));
580 >                Class<?> k = Node.class;
581 >                hashOffset = UNSAFE.objectFieldOffset
582 >                    (k.getDeclaredField("hash"));
583              } catch (Exception e) {
584                  throw new Error(e);
585              }
586          }
587      }
588  
589 +    /* ---------------- TreeBins -------------- */
590 +
591      /**
592 <     * Gets the ith element of given table (if nonnull) with volatile
213 <     * read semantics. Note: This is manually integrated into a few
214 <     * performance-sensitive methods to reduce call overhead.
592 >     * Nodes for use in TreeBins
593       */
594 <    @SuppressWarnings("unchecked")
595 <    static final <K,V> HashEntry<K,V> entryAt(HashEntry<K,V>[] tab, int i) {
596 <        return (tab == null) ? null :
597 <            (HashEntry<K,V>) UNSAFE.getObjectVolatile
598 <            (tab, ((long)i << TSHIFT) + TBASE);
599 <    }
594 >    static final class TreeNode extends Node {
595 >        TreeNode parent;  // red-black tree links
596 >        TreeNode left;
597 >        TreeNode right;
598 >        TreeNode prev;    // needed to unlink next upon deletion
599 >        boolean red;
600  
601 <    /**
602 <     * Sets the ith element of given table, with volatile write
603 <     * semantics. (See above about use of putOrderedObject.)
604 <     */
227 <    static final <K,V> void setEntryAt(HashEntry<K,V>[] tab, int i,
228 <                                       HashEntry<K,V> e) {
229 <        UNSAFE.putOrderedObject(tab, ((long)i << TSHIFT) + TBASE, e);
601 >        TreeNode(int hash, Object key, Object val, Node next, TreeNode parent) {
602 >            super(hash, key, val, next);
603 >            this.parent = parent;
604 >        }
605      }
606  
607      /**
608 <     * Applies a supplemental hash function to a given hashCode, which
609 <     * defends against poor quality hash functions.  This is critical
610 <     * because ConcurrentHashMap uses power-of-two length hash tables,
611 <     * that otherwise encounter collisions for hashCodes that do not
612 <     * differ in lower or upper bits.
613 <     */
614 <    private static int hash(int h) {
615 <        // Spread bits to regularize both segment and index locations,
616 <        // using variant of single-word Wang/Jenkins hash.
617 <        h += (h <<  15) ^ 0xffffcd7d;
618 <        h ^= (h >>> 10);
619 <        h += (h <<   3);
620 <        h ^= (h >>>  6);
621 <        h += (h <<   2) + (h << 14);
622 <        return h ^ (h >>> 16);
623 <    }
608 >     * A specialized form of red-black tree for use in bins
609 >     * whose size exceeds a threshold.
610 >     *
611 >     * TreeBins use a special form of comparison for search and
612 >     * related operations (which is the main reason we cannot use
613 >     * existing collections such as TreeMaps). TreeBins contain
614 >     * Comparable elements, but may contain others, as well as
615 >     * elements that are Comparable but not necessarily Comparable<T>
616 >     * for the same T, so we cannot invoke compareTo among them. To
617 >     * handle this, the tree is ordered primarily by hash value, then
618 >     * by getClass().getName() order, and then by Comparator order
619 >     * among elements of the same class.  On lookup at a node, if
620 >     * elements are not comparable or compare as 0, both left and
621 >     * right children may need to be searched in the case of tied hash
622 >     * values. (This corresponds to the full list search that would be
623 >     * necessary if all elements were non-Comparable and had tied
624 >     * hashes.)  The red-black balancing code is updated from
625 >     * pre-jdk-collections
626 >     * (http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java)
627 >     * based in turn on Cormen, Leiserson, and Rivest "Introduction to
628 >     * Algorithms" (CLR).
629 >     *
630 >     * TreeBins also maintain a separate locking discipline than
631 >     * regular bins. Because they are forwarded via special MOVED
632 >     * nodes at bin heads (which can never change once established),
633 >     * we cannot use those nodes as locks. Instead, TreeBin
634 >     * extends AbstractQueuedSynchronizer to support a simple form of
635 >     * read-write lock. For update operations and table validation,
636 >     * the exclusive form of lock behaves in the same way as bin-head
637 >     * locks. However, lookups use shared read-lock mechanics to allow
638 >     * multiple readers in the absence of writers.  Additionally,
639 >     * these lookups do not ever block: While the lock is not
640 >     * available, they proceed along the slow traversal path (via
641 >     * next-pointers) until the lock becomes available or the list is
642 >     * exhausted, whichever comes first. (These cases are not fast,
643 >     * but maximize aggregate expected throughput.)  The AQS mechanics
644 >     * for doing this are straightforward.  The lock state is held as
645 >     * AQS getState().  Read counts are negative; the write count (1)
646 >     * is positive.  There are no signalling preferences among readers
647 >     * and writers. Since we don't need to export full Lock API, we
648 >     * just override the minimal AQS methods and use them directly.
649 >     */
650 >    static final class TreeBin extends AbstractQueuedSynchronizer {
651 >        private static final long serialVersionUID = 2249069246763182397L;
652 >        transient TreeNode root;  // root of tree
653 >        transient TreeNode first; // head of next-pointer list
654  
655 <    /**
656 <     * Segments are specialized versions of hash tables.  This
657 <     * subclasses from ReentrantLock opportunistically, just to
658 <     * simplify some locking and avoid separate construction.
659 <     */
660 <    static final class Segment<K,V> extends ReentrantLock implements Serializable {
661 <        /*
662 <         * Segments maintain a table of entry lists that are always
663 <         * kept in a consistent state, so can be read (via volatile
664 <         * reads of segments and tables) without locking.  This
665 <         * requires replicating nodes when necessary during table
666 <         * resizing, so the old lists can be traversed by readers
667 <         * still using old version of table.
668 <         *
669 <         * This class defines only mutative methods requiring locking.
670 <         * Except as noted, the methods of this class perform the
671 <         * per-segment versions of ConcurrentHashMap methods.  (Other
672 <         * methods are integrated directly into ConcurrentHashMap
673 <         * methods.) These mutative methods use a form of controlled
674 <         * spinning on contention via methods scanAndLock and
675 <         * scanAndLockForPut. These intersperse tryLocks with
676 <         * traversals to locate nodes.  The main benefit is to absorb
677 <         * cache misses (which are very common for hash tables) while
678 <         * obtaining locks so that traversal is faster once
679 <         * acquired. We do not actually use the found nodes since they
680 <         * must be re-acquired under lock anyway to ensure sequential
681 <         * consistency of updates (and in any case may be undetectably
682 <         * stale), but they will normally be much faster to re-locate.
683 <         * Also, scanAndLockForPut speculatively creates a fresh node
684 <         * to use in put if no node is found.
685 <         */
655 >        /* AQS overrides */
656 >        public final boolean isHeldExclusively() { return getState() > 0; }
657 >        public final boolean tryAcquire(int ignore) {
658 >            if (compareAndSetState(0, 1)) {
659 >                setExclusiveOwnerThread(Thread.currentThread());
660 >                return true;
661 >            }
662 >            return false;
663 >        }
664 >        public final boolean tryRelease(int ignore) {
665 >            setExclusiveOwnerThread(null);
666 >            setState(0);
667 >            return true;
668 >        }
669 >        public final int tryAcquireShared(int ignore) {
670 >            for (int c;;) {
671 >                if ((c = getState()) > 0)
672 >                    return -1;
673 >                if (compareAndSetState(c, c -1))
674 >                    return 1;
675 >            }
676 >        }
677 >        public final boolean tryReleaseShared(int ignore) {
678 >            int c;
679 >            do {} while (!compareAndSetState(c = getState(), c + 1));
680 >            return c == -1;
681 >        }
682 >
683 >        /** From CLR */
684 >        private void rotateLeft(TreeNode p) {
685 >            if (p != null) {
686 >                TreeNode r = p.right, pp, rl;
687 >                if ((rl = p.right = r.left) != null)
688 >                    rl.parent = p;
689 >                if ((pp = r.parent = p.parent) == null)
690 >                    root = r;
691 >                else if (pp.left == p)
692 >                    pp.left = r;
693 >                else
694 >                    pp.right = r;
695 >                r.left = p;
696 >                p.parent = r;
697 >            }
698 >        }
699  
700 <        private static final long serialVersionUID = 2249069246763182397L;
700 >        /** From CLR */
701 >        private void rotateRight(TreeNode p) {
702 >            if (p != null) {
703 >                TreeNode l = p.left, pp, lr;
704 >                if ((lr = p.left = l.right) != null)
705 >                    lr.parent = p;
706 >                if ((pp = l.parent = p.parent) == null)
707 >                    root = l;
708 >                else if (pp.right == p)
709 >                    pp.right = l;
710 >                else
711 >                    pp.left = l;
712 >                l.right = p;
713 >                p.parent = l;
714 >            }
715 >        }
716  
717          /**
718 <         * The maximum number of times to tryLock in a prescan before
719 <         * possibly blocking on acquire in preparation for a locked
287 <         * segment operation. On multiprocessors, using a bounded
288 <         * number of retries maintains cache acquired while locating
289 <         * nodes.
718 >         * Returns the TreeNode (or null if not found) for the given key
719 >         * starting at given root.
720           */
721 <        static final int MAX_SCAN_RETRIES =
722 <            Runtime.getRuntime().availableProcessors() > 1 ? 64 : 1;
721 >        @SuppressWarnings("unchecked") // suppress Comparable cast warning
722 >        final TreeNode getTreeNode(int h, Object k, TreeNode p) {
723 >            Class<?> c = k.getClass();
724 >            while (p != null) {
725 >                int dir, ph;  Object pk; Class<?> pc;
726 >                if ((ph = p.hash) == h) {
727 >                    if ((pk = p.key) == k || k.equals(pk))
728 >                        return p;
729 >                    if (c != (pc = pk.getClass()) ||
730 >                        !(k instanceof Comparable) ||
731 >                        (dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) {
732 >                        dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName());
733 >                        TreeNode r = null, s = null, pl, pr;
734 >                        if (dir >= 0) {
735 >                            if ((pl = p.left) != null && h <= pl.hash)
736 >                                s = pl;
737 >                        }
738 >                        else if ((pr = p.right) != null && h >= pr.hash)
739 >                            s = pr;
740 >                        if (s != null && (r = getTreeNode(h, k, s)) != null)
741 >                            return r;
742 >                    }
743 >                }
744 >                else
745 >                    dir = (h < ph) ? -1 : 1;
746 >                p = (dir > 0) ? p.right : p.left;
747 >            }
748 >            return null;
749 >        }
750  
751          /**
752 <         * The per-segment table. Elements are accessed via
753 <         * entryAt/setEntryAt providing volatile semantics.
752 >         * Wrapper for getTreeNode used by CHM.get. Tries to obtain
753 >         * read-lock to call getTreeNode, but during failure to get
754 >         * lock, searches along next links.
755           */
756 <        transient volatile HashEntry<K,V>[] table;
756 >        final Object getValue(int h, Object k) {
757 >            Node r = null;
758 >            int c = getState(); // Must read lock state first
759 >            for (Node e = first; e != null; e = e.next) {
760 >                if (c <= 0 && compareAndSetState(c, c - 1)) {
761 >                    try {
762 >                        r = getTreeNode(h, k, root);
763 >                    } finally {
764 >                        releaseShared(0);
765 >                    }
766 >                    break;
767 >                }
768 >                else if ((e.hash & HASH_BITS) == h && k.equals(e.key)) {
769 >                    r = e;
770 >                    break;
771 >                }
772 >                else
773 >                    c = getState();
774 >            }
775 >            return r == null ? null : r.val;
776 >        }
777  
778          /**
779 <         * The number of elements. Accessed only either within locks
780 <         * or among other volatile reads that maintain visibility.
779 >         * Finds or adds a node.
780 >         * @return null if added
781           */
782 <        transient int count;
782 >        @SuppressWarnings("unchecked") // suppress Comparable cast warning
783 >        final TreeNode putTreeNode(int h, Object k, Object v) {
784 >            Class<?> c = k.getClass();
785 >            TreeNode pp = root, p = null;
786 >            int dir = 0;
787 >            while (pp != null) { // find existing node or leaf to insert at
788 >                int ph;  Object pk; Class<?> pc;
789 >                p = pp;
790 >                if ((ph = p.hash) == h) {
791 >                    if ((pk = p.key) == k || k.equals(pk))
792 >                        return p;
793 >                    if (c != (pc = pk.getClass()) ||
794 >                        !(k instanceof Comparable) ||
795 >                        (dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) {
796 >                        dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName());
797 >                        TreeNode r = null, s = null, pl, pr;
798 >                        if (dir >= 0) {
799 >                            if ((pl = p.left) != null && h <= pl.hash)
800 >                                s = pl;
801 >                        }
802 >                        else if ((pr = p.right) != null && h >= pr.hash)
803 >                            s = pr;
804 >                        if (s != null && (r = getTreeNode(h, k, s)) != null)
805 >                            return r;
806 >                    }
807 >                }
808 >                else
809 >                    dir = (h < ph) ? -1 : 1;
810 >                pp = (dir > 0) ? p.right : p.left;
811 >            }
812  
813 <        /**
814 <         * The total number of mutative operations in this segment.
815 <         * Even though this may overflows 32 bits, it provides
816 <         * sufficient accuracy for stability checks in CHM isEmpty()
817 <         * and size() methods.  Accessed only either within locks or
818 <         * among other volatile reads that maintain visibility.
819 <         */
820 <        transient int modCount;
813 >            TreeNode f = first;
814 >            TreeNode x = first = new TreeNode(h, k, v, f, p);
815 >            if (p == null)
816 >                root = x;
817 >            else { // attach and rebalance; adapted from CLR
818 >                TreeNode xp, xpp;
819 >                if (f != null)
820 >                    f.prev = x;
821 >                if (dir <= 0)
822 >                    p.left = x;
823 >                else
824 >                    p.right = x;
825 >                x.red = true;
826 >                while (x != null && (xp = x.parent) != null && xp.red &&
827 >                       (xpp = xp.parent) != null) {
828 >                    TreeNode xppl = xpp.left;
829 >                    if (xp == xppl) {
830 >                        TreeNode y = xpp.right;
831 >                        if (y != null && y.red) {
832 >                            y.red = false;
833 >                            xp.red = false;
834 >                            xpp.red = true;
835 >                            x = xpp;
836 >                        }
837 >                        else {
838 >                            if (x == xp.right) {
839 >                                rotateLeft(x = xp);
840 >                                xpp = (xp = x.parent) == null ? null : xp.parent;
841 >                            }
842 >                            if (xp != null) {
843 >                                xp.red = false;
844 >                                if (xpp != null) {
845 >                                    xpp.red = true;
846 >                                    rotateRight(xpp);
847 >                                }
848 >                            }
849 >                        }
850 >                    }
851 >                    else {
852 >                        TreeNode y = xppl;
853 >                        if (y != null && y.red) {
854 >                            y.red = false;
855 >                            xp.red = false;
856 >                            xpp.red = true;
857 >                            x = xpp;
858 >                        }
859 >                        else {
860 >                            if (x == xp.left) {
861 >                                rotateRight(x = xp);
862 >                                xpp = (xp = x.parent) == null ? null : xp.parent;
863 >                            }
864 >                            if (xp != null) {
865 >                                xp.red = false;
866 >                                if (xpp != null) {
867 >                                    xpp.red = true;
868 >                                    rotateLeft(xpp);
869 >                                }
870 >                            }
871 >                        }
872 >                    }
873 >                }
874 >                TreeNode r = root;
875 >                if (r != null && r.red)
876 >                    r.red = false;
877 >            }
878 >            return null;
879 >        }
880  
881          /**
882 <         * The table is rehashed when its size exceeds this threshold.
883 <         * (The value of this field is always <tt>(int)(capacity *
884 <         * loadFactor)</tt>.)
882 >         * Removes the given node, that must be present before this
883 >         * call.  This is messier than typical red-black deletion code
884 >         * because we cannot swap the contents of an interior node
885 >         * with a leaf successor that is pinned by "next" pointers
886 >         * that are accessible independently of lock. So instead we
887 >         * swap the tree linkages.
888           */
889 <        transient int threshold;
889 >        final void deleteTreeNode(TreeNode p) {
890 >            TreeNode next = (TreeNode)p.next; // unlink traversal pointers
891 >            TreeNode pred = p.prev;
892 >            if (pred == null)
893 >                first = next;
894 >            else
895 >                pred.next = next;
896 >            if (next != null)
897 >                next.prev = pred;
898 >            TreeNode replacement;
899 >            TreeNode pl = p.left;
900 >            TreeNode pr = p.right;
901 >            if (pl != null && pr != null) {
902 >                TreeNode s = pr, sl;
903 >                while ((sl = s.left) != null) // find successor
904 >                    s = sl;
905 >                boolean c = s.red; s.red = p.red; p.red = c; // swap colors
906 >                TreeNode sr = s.right;
907 >                TreeNode pp = p.parent;
908 >                if (s == pr) { // p was s's direct parent
909 >                    p.parent = s;
910 >                    s.right = p;
911 >                }
912 >                else {
913 >                    TreeNode sp = s.parent;
914 >                    if ((p.parent = sp) != null) {
915 >                        if (s == sp.left)
916 >                            sp.left = p;
917 >                        else
918 >                            sp.right = p;
919 >                    }
920 >                    if ((s.right = pr) != null)
921 >                        pr.parent = s;
922 >                }
923 >                p.left = null;
924 >                if ((p.right = sr) != null)
925 >                    sr.parent = p;
926 >                if ((s.left = pl) != null)
927 >                    pl.parent = s;
928 >                if ((s.parent = pp) == null)
929 >                    root = s;
930 >                else if (p == pp.left)
931 >                    pp.left = s;
932 >                else
933 >                    pp.right = s;
934 >                replacement = sr;
935 >            }
936 >            else
937 >                replacement = (pl != null) ? pl : pr;
938 >            TreeNode pp = p.parent;
939 >            if (replacement == null) {
940 >                if (pp == null) {
941 >                    root = null;
942 >                    return;
943 >                }
944 >                replacement = p;
945 >            }
946 >            else {
947 >                replacement.parent = pp;
948 >                if (pp == null)
949 >                    root = replacement;
950 >                else if (p == pp.left)
951 >                    pp.left = replacement;
952 >                else
953 >                    pp.right = replacement;
954 >                p.left = p.right = p.parent = null;
955 >            }
956 >            if (!p.red) { // rebalance, from CLR
957 >                TreeNode x = replacement;
958 >                while (x != null) {
959 >                    TreeNode xp, xpl;
960 >                    if (x.red || (xp = x.parent) == null) {
961 >                        x.red = false;
962 >                        break;
963 >                    }
964 >                    if (x == (xpl = xp.left)) {
965 >                        TreeNode sib = xp.right;
966 >                        if (sib != null && sib.red) {
967 >                            sib.red = false;
968 >                            xp.red = true;
969 >                            rotateLeft(xp);
970 >                            sib = (xp = x.parent) == null ? null : xp.right;
971 >                        }
972 >                        if (sib == null)
973 >                            x = xp;
974 >                        else {
975 >                            TreeNode sl = sib.left, sr = sib.right;
976 >                            if ((sr == null || !sr.red) &&
977 >                                (sl == null || !sl.red)) {
978 >                                sib.red = true;
979 >                                x = xp;
980 >                            }
981 >                            else {
982 >                                if (sr == null || !sr.red) {
983 >                                    if (sl != null)
984 >                                        sl.red = false;
985 >                                    sib.red = true;
986 >                                    rotateRight(sib);
987 >                                    sib = (xp = x.parent) == null ? null : xp.right;
988 >                                }
989 >                                if (sib != null) {
990 >                                    sib.red = (xp == null) ? false : xp.red;
991 >                                    if ((sr = sib.right) != null)
992 >                                        sr.red = false;
993 >                                }
994 >                                if (xp != null) {
995 >                                    xp.red = false;
996 >                                    rotateLeft(xp);
997 >                                }
998 >                                x = root;
999 >                            }
1000 >                        }
1001 >                    }
1002 >                    else { // symmetric
1003 >                        TreeNode sib = xpl;
1004 >                        if (sib != null && sib.red) {
1005 >                            sib.red = false;
1006 >                            xp.red = true;
1007 >                            rotateRight(xp);
1008 >                            sib = (xp = x.parent) == null ? null : xp.left;
1009 >                        }
1010 >                        if (sib == null)
1011 >                            x = xp;
1012 >                        else {
1013 >                            TreeNode sl = sib.left, sr = sib.right;
1014 >                            if ((sl == null || !sl.red) &&
1015 >                                (sr == null || !sr.red)) {
1016 >                                sib.red = true;
1017 >                                x = xp;
1018 >                            }
1019 >                            else {
1020 >                                if (sl == null || !sl.red) {
1021 >                                    if (sr != null)
1022 >                                        sr.red = false;
1023 >                                    sib.red = true;
1024 >                                    rotateLeft(sib);
1025 >                                    sib = (xp = x.parent) == null ? null : xp.left;
1026 >                                }
1027 >                                if (sib != null) {
1028 >                                    sib.red = (xp == null) ? false : xp.red;
1029 >                                    if ((sl = sib.left) != null)
1030 >                                        sl.red = false;
1031 >                                }
1032 >                                if (xp != null) {
1033 >                                    xp.red = false;
1034 >                                    rotateRight(xp);
1035 >                                }
1036 >                                x = root;
1037 >                            }
1038 >                        }
1039 >                    }
1040 >                }
1041 >            }
1042 >            if (p == replacement && (pp = p.parent) != null) {
1043 >                if (p == pp.left) // detach pointers
1044 >                    pp.left = null;
1045 >                else if (p == pp.right)
1046 >                    pp.right = null;
1047 >                p.parent = null;
1048 >            }
1049 >        }
1050 >    }
1051  
1052 <        /**
323 <         * The load factor for the hash table.  Even though this value
324 <         * is same for all segments, it is replicated to avoid needing
325 <         * links to outer object.
326 <         * @serial
327 <         */
328 <        final float loadFactor;
1052 >    /* ---------------- Collision reduction methods -------------- */
1053  
1054 <        Segment(float lf, int threshold, HashEntry<K,V>[] tab) {
1055 <            this.loadFactor = lf;
1056 <            this.threshold = threshold;
1057 <            this.table = tab;
1054 >    /**
1055 >     * Spreads higher bits to lower, and also forces top 2 bits to 0.
1056 >     * Because the table uses power-of-two masking, sets of hashes
1057 >     * that vary only in bits above the current mask will always
1058 >     * collide. (Among known examples are sets of Float keys holding
1059 >     * consecutive whole numbers in small tables.)  To counter this,
1060 >     * we apply a transform that spreads the impact of higher bits
1061 >     * downward. There is a tradeoff between speed, utility, and
1062 >     * quality of bit-spreading. Because many common sets of hashes
1063 >     * are already reasonably distributed across bits (so don't benefit
1064 >     * from spreading), and because we use trees to handle large sets
1065 >     * of collisions in bins, we don't need excessively high quality.
1066 >     */
1067 >    private static final int spread(int h) {
1068 >        h ^= (h >>> 18) ^ (h >>> 12);
1069 >        return (h ^ (h >>> 10)) & HASH_BITS;
1070 >    }
1071 >
1072 >    /**
1073 >     * Replaces a list bin with a tree bin. Call only when locked.
1074 >     * Fails to replace if the given key is non-comparable or table
1075 >     * is, or needs, resizing.
1076 >     */
1077 >    private final void replaceWithTreeBin(Node[] tab, int index, Object key) {
1078 >        if ((key instanceof Comparable) &&
1079 >            (tab.length >= MAXIMUM_CAPACITY || counter.sum() < (long)sizeCtl)) {
1080 >            TreeBin t = new TreeBin();
1081 >            for (Node e = tabAt(tab, index); e != null; e = e.next)
1082 >                t.putTreeNode(e.hash & HASH_BITS, e.key, e.val);
1083 >            setTabAt(tab, index, new Node(MOVED, t, null, null));
1084          }
1085 +    }
1086  
1087 <        final V put(K key, int hash, V value, boolean onlyIfAbsent) {
1088 <            HashEntry<K,V> node = tryLock() ? null :
1089 <                scanAndLockForPut(key, hash, value);
1090 <            V oldValue;
1091 <            try {
1092 <                HashEntry<K,V>[] tab = table;
1093 <                int index = (tab.length - 1) & hash;
1094 <                HashEntry<K,V> first = entryAt(tab, index);
1095 <                for (HashEntry<K,V> e = first;;) {
1096 <                    if (e != null) {
1097 <                        K k;
1098 <                        if ((k = e.key) == key ||
1099 <                            (e.hash == hash && key.equals(k))) {
1100 <                            oldValue = e.value;
1101 <                            if (!onlyIfAbsent) {
1102 <                                e.value = value;
1103 <                                ++modCount;
1087 >    /* ---------------- Internal access and update methods -------------- */
1088 >
1089 >    /** Implementation for get and containsKey */
1090 >    private final Object internalGet(Object k) {
1091 >        int h = spread(k.hashCode());
1092 >        retry: for (Node[] tab = table; tab != null;) {
1093 >            Node e, p; Object ek, ev; int eh;      // locals to read fields once
1094 >            for (e = tabAt(tab, (tab.length - 1) & h); e != null; e = e.next) {
1095 >                if ((eh = e.hash) == MOVED) {
1096 >                    if ((ek = e.key) instanceof TreeBin)  // search TreeBin
1097 >                        return ((TreeBin)ek).getValue(h, k);
1098 >                    else {                        // restart with new table
1099 >                        tab = (Node[])ek;
1100 >                        continue retry;
1101 >                    }
1102 >                }
1103 >                else if ((eh & HASH_BITS) == h && (ev = e.val) != null &&
1104 >                         ((ek = e.key) == k || k.equals(ek)))
1105 >                    return ev;
1106 >            }
1107 >            break;
1108 >        }
1109 >        return null;
1110 >    }
1111 >
1112 >    /**
1113 >     * Implementation for the four public remove/replace methods:
1114 >     * Replaces node value with v, conditional upon match of cv if
1115 >     * non-null.  If resulting value is null, delete.
1116 >     */
1117 >    private final Object internalReplace(Object k, Object v, Object cv) {
1118 >        int h = spread(k.hashCode());
1119 >        Object oldVal = null;
1120 >        for (Node[] tab = table;;) {
1121 >            Node f; int i, fh; Object fk;
1122 >            if (tab == null ||
1123 >                (f = tabAt(tab, i = (tab.length - 1) & h)) == null)
1124 >                break;
1125 >            else if ((fh = f.hash) == MOVED) {
1126 >                if ((fk = f.key) instanceof TreeBin) {
1127 >                    TreeBin t = (TreeBin)fk;
1128 >                    boolean validated = false;
1129 >                    boolean deleted = false;
1130 >                    t.acquire(0);
1131 >                    try {
1132 >                        if (tabAt(tab, i) == f) {
1133 >                            validated = true;
1134 >                            TreeNode p = t.getTreeNode(h, k, t.root);
1135 >                            if (p != null) {
1136 >                                Object pv = p.val;
1137 >                                if (cv == null || cv == pv || cv.equals(pv)) {
1138 >                                    oldVal = pv;
1139 >                                    if ((p.val = v) == null) {
1140 >                                        deleted = true;
1141 >                                        t.deleteTreeNode(p);
1142 >                                    }
1143 >                                }
1144                              }
354                            break;
1145                          }
1146 <                        e = e.next;
1146 >                    } finally {
1147 >                        t.release(0);
1148                      }
1149 <                    else {
1150 <                        if (node != null)
1151 <                            node.setNext(first);
361 <                        else
362 <                            node = new HashEntry<K,V>(hash, key, value, first);
363 <                        int c = count + 1;
364 <                        if (c > threshold && tab.length < MAXIMUM_CAPACITY)
365 <                            rehash(node);
366 <                        else
367 <                            setEntryAt(tab, index, node);
368 <                        ++modCount;
369 <                        count = c;
370 <                        oldValue = null;
1149 >                    if (validated) {
1150 >                        if (deleted)
1151 >                            counter.add(-1L);
1152                          break;
1153                      }
1154                  }
1155 <            } finally {
1156 <                unlock();
1155 >                else
1156 >                    tab = (Node[])fk;
1157 >            }
1158 >            else if ((fh & HASH_BITS) != h && f.next == null) // precheck
1159 >                break;                          // rules out possible existence
1160 >            else if ((fh & LOCKED) != 0) {
1161 >                checkForResize();               // try resizing if can't get lock
1162 >                f.tryAwaitLock(tab, i);
1163 >            }
1164 >            else if (f.casHash(fh, fh | LOCKED)) {
1165 >                boolean validated = false;
1166 >                boolean deleted = false;
1167 >                try {
1168 >                    if (tabAt(tab, i) == f) {
1169 >                        validated = true;
1170 >                        for (Node e = f, pred = null;;) {
1171 >                            Object ek, ev;
1172 >                            if ((e.hash & HASH_BITS) == h &&
1173 >                                ((ev = e.val) != null) &&
1174 >                                ((ek = e.key) == k || k.equals(ek))) {
1175 >                                if (cv == null || cv == ev || cv.equals(ev)) {
1176 >                                    oldVal = ev;
1177 >                                    if ((e.val = v) == null) {
1178 >                                        deleted = true;
1179 >                                        Node en = e.next;
1180 >                                        if (pred != null)
1181 >                                            pred.next = en;
1182 >                                        else
1183 >                                            setTabAt(tab, i, en);
1184 >                                    }
1185 >                                }
1186 >                                break;
1187 >                            }
1188 >                            pred = e;
1189 >                            if ((e = e.next) == null)
1190 >                                break;
1191 >                        }
1192 >                    }
1193 >                } finally {
1194 >                    if (!f.casHash(fh | LOCKED, fh)) {
1195 >                        f.hash = fh;
1196 >                        synchronized (f) { f.notifyAll(); };
1197 >                    }
1198 >                }
1199 >                if (validated) {
1200 >                    if (deleted)
1201 >                        counter.add(-1L);
1202 >                    break;
1203 >                }
1204              }
377            return oldValue;
1205          }
1206 +        return oldVal;
1207 +    }
1208  
1209 <        /**
1210 <         * Doubles size of table and repacks entries, also adding the
1211 <         * given node to new table
1212 <         */
1213 <        @SuppressWarnings("unchecked")
1214 <        private void rehash(HashEntry<K,V> node) {
1215 <            /*
1216 <             * Reclassify nodes in each list to new table.  Because we
1217 <             * are using power-of-two expansion, the elements from
1218 <             * each bin must either stay at same index, or move with a
1219 <             * power of two offset. We eliminate unnecessary node
1220 <             * creation by catching cases where old nodes can be
1221 <             * reused because their next fields won't change.
1222 <             * Statistically, at the default threshold, only about
1223 <             * one-sixth of them need cloning when a table
1224 <             * doubles. The nodes they replace will be garbage
1225 <             * collectable as soon as they are no longer referenced by
1226 <             * any reader thread that may be in the midst of
1227 <             * concurrently traversing table. Entry accesses use plain
1228 <             * array indexing because they are followed by volatile
1229 <             * table write.
1230 <             */
1231 <            HashEntry<K,V>[] oldTable = table;
1232 <            int oldCapacity = oldTable.length;
1233 <            int newCapacity = oldCapacity << 1;
1234 <            threshold = (int)(newCapacity * loadFactor);
1235 <            HashEntry<K,V>[] newTable =
1236 <                (HashEntry<K,V>[]) new HashEntry<?,?>[newCapacity];
1237 <            int sizeMask = newCapacity - 1;
1238 <            for (int i = 0; i < oldCapacity ; i++) {
1239 <                HashEntry<K,V> e = oldTable[i];
1240 <                if (e != null) {
1241 <                    HashEntry<K,V> next = e.next;
1242 <                    int idx = e.hash & sizeMask;
1243 <                    if (next == null)   //  Single node on list
1244 <                        newTable[idx] = e;
1245 <                    else { // Reuse consecutive sequence at same slot
1246 <                        HashEntry<K,V> lastRun = e;
1247 <                        int lastIdx = idx;
1248 <                        for (HashEntry<K,V> last = next;
1249 <                             last != null;
1250 <                             last = last.next) {
1251 <                            int k = last.hash & sizeMask;
1252 <                            if (k != lastIdx) {
1253 <                                lastIdx = k;
1254 <                                lastRun = last;
1255 <                            }
1256 <                        }
1257 <                        newTable[lastIdx] = lastRun;
1258 <                        // Clone remaining nodes
1259 <                        for (HashEntry<K,V> p = e; p != lastRun; p = p.next) {
1260 <                            V v = p.value;
1261 <                            int h = p.hash;
1262 <                            int k = h & sizeMask;
1263 <                            HashEntry<K,V> n = newTable[k];
1264 <                            newTable[k] = new HashEntry<K,V>(h, p.key, v, n);
1265 <                        }
1266 <                    }
1267 <                }
1268 <            }
1269 <            int nodeIndex = node.hash & sizeMask; // add the new node
1270 <            node.setNext(newTable[nodeIndex]);
1271 <            newTable[nodeIndex] = node;
1272 <            table = newTable;
1273 <        }
1274 <
1275 <        /**
1276 <         * Scans for a node containing given key while trying to
1277 <         * acquire lock, creating and returning one if not found. Upon
1278 <         * return, guarantees that lock is held. Unlike in most
1279 <         * methods, calls to method equals are not screened: Since
1280 <         * traversal speed doesn't matter, we might as well help warm
1281 <         * up the associated code and accesses as well.
1282 <         *
1283 <         * @return a new node if key not found, else null
1284 <         */
1285 <        private HashEntry<K,V> scanAndLockForPut(K key, int hash, V value) {
1286 <            HashEntry<K,V> first = entryForHash(this, hash);
1287 <            HashEntry<K,V> e = first;
1288 <            HashEntry<K,V> node = null;
1289 <            int retries = -1; // negative while locating node
1290 <            while (!tryLock()) {
1291 <                HashEntry<K,V> f; // to recheck first below
1292 <                if (retries < 0) {
1293 <                    if (e == null) {
1294 <                        if (node == null) // speculatively create node
1295 <                            node = new HashEntry<K,V>(hash, key, value, null);
1296 <                        retries = 0;
1209 >    /*
1210 >     * Internal versions of the six insertion methods, each a
1211 >     * little more complicated than the last. All have
1212 >     * the same basic structure as the first (internalPut):
1213 >     *  1. If table uninitialized, create
1214 >     *  2. If bin empty, try to CAS new node
1215 >     *  3. If bin stale, use new table
1216 >     *  4. if bin converted to TreeBin, validate and relay to TreeBin methods
1217 >     *  5. Lock and validate; if valid, scan and add or update
1218 >     *
1219 >     * The others interweave other checks and/or alternative actions:
1220 >     *  * Plain put checks for and performs resize after insertion.
1221 >     *  * putIfAbsent prescans for mapping without lock (and fails to add
1222 >     *    if present), which also makes pre-emptive resize checks worthwhile.
1223 >     *  * computeIfAbsent extends form used in putIfAbsent with additional
1224 >     *    mechanics to deal with, calls, potential exceptions and null
1225 >     *    returns from function call.
1226 >     *  * compute uses the same function-call mechanics, but without
1227 >     *    the prescans
1228 >     *  * merge acts as putIfAbsent in the absent case, but invokes the
1229 >     *    update function if present
1230 >     *  * putAll attempts to pre-allocate enough table space
1231 >     *    and more lazily performs count updates and checks.
1232 >     *
1233 >     * Someday when details settle down a bit more, it might be worth
1234 >     * some factoring to reduce sprawl.
1235 >     */
1236 >
1237 >    /** Implementation for put */
1238 >    private final Object internalPut(Object k, Object v) {
1239 >        int h = spread(k.hashCode());
1240 >        int count = 0;
1241 >        for (Node[] tab = table;;) {
1242 >            int i; Node f; int fh; Object fk;
1243 >            if (tab == null)
1244 >                tab = initTable();
1245 >            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1246 >                if (casTabAt(tab, i, null, new Node(h, k, v, null)))
1247 >                    break;                   // no lock when adding to empty bin
1248 >            }
1249 >            else if ((fh = f.hash) == MOVED) {
1250 >                if ((fk = f.key) instanceof TreeBin) {
1251 >                    TreeBin t = (TreeBin)fk;
1252 >                    Object oldVal = null;
1253 >                    t.acquire(0);
1254 >                    try {
1255 >                        if (tabAt(tab, i) == f) {
1256 >                            count = 2;
1257 >                            TreeNode p = t.putTreeNode(h, k, v);
1258 >                            if (p != null) {
1259 >                                oldVal = p.val;
1260 >                                p.val = v;
1261 >                            }
1262 >                        }
1263 >                    } finally {
1264 >                        t.release(0);
1265 >                    }
1266 >                    if (count != 0) {
1267 >                        if (oldVal != null)
1268 >                            return oldVal;
1269 >                        break;
1270 >                    }
1271 >                }
1272 >                else
1273 >                    tab = (Node[])fk;
1274 >            }
1275 >            else if ((fh & LOCKED) != 0) {
1276 >                checkForResize();
1277 >                f.tryAwaitLock(tab, i);
1278 >            }
1279 >            else if (f.casHash(fh, fh | LOCKED)) {
1280 >                Object oldVal = null;
1281 >                try {                        // needed in case equals() throws
1282 >                    if (tabAt(tab, i) == f) {
1283 >                        count = 1;
1284 >                        for (Node e = f;; ++count) {
1285 >                            Object ek, ev;
1286 >                            if ((e.hash & HASH_BITS) == h &&
1287 >                                (ev = e.val) != null &&
1288 >                                ((ek = e.key) == k || k.equals(ek))) {
1289 >                                oldVal = ev;
1290 >                                e.val = v;
1291 >                                break;
1292 >                            }
1293 >                            Node last = e;
1294 >                            if ((e = e.next) == null) {
1295 >                                last.next = new Node(h, k, v, null);
1296 >                                if (count >= TREE_THRESHOLD)
1297 >                                    replaceWithTreeBin(tab, i, k);
1298 >                                break;
1299 >                            }
1300 >                        }
1301 >                    }
1302 >                } finally {                  // unlock and signal if needed
1303 >                    if (!f.casHash(fh | LOCKED, fh)) {
1304 >                        f.hash = fh;
1305 >                        synchronized (f) { f.notifyAll(); };
1306                      }
469                    else if (key.equals(e.key))
470                        retries = 0;
471                    else
472                        e = e.next;
1307                  }
1308 <                else if (++retries > MAX_SCAN_RETRIES) {
1309 <                    lock();
1308 >                if (count != 0) {
1309 >                    if (oldVal != null)
1310 >                        return oldVal;
1311 >                    if (tab.length <= 64)
1312 >                        count = 2;
1313                      break;
1314                  }
1315 <                else if ((retries & 1) == 0 &&
1316 <                         (f = entryForHash(this, hash)) != first) {
1317 <                    e = first = f; // re-traverse if entry changed
1318 <                    retries = -1;
1315 >            }
1316 >        }
1317 >        counter.add(1L);
1318 >        if (count > 1)
1319 >            checkForResize();
1320 >        return null;
1321 >    }
1322 >
1323 >    /** Implementation for putIfAbsent */
1324 >    private final Object internalPutIfAbsent(Object k, Object v) {
1325 >        int h = spread(k.hashCode());
1326 >        int count = 0;
1327 >        for (Node[] tab = table;;) {
1328 >            int i; Node f; int fh; Object fk, fv;
1329 >            if (tab == null)
1330 >                tab = initTable();
1331 >            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1332 >                if (casTabAt(tab, i, null, new Node(h, k, v, null)))
1333 >                    break;
1334 >            }
1335 >            else if ((fh = f.hash) == MOVED) {
1336 >                if ((fk = f.key) instanceof TreeBin) {
1337 >                    TreeBin t = (TreeBin)fk;
1338 >                    Object oldVal = null;
1339 >                    t.acquire(0);
1340 >                    try {
1341 >                        if (tabAt(tab, i) == f) {
1342 >                            count = 2;
1343 >                            TreeNode p = t.putTreeNode(h, k, v);
1344 >                            if (p != null)
1345 >                                oldVal = p.val;
1346 >                        }
1347 >                    } finally {
1348 >                        t.release(0);
1349 >                    }
1350 >                    if (count != 0) {
1351 >                        if (oldVal != null)
1352 >                            return oldVal;
1353 >                        break;
1354 >                    }
1355 >                }
1356 >                else
1357 >                    tab = (Node[])fk;
1358 >            }
1359 >            else if ((fh & HASH_BITS) == h && (fv = f.val) != null &&
1360 >                     ((fk = f.key) == k || k.equals(fk)))
1361 >                return fv;
1362 >            else {
1363 >                Node g = f.next;
1364 >                if (g != null) { // at least 2 nodes -- search and maybe resize
1365 >                    for (Node e = g;;) {
1366 >                        Object ek, ev;
1367 >                        if ((e.hash & HASH_BITS) == h && (ev = e.val) != null &&
1368 >                            ((ek = e.key) == k || k.equals(ek)))
1369 >                            return ev;
1370 >                        if ((e = e.next) == null) {
1371 >                            checkForResize();
1372 >                            break;
1373 >                        }
1374 >                    }
1375 >                }
1376 >                if (((fh = f.hash) & LOCKED) != 0) {
1377 >                    checkForResize();
1378 >                    f.tryAwaitLock(tab, i);
1379 >                }
1380 >                else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) {
1381 >                    Object oldVal = null;
1382 >                    try {
1383 >                        if (tabAt(tab, i) == f) {
1384 >                            count = 1;
1385 >                            for (Node e = f;; ++count) {
1386 >                                Object ek, ev;
1387 >                                if ((e.hash & HASH_BITS) == h &&
1388 >                                    (ev = e.val) != null &&
1389 >                                    ((ek = e.key) == k || k.equals(ek))) {
1390 >                                    oldVal = ev;
1391 >                                    break;
1392 >                                }
1393 >                                Node last = e;
1394 >                                if ((e = e.next) == null) {
1395 >                                    last.next = new Node(h, k, v, null);
1396 >                                    if (count >= TREE_THRESHOLD)
1397 >                                        replaceWithTreeBin(tab, i, k);
1398 >                                    break;
1399 >                                }
1400 >                            }
1401 >                        }
1402 >                    } finally {
1403 >                        if (!f.casHash(fh | LOCKED, fh)) {
1404 >                            f.hash = fh;
1405 >                            synchronized (f) { f.notifyAll(); };
1406 >                        }
1407 >                    }
1408 >                    if (count != 0) {
1409 >                        if (oldVal != null)
1410 >                            return oldVal;
1411 >                        if (tab.length <= 64)
1412 >                            count = 2;
1413 >                        break;
1414 >                    }
1415                  }
1416              }
484            return node;
1417          }
1418 +        counter.add(1L);
1419 +        if (count > 1)
1420 +            checkForResize();
1421 +        return null;
1422 +    }
1423  
1424 <        /**
1425 <         * Scans for a node containing the given key while trying to
1426 <         * acquire lock for a remove or replace operation. Upon
1427 <         * return, guarantees that lock is held.  Note that we must
1428 <         * lock even if the key is not found, to ensure sequential
1429 <         * consistency of updates.
1430 <         */
1431 <        private void scanAndLock(Object key, int hash) {
1432 <            // similar to but simpler than scanAndLockForPut
1433 <            HashEntry<K,V> first = entryForHash(this, hash);
1434 <            HashEntry<K,V> e = first;
1435 <            int retries = -1;
1436 <            while (!tryLock()) {
1437 <                HashEntry<K,V> f;
1438 <                if (retries < 0) {
1439 <                    if (e == null || key.equals(e.key))
1440 <                        retries = 0;
1441 <                    else
1442 <                        e = e.next;
1424 >    /** Implementation for computeIfAbsent */
1425 >    private final Object internalComputeIfAbsent(K k,
1426 >                                                 Fun<? super K, ?> mf) {
1427 >        int h = spread(k.hashCode());
1428 >        Object val = null;
1429 >        int count = 0;
1430 >        for (Node[] tab = table;;) {
1431 >            Node f; int i, fh; Object fk, fv;
1432 >            if (tab == null)
1433 >                tab = initTable();
1434 >            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1435 >                Node node = new Node(fh = h | LOCKED, k, null, null);
1436 >                if (casTabAt(tab, i, null, node)) {
1437 >                    count = 1;
1438 >                    try {
1439 >                        if ((val = mf.apply(k)) != null)
1440 >                            node.val = val;
1441 >                    } finally {
1442 >                        if (val == null)
1443 >                            setTabAt(tab, i, null);
1444 >                        if (!node.casHash(fh, h)) {
1445 >                            node.hash = h;
1446 >                            synchronized (node) { node.notifyAll(); };
1447 >                        }
1448 >                    }
1449                  }
1450 <                else if (++retries > MAX_SCAN_RETRIES) {
508 <                    lock();
1450 >                if (count != 0)
1451                      break;
1452 +            }
1453 +            else if ((fh = f.hash) == MOVED) {
1454 +                if ((fk = f.key) instanceof TreeBin) {
1455 +                    TreeBin t = (TreeBin)fk;
1456 +                    boolean added = false;
1457 +                    t.acquire(0);
1458 +                    try {
1459 +                        if (tabAt(tab, i) == f) {
1460 +                            count = 1;
1461 +                            TreeNode p = t.getTreeNode(h, k, t.root);
1462 +                            if (p != null)
1463 +                                val = p.val;
1464 +                            else if ((val = mf.apply(k)) != null) {
1465 +                                added = true;
1466 +                                count = 2;
1467 +                                t.putTreeNode(h, k, val);
1468 +                            }
1469 +                        }
1470 +                    } finally {
1471 +                        t.release(0);
1472 +                    }
1473 +                    if (count != 0) {
1474 +                        if (!added)
1475 +                            return val;
1476 +                        break;
1477 +                    }
1478                  }
1479 <                else if ((retries & 1) == 0 &&
1480 <                         (f = entryForHash(this, hash)) != first) {
1481 <                    e = first = f;
1482 <                    retries = -1;
1479 >                else
1480 >                    tab = (Node[])fk;
1481 >            }
1482 >            else if ((fh & HASH_BITS) == h && (fv = f.val) != null &&
1483 >                     ((fk = f.key) == k || k.equals(fk)))
1484 >                return fv;
1485 >            else {
1486 >                Node g = f.next;
1487 >                if (g != null) {
1488 >                    for (Node e = g;;) {
1489 >                        Object ek, ev;
1490 >                        if ((e.hash & HASH_BITS) == h && (ev = e.val) != null &&
1491 >                            ((ek = e.key) == k || k.equals(ek)))
1492 >                            return ev;
1493 >                        if ((e = e.next) == null) {
1494 >                            checkForResize();
1495 >                            break;
1496 >                        }
1497 >                    }
1498 >                }
1499 >                if (((fh = f.hash) & LOCKED) != 0) {
1500 >                    checkForResize();
1501 >                    f.tryAwaitLock(tab, i);
1502 >                }
1503 >                else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) {
1504 >                    boolean added = false;
1505 >                    try {
1506 >                        if (tabAt(tab, i) == f) {
1507 >                            count = 1;
1508 >                            for (Node e = f;; ++count) {
1509 >                                Object ek, ev;
1510 >                                if ((e.hash & HASH_BITS) == h &&
1511 >                                    (ev = e.val) != null &&
1512 >                                    ((ek = e.key) == k || k.equals(ek))) {
1513 >                                    val = ev;
1514 >                                    break;
1515 >                                }
1516 >                                Node last = e;
1517 >                                if ((e = e.next) == null) {
1518 >                                    if ((val = mf.apply(k)) != null) {
1519 >                                        added = true;
1520 >                                        last.next = new Node(h, k, val, null);
1521 >                                        if (count >= TREE_THRESHOLD)
1522 >                                            replaceWithTreeBin(tab, i, k);
1523 >                                    }
1524 >                                    break;
1525 >                                }
1526 >                            }
1527 >                        }
1528 >                    } finally {
1529 >                        if (!f.casHash(fh | LOCKED, fh)) {
1530 >                            f.hash = fh;
1531 >                            synchronized (f) { f.notifyAll(); };
1532 >                        }
1533 >                    }
1534 >                    if (count != 0) {
1535 >                        if (!added)
1536 >                            return val;
1537 >                        if (tab.length <= 64)
1538 >                            count = 2;
1539 >                        break;
1540 >                    }
1541                  }
1542              }
1543          }
1544 +        if (val != null) {
1545 +            counter.add(1L);
1546 +            if (count > 1)
1547 +                checkForResize();
1548 +        }
1549 +        return val;
1550 +    }
1551  
1552 <        /**
1553 <         * Remove; match on key only if value null, else match both.
1554 <         */
1555 <        final V remove(Object key, int hash, Object value) {
1556 <            if (!tryLock())
1557 <                scanAndLock(key, hash);
1558 <            V oldValue = null;
1559 <            try {
1560 <                HashEntry<K,V>[] tab = table;
1561 <                int index = (tab.length - 1) & hash;
1562 <                HashEntry<K,V> e = entryAt(tab, index);
1563 <                HashEntry<K,V> pred = null;
1564 <                while (e != null) {
1565 <                    K k;
1566 <                    HashEntry<K,V> next = e.next;
1567 <                    if ((k = e.key) == key ||
1568 <                        (e.hash == hash && key.equals(k))) {
1569 <                        V v = e.value;
1570 <                        if (value == null || value == v || value.equals(v)) {
1571 <                            if (pred == null)
1572 <                                setEntryAt(tab, index, next);
1573 <                            else
1574 <                                pred.setNext(next);
1575 <                            ++modCount;
1576 <                            --count;
1577 <                            oldValue = v;
1552 >    /** Implementation for compute */
1553 >    @SuppressWarnings("unchecked")
1554 >    private final Object internalCompute(K k, boolean onlyIfPresent,
1555 >                                             BiFun<? super K, ? super V, ? extends V> mf) {
1556 >        int h = spread(k.hashCode());
1557 >        Object val = null;
1558 >        int delta = 0;
1559 >        int count = 0;
1560 >        for (Node[] tab = table;;) {
1561 >            Node f; int i, fh; Object fk;
1562 >            if (tab == null)
1563 >                tab = initTable();
1564 >            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1565 >                if (onlyIfPresent)
1566 >                    break;
1567 >                Node node = new Node(fh = h | LOCKED, k, null, null);
1568 >                if (casTabAt(tab, i, null, node)) {
1569 >                    try {
1570 >                        count = 1;
1571 >                        if ((val = mf.apply(k, null)) != null) {
1572 >                            node.val = val;
1573 >                            delta = 1;
1574 >                        }
1575 >                    } finally {
1576 >                        if (delta == 0)
1577 >                            setTabAt(tab, i, null);
1578 >                        if (!node.casHash(fh, h)) {
1579 >                            node.hash = h;
1580 >                            synchronized (node) { node.notifyAll(); };
1581                          }
1582 +                    }
1583 +                }
1584 +                if (count != 0)
1585 +                    break;
1586 +            }
1587 +            else if ((fh = f.hash) == MOVED) {
1588 +                if ((fk = f.key) instanceof TreeBin) {
1589 +                    TreeBin t = (TreeBin)fk;
1590 +                    t.acquire(0);
1591 +                    try {
1592 +                        if (tabAt(tab, i) == f) {
1593 +                            count = 1;
1594 +                            TreeNode p = t.getTreeNode(h, k, t.root);
1595 +                            Object pv = (p == null) ? null : p.val;
1596 +                            if ((val = mf.apply(k, (V)pv)) != null) {
1597 +                                if (p != null)
1598 +                                    p.val = val;
1599 +                                else {
1600 +                                    count = 2;
1601 +                                    delta = 1;
1602 +                                    t.putTreeNode(h, k, val);
1603 +                                }
1604 +                            }
1605 +                            else if (p != null) {
1606 +                                delta = -1;
1607 +                                t.deleteTreeNode(p);
1608 +                            }
1609 +                        }
1610 +                    } finally {
1611 +                        t.release(0);
1612 +                    }
1613 +                    if (count != 0)
1614                          break;
1615 +                }
1616 +                else
1617 +                    tab = (Node[])fk;
1618 +            }
1619 +            else if ((fh & LOCKED) != 0) {
1620 +                checkForResize();
1621 +                f.tryAwaitLock(tab, i);
1622 +            }
1623 +            else if (f.casHash(fh, fh | LOCKED)) {
1624 +                try {
1625 +                    if (tabAt(tab, i) == f) {
1626 +                        count = 1;
1627 +                        for (Node e = f, pred = null;; ++count) {
1628 +                            Object ek, ev;
1629 +                            if ((e.hash & HASH_BITS) == h &&
1630 +                                (ev = e.val) != null &&
1631 +                                ((ek = e.key) == k || k.equals(ek))) {
1632 +                                val = mf.apply(k, (V)ev);
1633 +                                if (val != null)
1634 +                                    e.val = val;
1635 +                                else {
1636 +                                    delta = -1;
1637 +                                    Node en = e.next;
1638 +                                    if (pred != null)
1639 +                                        pred.next = en;
1640 +                                    else
1641 +                                        setTabAt(tab, i, en);
1642 +                                }
1643 +                                break;
1644 +                            }
1645 +                            pred = e;
1646 +                            if ((e = e.next) == null) {
1647 +                                if (!onlyIfPresent && (val = mf.apply(k, null)) != null) {
1648 +                                    pred.next = new Node(h, k, val, null);
1649 +                                    delta = 1;
1650 +                                    if (count >= TREE_THRESHOLD)
1651 +                                        replaceWithTreeBin(tab, i, k);
1652 +                                }
1653 +                                break;
1654 +                            }
1655 +                        }
1656 +                    }
1657 +                } finally {
1658 +                    if (!f.casHash(fh | LOCKED, fh)) {
1659 +                        f.hash = fh;
1660 +                        synchronized (f) { f.notifyAll(); };
1661                      }
548                    pred = e;
549                    e = next;
1662                  }
1663 <            } finally {
1664 <                unlock();
1663 >                if (count != 0) {
1664 >                    if (tab.length <= 64)
1665 >                        count = 2;
1666 >                    break;
1667 >                }
1668              }
554            return oldValue;
1669          }
1670 +        if (delta != 0) {
1671 +            counter.add((long)delta);
1672 +            if (count > 1)
1673 +                checkForResize();
1674 +        }
1675 +        return val;
1676 +    }
1677  
1678 <        final boolean replace(K key, int hash, V oldValue, V newValue) {
1679 <            if (!tryLock())
1680 <                scanAndLock(key, hash);
1681 <            boolean replaced = false;
1682 <            try {
1683 <                HashEntry<K,V> e;
1684 <                for (e = entryForHash(this, hash); e != null; e = e.next) {
1685 <                    K k;
1686 <                    if ((k = e.key) == key ||
1687 <                        (e.hash == hash && key.equals(k))) {
1688 <                        if (oldValue.equals(e.value)) {
1689 <                            e.value = newValue;
1690 <                            ++modCount;
1691 <                            replaced = true;
1678 >    /** Implementation for merge */
1679 >    @SuppressWarnings("unchecked")
1680 >    private final Object internalMerge(K k, V v,
1681 >                                       BiFun<? super V, ? super V, ? extends V> mf) {
1682 >        int h = spread(k.hashCode());
1683 >        Object val = null;
1684 >        int delta = 0;
1685 >        int count = 0;
1686 >        for (Node[] tab = table;;) {
1687 >            int i; Node f; int fh; Object fk, fv;
1688 >            if (tab == null)
1689 >                tab = initTable();
1690 >            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1691 >                if (casTabAt(tab, i, null, new Node(h, k, v, null))) {
1692 >                    delta = 1;
1693 >                    val = v;
1694 >                    break;
1695 >                }
1696 >            }
1697 >            else if ((fh = f.hash) == MOVED) {
1698 >                if ((fk = f.key) instanceof TreeBin) {
1699 >                    TreeBin t = (TreeBin)fk;
1700 >                    t.acquire(0);
1701 >                    try {
1702 >                        if (tabAt(tab, i) == f) {
1703 >                            count = 1;
1704 >                            TreeNode p = t.getTreeNode(h, k, t.root);
1705 >                            val = (p == null) ? v : mf.apply((V)p.val, v);
1706 >                            if (val != null) {
1707 >                                if (p != null)
1708 >                                    p.val = val;
1709 >                                else {
1710 >                                    count = 2;
1711 >                                    delta = 1;
1712 >                                    t.putTreeNode(h, k, val);
1713 >                                }
1714 >                            }
1715 >                            else if (p != null) {
1716 >                                delta = -1;
1717 >                                t.deleteTreeNode(p);
1718 >                            }
1719                          }
1720 +                    } finally {
1721 +                        t.release(0);
1722 +                    }
1723 +                    if (count != 0)
1724                          break;
1725 +                }
1726 +                else
1727 +                    tab = (Node[])fk;
1728 +            }
1729 +            else if ((fh & LOCKED) != 0) {
1730 +                checkForResize();
1731 +                f.tryAwaitLock(tab, i);
1732 +            }
1733 +            else if (f.casHash(fh, fh | LOCKED)) {
1734 +                try {
1735 +                    if (tabAt(tab, i) == f) {
1736 +                        count = 1;
1737 +                        for (Node e = f, pred = null;; ++count) {
1738 +                            Object ek, ev;
1739 +                            if ((e.hash & HASH_BITS) == h &&
1740 +                                (ev = e.val) != null &&
1741 +                                ((ek = e.key) == k || k.equals(ek))) {
1742 +                                val = mf.apply(v, (V)ev);
1743 +                                if (val != null)
1744 +                                    e.val = val;
1745 +                                else {
1746 +                                    delta = -1;
1747 +                                    Node en = e.next;
1748 +                                    if (pred != null)
1749 +                                        pred.next = en;
1750 +                                    else
1751 +                                        setTabAt(tab, i, en);
1752 +                                }
1753 +                                break;
1754 +                            }
1755 +                            pred = e;
1756 +                            if ((e = e.next) == null) {
1757 +                                val = v;
1758 +                                pred.next = new Node(h, k, val, null);
1759 +                                delta = 1;
1760 +                                if (count >= TREE_THRESHOLD)
1761 +                                    replaceWithTreeBin(tab, i, k);
1762 +                                break;
1763 +                            }
1764 +                        }
1765 +                    }
1766 +                } finally {
1767 +                    if (!f.casHash(fh | LOCKED, fh)) {
1768 +                        f.hash = fh;
1769 +                        synchronized (f) { f.notifyAll(); };
1770 +                    }
1771 +                }
1772 +                if (count != 0) {
1773 +                    if (tab.length <= 64)
1774 +                        count = 2;
1775 +                    break;
1776 +                }
1777 +            }
1778 +        }
1779 +        if (delta != 0) {
1780 +            counter.add((long)delta);
1781 +            if (count > 1)
1782 +                checkForResize();
1783 +        }
1784 +        return val;
1785 +    }
1786 +
1787 +    /** Implementation for putAll */
1788 +    private final void internalPutAll(Map<?, ?> m) {
1789 +        tryPresize(m.size());
1790 +        long delta = 0L;     // number of uncommitted additions
1791 +        boolean npe = false; // to throw exception on exit for nulls
1792 +        try {                // to clean up counts on other exceptions
1793 +            for (Map.Entry<?, ?> entry : m.entrySet()) {
1794 +                Object k, v;
1795 +                if (entry == null || (k = entry.getKey()) == null ||
1796 +                    (v = entry.getValue()) == null) {
1797 +                    npe = true;
1798 +                    break;
1799 +                }
1800 +                int h = spread(k.hashCode());
1801 +                for (Node[] tab = table;;) {
1802 +                    int i; Node f; int fh; Object fk;
1803 +                    if (tab == null)
1804 +                        tab = initTable();
1805 +                    else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null){
1806 +                        if (casTabAt(tab, i, null, new Node(h, k, v, null))) {
1807 +                            ++delta;
1808 +                            break;
1809 +                        }
1810 +                    }
1811 +                    else if ((fh = f.hash) == MOVED) {
1812 +                        if ((fk = f.key) instanceof TreeBin) {
1813 +                            TreeBin t = (TreeBin)fk;
1814 +                            boolean validated = false;
1815 +                            t.acquire(0);
1816 +                            try {
1817 +                                if (tabAt(tab, i) == f) {
1818 +                                    validated = true;
1819 +                                    TreeNode p = t.getTreeNode(h, k, t.root);
1820 +                                    if (p != null)
1821 +                                        p.val = v;
1822 +                                    else {
1823 +                                        t.putTreeNode(h, k, v);
1824 +                                        ++delta;
1825 +                                    }
1826 +                                }
1827 +                            } finally {
1828 +                                t.release(0);
1829 +                            }
1830 +                            if (validated)
1831 +                                break;
1832 +                        }
1833 +                        else
1834 +                            tab = (Node[])fk;
1835 +                    }
1836 +                    else if ((fh & LOCKED) != 0) {
1837 +                        counter.add(delta);
1838 +                        delta = 0L;
1839 +                        checkForResize();
1840 +                        f.tryAwaitLock(tab, i);
1841 +                    }
1842 +                    else if (f.casHash(fh, fh | LOCKED)) {
1843 +                        int count = 0;
1844 +                        try {
1845 +                            if (tabAt(tab, i) == f) {
1846 +                                count = 1;
1847 +                                for (Node e = f;; ++count) {
1848 +                                    Object ek, ev;
1849 +                                    if ((e.hash & HASH_BITS) == h &&
1850 +                                        (ev = e.val) != null &&
1851 +                                        ((ek = e.key) == k || k.equals(ek))) {
1852 +                                        e.val = v;
1853 +                                        break;
1854 +                                    }
1855 +                                    Node last = e;
1856 +                                    if ((e = e.next) == null) {
1857 +                                        ++delta;
1858 +                                        last.next = new Node(h, k, v, null);
1859 +                                        if (count >= TREE_THRESHOLD)
1860 +                                            replaceWithTreeBin(tab, i, k);
1861 +                                        break;
1862 +                                    }
1863 +                                }
1864 +                            }
1865 +                        } finally {
1866 +                            if (!f.casHash(fh | LOCKED, fh)) {
1867 +                                f.hash = fh;
1868 +                                synchronized (f) { f.notifyAll(); };
1869 +                            }
1870 +                        }
1871 +                        if (count != 0) {
1872 +                            if (count > 1) {
1873 +                                counter.add(delta);
1874 +                                delta = 0L;
1875 +                                checkForResize();
1876 +                            }
1877 +                            break;
1878 +                        }
1879                      }
1880                  }
575            } finally {
576                unlock();
1881              }
1882 <            return replaced;
1882 >        } finally {
1883 >            if (delta != 0)
1884 >                counter.add(delta);
1885          }
1886 +        if (npe)
1887 +            throw new NullPointerException();
1888 +    }
1889  
1890 <        final V replace(K key, int hash, V value) {
1891 <            if (!tryLock())
1892 <                scanAndLock(key, hash);
1893 <            V oldValue = null;
1894 <            try {
1895 <                HashEntry<K,V> e;
1896 <                for (e = entryForHash(this, hash); e != null; e = e.next) {
1897 <                    K k;
1898 <                    if ((k = e.key) == key ||
1899 <                        (e.hash == hash && key.equals(k))) {
1900 <                        oldValue = e.value;
1901 <                        e.value = value;
1902 <                        ++modCount;
1903 <                        break;
1890 >    /* ---------------- Table Initialization and Resizing -------------- */
1891 >
1892 >    /**
1893 >     * Returns a power of two table size for the given desired capacity.
1894 >     * See Hackers Delight, sec 3.2
1895 >     */
1896 >    private static final int tableSizeFor(int c) {
1897 >        int n = c - 1;
1898 >        n |= n >>> 1;
1899 >        n |= n >>> 2;
1900 >        n |= n >>> 4;
1901 >        n |= n >>> 8;
1902 >        n |= n >>> 16;
1903 >        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
1904 >    }
1905 >
1906 >    /**
1907 >     * Initializes table, using the size recorded in sizeCtl.
1908 >     */
1909 >    private final Node[] initTable() {
1910 >        Node[] tab; int sc;
1911 >        while ((tab = table) == null) {
1912 >            if ((sc = sizeCtl) < 0)
1913 >                Thread.yield(); // lost initialization race; just spin
1914 >            else if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1915 >                try {
1916 >                    if ((tab = table) == null) {
1917 >                        int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
1918 >                        tab = table = new Node[n];
1919 >                        sc = n - (n >>> 2);
1920                      }
1921 +                } finally {
1922 +                    sizeCtl = sc;
1923                  }
1924 <            } finally {
598 <                unlock();
1924 >                break;
1925              }
600            return oldValue;
1926          }
1927 +        return tab;
1928 +    }
1929  
1930 <        final void clear() {
1931 <            lock();
1930 >    /**
1931 >     * If table is too small and not already resizing, creates next
1932 >     * table and transfers bins.  Rechecks occupancy after a transfer
1933 >     * to see if another resize is already needed because resizings
1934 >     * are lagging additions.
1935 >     */
1936 >    private final void checkForResize() {
1937 >        Node[] tab; int n, sc;
1938 >        while ((tab = table) != null &&
1939 >               (n = tab.length) < MAXIMUM_CAPACITY &&
1940 >               (sc = sizeCtl) >= 0 && counter.sum() >= (long)sc &&
1941 >               UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1942              try {
1943 <                HashEntry<K,V>[] tab = table;
1944 <                for (int i = 0; i < tab.length ; i++)
1945 <                    setEntryAt(tab, i, null);
1946 <                ++modCount;
610 <                count = 0;
1943 >                if (tab == table) {
1944 >                    table = rebuild(tab);
1945 >                    sc = (n << 1) - (n >>> 1);
1946 >                }
1947              } finally {
1948 <                unlock();
1948 >                sizeCtl = sc;
1949              }
1950          }
1951      }
1952  
617    // Accessing segments
618
1953      /**
1954 <     * Gets the jth element of given segment array (if nonnull) with
1955 <     * volatile element access semantics via Unsafe. (The null check
1956 <     * can trigger harmlessly only during deserialization.) Note:
623 <     * because each element of segments array is set only once (using
624 <     * fully ordered writes), some performance-sensitive methods rely
625 <     * on this method only as a recheck upon null reads.
1954 >     * Tries to presize table to accommodate the given number of elements.
1955 >     *
1956 >     * @param size number of elements (doesn't need to be perfectly accurate)
1957       */
1958 <    @SuppressWarnings("unchecked")
1959 <    static final <K,V> Segment<K,V> segmentAt(Segment<K,V>[] ss, int j) {
1960 <        long u = (j << SSHIFT) + SBASE;
1961 <        return ss == null ? null :
1962 <            (Segment<K,V>) UNSAFE.getObjectVolatile(ss, u);
1958 >    private final void tryPresize(int size) {
1959 >        int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
1960 >            tableSizeFor(size + (size >>> 1) + 1);
1961 >        int sc;
1962 >        while ((sc = sizeCtl) >= 0) {
1963 >            Node[] tab = table; int n;
1964 >            if (tab == null || (n = tab.length) == 0) {
1965 >                n = (sc > c) ? sc : c;
1966 >                if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1967 >                    try {
1968 >                        if (table == tab) {
1969 >                            table = new Node[n];
1970 >                            sc = n - (n >>> 2);
1971 >                        }
1972 >                    } finally {
1973 >                        sizeCtl = sc;
1974 >                    }
1975 >                }
1976 >            }
1977 >            else if (c <= sc || n >= MAXIMUM_CAPACITY)
1978 >                break;
1979 >            else if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1980 >                try {
1981 >                    if (table == tab) {
1982 >                        table = rebuild(tab);
1983 >                        sc = (n << 1) - (n >>> 1);
1984 >                    }
1985 >                } finally {
1986 >                    sizeCtl = sc;
1987 >                }
1988 >            }
1989 >        }
1990      }
1991  
1992 <    /**
1993 <     * Returns the segment for the given index, creating it and
1994 <     * recording in segment table (via CAS) if not already present.
1992 >    /*
1993 >     * Moves and/or copies the nodes in each bin to new table. See
1994 >     * above for explanation.
1995       *
1996 <     * @param k the index
639 <     * @return the segment
1996 >     * @return the new table
1997       */
1998 <    @SuppressWarnings("unchecked")
1999 <    private Segment<K,V> ensureSegment(int k) {
2000 <        final Segment<K,V>[] ss = this.segments;
2001 <        long u = (k << SSHIFT) + SBASE; // raw offset
2002 <        Segment<K,V> seg;
2003 <        if ((seg = (Segment<K,V>)UNSAFE.getObjectVolatile(ss, u)) == null) {
2004 <            Segment<K,V> proto = ss[0]; // use segment 0 as prototype
2005 <            int cap = proto.table.length;
2006 <            float lf = proto.loadFactor;
2007 <            int threshold = (int)(cap * lf);
2008 <            HashEntry<K,V>[] tab = (HashEntry<K,V>[])new HashEntry<?,?>[cap];
2009 <            if ((seg = (Segment<K,V>)UNSAFE.getObjectVolatile(ss, u))
2010 <                == null) { // recheck
2011 <                Segment<K,V> s = new Segment<K,V>(lf, threshold, tab);
2012 <                while ((seg = (Segment<K,V>)UNSAFE.getObjectVolatile(ss, u))
2013 <                       == null) {
2014 <                    if (UNSAFE.compareAndSwapObject(ss, u, null, seg = s))
2015 <                        break;
1998 >    private static final Node[] rebuild(Node[] tab) {
1999 >        int n = tab.length;
2000 >        Node[] nextTab = new Node[n << 1];
2001 >        Node fwd = new Node(MOVED, nextTab, null, null);
2002 >        int[] buffer = null;       // holds bins to revisit; null until needed
2003 >        Node rev = null;           // reverse forwarder; null until needed
2004 >        int nbuffered = 0;         // the number of bins in buffer list
2005 >        int bufferIndex = 0;       // buffer index of current buffered bin
2006 >        int bin = n - 1;           // current non-buffered bin or -1 if none
2007 >
2008 >        for (int i = bin;;) {      // start upwards sweep
2009 >            int fh; Node f;
2010 >            if ((f = tabAt(tab, i)) == null) {
2011 >                if (bin >= 0) {    // no lock needed (or available)
2012 >                    if (!casTabAt(tab, i, f, fwd))
2013 >                        continue;
2014 >                }
2015 >                else {             // transiently use a locked forwarding node
2016 >                    Node g = new Node(MOVED|LOCKED, nextTab, null, null);
2017 >                    if (!casTabAt(tab, i, f, g))
2018 >                        continue;
2019 >                    setTabAt(nextTab, i, null);
2020 >                    setTabAt(nextTab, i + n, null);
2021 >                    setTabAt(tab, i, fwd);
2022 >                    if (!g.casHash(MOVED|LOCKED, MOVED)) {
2023 >                        g.hash = MOVED;
2024 >                        synchronized (g) { g.notifyAll(); }
2025 >                    }
2026                  }
2027              }
2028 +            else if ((fh = f.hash) == MOVED) {
2029 +                Object fk = f.key;
2030 +                if (fk instanceof TreeBin) {
2031 +                    TreeBin t = (TreeBin)fk;
2032 +                    boolean validated = false;
2033 +                    t.acquire(0);
2034 +                    try {
2035 +                        if (tabAt(tab, i) == f) {
2036 +                            validated = true;
2037 +                            splitTreeBin(nextTab, i, t);
2038 +                            setTabAt(tab, i, fwd);
2039 +                        }
2040 +                    } finally {
2041 +                        t.release(0);
2042 +                    }
2043 +                    if (!validated)
2044 +                        continue;
2045 +                }
2046 +            }
2047 +            else if ((fh & LOCKED) == 0 && f.casHash(fh, fh|LOCKED)) {
2048 +                boolean validated = false;
2049 +                try {              // split to lo and hi lists; copying as needed
2050 +                    if (tabAt(tab, i) == f) {
2051 +                        validated = true;
2052 +                        splitBin(nextTab, i, f);
2053 +                        setTabAt(tab, i, fwd);
2054 +                    }
2055 +                } finally {
2056 +                    if (!f.casHash(fh | LOCKED, fh)) {
2057 +                        f.hash = fh;
2058 +                        synchronized (f) { f.notifyAll(); };
2059 +                    }
2060 +                }
2061 +                if (!validated)
2062 +                    continue;
2063 +            }
2064 +            else {
2065 +                if (buffer == null) // initialize buffer for revisits
2066 +                    buffer = new int[TRANSFER_BUFFER_SIZE];
2067 +                if (bin < 0 && bufferIndex > 0) {
2068 +                    int j = buffer[--bufferIndex];
2069 +                    buffer[bufferIndex] = i;
2070 +                    i = j;         // swap with another bin
2071 +                    continue;
2072 +                }
2073 +                if (bin < 0 || nbuffered >= TRANSFER_BUFFER_SIZE) {
2074 +                    f.tryAwaitLock(tab, i);
2075 +                    continue;      // no other options -- block
2076 +                }
2077 +                if (rev == null)   // initialize reverse-forwarder
2078 +                    rev = new Node(MOVED, tab, null, null);
2079 +                if (tabAt(tab, i) != f || (f.hash & LOCKED) == 0)
2080 +                    continue;      // recheck before adding to list
2081 +                buffer[nbuffered++] = i;
2082 +                setTabAt(nextTab, i, rev);     // install place-holders
2083 +                setTabAt(nextTab, i + n, rev);
2084 +            }
2085 +
2086 +            if (bin > 0)
2087 +                i = --bin;
2088 +            else if (buffer != null && nbuffered > 0) {
2089 +                bin = -1;
2090 +                i = buffer[bufferIndex = --nbuffered];
2091 +            }
2092 +            else
2093 +                return nextTab;
2094          }
662        return seg;
2095      }
2096  
665    // Hash-based segment and entry accesses
666
2097      /**
2098 <     * Gets the segment for the given hash code.
2098 >     * Splits a normal bin with list headed by e into lo and hi parts;
2099 >     * installs in given table.
2100       */
2101 <    @SuppressWarnings("unchecked")
2102 <    private Segment<K,V> segmentForHash(int h) {
2103 <        long u = (((h >>> segmentShift) & segmentMask) << SSHIFT) + SBASE;
2104 <        return (Segment<K,V>) UNSAFE.getObjectVolatile(segments, u);
2101 >    private static void splitBin(Node[] nextTab, int i, Node e) {
2102 >        int bit = nextTab.length >>> 1; // bit to split on
2103 >        int runBit = e.hash & bit;
2104 >        Node lastRun = e, lo = null, hi = null;
2105 >        for (Node p = e.next; p != null; p = p.next) {
2106 >            int b = p.hash & bit;
2107 >            if (b != runBit) {
2108 >                runBit = b;
2109 >                lastRun = p;
2110 >            }
2111 >        }
2112 >        if (runBit == 0)
2113 >            lo = lastRun;
2114 >        else
2115 >            hi = lastRun;
2116 >        for (Node p = e; p != lastRun; p = p.next) {
2117 >            int ph = p.hash & HASH_BITS;
2118 >            Object pk = p.key, pv = p.val;
2119 >            if ((ph & bit) == 0)
2120 >                lo = new Node(ph, pk, pv, lo);
2121 >            else
2122 >                hi = new Node(ph, pk, pv, hi);
2123 >        }
2124 >        setTabAt(nextTab, i, lo);
2125 >        setTabAt(nextTab, i + bit, hi);
2126      }
2127  
2128      /**
2129 <     * Gets the table entry for the given segment and hash code.
2130 <     */
2131 <    @SuppressWarnings("unchecked")
2132 <    static final <K,V> HashEntry<K,V> entryForHash(Segment<K,V> seg, int h) {
2133 <        HashEntry<K,V>[] tab;
2134 <        return (seg == null || (tab = seg.table) == null) ? null :
2135 <            (HashEntry<K,V>) UNSAFE.getObjectVolatile
2136 <            (tab, ((long)(((tab.length - 1) & h)) << TSHIFT) + TBASE);
2129 >     * Splits a tree bin into lo and hi parts; installs in given table.
2130 >     */
2131 >    private static void splitTreeBin(Node[] nextTab, int i, TreeBin t) {
2132 >        int bit = nextTab.length >>> 1;
2133 >        TreeBin lt = new TreeBin();
2134 >        TreeBin ht = new TreeBin();
2135 >        int lc = 0, hc = 0;
2136 >        for (Node e = t.first; e != null; e = e.next) {
2137 >            int h = e.hash & HASH_BITS;
2138 >            Object k = e.key, v = e.val;
2139 >            if ((h & bit) == 0) {
2140 >                ++lc;
2141 >                lt.putTreeNode(h, k, v);
2142 >            }
2143 >            else {
2144 >                ++hc;
2145 >                ht.putTreeNode(h, k, v);
2146 >            }
2147 >        }
2148 >        Node ln, hn; // throw away trees if too small
2149 >        if (lc <= (TREE_THRESHOLD >>> 1)) {
2150 >            ln = null;
2151 >            for (Node p = lt.first; p != null; p = p.next)
2152 >                ln = new Node(p.hash, p.key, p.val, ln);
2153 >        }
2154 >        else
2155 >            ln = new Node(MOVED, lt, null, null);
2156 >        setTabAt(nextTab, i, ln);
2157 >        if (hc <= (TREE_THRESHOLD >>> 1)) {
2158 >            hn = null;
2159 >            for (Node p = ht.first; p != null; p = p.next)
2160 >                hn = new Node(p.hash, p.key, p.val, hn);
2161 >        }
2162 >        else
2163 >            hn = new Node(MOVED, ht, null, null);
2164 >        setTabAt(nextTab, i + bit, hn);
2165      }
2166  
2167 <    /* ---------------- Public operations -------------- */
2167 >    /**
2168 >     * Implementation for clear. Steps through each bin, removing all
2169 >     * nodes.
2170 >     */
2171 >    private final void internalClear() {
2172 >        long delta = 0L; // negative number of deletions
2173 >        int i = 0;
2174 >        Node[] tab = table;
2175 >        while (tab != null && i < tab.length) {
2176 >            int fh; Object fk;
2177 >            Node f = tabAt(tab, i);
2178 >            if (f == null)
2179 >                ++i;
2180 >            else if ((fh = f.hash) == MOVED) {
2181 >                if ((fk = f.key) instanceof TreeBin) {
2182 >                    TreeBin t = (TreeBin)fk;
2183 >                    t.acquire(0);
2184 >                    try {
2185 >                        if (tabAt(tab, i) == f) {
2186 >                            for (Node p = t.first; p != null; p = p.next) {
2187 >                                p.val = null;
2188 >                                --delta;
2189 >                            }
2190 >                            t.first = null;
2191 >                            t.root = null;
2192 >                            ++i;
2193 >                        }
2194 >                    } finally {
2195 >                        t.release(0);
2196 >                    }
2197 >                }
2198 >                else
2199 >                    tab = (Node[])fk;
2200 >            }
2201 >            else if ((fh & LOCKED) != 0) {
2202 >                counter.add(delta); // opportunistically update count
2203 >                delta = 0L;
2204 >                f.tryAwaitLock(tab, i);
2205 >            }
2206 >            else if (f.casHash(fh, fh | LOCKED)) {
2207 >                try {
2208 >                    if (tabAt(tab, i) == f) {
2209 >                        for (Node e = f; e != null; e = e.next) {
2210 >                            e.val = null;
2211 >                            --delta;
2212 >                        }
2213 >                        setTabAt(tab, i, null);
2214 >                        ++i;
2215 >                    }
2216 >                } finally {
2217 >                    if (!f.casHash(fh | LOCKED, fh)) {
2218 >                        f.hash = fh;
2219 >                        synchronized (f) { f.notifyAll(); };
2220 >                    }
2221 >                }
2222 >            }
2223 >        }
2224 >        if (delta != 0)
2225 >            counter.add(delta);
2226 >    }
2227 >
2228 >    /* ----------------Table Traversal -------------- */
2229  
2230      /**
2231 <     * Creates a new, empty map with the specified initial
2232 <     * capacity, load factor and concurrency level.
2231 >     * Encapsulates traversal for methods such as containsValue; also
2232 >     * serves as a base class for other iterators and bulk tasks.
2233       *
2234 <     * @param initialCapacity the initial capacity. The implementation
2235 <     * performs internal sizing to accommodate this many elements.
2236 <     * @param loadFactor  the load factor threshold, used to control resizing.
2237 <     * Resizing may be performed when the average number of elements per
2238 <     * bin exceeds this threshold.
2239 <     * @param concurrencyLevel the estimated number of concurrently
2240 <     * updating threads. The implementation performs internal sizing
2241 <     * to try to accommodate this many threads.
2242 <     * @throws IllegalArgumentException if the initial capacity is
2243 <     * negative or the load factor or concurrencyLevel are
2244 <     * nonpositive.
2234 >     * At each step, the iterator snapshots the key ("nextKey") and
2235 >     * value ("nextVal") of a valid node (i.e., one that, at point of
2236 >     * snapshot, has a non-null user value). Because val fields can
2237 >     * change (including to null, indicating deletion), field nextVal
2238 >     * might not be accurate at point of use, but still maintains the
2239 >     * weak consistency property of holding a value that was once
2240 >     * valid.
2241 >     *
2242 >     * Internal traversals directly access these fields, as in:
2243 >     * {@code while (it.advance() != null) { process(it.nextKey); }}
2244 >     *
2245 >     * Exported iterators must track whether the iterator has advanced
2246 >     * (in hasNext vs next) (by setting/checking/nulling field
2247 >     * nextVal), and then extract key, value, or key-value pairs as
2248 >     * return values of next().
2249 >     *
2250 >     * The iterator visits once each still-valid node that was
2251 >     * reachable upon iterator construction. It might miss some that
2252 >     * were added to a bin after the bin was visited, which is OK wrt
2253 >     * consistency guarantees. Maintaining this property in the face
2254 >     * of possible ongoing resizes requires a fair amount of
2255 >     * bookkeeping state that is difficult to optimize away amidst
2256 >     * volatile accesses.  Even so, traversal maintains reasonable
2257 >     * throughput.
2258 >     *
2259 >     * Normally, iteration proceeds bin-by-bin traversing lists.
2260 >     * However, if the table has been resized, then all future steps
2261 >     * must traverse both the bin at the current index as well as at
2262 >     * (index + baseSize); and so on for further resizings. To
2263 >     * paranoically cope with potential sharing by users of iterators
2264 >     * across threads, iteration terminates if a bounds checks fails
2265 >     * for a table read.
2266 >     *
2267 >     * This class extends ForkJoinTask to streamline parallel
2268 >     * iteration in bulk operations (see BulkTask). This adds only an
2269 >     * int of space overhead, which is close enough to negligible in
2270 >     * cases where it is not needed to not worry about it.  Because
2271 >     * ForkJoinTask is Serializable, but iterators need not be, we
2272 >     * need to add warning suppressions.
2273 >     */
2274 >    @SuppressWarnings("serial")
2275 >    static class Traverser<K,V,R> extends ForkJoinTask<R> {
2276 >        final ConcurrentHashMap<K, V> map;
2277 >        Node next;           // the next entry to use
2278 >        Node last;           // the last entry used
2279 >        Object nextKey;      // cached key field of next
2280 >        Object nextVal;      // cached val field of next
2281 >        Node[] tab;          // current table; updated if resized
2282 >        int index;           // index of bin to use next
2283 >        int baseIndex;       // current index of initial table
2284 >        int baseLimit;       // index bound for initial table
2285 >        final int baseSize;  // initial table size
2286 >
2287 >        /** Creates iterator for all entries in the table. */
2288 >        Traverser(ConcurrentHashMap<K, V> map) {
2289 >            this.tab = (this.map = map).table;
2290 >            baseLimit = baseSize = (tab == null) ? 0 : tab.length;
2291 >        }
2292 >
2293 >        /** Creates iterator for split() methods */
2294 >        Traverser(Traverser<K,V,?> it, boolean split) {
2295 >            this.map = it.map;
2296 >            this.tab = it.tab;
2297 >            this.baseSize = it.baseSize;
2298 >            int lo = it.baseIndex;
2299 >            int hi = this.baseLimit = it.baseLimit;
2300 >            int i;
2301 >            if (split) // adjust parent
2302 >                i = it.baseLimit = (lo + hi + 1) >>> 1;
2303 >            else       // clone parent
2304 >                i = lo;
2305 >            this.index = this.baseIndex = i;
2306 >        }
2307 >
2308 >        /**
2309 >         * Advances next; returns nextVal or null if terminated.
2310 >         * See above for explanation.
2311 >         */
2312 >        final Object advance() {
2313 >            Node e = last = next;
2314 >            Object ev = null;
2315 >            outer: do {
2316 >                if (e != null)                  // advance past used/skipped node
2317 >                    e = e.next;
2318 >                while (e == null) {             // get to next non-null bin
2319 >                    Node[] t; int b, i, n; Object ek; // checks must use locals
2320 >                    if ((b = baseIndex) >= baseLimit || (i = index) < 0 ||
2321 >                        (t = tab) == null || i >= (n = t.length))
2322 >                        break outer;
2323 >                    else if ((e = tabAt(t, i)) != null && e.hash == MOVED) {
2324 >                        if ((ek = e.key) instanceof TreeBin)
2325 >                            e = ((TreeBin)ek).first;
2326 >                        else {
2327 >                            tab = (Node[])ek;
2328 >                            continue;           // restarts due to null val
2329 >                        }
2330 >                    }                           // visit upper slots if present
2331 >                    index = (i += baseSize) < n ? i : (baseIndex = b + 1);
2332 >                }
2333 >                nextKey = e.key;
2334 >            } while ((ev = e.val) == null);    // skip deleted or special nodes
2335 >            next = e;
2336 >            return nextVal = ev;
2337 >        }
2338 >
2339 >        public final void remove() {
2340 >            if (nextVal == null && last == null)
2341 >                advance();
2342 >            Node e = last;
2343 >            if (e == null)
2344 >                throw new IllegalStateException();
2345 >            last = null;
2346 >            map.remove(e.key);
2347 >        }
2348 >
2349 >        public final boolean hasNext() {
2350 >            return nextVal != null || advance() != null;
2351 >        }
2352 >
2353 >        public final boolean hasMoreElements() { return hasNext(); }
2354 >        public final void setRawResult(Object x) { }
2355 >        public R getRawResult() { return null; }
2356 >        public boolean exec() { return true; }
2357 >    }
2358 >
2359 >    /* ---------------- Public operations -------------- */
2360 >
2361 >    /**
2362 >     * Creates a new, empty map with the default initial table size (16).
2363       */
2364 <    @SuppressWarnings("unchecked")
2365 <    public ConcurrentHashMap(int initialCapacity,
707 <                             float loadFactor, int concurrencyLevel) {
708 <        if (!(loadFactor > 0) || initialCapacity < 0 || concurrencyLevel <= 0)
709 <            throw new IllegalArgumentException();
710 <        if (concurrencyLevel > MAX_SEGMENTS)
711 <            concurrencyLevel = MAX_SEGMENTS;
712 <        // Find power-of-two sizes best matching arguments
713 <        int sshift = 0;
714 <        int ssize = 1;
715 <        while (ssize < concurrencyLevel) {
716 <            ++sshift;
717 <            ssize <<= 1;
718 <        }
719 <        this.segmentShift = 32 - sshift;
720 <        this.segmentMask = ssize - 1;
721 <        if (initialCapacity > MAXIMUM_CAPACITY)
722 <            initialCapacity = MAXIMUM_CAPACITY;
723 <        int c = initialCapacity / ssize;
724 <        if (c * ssize < initialCapacity)
725 <            ++c;
726 <        int cap = MIN_SEGMENT_TABLE_CAPACITY;
727 <        while (cap < c)
728 <            cap <<= 1;
729 <        // create segments and segments[0]
730 <        Segment<K,V> s0 =
731 <            new Segment<K,V>(loadFactor, (int)(cap * loadFactor),
732 <                             (HashEntry<K,V>[])new HashEntry<?,?>[cap]);
733 <        Segment<K,V>[] ss = (Segment<K,V>[])new Segment<?,?>[ssize];
734 <        UNSAFE.putOrderedObject(ss, SBASE, s0); // ordered write of segments[0]
735 <        this.segments = ss;
2364 >    public ConcurrentHashMap() {
2365 >        this.counter = new LongAdder();
2366      }
2367  
2368      /**
2369 <     * Creates a new, empty map with the specified initial capacity
2370 <     * and load factor and with the default concurrencyLevel (16).
2369 >     * Creates a new, empty map with an initial table size
2370 >     * accommodating the specified number of elements without the need
2371 >     * to dynamically resize.
2372       *
2373       * @param initialCapacity The implementation performs internal
2374       * sizing to accommodate this many elements.
744     * @param loadFactor  the load factor threshold, used to control resizing.
745     * Resizing may be performed when the average number of elements per
746     * bin exceeds this threshold.
2375       * @throws IllegalArgumentException if the initial capacity of
2376 <     * elements is negative or the load factor is nonpositive
749 <     *
750 <     * @since 1.6
2376 >     * elements is negative
2377       */
2378 <    public ConcurrentHashMap(int initialCapacity, float loadFactor) {
2379 <        this(initialCapacity, loadFactor, DEFAULT_CONCURRENCY_LEVEL);
2378 >    public ConcurrentHashMap(int initialCapacity) {
2379 >        if (initialCapacity < 0)
2380 >            throw new IllegalArgumentException();
2381 >        int cap = ((initialCapacity >= (MAXIMUM_CAPACITY >>> 1)) ?
2382 >                   MAXIMUM_CAPACITY :
2383 >                   tableSizeFor(initialCapacity + (initialCapacity >>> 1) + 1));
2384 >        this.counter = new LongAdder();
2385 >        this.sizeCtl = cap;
2386      }
2387  
2388      /**
2389 <     * Creates a new, empty map with the specified initial capacity,
758 <     * and with default load factor (0.75) and concurrencyLevel (16).
2389 >     * Creates a new map with the same mappings as the given map.
2390       *
2391 <     * @param initialCapacity the initial capacity. The implementation
761 <     * performs internal sizing to accommodate this many elements.
762 <     * @throws IllegalArgumentException if the initial capacity of
763 <     * elements is negative.
2391 >     * @param m the map
2392       */
2393 <    public ConcurrentHashMap(int initialCapacity) {
2394 <        this(initialCapacity, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
2393 >    public ConcurrentHashMap(Map<? extends K, ? extends V> m) {
2394 >        this.counter = new LongAdder();
2395 >        this.sizeCtl = DEFAULT_CAPACITY;
2396 >        internalPutAll(m);
2397      }
2398  
2399      /**
2400 <     * Creates a new, empty map with a default initial capacity (16),
2401 <     * load factor (0.75) and concurrencyLevel (16).
2400 >     * Creates a new, empty map with an initial table size based on
2401 >     * the given number of elements ({@code initialCapacity}) and
2402 >     * initial table density ({@code loadFactor}).
2403 >     *
2404 >     * @param initialCapacity the initial capacity. The implementation
2405 >     * performs internal sizing to accommodate this many elements,
2406 >     * given the specified load factor.
2407 >     * @param loadFactor the load factor (table density) for
2408 >     * establishing the initial table size
2409 >     * @throws IllegalArgumentException if the initial capacity of
2410 >     * elements is negative or the load factor is nonpositive
2411 >     *
2412 >     * @since 1.6
2413       */
2414 <    public ConcurrentHashMap() {
2415 <        this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
2414 >    public ConcurrentHashMap(int initialCapacity, float loadFactor) {
2415 >        this(initialCapacity, loadFactor, 1);
2416      }
2417  
2418      /**
2419 <     * Creates a new map with the same mappings as the given map.
2420 <     * The map is created with a capacity of 1.5 times the number
2421 <     * of mappings in the given map or 16 (whichever is greater),
2422 <     * and a default load factor (0.75) and concurrencyLevel (16).
2419 >     * Creates a new, empty map with an initial table size based on
2420 >     * the given number of elements ({@code initialCapacity}), table
2421 >     * density ({@code loadFactor}), and number of concurrently
2422 >     * updating threads ({@code concurrencyLevel}).
2423       *
2424 <     * @param m the map
2424 >     * @param initialCapacity the initial capacity. The implementation
2425 >     * performs internal sizing to accommodate this many elements,
2426 >     * given the specified load factor.
2427 >     * @param loadFactor the load factor (table density) for
2428 >     * establishing the initial table size
2429 >     * @param concurrencyLevel the estimated number of concurrently
2430 >     * updating threads. The implementation may use this value as
2431 >     * a sizing hint.
2432 >     * @throws IllegalArgumentException if the initial capacity is
2433 >     * negative or the load factor or concurrencyLevel are
2434 >     * nonpositive
2435       */
2436 <    public ConcurrentHashMap(Map<? extends K, ? extends V> m) {
2437 <        this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
2438 <                      DEFAULT_INITIAL_CAPACITY),
2439 <             DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
2440 <        putAll(m);
2436 >    public ConcurrentHashMap(int initialCapacity,
2437 >                               float loadFactor, int concurrencyLevel) {
2438 >        if (!(loadFactor > 0.0f) || initialCapacity < 0 || concurrencyLevel <= 0)
2439 >            throw new IllegalArgumentException();
2440 >        if (initialCapacity < concurrencyLevel)   // Use at least as many bins
2441 >            initialCapacity = concurrencyLevel;   // as estimated threads
2442 >        long size = (long)(1.0 + (long)initialCapacity / loadFactor);
2443 >        int cap = (size >= (long)MAXIMUM_CAPACITY) ?
2444 >            MAXIMUM_CAPACITY : tableSizeFor((int)size);
2445 >        this.counter = new LongAdder();
2446 >        this.sizeCtl = cap;
2447      }
2448  
2449      /**
2450 <     * Returns <tt>true</tt> if this map contains no key-value mappings.
794 <     *
795 <     * @return <tt>true</tt> if this map contains no key-value mappings
2450 >     * {@inheritDoc}
2451       */
2452      public boolean isEmpty() {
2453 <        /*
799 <         * Sum per-segment modCounts to avoid mis-reporting when
800 <         * elements are concurrently added and removed in one segment
801 <         * while checking another, in which case the table was never
802 <         * actually empty at any point. (The sum ensures accuracy up
803 <         * through at least 1<<31 per-segment modifications before
804 <         * recheck.)  Methods size() and containsValue() use similar
805 <         * constructions for stability checks.
806 <         */
807 <        long sum = 0L;
808 <        final Segment<K,V>[] segments = this.segments;
809 <        for (int j = 0; j < segments.length; ++j) {
810 <            Segment<K,V> seg = segmentAt(segments, j);
811 <            if (seg != null) {
812 <                if (seg.count != 0)
813 <                    return false;
814 <                sum += seg.modCount;
815 <            }
816 <        }
817 <        if (sum != 0L) { // recheck unless no modifications
818 <            for (int j = 0; j < segments.length; ++j) {
819 <                Segment<K,V> seg = segmentAt(segments, j);
820 <                if (seg != null) {
821 <                    if (seg.count != 0)
822 <                        return false;
823 <                    sum -= seg.modCount;
824 <                }
825 <            }
826 <            if (sum != 0L)
827 <                return false;
828 <        }
829 <        return true;
2453 >        return counter.sum() <= 0L; // ignore transient negative values
2454      }
2455  
2456      /**
2457 <     * Returns the number of key-value mappings in this map.  If the
834 <     * map contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
835 <     * <tt>Integer.MAX_VALUE</tt>.
836 <     *
837 <     * @return the number of key-value mappings in this map
2457 >     * {@inheritDoc}
2458       */
2459      public int size() {
2460 <        // Try a few times to get accurate count. On failure due to
2461 <        // continuous async changes in table, resort to locking.
2462 <        final Segment<K,V>[] segments = this.segments;
2463 <        final int segmentCount = segments.length;
2464 <
845 <        long previousSum = 0L;
846 <        for (int retries = -1; retries < RETRIES_BEFORE_LOCK; retries++) {
847 <            long sum = 0L;    // sum of modCounts
848 <            long size = 0L;
849 <            for (int i = 0; i < segmentCount; i++) {
850 <                Segment<K,V> segment = segmentAt(segments, i);
851 <                if (segment != null) {
852 <                    sum += segment.modCount;
853 <                    size += segment.count;
854 <                }
855 <            }
856 <            if (sum == previousSum)
857 <                return ((size >>> 31) == 0) ? (int) size : Integer.MAX_VALUE;
858 <            previousSum = sum;
859 <        }
2460 >        long n = counter.sum();
2461 >        return ((n < 0L) ? 0 :
2462 >                (n > (long)Integer.MAX_VALUE) ? Integer.MAX_VALUE :
2463 >                (int)n);
2464 >    }
2465  
2466 <        long size = 0L;
2467 <        for (int i = 0; i < segmentCount; i++) {
2468 <            Segment<K,V> segment = ensureSegment(i);
2469 <            segment.lock();
2470 <            size += segment.count;
2471 <        }
2472 <        for (int i = 0; i < segmentCount; i++)
2473 <            segments[i].unlock();
2474 <        return ((size >>> 31) == 0) ? (int) size : Integer.MAX_VALUE;
2466 >    /**
2467 >     * Returns the number of mappings. This method should be used
2468 >     * instead of {@link #size} because a ConcurrentHashMap may
2469 >     * contain more mappings than can be represented as an int. The
2470 >     * value returned is a snapshot; the actual count may differ if
2471 >     * there are ongoing concurrent insertions of removals.
2472 >     *
2473 >     * @return the number of mappings
2474 >     */
2475 >    public long mappingCount() {
2476 >        long n = counter.sum();
2477 >        return (n < 0L) ? 0L : n; // ignore transient negative values
2478      }
2479  
2480      /**
# Line 880 | Line 2488 | public class ConcurrentHashMap<K, V> ext
2488       *
2489       * @throws NullPointerException if the specified key is null
2490       */
2491 +    @SuppressWarnings("unchecked")
2492      public V get(Object key) {
2493 <        Segment<K,V> s; // manually integrate access methods to reduce overhead
2494 <        HashEntry<K,V>[] tab;
2495 <        int h = hash(key.hashCode());
887 <        long u = (((h >>> segmentShift) & segmentMask) << SSHIFT) + SBASE;
888 <        if ((s = (Segment<K,V>)UNSAFE.getObjectVolatile(segments, u)) != null &&
889 <            (tab = s.table) != null) {
890 <            for (HashEntry<K,V> e = (HashEntry<K,V>) UNSAFE.getObjectVolatile
891 <                     (tab, ((long)(((tab.length - 1) & h)) << TSHIFT) + TBASE);
892 <                 e != null; e = e.next) {
893 <                K k;
894 <                if ((k = e.key) == key || (e.hash == h && key.equals(k)))
895 <                    return e.value;
896 <            }
897 <        }
898 <        return null;
2493 >        if (key == null)
2494 >            throw new NullPointerException();
2495 >        return (V)internalGet(key);
2496      }
2497  
2498      /**
2499       * Tests if the specified object is a key in this table.
2500       *
2501       * @param  key   possible key
2502 <     * @return <tt>true</tt> if and only if the specified object
2502 >     * @return {@code true} if and only if the specified object
2503       *         is a key in this table, as determined by the
2504 <     *         <tt>equals</tt> method; <tt>false</tt> otherwise.
2504 >     *         {@code equals} method; {@code false} otherwise
2505       * @throws NullPointerException if the specified key is null
2506       */
910    @SuppressWarnings("unchecked")
2507      public boolean containsKey(Object key) {
2508 <        Segment<K,V> s; // same as get() except no need for volatile value read
2509 <        HashEntry<K,V>[] tab;
2510 <        int h = hash(key.hashCode());
915 <        long u = (((h >>> segmentShift) & segmentMask) << SSHIFT) + SBASE;
916 <        if ((s = (Segment<K,V>)UNSAFE.getObjectVolatile(segments, u)) != null &&
917 <            (tab = s.table) != null) {
918 <            for (HashEntry<K,V> e = (HashEntry<K,V>) UNSAFE.getObjectVolatile
919 <                     (tab, ((long)(((tab.length - 1) & h)) << TSHIFT) + TBASE);
920 <                 e != null; e = e.next) {
921 <                K k;
922 <                if ((k = e.key) == key || (e.hash == h && key.equals(k)))
923 <                    return true;
924 <            }
925 <        }
926 <        return false;
2508 >        if (key == null)
2509 >            throw new NullPointerException();
2510 >        return internalGet(key) != null;
2511      }
2512  
2513      /**
2514 <     * Returns <tt>true</tt> if this map maps one or more keys to the
2515 <     * specified value. Note: This method requires a full internal
2516 <     * traversal of the hash table, and so is much slower than
933 <     * method <tt>containsKey</tt>.
2514 >     * Returns {@code true} if this map maps one or more keys to the
2515 >     * specified value. Note: This method may require a full traversal
2516 >     * of the map, and is much slower than method {@code containsKey}.
2517       *
2518       * @param value value whose presence in this map is to be tested
2519 <     * @return <tt>true</tt> if this map maps one or more keys to the
2519 >     * @return {@code true} if this map maps one or more keys to the
2520       *         specified value
2521       * @throws NullPointerException if the specified value is null
2522       */
2523      public boolean containsValue(Object value) {
941        // Same idea as size()
2524          if (value == null)
2525              throw new NullPointerException();
2526 <        final Segment<K,V>[] segments = this.segments;
2527 <        long previousSum = 0L;
2528 <        int lockCount = 0;
2529 <        try {
2530 <            for (int retries = -1; ; retries++) {
949 <                long sum = 0L;    // sum of modCounts
950 <                for (int j = 0; j < segments.length; j++) {
951 <                    Segment<K,V> segment;
952 <                    if (retries == RETRIES_BEFORE_LOCK) {
953 <                        segment = ensureSegment(j);
954 <                        segment.lock();
955 <                        lockCount++;
956 <                    } else {
957 <                        segment = segmentAt(segments, j);
958 <                        if (segment == null)
959 <                            continue;
960 <                    }
961 <                    HashEntry<K,V>[] tab = segment.table;
962 <                    if (tab != null) {
963 <                        for (int i = 0 ; i < tab.length; i++) {
964 <                            HashEntry<K,V> e;
965 <                            for (e = entryAt(tab, i); e != null; e = e.next) {
966 <                                V v = e.value;
967 <                                if (v != null && value.equals(v))
968 <                                    return true;
969 <                            }
970 <                        }
971 <                        sum += segment.modCount;
972 <                    }
973 <                }
974 <                if ((retries >= 0 && sum == previousSum) || lockCount > 0)
975 <                    return false;
976 <                previousSum = sum;
977 <            }
978 <        } finally {
979 <            for (int j = 0; j < lockCount; j++)
980 <                segments[j].unlock();
2526 >        Object v;
2527 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
2528 >        while ((v = it.advance()) != null) {
2529 >            if (v == value || value.equals(v))
2530 >                return true;
2531          }
2532 +        return false;
2533      }
2534  
2535      /**
# Line 990 | Line 2541 | public class ConcurrentHashMap<K, V> ext
2541       * Java Collections framework.
2542       *
2543       * @param  value a value to search for
2544 <     * @return <tt>true</tt> if and only if some key maps to the
2545 <     *         <tt>value</tt> argument in this table as
2546 <     *         determined by the <tt>equals</tt> method;
2547 <     *         <tt>false</tt> otherwise
2544 >     * @return {@code true} if and only if some key maps to the
2545 >     *         {@code value} argument in this table as
2546 >     *         determined by the {@code equals} method;
2547 >     *         {@code false} otherwise
2548       * @throws NullPointerException if the specified value is null
2549       */
2550      public boolean contains(Object value) {
# Line 1004 | Line 2555 | public class ConcurrentHashMap<K, V> ext
2555       * Maps the specified key to the specified value in this table.
2556       * Neither the key nor the value can be null.
2557       *
2558 <     * <p> The value can be retrieved by calling the <tt>get</tt> method
2558 >     * <p> The value can be retrieved by calling the {@code get} method
2559       * with a key that is equal to the original key.
2560       *
2561       * @param key key with which the specified value is to be associated
2562       * @param value value to be associated with the specified key
2563 <     * @return the previous value associated with <tt>key</tt>, or
2564 <     *         <tt>null</tt> if there was no mapping for <tt>key</tt>
2563 >     * @return the previous value associated with {@code key}, or
2564 >     *         {@code null} if there was no mapping for {@code key}
2565       * @throws NullPointerException if the specified key or value is null
2566       */
2567      @SuppressWarnings("unchecked")
2568      public V put(K key, V value) {
2569 <        Segment<K,V> s;
1019 <        if (value == null)
2569 >        if (key == null || value == null)
2570              throw new NullPointerException();
2571 <        int hash = hash(key.hashCode());
1022 <        int j = (hash >>> segmentShift) & segmentMask;
1023 <        if ((s = (Segment<K,V>)UNSAFE.getObject          // nonvolatile; recheck
1024 <             (segments, (j << SSHIFT) + SBASE)) == null) //  in ensureSegment
1025 <            s = ensureSegment(j);
1026 <        return s.put(key, hash, value, false);
2571 >        return (V)internalPut(key, value);
2572      }
2573  
2574      /**
2575       * {@inheritDoc}
2576       *
2577       * @return the previous value associated with the specified key,
2578 <     *         or <tt>null</tt> if there was no mapping for the key
2578 >     *         or {@code null} if there was no mapping for the key
2579       * @throws NullPointerException if the specified key or value is null
2580       */
2581      @SuppressWarnings("unchecked")
2582      public V putIfAbsent(K key, V value) {
2583 <        Segment<K,V> s;
1039 <        if (value == null)
2583 >        if (key == null || value == null)
2584              throw new NullPointerException();
2585 <        int hash = hash(key.hashCode());
1042 <        int j = (hash >>> segmentShift) & segmentMask;
1043 <        if ((s = (Segment<K,V>)UNSAFE.getObject
1044 <             (segments, (j << SSHIFT) + SBASE)) == null)
1045 <            s = ensureSegment(j);
1046 <        return s.put(key, hash, value, true);
2585 >        return (V)internalPutIfAbsent(key, value);
2586      }
2587  
2588      /**
# Line 1054 | Line 2593 | public class ConcurrentHashMap<K, V> ext
2593       * @param m mappings to be stored in this map
2594       */
2595      public void putAll(Map<? extends K, ? extends V> m) {
2596 <        for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
2597 <            put(e.getKey(), e.getValue());
2596 >        internalPutAll(m);
2597 >    }
2598 >
2599 >    /**
2600 >     * If the specified key is not already associated with a value,
2601 >     * computes its value using the given mappingFunction and enters
2602 >     * it into the map unless null.  This is equivalent to
2603 >     * <pre> {@code
2604 >     * if (map.containsKey(key))
2605 >     *   return map.get(key);
2606 >     * value = mappingFunction.apply(key);
2607 >     * if (value != null)
2608 >     *   map.put(key, value);
2609 >     * return value;}</pre>
2610 >     *
2611 >     * except that the action is performed atomically.  If the
2612 >     * function returns {@code null} no mapping is recorded. If the
2613 >     * function itself throws an (unchecked) exception, the exception
2614 >     * is rethrown to its caller, and no mapping is recorded.  Some
2615 >     * attempted update operations on this map by other threads may be
2616 >     * blocked while computation is in progress, so the computation
2617 >     * should be short and simple, and must not attempt to update any
2618 >     * other mappings of this Map. The most appropriate usage is to
2619 >     * construct a new object serving as an initial mapped value, or
2620 >     * memoized result, as in:
2621 >     *
2622 >     *  <pre> {@code
2623 >     * map.computeIfAbsent(key, new Fun<K, V>() {
2624 >     *   public V map(K k) { return new Value(f(k)); }});}</pre>
2625 >     *
2626 >     * @param key key with which the specified value is to be associated
2627 >     * @param mappingFunction the function to compute a value
2628 >     * @return the current (existing or computed) value associated with
2629 >     *         the specified key, or null if the computed value is null.
2630 >     * @throws NullPointerException if the specified key or mappingFunction
2631 >     *         is null
2632 >     * @throws IllegalStateException if the computation detectably
2633 >     *         attempts a recursive update to this map that would
2634 >     *         otherwise never complete
2635 >     * @throws RuntimeException or Error if the mappingFunction does so,
2636 >     *         in which case the mapping is left unestablished
2637 >     */
2638 >    @SuppressWarnings("unchecked")
2639 >    public V computeIfAbsent(K key, Fun<? super K, ? extends V> mappingFunction) {
2640 >        if (key == null || mappingFunction == null)
2641 >            throw new NullPointerException();
2642 >        return (V)internalComputeIfAbsent(key, mappingFunction);
2643 >    }
2644 >
2645 >    /**
2646 >     * If the given key is present, computes a new mapping value given a key and
2647 >     * its current mapped value. This is equivalent to
2648 >     *  <pre> {@code
2649 >     *   if (map.containsKey(key)) {
2650 >     *     value = remappingFunction.apply(key, map.get(key));
2651 >     *     if (value != null)
2652 >     *       map.put(key, value);
2653 >     *     else
2654 >     *       map.remove(key);
2655 >     *   }
2656 >     * }</pre>
2657 >     *
2658 >     * except that the action is performed atomically.  If the
2659 >     * function returns {@code null}, the mapping is removed.  If the
2660 >     * function itself throws an (unchecked) exception, the exception
2661 >     * is rethrown to its caller, and the current mapping is left
2662 >     * unchanged.  Some attempted update operations on this map by
2663 >     * other threads may be blocked while computation is in progress,
2664 >     * so the computation should be short and simple, and must not
2665 >     * attempt to update any other mappings of this Map. For example,
2666 >     * to either create or append new messages to a value mapping:
2667 >     *
2668 >     * @param key key with which the specified value is to be associated
2669 >     * @param remappingFunction the function to compute a value
2670 >     * @return the new value associated with the specified key, or null if none
2671 >     * @throws NullPointerException if the specified key or remappingFunction
2672 >     *         is null
2673 >     * @throws IllegalStateException if the computation detectably
2674 >     *         attempts a recursive update to this map that would
2675 >     *         otherwise never complete
2676 >     * @throws RuntimeException or Error if the remappingFunction does so,
2677 >     *         in which case the mapping is unchanged
2678 >     */
2679 >    @SuppressWarnings("unchecked")
2680 >    public V computeIfPresent(K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
2681 >        if (key == null || remappingFunction == null)
2682 >            throw new NullPointerException();
2683 >        return (V)internalCompute(key, true, remappingFunction);
2684 >    }
2685 >
2686 >    /**
2687 >     * Computes a new mapping value given a key and
2688 >     * its current mapped value (or {@code null} if there is no current
2689 >     * mapping). This is equivalent to
2690 >     *  <pre> {@code
2691 >     *   value = remappingFunction.apply(key, map.get(key));
2692 >     *   if (value != null)
2693 >     *     map.put(key, value);
2694 >     *   else
2695 >     *     map.remove(key);
2696 >     * }</pre>
2697 >     *
2698 >     * except that the action is performed atomically.  If the
2699 >     * function returns {@code null}, the mapping is removed.  If the
2700 >     * function itself throws an (unchecked) exception, the exception
2701 >     * is rethrown to its caller, and the current mapping is left
2702 >     * unchanged.  Some attempted update operations on this map by
2703 >     * other threads may be blocked while computation is in progress,
2704 >     * so the computation should be short and simple, and must not
2705 >     * attempt to update any other mappings of this Map. For example,
2706 >     * to either create or append new messages to a value mapping:
2707 >     *
2708 >     * <pre> {@code
2709 >     * Map<Key, String> map = ...;
2710 >     * final String msg = ...;
2711 >     * map.compute(key, new BiFun<Key, String, String>() {
2712 >     *   public String apply(Key k, String v) {
2713 >     *    return (v == null) ? msg : v + msg;});}}</pre>
2714 >     *
2715 >     * @param key key with which the specified value is to be associated
2716 >     * @param remappingFunction the function to compute a value
2717 >     * @return the new value associated with the specified key, or null if none
2718 >     * @throws NullPointerException if the specified key or remappingFunction
2719 >     *         is null
2720 >     * @throws IllegalStateException if the computation detectably
2721 >     *         attempts a recursive update to this map that would
2722 >     *         otherwise never complete
2723 >     * @throws RuntimeException or Error if the remappingFunction does so,
2724 >     *         in which case the mapping is unchanged
2725 >     */
2726 >    @SuppressWarnings("unchecked")
2727 >    public V compute(K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
2728 >        if (key == null || remappingFunction == null)
2729 >            throw new NullPointerException();
2730 >        return (V)internalCompute(key, false, remappingFunction);
2731 >    }
2732 >
2733 >    /**
2734 >     * If the specified key is not already associated
2735 >     * with a value, associate it with the given value.
2736 >     * Otherwise, replace the value with the results of
2737 >     * the given remapping function. This is equivalent to:
2738 >     *  <pre> {@code
2739 >     *   if (!map.containsKey(key))
2740 >     *     map.put(value);
2741 >     *   else {
2742 >     *     newValue = remappingFunction.apply(map.get(key), value);
2743 >     *     if (value != null)
2744 >     *       map.put(key, value);
2745 >     *     else
2746 >     *       map.remove(key);
2747 >     *   }
2748 >     * }</pre>
2749 >     * except that the action is performed atomically.  If the
2750 >     * function returns {@code null}, the mapping is removed.  If the
2751 >     * function itself throws an (unchecked) exception, the exception
2752 >     * is rethrown to its caller, and the current mapping is left
2753 >     * unchanged.  Some attempted update operations on this map by
2754 >     * other threads may be blocked while computation is in progress,
2755 >     * so the computation should be short and simple, and must not
2756 >     * attempt to update any other mappings of this Map.
2757 >     */
2758 >    @SuppressWarnings("unchecked")
2759 >    public V merge(K key, V value, BiFun<? super V, ? super V, ? extends V> remappingFunction) {
2760 >        if (key == null || value == null || remappingFunction == null)
2761 >            throw new NullPointerException();
2762 >        return (V)internalMerge(key, value, remappingFunction);
2763      }
2764  
2765      /**
# Line 1063 | Line 2767 | public class ConcurrentHashMap<K, V> ext
2767       * This method does nothing if the key is not in the map.
2768       *
2769       * @param  key the key that needs to be removed
2770 <     * @return the previous value associated with <tt>key</tt>, or
2771 <     *         <tt>null</tt> if there was no mapping for <tt>key</tt>
2770 >     * @return the previous value associated with {@code key}, or
2771 >     *         {@code null} if there was no mapping for {@code key}
2772       * @throws NullPointerException if the specified key is null
2773       */
2774 <    public V remove(Object key) {
2775 <        int hash = hash(key.hashCode());
2776 <        Segment<K,V> s = segmentForHash(hash);
2777 <        return s == null ? null : s.remove(key, hash, null);
2774 >    @SuppressWarnings("unchecked")
2775 >        public V remove(Object key) {
2776 >        if (key == null)
2777 >            throw new NullPointerException();
2778 >        return (V)internalReplace(key, null, null);
2779      }
2780  
2781      /**
# Line 1079 | Line 2784 | public class ConcurrentHashMap<K, V> ext
2784       * @throws NullPointerException if the specified key is null
2785       */
2786      public boolean remove(Object key, Object value) {
2787 <        int hash = hash(key.hashCode());
2788 <        Segment<K,V> s;
2789 <        return value != null && (s = segmentForHash(hash)) != null &&
2790 <            s.remove(key, hash, value) != null;
2787 >        if (key == null)
2788 >            throw new NullPointerException();
2789 >        if (value == null)
2790 >            return false;
2791 >        return internalReplace(key, null, value) != null;
2792      }
2793  
2794      /**
# Line 1091 | Line 2797 | public class ConcurrentHashMap<K, V> ext
2797       * @throws NullPointerException if any of the arguments are null
2798       */
2799      public boolean replace(K key, V oldValue, V newValue) {
2800 <        int hash = hash(key.hashCode());
1095 <        if (oldValue == null || newValue == null)
2800 >        if (key == null || oldValue == null || newValue == null)
2801              throw new NullPointerException();
2802 <        Segment<K,V> s = segmentForHash(hash);
1098 <        return s != null && s.replace(key, hash, oldValue, newValue);
2802 >        return internalReplace(key, newValue, oldValue) != null;
2803      }
2804  
2805      /**
2806       * {@inheritDoc}
2807       *
2808       * @return the previous value associated with the specified key,
2809 <     *         or <tt>null</tt> if there was no mapping for the key
2809 >     *         or {@code null} if there was no mapping for the key
2810       * @throws NullPointerException if the specified key or value is null
2811       */
2812 <    public V replace(K key, V value) {
2813 <        int hash = hash(key.hashCode());
2814 <        if (value == null)
2812 >    @SuppressWarnings("unchecked")
2813 >        public V replace(K key, V value) {
2814 >        if (key == null || value == null)
2815              throw new NullPointerException();
2816 <        Segment<K,V> s = segmentForHash(hash);
1113 <        return s == null ? null : s.replace(key, hash, value);
2816 >        return (V)internalReplace(key, value, null);
2817      }
2818  
2819      /**
2820       * Removes all of the mappings from this map.
2821       */
2822      public void clear() {
2823 <        final Segment<K,V>[] segments = this.segments;
1121 <        for (int j = 0; j < segments.length; ++j) {
1122 <            Segment<K,V> s = segmentAt(segments, j);
1123 <            if (s != null)
1124 <                s.clear();
1125 <        }
2823 >        internalClear();
2824      }
2825  
2826      /**
# Line 1130 | Line 2828 | public class ConcurrentHashMap<K, V> ext
2828       * The set is backed by the map, so changes to the map are
2829       * reflected in the set, and vice-versa.  The set supports element
2830       * removal, which removes the corresponding mapping from this map,
2831 <     * via the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
2832 <     * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
2833 <     * operations.  It does not support the <tt>add</tt> or
2834 <     * <tt>addAll</tt> operations.
2831 >     * via the {@code Iterator.remove}, {@code Set.remove},
2832 >     * {@code removeAll}, {@code retainAll}, and {@code clear}
2833 >     * operations.  It does not support the {@code add} or
2834 >     * {@code addAll} operations.
2835       *
2836 <     * <p>The view's <tt>iterator</tt> is a "weakly consistent" iterator
2836 >     * <p>The view's {@code iterator} is a "weakly consistent" iterator
2837       * that will never throw {@link ConcurrentModificationException},
2838       * and guarantees to traverse elements as they existed upon
2839       * construction of the iterator, and may (but is not guaranteed to)
2840       * reflect any modifications subsequent to construction.
2841       */
2842      public Set<K> keySet() {
2843 <        Set<K> ks = keySet;
2844 <        return (ks != null) ? ks : (keySet = new KeySet());
2843 >        KeySet<K,V> ks = keySet;
2844 >        return (ks != null) ? ks : (keySet = new KeySet<K,V>(this));
2845      }
2846  
2847      /**
# Line 1151 | Line 2849 | public class ConcurrentHashMap<K, V> ext
2849       * The collection is backed by the map, so changes to the map are
2850       * reflected in the collection, and vice-versa.  The collection
2851       * supports element removal, which removes the corresponding
2852 <     * mapping from this map, via the <tt>Iterator.remove</tt>,
2853 <     * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
2854 <     * <tt>retainAll</tt>, and <tt>clear</tt> operations.  It does not
2855 <     * support the <tt>add</tt> or <tt>addAll</tt> operations.
2852 >     * mapping from this map, via the {@code Iterator.remove},
2853 >     * {@code Collection.remove}, {@code removeAll},
2854 >     * {@code retainAll}, and {@code clear} operations.  It does not
2855 >     * support the {@code add} or {@code addAll} operations.
2856       *
2857 <     * <p>The view's <tt>iterator</tt> is a "weakly consistent" iterator
2857 >     * <p>The view's {@code iterator} is a "weakly consistent" iterator
2858       * that will never throw {@link ConcurrentModificationException},
2859       * and guarantees to traverse elements as they existed upon
2860       * construction of the iterator, and may (but is not guaranteed to)
2861       * reflect any modifications subsequent to construction.
2862       */
2863      public Collection<V> values() {
2864 <        Collection<V> vs = values;
2865 <        return (vs != null) ? vs : (values = new Values());
2864 >        Values<K,V> vs = values;
2865 >        return (vs != null) ? vs : (values = new Values<K,V>(this));
2866      }
2867  
2868      /**
# Line 1172 | Line 2870 | public class ConcurrentHashMap<K, V> ext
2870       * The set is backed by the map, so changes to the map are
2871       * reflected in the set, and vice-versa.  The set supports element
2872       * removal, which removes the corresponding mapping from the map,
2873 <     * via the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
2874 <     * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
2875 <     * operations.  It does not support the <tt>add</tt> or
2876 <     * <tt>addAll</tt> operations.
2873 >     * via the {@code Iterator.remove}, {@code Set.remove},
2874 >     * {@code removeAll}, {@code retainAll}, and {@code clear}
2875 >     * operations.  It does not support the {@code add} or
2876 >     * {@code addAll} operations.
2877       *
2878 <     * <p>The view's <tt>iterator</tt> is a "weakly consistent" iterator
2878 >     * <p>The view's {@code iterator} is a "weakly consistent" iterator
2879       * that will never throw {@link ConcurrentModificationException},
2880       * and guarantees to traverse elements as they existed upon
2881       * construction of the iterator, and may (but is not guaranteed to)
2882       * reflect any modifications subsequent to construction.
2883       */
2884      public Set<Map.Entry<K,V>> entrySet() {
2885 <        Set<Map.Entry<K,V>> es = entrySet;
2886 <        return (es != null) ? es : (entrySet = new EntrySet());
2885 >        EntrySet<K,V> es = entrySet;
2886 >        return (es != null) ? es : (entrySet = new EntrySet<K,V>(this));
2887      }
2888  
2889      /**
# Line 1195 | Line 2893 | public class ConcurrentHashMap<K, V> ext
2893       * @see #keySet()
2894       */
2895      public Enumeration<K> keys() {
2896 <        return new KeyIterator();
2896 >        return new KeyIterator<K,V>(this);
2897      }
2898  
2899      /**
# Line 1205 | Line 2903 | public class ConcurrentHashMap<K, V> ext
2903       * @see #values()
2904       */
2905      public Enumeration<V> elements() {
2906 <        return new ValueIterator();
2906 >        return new ValueIterator<K,V>(this);
2907      }
2908  
2909 <    /* ---------------- Iterator Support -------------- */
2909 >    /**
2910 >     * Returns a partitionable iterator of the keys in this map.
2911 >     *
2912 >     * @return a partitionable iterator of the keys in this map
2913 >     */
2914 >    public Spliterator<K> keySpliterator() {
2915 >        return new KeyIterator<K,V>(this);
2916 >    }
2917  
2918 <    abstract class HashIterator {
2919 <        int nextSegmentIndex;
2920 <        int nextTableIndex;
2921 <        HashEntry<K,V>[] currentTable;
2922 <        HashEntry<K, V> nextEntry;
2923 <        HashEntry<K, V> lastReturned;
2918 >    /**
2919 >     * Returns a partitionable iterator of the values in this map.
2920 >     *
2921 >     * @return a partitionable iterator of the values in this map
2922 >     */
2923 >    public Spliterator<V> valueSpliterator() {
2924 >        return new ValueIterator<K,V>(this);
2925 >    }
2926  
2927 <        HashIterator() {
2928 <            nextSegmentIndex = segments.length - 1;
2929 <            nextTableIndex = -1;
2930 <            advance();
2931 <        }
2927 >    /**
2928 >     * Returns a partitionable iterator of the entries in this map.
2929 >     *
2930 >     * @return a partitionable iterator of the entries in this map
2931 >     */
2932 >    public Spliterator<Map.Entry<K,V>> entrySpliterator() {
2933 >        return new EntryIterator<K,V>(this);
2934 >    }
2935  
2936 <        /**
2937 <         * Sets nextEntry to first node of next non-empty table
2938 <         * (in backwards order, to simplify checks).
2939 <         */
2940 <        final void advance() {
2936 >    /**
2937 >     * Returns the hash code value for this {@link Map}, i.e.,
2938 >     * the sum of, for each key-value pair in the map,
2939 >     * {@code key.hashCode() ^ value.hashCode()}.
2940 >     *
2941 >     * @return the hash code value for this map
2942 >     */
2943 >    public int hashCode() {
2944 >        int h = 0;
2945 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
2946 >        Object v;
2947 >        while ((v = it.advance()) != null) {
2948 >            h += it.nextKey.hashCode() ^ v.hashCode();
2949 >        }
2950 >        return h;
2951 >    }
2952 >
2953 >    /**
2954 >     * Returns a string representation of this map.  The string
2955 >     * representation consists of a list of key-value mappings (in no
2956 >     * particular order) enclosed in braces ("{@code {}}").  Adjacent
2957 >     * mappings are separated by the characters {@code ", "} (comma
2958 >     * and space).  Each key-value mapping is rendered as the key
2959 >     * followed by an equals sign ("{@code =}") followed by the
2960 >     * associated value.
2961 >     *
2962 >     * @return a string representation of this map
2963 >     */
2964 >    public String toString() {
2965 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
2966 >        StringBuilder sb = new StringBuilder();
2967 >        sb.append('{');
2968 >        Object v;
2969 >        if ((v = it.advance()) != null) {
2970              for (;;) {
2971 <                if (nextTableIndex >= 0) {
2972 <                    if ((nextEntry = entryAt(currentTable,
2973 <                                             nextTableIndex--)) != null)
2974 <                        break;
2975 <                }
1237 <                else if (nextSegmentIndex >= 0) {
1238 <                    Segment<K,V> seg = segmentAt(segments, nextSegmentIndex--);
1239 <                    if (seg != null && (currentTable = seg.table) != null)
1240 <                        nextTableIndex = currentTable.length - 1;
1241 <                }
1242 <                else
2971 >                Object k = it.nextKey;
2972 >                sb.append(k == this ? "(this Map)" : k);
2973 >                sb.append('=');
2974 >                sb.append(v == this ? "(this Map)" : v);
2975 >                if ((v = it.advance()) == null)
2976                      break;
2977 +                sb.append(',').append(' ');
2978              }
2979          }
2980 +        return sb.append('}').toString();
2981 +    }
2982  
2983 <        final HashEntry<K,V> nextEntry() {
2984 <            HashEntry<K,V> e = nextEntry;
2985 <            if (e == null)
2986 <                throw new NoSuchElementException();
2987 <            lastReturned = e; // cannot assign until after null check
2988 <            if ((nextEntry = e.next) == null)
2989 <                advance();
2990 <            return e;
2983 >    /**
2984 >     * Compares the specified object with this map for equality.
2985 >     * Returns {@code true} if the given object is a map with the same
2986 >     * mappings as this map.  This operation may return misleading
2987 >     * results if either map is concurrently modified during execution
2988 >     * of this method.
2989 >     *
2990 >     * @param o object to be compared for equality with this map
2991 >     * @return {@code true} if the specified object is equal to this map
2992 >     */
2993 >    public boolean equals(Object o) {
2994 >        if (o != this) {
2995 >            if (!(o instanceof Map))
2996 >                return false;
2997 >            Map<?,?> m = (Map<?,?>) o;
2998 >            Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
2999 >            Object val;
3000 >            while ((val = it.advance()) != null) {
3001 >                Object v = m.get(it.nextKey);
3002 >                if (v == null || (v != val && !v.equals(val)))
3003 >                    return false;
3004 >            }
3005 >            for (Map.Entry<?,?> e : m.entrySet()) {
3006 >                Object mk, mv, v;
3007 >                if ((mk = e.getKey()) == null ||
3008 >                    (mv = e.getValue()) == null ||
3009 >                    (v = internalGet(mk)) == null ||
3010 >                    (mv != v && !mv.equals(v)))
3011 >                    return false;
3012 >            }
3013          }
3014 +        return true;
3015 +    }
3016  
3017 <        public final boolean hasNext() { return nextEntry != null; }
1258 <        public final boolean hasMoreElements() { return nextEntry != null; }
3017 >    /* ----------------Iterators -------------- */
3018  
3019 <        public final void remove() {
3020 <            if (lastReturned == null)
3019 >    @SuppressWarnings("serial")
3020 >    static final class KeyIterator<K,V> extends Traverser<K,V,Object>
3021 >        implements Spliterator<K>, Enumeration<K> {
3022 >        KeyIterator(ConcurrentHashMap<K, V> map) { super(map); }
3023 >        KeyIterator(Traverser<K,V,Object> it, boolean split) {
3024 >            super(it, split);
3025 >        }
3026 >        public KeyIterator<K,V> split() {
3027 >            if (last != null || (next != null && nextVal == null))
3028                  throw new IllegalStateException();
3029 <            ConcurrentHashMap.this.remove(lastReturned.key);
3030 <            lastReturned = null;
3029 >            return new KeyIterator<K,V>(this, true);
3030 >        }
3031 >        @SuppressWarnings("unchecked")
3032 >            public final K next() {
3033 >            if (nextVal == null && advance() == null)
3034 >                throw new NoSuchElementException();
3035 >            Object k = nextKey;
3036 >            nextVal = null;
3037 >            return (K) k;
3038          }
3039 +
3040 +        public final K nextElement() { return next(); }
3041      }
3042  
3043 <    final class KeyIterator
3044 <        extends HashIterator
3045 <        implements Iterator<K>, Enumeration<K>
3046 <    {
3047 <        public final K next()        { return super.nextEntry().key; }
3048 <        public final K nextElement() { return super.nextEntry().key; }
3043 >    @SuppressWarnings("serial")
3044 >    static final class ValueIterator<K,V> extends Traverser<K,V,Object>
3045 >        implements Spliterator<V>, Enumeration<V> {
3046 >        ValueIterator(ConcurrentHashMap<K, V> map) { super(map); }
3047 >        ValueIterator(Traverser<K,V,Object> it, boolean split) {
3048 >            super(it, split);
3049 >        }
3050 >        public ValueIterator<K,V> split() {
3051 >            if (last != null || (next != null && nextVal == null))
3052 >                throw new IllegalStateException();
3053 >            return new ValueIterator<K,V>(this, true);
3054 >        }
3055 >
3056 >        @SuppressWarnings("unchecked")
3057 >            public final V next() {
3058 >            Object v;
3059 >            if ((v = nextVal) == null && (v = advance()) == null)
3060 >                throw new NoSuchElementException();
3061 >            nextVal = null;
3062 >            return (V) v;
3063 >        }
3064 >
3065 >        public final V nextElement() { return next(); }
3066      }
3067  
3068 <    final class ValueIterator
3069 <        extends HashIterator
3070 <        implements Iterator<V>, Enumeration<V>
3071 <    {
3072 <        public final V next()        { return super.nextEntry().value; }
3073 <        public final V nextElement() { return super.nextEntry().value; }
3068 >    @SuppressWarnings("serial")
3069 >    static final class EntryIterator<K,V> extends Traverser<K,V,Object>
3070 >        implements Spliterator<Map.Entry<K,V>> {
3071 >        EntryIterator(ConcurrentHashMap<K, V> map) { super(map); }
3072 >        EntryIterator(Traverser<K,V,Object> it, boolean split) {
3073 >            super(it, split);
3074 >        }
3075 >        public EntryIterator<K,V> split() {
3076 >            if (last != null || (next != null && nextVal == null))
3077 >                throw new IllegalStateException();
3078 >            return new EntryIterator<K,V>(this, true);
3079 >        }
3080 >
3081 >        @SuppressWarnings("unchecked")
3082 >            public final Map.Entry<K,V> next() {
3083 >            Object v;
3084 >            if ((v = nextVal) == null && (v = advance()) == null)
3085 >                throw new NoSuchElementException();
3086 >            Object k = nextKey;
3087 >            nextVal = null;
3088 >            return new MapEntry<K,V>((K)k, (V)v, map);
3089 >        }
3090      }
3091  
3092      /**
3093 <     * Custom Entry class used by EntryIterator.next(), that relays
1286 <     * setValue changes to the underlying map.
3093 >     * Exported Entry for iterators
3094       */
3095 <    final class WriteThroughEntry
3096 <        extends AbstractMap.SimpleEntry<K,V>
3097 <    {
3098 <        WriteThroughEntry(K k, V v) {
3099 <            super(k,v);
3095 >    static final class MapEntry<K,V> implements Map.Entry<K, V> {
3096 >        final K key; // non-null
3097 >        V val;       // non-null
3098 >        final ConcurrentHashMap<K, V> map;
3099 >        MapEntry(K key, V val, ConcurrentHashMap<K, V> map) {
3100 >            this.key = key;
3101 >            this.val = val;
3102 >            this.map = map;
3103 >        }
3104 >        public final K getKey()       { return key; }
3105 >        public final V getValue()     { return val; }
3106 >        public final int hashCode()   { return key.hashCode() ^ val.hashCode(); }
3107 >        public final String toString(){ return key + "=" + val; }
3108 >
3109 >        public final boolean equals(Object o) {
3110 >            Object k, v; Map.Entry<?,?> e;
3111 >            return ((o instanceof Map.Entry) &&
3112 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3113 >                    (v = e.getValue()) != null &&
3114 >                    (k == key || k.equals(key)) &&
3115 >                    (v == val || v.equals(val)));
3116          }
3117  
3118          /**
3119           * Sets our entry's value and writes through to the map. The
3120 <         * value to return is somewhat arbitrary here. Since a
3121 <         * WriteThroughEntry does not necessarily track asynchronous
3122 <         * changes, the most recent "previous" value could be
3123 <         * different from what we return (or could even have been
3124 <         * removed in which case the put will re-establish). We do not
1302 <         * and cannot guarantee more.
3120 >         * value to return is somewhat arbitrary here. Since we do not
3121 >         * necessarily track asynchronous changes, the most recent
3122 >         * "previous" value could be different from what we return (or
3123 >         * could even have been removed in which case the put will
3124 >         * re-establish). We do not and cannot guarantee more.
3125           */
3126 <        public V setValue(V value) {
3126 >        public final V setValue(V value) {
3127              if (value == null) throw new NullPointerException();
3128 <            V v = super.setValue(value);
3129 <            ConcurrentHashMap.this.put(getKey(), value);
3128 >            V v = val;
3129 >            val = value;
3130 >            map.put(key, value);
3131              return v;
3132          }
3133      }
3134  
3135 <    final class EntryIterator
1313 <        extends HashIterator
1314 <        implements Iterator<Entry<K,V>>
1315 <    {
1316 <        public Map.Entry<K,V> next() {
1317 <            HashEntry<K,V> e = super.nextEntry();
1318 <            return new WriteThroughEntry(e.key, e.value);
1319 <        }
1320 <    }
3135 >    /* ----------------Views -------------- */
3136  
3137 <    final class KeySet extends AbstractSet<K> {
3138 <        public Iterator<K> iterator() {
3139 <            return new KeyIterator();
3137 >    /**
3138 >     * Base class for views.
3139 >     */
3140 >    static abstract class CHMView<K, V> {
3141 >        final ConcurrentHashMap<K, V> map;
3142 >        CHMView(ConcurrentHashMap<K, V> map)  { this.map = map; }
3143 >        public final int size()                 { return map.size(); }
3144 >        public final boolean isEmpty()          { return map.isEmpty(); }
3145 >        public final void clear()               { map.clear(); }
3146 >
3147 >        // implementations below rely on concrete classes supplying these
3148 >        abstract public Iterator<?> iterator();
3149 >        abstract public boolean contains(Object o);
3150 >        abstract public boolean remove(Object o);
3151 >
3152 >        private static final String oomeMsg = "Required array size too large";
3153 >
3154 >        public final Object[] toArray() {
3155 >            long sz = map.mappingCount();
3156 >            if (sz > (long)(MAX_ARRAY_SIZE))
3157 >                throw new OutOfMemoryError(oomeMsg);
3158 >            int n = (int)sz;
3159 >            Object[] r = new Object[n];
3160 >            int i = 0;
3161 >            Iterator<?> it = iterator();
3162 >            while (it.hasNext()) {
3163 >                if (i == n) {
3164 >                    if (n >= MAX_ARRAY_SIZE)
3165 >                        throw new OutOfMemoryError(oomeMsg);
3166 >                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
3167 >                        n = MAX_ARRAY_SIZE;
3168 >                    else
3169 >                        n += (n >>> 1) + 1;
3170 >                    r = Arrays.copyOf(r, n);
3171 >                }
3172 >                r[i++] = it.next();
3173 >            }
3174 >            return (i == n) ? r : Arrays.copyOf(r, i);
3175          }
3176 <        public int size() {
3177 <            return ConcurrentHashMap.this.size();
3176 >
3177 >        @SuppressWarnings("unchecked")
3178 >            public final <T> T[] toArray(T[] a) {
3179 >            long sz = map.mappingCount();
3180 >            if (sz > (long)(MAX_ARRAY_SIZE))
3181 >                throw new OutOfMemoryError(oomeMsg);
3182 >            int m = (int)sz;
3183 >            T[] r = (a.length >= m) ? a :
3184 >                (T[])java.lang.reflect.Array
3185 >                .newInstance(a.getClass().getComponentType(), m);
3186 >            int n = r.length;
3187 >            int i = 0;
3188 >            Iterator<?> it = iterator();
3189 >            while (it.hasNext()) {
3190 >                if (i == n) {
3191 >                    if (n >= MAX_ARRAY_SIZE)
3192 >                        throw new OutOfMemoryError(oomeMsg);
3193 >                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
3194 >                        n = MAX_ARRAY_SIZE;
3195 >                    else
3196 >                        n += (n >>> 1) + 1;
3197 >                    r = Arrays.copyOf(r, n);
3198 >                }
3199 >                r[i++] = (T)it.next();
3200 >            }
3201 >            if (a == r && i < n) {
3202 >                r[i] = null; // null-terminate
3203 >                return r;
3204 >            }
3205 >            return (i == n) ? r : Arrays.copyOf(r, i);
3206          }
3207 <        public boolean isEmpty() {
3208 <            return ConcurrentHashMap.this.isEmpty();
3207 >
3208 >        public final int hashCode() {
3209 >            int h = 0;
3210 >            for (Iterator<?> it = iterator(); it.hasNext();)
3211 >                h += it.next().hashCode();
3212 >            return h;
3213 >        }
3214 >
3215 >        public final String toString() {
3216 >            StringBuilder sb = new StringBuilder();
3217 >            sb.append('[');
3218 >            Iterator<?> it = iterator();
3219 >            if (it.hasNext()) {
3220 >                for (;;) {
3221 >                    Object e = it.next();
3222 >                    sb.append(e == this ? "(this Collection)" : e);
3223 >                    if (!it.hasNext())
3224 >                        break;
3225 >                    sb.append(',').append(' ');
3226 >                }
3227 >            }
3228 >            return sb.append(']').toString();
3229          }
3230 <        public boolean contains(Object o) {
3231 <            return ConcurrentHashMap.this.containsKey(o);
3230 >
3231 >        public final boolean containsAll(Collection<?> c) {
3232 >            if (c != this) {
3233 >                for (Iterator<?> it = c.iterator(); it.hasNext();) {
3234 >                    Object e = it.next();
3235 >                    if (e == null || !contains(e))
3236 >                        return false;
3237 >                }
3238 >            }
3239 >            return true;
3240          }
3241 <        public boolean remove(Object o) {
3242 <            return ConcurrentHashMap.this.remove(o) != null;
3241 >
3242 >        public final boolean removeAll(Collection<?> c) {
3243 >            boolean modified = false;
3244 >            for (Iterator<?> it = iterator(); it.hasNext();) {
3245 >                if (c.contains(it.next())) {
3246 >                    it.remove();
3247 >                    modified = true;
3248 >                }
3249 >            }
3250 >            return modified;
3251          }
3252 <        public void clear() {
3253 <            ConcurrentHashMap.this.clear();
3252 >
3253 >        public final boolean retainAll(Collection<?> c) {
3254 >            boolean modified = false;
3255 >            for (Iterator<?> it = iterator(); it.hasNext();) {
3256 >                if (!c.contains(it.next())) {
3257 >                    it.remove();
3258 >                    modified = true;
3259 >                }
3260 >            }
3261 >            return modified;
3262          }
3263 +
3264      }
3265  
3266 <    final class Values extends AbstractCollection<V> {
3267 <        public Iterator<V> iterator() {
3268 <            return new ValueIterator();
3266 >    static final class KeySet<K,V> extends CHMView<K,V> implements Set<K> {
3267 >        KeySet(ConcurrentHashMap<K, V> map)  {
3268 >            super(map);
3269 >        }
3270 >        public final boolean contains(Object o) { return map.containsKey(o); }
3271 >        public final boolean remove(Object o)   { return map.remove(o) != null; }
3272 >        public final Iterator<K> iterator() {
3273 >            return new KeyIterator<K,V>(map);
3274          }
3275 <        public int size() {
3276 <            return ConcurrentHashMap.this.size();
3275 >        public final boolean add(K e) {
3276 >            throw new UnsupportedOperationException();
3277          }
3278 <        public boolean isEmpty() {
3279 <            return ConcurrentHashMap.this.isEmpty();
3278 >        public final boolean addAll(Collection<? extends K> c) {
3279 >            throw new UnsupportedOperationException();
3280          }
3281 <        public boolean contains(Object o) {
3282 <            return ConcurrentHashMap.this.containsValue(o);
3281 >        public boolean equals(Object o) {
3282 >            Set<?> c;
3283 >            return ((o instanceof Set) &&
3284 >                    ((c = (Set<?>)o) == this ||
3285 >                     (containsAll(c) && c.containsAll(this))));
3286          }
3287 <        public void clear() {
3288 <            ConcurrentHashMap.this.clear();
3287 >    }
3288 >
3289 >
3290 >    static final class Values<K,V> extends CHMView<K,V>
3291 >        implements Collection<V> {
3292 >        Values(ConcurrentHashMap<K, V> map)   { super(map); }
3293 >        public final boolean contains(Object o) { return map.containsValue(o); }
3294 >        public final boolean remove(Object o) {
3295 >            if (o != null) {
3296 >                Iterator<V> it = new ValueIterator<K,V>(map);
3297 >                while (it.hasNext()) {
3298 >                    if (o.equals(it.next())) {
3299 >                        it.remove();
3300 >                        return true;
3301 >                    }
3302 >                }
3303 >            }
3304 >            return false;
3305          }
3306 +        public final Iterator<V> iterator() {
3307 +            return new ValueIterator<K,V>(map);
3308 +        }
3309 +        public final boolean add(V e) {
3310 +            throw new UnsupportedOperationException();
3311 +        }
3312 +        public final boolean addAll(Collection<? extends V> c) {
3313 +            throw new UnsupportedOperationException();
3314 +        }
3315 +
3316      }
3317  
3318 <    final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
3319 <        public Iterator<Map.Entry<K,V>> iterator() {
3320 <            return new EntryIterator();
3318 >    static final class EntrySet<K,V> extends CHMView<K,V>
3319 >        implements Set<Map.Entry<K,V>> {
3320 >        EntrySet(ConcurrentHashMap<K, V> map) { super(map); }
3321 >        public final boolean contains(Object o) {
3322 >            Object k, v, r; Map.Entry<?,?> e;
3323 >            return ((o instanceof Map.Entry) &&
3324 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3325 >                    (r = map.get(k)) != null &&
3326 >                    (v = e.getValue()) != null &&
3327 >                    (v == r || v.equals(r)));
3328          }
3329 <        public boolean contains(Object o) {
3330 <            if (!(o instanceof Map.Entry))
3331 <                return false;
3332 <            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
3333 <            V v = ConcurrentHashMap.this.get(e.getKey());
3334 <            return v != null && v.equals(e.getValue());
3329 >        public final boolean remove(Object o) {
3330 >            Object k, v; Map.Entry<?,?> e;
3331 >            return ((o instanceof Map.Entry) &&
3332 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3333 >                    (v = e.getValue()) != null &&
3334 >                    map.remove(k, v));
3335          }
3336 <        public boolean remove(Object o) {
3337 <            if (!(o instanceof Map.Entry))
1374 <                return false;
1375 <            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
1376 <            return ConcurrentHashMap.this.remove(e.getKey(), e.getValue());
3336 >        public final Iterator<Map.Entry<K,V>> iterator() {
3337 >            return new EntryIterator<K,V>(map);
3338          }
3339 <        public int size() {
3340 <            return ConcurrentHashMap.this.size();
3339 >        public final boolean add(Entry<K,V> e) {
3340 >            throw new UnsupportedOperationException();
3341          }
3342 <        public boolean isEmpty() {
3343 <            return ConcurrentHashMap.this.isEmpty();
3342 >        public final boolean addAll(Collection<? extends Entry<K,V>> c) {
3343 >            throw new UnsupportedOperationException();
3344          }
3345 <        public void clear() {
3346 <            ConcurrentHashMap.this.clear();
3345 >        public boolean equals(Object o) {
3346 >            Set<?> c;
3347 >            return ((o instanceof Set) &&
3348 >                    ((c = (Set<?>)o) == this ||
3349 >                     (containsAll(c) && c.containsAll(this))));
3350          }
3351      }
3352  
3353      /* ---------------- Serialization Support -------------- */
3354  
3355      /**
3356 <     * Saves the state of the <tt>ConcurrentHashMap</tt> instance to a
3356 >     * Stripped-down version of helper class used in previous version,
3357 >     * declared for the sake of serialization compatibility
3358 >     */
3359 >    static class Segment<K,V> implements Serializable {
3360 >        private static final long serialVersionUID = 2249069246763182397L;
3361 >        final float loadFactor;
3362 >        Segment(float lf) { this.loadFactor = lf; }
3363 >    }
3364 >
3365 >    /**
3366 >     * Saves the state of the {@code ConcurrentHashMap} instance to a
3367       * stream (i.e., serializes it).
3368       * @param s the stream
3369       * @serialData
# Line 1397 | Line 3371 | public class ConcurrentHashMap<K, V> ext
3371       * for each key-value mapping, followed by a null pair.
3372       * The key-value mappings are emitted in no particular order.
3373       */
3374 <    private void writeObject(java.io.ObjectOutputStream s) throws IOException {
3375 <        // force all segments for serialization compatibility
3376 <        for (int k = 0; k < segments.length; ++k)
3377 <            ensureSegment(k);
3374 >    @SuppressWarnings("unchecked")
3375 >        private void writeObject(java.io.ObjectOutputStream s)
3376 >        throws java.io.IOException {
3377 >        if (segments == null) { // for serialization compatibility
3378 >            segments = (Segment<K,V>[])
3379 >                new Segment<?,?>[DEFAULT_CONCURRENCY_LEVEL];
3380 >            for (int i = 0; i < segments.length; ++i)
3381 >                segments[i] = new Segment<K,V>(LOAD_FACTOR);
3382 >        }
3383          s.defaultWriteObject();
3384 <
3385 <        final Segment<K,V>[] segments = this.segments;
3386 <        for (int k = 0; k < segments.length; ++k) {
3387 <            Segment<K,V> seg = segmentAt(segments, k);
3388 <            seg.lock();
1410 <            try {
1411 <                HashEntry<K,V>[] tab = seg.table;
1412 <                for (int i = 0; i < tab.length; ++i) {
1413 <                    HashEntry<K,V> e;
1414 <                    for (e = entryAt(tab, i); e != null; e = e.next) {
1415 <                        s.writeObject(e.key);
1416 <                        s.writeObject(e.value);
1417 <                    }
1418 <                }
1419 <            } finally {
1420 <                seg.unlock();
1421 <            }
3384 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3385 >        Object v;
3386 >        while ((v = it.advance()) != null) {
3387 >            s.writeObject(it.nextKey);
3388 >            s.writeObject(v);
3389          }
3390          s.writeObject(null);
3391          s.writeObject(null);
3392 +        segments = null; // throw away
3393      }
3394  
3395      /**
3396 <     * Reconstitutes the <tt>ConcurrentHashMap</tt> instance from a
1429 <     * stream (i.e., deserializes it).
3396 >     * Reconstitutes the instance from a stream (that is, deserializes it).
3397       * @param s the stream
3398       */
3399      @SuppressWarnings("unchecked")
3400 <    private void readObject(java.io.ObjectInputStream s)
3401 <        throws IOException, ClassNotFoundException {
3400 >        private void readObject(java.io.ObjectInputStream s)
3401 >        throws java.io.IOException, ClassNotFoundException {
3402          s.defaultReadObject();
3403 +        this.segments = null; // unneeded
3404 +        // initialize transient final field
3405 +        UNSAFE.putObjectVolatile(this, counterOffset, new LongAdder());
3406  
3407 <        // Re-initialize segments to be minimally sized, and let grow.
3408 <        int cap = MIN_SEGMENT_TABLE_CAPACITY;
3409 <        final Segment<K,V>[] segments = this.segments;
3410 <        for (int k = 0; k < segments.length; ++k) {
3411 <            Segment<K,V> seg = segments[k];
3412 <            if (seg != null) {
3413 <                seg.threshold = (int)(cap * seg.loadFactor);
3414 <                seg.table = (HashEntry<K,V>[]) new HashEntry<?,?>[cap];
3407 >        // Create all nodes, then place in table once size is known
3408 >        long size = 0L;
3409 >        Node p = null;
3410 >        for (;;) {
3411 >            K k = (K) s.readObject();
3412 >            V v = (V) s.readObject();
3413 >            if (k != null && v != null) {
3414 >                int h = spread(k.hashCode());
3415 >                p = new Node(h, k, v, p);
3416 >                ++size;
3417              }
3418 +            else
3419 +                break;
3420          }
3421 +        if (p != null) {
3422 +            boolean init = false;
3423 +            int n;
3424 +            if (size >= (long)(MAXIMUM_CAPACITY >>> 1))
3425 +                n = MAXIMUM_CAPACITY;
3426 +            else {
3427 +                int sz = (int)size;
3428 +                n = tableSizeFor(sz + (sz >>> 1) + 1);
3429 +            }
3430 +            int sc = sizeCtl;
3431 +            boolean collide = false;
3432 +            if (n > sc &&
3433 +                UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
3434 +                try {
3435 +                    if (table == null) {
3436 +                        init = true;
3437 +                        Node[] tab = new Node[n];
3438 +                        int mask = n - 1;
3439 +                        while (p != null) {
3440 +                            int j = p.hash & mask;
3441 +                            Node next = p.next;
3442 +                            Node q = p.next = tabAt(tab, j);
3443 +                            setTabAt(tab, j, p);
3444 +                            if (!collide && q != null && q.hash == p.hash)
3445 +                                collide = true;
3446 +                            p = next;
3447 +                        }
3448 +                        table = tab;
3449 +                        counter.add(size);
3450 +                        sc = n - (n >>> 2);
3451 +                    }
3452 +                } finally {
3453 +                    sizeCtl = sc;
3454 +                }
3455 +                if (collide) { // rescan and convert to TreeBins
3456 +                    Node[] tab = table;
3457 +                    for (int i = 0; i < tab.length; ++i) {
3458 +                        int c = 0;
3459 +                        for (Node e = tabAt(tab, i); e != null; e = e.next) {
3460 +                            if (++c > TREE_THRESHOLD &&
3461 +                                (e.key instanceof Comparable)) {
3462 +                                replaceWithTreeBin(tab, i, e.key);
3463 +                                break;
3464 +                            }
3465 +                        }
3466 +                    }
3467 +                }
3468 +            }
3469 +            if (!init) { // Can only happen if unsafely published.
3470 +                while (p != null) {
3471 +                    internalPut(p.key, p.val);
3472 +                    p = p.next;
3473 +                }
3474 +            }
3475 +        }
3476 +    }
3477  
3478 <        // Read the keys and values, and put the mappings in the table
3479 <        for (;;) {
3480 <            K key = (K) s.readObject();
3481 <            V value = (V) s.readObject();
3482 <            if (key == null)
3483 <                break;
3484 <            put(key, value);
3478 >
3479 >    // -------------------------------------------------------
3480 >
3481 >    // Sams
3482 >    /** Interface describing a void action of one argument */
3483 >    public interface Action<A> { void apply(A a); }
3484 >    /** Interface describing a void action of two arguments */
3485 >    public interface BiAction<A,B> { void apply(A a, B b); }
3486 >    /** Interface describing a function of one argument */
3487 >    public interface Fun<A,T> { T apply(A a); }
3488 >    /** Interface describing a function of two arguments */
3489 >    public interface BiFun<A,B,T> { T apply(A a, B b); }
3490 >    /** Interface describing a function of no arguments */
3491 >    public interface Generator<T> { T apply(); }
3492 >    /** Interface describing a function mapping its argument to a double */
3493 >    public interface ObjectToDouble<A> { double apply(A a); }
3494 >    /** Interface describing a function mapping its argument to a long */
3495 >    public interface ObjectToLong<A> { long apply(A a); }
3496 >    /** Interface describing a function mapping its argument to an int */
3497 >    public interface ObjectToInt<A> {int apply(A a); }
3498 >    /** Interface describing a function mapping two arguments to a double */
3499 >    public interface ObjectByObjectToDouble<A,B> { double apply(A a, B b); }
3500 >    /** Interface describing a function mapping two arguments to a long */
3501 >    public interface ObjectByObjectToLong<A,B> { long apply(A a, B b); }
3502 >    /** Interface describing a function mapping two arguments to an int */
3503 >    public interface ObjectByObjectToInt<A,B> {int apply(A a, B b); }
3504 >    /** Interface describing a function mapping a double to a double */
3505 >    public interface DoubleToDouble { double apply(double a); }
3506 >    /** Interface describing a function mapping a long to a long */
3507 >    public interface LongToLong { long apply(long a); }
3508 >    /** Interface describing a function mapping an int to an int */
3509 >    public interface IntToInt { int apply(int a); }
3510 >    /** Interface describing a function mapping two doubles to a double */
3511 >    public interface DoubleByDoubleToDouble { double apply(double a, double b); }
3512 >    /** Interface describing a function mapping two longs to a long */
3513 >    public interface LongByLongToLong { long apply(long a, long b); }
3514 >    /** Interface describing a function mapping two ints to an int */
3515 >    public interface IntByIntToInt { int apply(int a, int b); }
3516 >
3517 >
3518 >    // -------------------------------------------------------
3519 >
3520 >    /**
3521 >     * Returns an extended {@link Parallel} view of this map using the
3522 >     * given executor for bulk parallel operations.
3523 >     *
3524 >     * @param executor the executor
3525 >     * @return a parallel view
3526 >     */
3527 >    public Parallel parallel(ForkJoinPool executor)  {
3528 >        return new Parallel(executor);
3529 >    }
3530 >
3531 >    /**
3532 >     * An extended view of a ConcurrentHashMap supporting bulk
3533 >     * parallel operations. These operations are designed to be
3534 >     * safely, and often sensibly, applied even with maps that are
3535 >     * being concurrently updated by other threads; for example, when
3536 >     * computing a snapshot summary of the values in a shared
3537 >     * registry.  There are three kinds of operation, each with four
3538 >     * forms, accepting functions with Keys, Values, Entries, and
3539 >     * (Key, Value) arguments and/or return values. Because the
3540 >     * elements of a ConcurrentHashMap are not ordered in any
3541 >     * particular way, and may be processed in different orders in
3542 >     * different parallel executions, the correctness of supplied
3543 >     * functions should not depend on any ordering, or on any other
3544 >     * objects or values that may transiently change while computation
3545 >     * is in progress; and except for forEach actions, should ideally
3546 >     * be side-effect-free.
3547 >     *
3548 >     * <ul>
3549 >     * <li> forEach: Perform a given action on each element.
3550 >     * A variant form applies a given transformation on each element
3551 >     * before performing the action.</li>
3552 >     *
3553 >     * <li> search: Return the first available non-null result of
3554 >     * applying a given function on each element; skipping further
3555 >     * search when a result is found.</li>
3556 >     *
3557 >     * <li> reduce: Accumulate each element.  The supplied reduction
3558 >     * function cannot rely on ordering (more formally, it should be
3559 >     * both associative and commutative).  There are five variants:
3560 >     *
3561 >     * <ul>
3562 >     *
3563 >     * <li> Plain reductions. (There is not a form of this method for
3564 >     * (key, value) function arguments since there is no corresponding
3565 >     * return type.)</li>
3566 >     *
3567 >     * <li> Mapped reductions that accumulate the results of a given
3568 >     * function applied to each element.</li>
3569 >     *
3570 >     * <li> Reductions to scalar doubles, longs, and ints, using a
3571 >     * given basis value.</li>
3572 >     *
3573 >     * </li>
3574 >     * </ul>
3575 >     * </ul>
3576 >     *
3577 >     * <p>The concurrency properties of the bulk operations follow
3578 >     * from those of ConcurrentHashMap: Any non-null result returned
3579 >     * from {@code get(key)} and related access methods bears a
3580 >     * happens-before relation with the associated insertion or
3581 >     * update.  The result of any bulk operation reflects the
3582 >     * composition of these per-element relations (but is not
3583 >     * necessarily atomic with respect to the map as a whole unless it
3584 >     * is somehow known to be quiescent).  Conversely, because keys
3585 >     * and values in the map are never null, null serves as a reliable
3586 >     * atomic indicator of the current lack of any result.  To
3587 >     * maintain this property, null serves as an implicit basis for
3588 >     * all non-scalar reduction operations. For the double, long, and
3589 >     * int versions, the basis should be one that, when combined with
3590 >     * any other value, returns that other value (more formally, it
3591 >     * should be the identity element for the reduction). Most common
3592 >     * reductions have these properties; for example, computing a sum
3593 >     * with basis 0 or a minimum with basis MAX_VALUE.
3594 >     *
3595 >     * <p>Search and transformation functions provided as arguments
3596 >     * should similarly return null to indicate the lack of any result
3597 >     * (in which case it is not used). In the case of mapped
3598 >     * reductions, this also enables transformations to serve as
3599 >     * filters, returning null (or, in the case of primitive
3600 >     * specializations, the identity basis) if the element should not
3601 >     * be combined. You can create compound transformations and
3602 >     * filterings by composing them yourself under this "null means
3603 >     * there is nothing there now" rule before using them in search or
3604 >     * reduce operations.
3605 >     *
3606 >     * <p>Methods accepting and/or returning Entry arguments maintain
3607 >     * key-value associations. They may be useful for example when
3608 >     * finding the key for the greatest value. Note that "plain" Entry
3609 >     * arguments can be supplied using {@code new
3610 >     * AbstractMap.SimpleEntry(k,v)}.
3611 >     *
3612 >     * <p> Bulk operations may complete abruptly, throwing an
3613 >     * exception encountered in the application of a supplied
3614 >     * function. Bear in mind when handling such exceptions that other
3615 >     * concurrently executing functions could also have thrown
3616 >     * exceptions, or would have done so if the first exception had
3617 >     * not occurred.
3618 >     *
3619 >     * <p>Parallel speedups compared to sequential processing are
3620 >     * common but not guaranteed.  Operations involving brief
3621 >     * functions on small maps may execute more slowly than sequential
3622 >     * loops if the underlying work to parallelize the computation is
3623 >     * more expensive than the computation itself. Similarly,
3624 >     * parallelization may not lead to much actual parallelism if all
3625 >     * processors are busy performing unrelated tasks.
3626 >     *
3627 >     * <p> All arguments to all task methods must be non-null.
3628 >     *
3629 >     * <p><em>jsr166e note: During transition, this class
3630 >     * uses nested functional interfaces with different names but the
3631 >     * same forms as those expected for JDK8.<em>
3632 >     */
3633 >    public class Parallel {
3634 >        final ForkJoinPool fjp;
3635 >
3636 >        /**
3637 >         * Returns an extended view of this map using the given
3638 >         * executor for bulk parallel operations.
3639 >         *
3640 >         * @param executor the executor
3641 >         */
3642 >        public Parallel(ForkJoinPool executor)  {
3643 >            this.fjp = executor;
3644 >        }
3645 >
3646 >        /**
3647 >         * Performs the given action for each (key, value).
3648 >         *
3649 >         * @param action the action
3650 >         */
3651 >        public void forEach(BiAction<K,V> action) {
3652 >            fjp.invoke(ForkJoinTasks.forEach
3653 >                       (ConcurrentHashMap.this, action));
3654 >        }
3655 >
3656 >        /**
3657 >         * Performs the given action for each non-null transformation
3658 >         * of each (key, value).
3659 >         *
3660 >         * @param transformer a function returning the transformation
3661 >         * for an element, or null of there is no transformation (in
3662 >         * which case the action is not applied).
3663 >         * @param action the action
3664 >         */
3665 >        public <U> void forEach(BiFun<? super K, ? super V, ? extends U> transformer,
3666 >                                Action<U> action) {
3667 >            fjp.invoke(ForkJoinTasks.forEach
3668 >                       (ConcurrentHashMap.this, transformer, action));
3669 >        }
3670 >
3671 >        /**
3672 >         * Returns a non-null result from applying the given search
3673 >         * function on each (key, value), or null if none.  Upon
3674 >         * success, further element processing is suppressed and the
3675 >         * results of any other parallel invocations of the search
3676 >         * function are ignored.
3677 >         *
3678 >         * @param searchFunction a function returning a non-null
3679 >         * result on success, else null
3680 >         * @return a non-null result from applying the given search
3681 >         * function on each (key, value), or null if none
3682 >         */
3683 >        public <U> U search(BiFun<? super K, ? super V, ? extends U> searchFunction) {
3684 >            return fjp.invoke(ForkJoinTasks.search
3685 >                              (ConcurrentHashMap.this, searchFunction));
3686 >        }
3687 >
3688 >        /**
3689 >         * Returns the result of accumulating the given transformation
3690 >         * of all (key, value) pairs using the given reducer to
3691 >         * combine values, or null if none.
3692 >         *
3693 >         * @param transformer a function returning the transformation
3694 >         * for an element, or null of there is no transformation (in
3695 >         * which case it is not combined).
3696 >         * @param reducer a commutative associative combining function
3697 >         * @return the result of accumulating the given transformation
3698 >         * of all (key, value) pairs
3699 >         */
3700 >        public <U> U reduce(BiFun<? super K, ? super V, ? extends U> transformer,
3701 >                            BiFun<? super U, ? super U, ? extends U> reducer) {
3702 >            return fjp.invoke(ForkJoinTasks.reduce
3703 >                              (ConcurrentHashMap.this, transformer, reducer));
3704 >        }
3705 >
3706 >        /**
3707 >         * Returns the result of accumulating the given transformation
3708 >         * of all (key, value) pairs using the given reducer to
3709 >         * combine values, and the given basis as an identity value.
3710 >         *
3711 >         * @param transformer a function returning the transformation
3712 >         * for an element
3713 >         * @param basis the identity (initial default value) for the reduction
3714 >         * @param reducer a commutative associative combining function
3715 >         * @return the result of accumulating the given transformation
3716 >         * of all (key, value) pairs
3717 >         */
3718 >        public double reduceToDouble(ObjectByObjectToDouble<? super K, ? super V> transformer,
3719 >                                     double basis,
3720 >                                     DoubleByDoubleToDouble reducer) {
3721 >            return fjp.invoke(ForkJoinTasks.reduceToDouble
3722 >                              (ConcurrentHashMap.this, transformer, basis, reducer));
3723 >        }
3724 >
3725 >        /**
3726 >         * Returns the result of accumulating the given transformation
3727 >         * of all (key, value) pairs using the given reducer to
3728 >         * combine values, and the given basis as an identity value.
3729 >         *
3730 >         * @param transformer a function returning the transformation
3731 >         * for an element
3732 >         * @param basis the identity (initial default value) for the reduction
3733 >         * @param reducer a commutative associative combining function
3734 >         * @return the result of accumulating the given transformation
3735 >         * of all (key, value) pairs
3736 >         */
3737 >        public long reduceToLong(ObjectByObjectToLong<? super K, ? super V> transformer,
3738 >                                 long basis,
3739 >                                 LongByLongToLong reducer) {
3740 >            return fjp.invoke(ForkJoinTasks.reduceToLong
3741 >                              (ConcurrentHashMap.this, transformer, basis, reducer));
3742 >        }
3743 >
3744 >        /**
3745 >         * Returns the result of accumulating the given transformation
3746 >         * of all (key, value) pairs using the given reducer to
3747 >         * combine values, and the given basis as an identity value.
3748 >         *
3749 >         * @param transformer a function returning the transformation
3750 >         * for an element
3751 >         * @param basis the identity (initial default value) for the reduction
3752 >         * @param reducer a commutative associative combining function
3753 >         * @return the result of accumulating the given transformation
3754 >         * of all (key, value) pairs
3755 >         */
3756 >        public int reduceToInt(ObjectByObjectToInt<? super K, ? super V> transformer,
3757 >                               int basis,
3758 >                               IntByIntToInt reducer) {
3759 >            return fjp.invoke(ForkJoinTasks.reduceToInt
3760 >                              (ConcurrentHashMap.this, transformer, basis, reducer));
3761 >        }
3762 >
3763 >        /**
3764 >         * Performs the given action for each key.
3765 >         *
3766 >         * @param action the action
3767 >         */
3768 >        public void forEachKey(Action<K> action) {
3769 >            fjp.invoke(ForkJoinTasks.forEachKey
3770 >                       (ConcurrentHashMap.this, action));
3771 >        }
3772 >
3773 >        /**
3774 >         * Performs the given action for each non-null transformation
3775 >         * of each key.
3776 >         *
3777 >         * @param transformer a function returning the transformation
3778 >         * for an element, or null of there is no transformation (in
3779 >         * which case the action is not applied).
3780 >         * @param action the action
3781 >         */
3782 >        public <U> void forEachKey(Fun<? super K, ? extends U> transformer,
3783 >                                   Action<U> action) {
3784 >            fjp.invoke(ForkJoinTasks.forEachKey
3785 >                       (ConcurrentHashMap.this, transformer, action));
3786 >        }
3787 >
3788 >        /**
3789 >         * Returns a non-null result from applying the given search
3790 >         * function on each key, or null if none. Upon success,
3791 >         * further element processing is suppressed and the results of
3792 >         * any other parallel invocations of the search function are
3793 >         * ignored.
3794 >         *
3795 >         * @param searchFunction a function returning a non-null
3796 >         * result on success, else null
3797 >         * @return a non-null result from applying the given search
3798 >         * function on each key, or null if none
3799 >         */
3800 >        public <U> U searchKeys(Fun<? super K, ? extends U> searchFunction) {
3801 >            return fjp.invoke(ForkJoinTasks.searchKeys
3802 >                              (ConcurrentHashMap.this, searchFunction));
3803 >        }
3804 >
3805 >        /**
3806 >         * Returns the result of accumulating all keys using the given
3807 >         * reducer to combine values, or null if none.
3808 >         *
3809 >         * @param reducer a commutative associative combining function
3810 >         * @return the result of accumulating all keys using the given
3811 >         * reducer to combine values, or null if none
3812 >         */
3813 >        public K reduceKeys(BiFun<? super K, ? super K, ? extends K> reducer) {
3814 >            return fjp.invoke(ForkJoinTasks.reduceKeys
3815 >                              (ConcurrentHashMap.this, reducer));
3816 >        }
3817 >
3818 >        /**
3819 >         * Returns the result of accumulating the given transformation
3820 >         * of all keys using the given reducer to combine values, or
3821 >         * null if none.
3822 >         *
3823 >         * @param transformer a function returning the transformation
3824 >         * for an element, or null of there is no transformation (in
3825 >         * which case it is not combined).
3826 >         * @param reducer a commutative associative combining function
3827 >         * @return the result of accumulating the given transformation
3828 >         * of all keys
3829 >         */
3830 >        public <U> U reduceKeys(Fun<? super K, ? extends U> transformer,
3831 >                                BiFun<? super U, ? super U, ? extends U> reducer) {
3832 >            return fjp.invoke(ForkJoinTasks.reduceKeys
3833 >                              (ConcurrentHashMap.this, transformer, reducer));
3834 >        }
3835 >
3836 >        /**
3837 >         * Returns the result of accumulating the given transformation
3838 >         * of all keys using the given reducer to combine values, and
3839 >         * the given basis as an identity value.
3840 >         *
3841 >         * @param transformer a function returning the transformation
3842 >         * for an element
3843 >         * @param basis the identity (initial default value) for the reduction
3844 >         * @param reducer a commutative associative combining function
3845 >         * @return  the result of accumulating the given transformation
3846 >         * of all keys
3847 >         */
3848 >        public double reduceKeysToDouble(ObjectToDouble<? super K> transformer,
3849 >                                         double basis,
3850 >                                         DoubleByDoubleToDouble reducer) {
3851 >            return fjp.invoke(ForkJoinTasks.reduceKeysToDouble
3852 >                              (ConcurrentHashMap.this, transformer, basis, reducer));
3853 >        }
3854 >
3855 >        /**
3856 >         * Returns the result of accumulating the given transformation
3857 >         * of all keys using the given reducer to combine values, and
3858 >         * the given basis as an identity value.
3859 >         *
3860 >         * @param transformer a function returning the transformation
3861 >         * for an element
3862 >         * @param basis the identity (initial default value) for the reduction
3863 >         * @param reducer a commutative associative combining function
3864 >         * @return the result of accumulating the given transformation
3865 >         * of all keys
3866 >         */
3867 >        public long reduceKeysToLong(ObjectToLong<? super K> transformer,
3868 >                                     long basis,
3869 >                                     LongByLongToLong reducer) {
3870 >            return fjp.invoke(ForkJoinTasks.reduceKeysToLong
3871 >                              (ConcurrentHashMap.this, transformer, basis, reducer));
3872 >        }
3873 >
3874 >        /**
3875 >         * Returns the result of accumulating the given transformation
3876 >         * of all keys using the given reducer to combine values, and
3877 >         * the given basis as an identity value.
3878 >         *
3879 >         * @param transformer a function returning the transformation
3880 >         * for an element
3881 >         * @param basis the identity (initial default value) for the reduction
3882 >         * @param reducer a commutative associative combining function
3883 >         * @return the result of accumulating the given transformation
3884 >         * of all keys
3885 >         */
3886 >        public int reduceKeysToInt(ObjectToInt<? super K> transformer,
3887 >                                   int basis,
3888 >                                   IntByIntToInt reducer) {
3889 >            return fjp.invoke(ForkJoinTasks.reduceKeysToInt
3890 >                              (ConcurrentHashMap.this, transformer, basis, reducer));
3891 >        }
3892 >
3893 >        /**
3894 >         * Performs the given action for each value.
3895 >         *
3896 >         * @param action the action
3897 >         */
3898 >        public void forEachValue(Action<V> action) {
3899 >            fjp.invoke(ForkJoinTasks.forEachValue
3900 >                       (ConcurrentHashMap.this, action));
3901 >        }
3902 >
3903 >        /**
3904 >         * Performs the given action for each non-null transformation
3905 >         * of each value.
3906 >         *
3907 >         * @param transformer a function returning the transformation
3908 >         * for an element, or null of there is no transformation (in
3909 >         * which case the action is not applied).
3910 >         */
3911 >        public <U> void forEachValue(Fun<? super V, ? extends U> transformer,
3912 >                                     Action<U> action) {
3913 >            fjp.invoke(ForkJoinTasks.forEachValue
3914 >                       (ConcurrentHashMap.this, transformer, action));
3915 >        }
3916 >
3917 >        /**
3918 >         * Returns a non-null result from applying the given search
3919 >         * function on each value, or null if none.  Upon success,
3920 >         * further element processing is suppressed and the results of
3921 >         * any other parallel invocations of the search function are
3922 >         * ignored.
3923 >         *
3924 >         * @param searchFunction a function returning a non-null
3925 >         * result on success, else null
3926 >         * @return a non-null result from applying the given search
3927 >         * function on each value, or null if none
3928 >         *
3929 >         */
3930 >        public <U> U searchValues(Fun<? super V, ? extends U> searchFunction) {
3931 >            return fjp.invoke(ForkJoinTasks.searchValues
3932 >                              (ConcurrentHashMap.this, searchFunction));
3933 >        }
3934 >
3935 >        /**
3936 >         * Returns the result of accumulating all values using the
3937 >         * given reducer to combine values, or null if none.
3938 >         *
3939 >         * @param reducer a commutative associative combining function
3940 >         * @return  the result of accumulating all values
3941 >         */
3942 >        public V reduceValues(BiFun<? super V, ? super V, ? extends V> reducer) {
3943 >            return fjp.invoke(ForkJoinTasks.reduceValues
3944 >                              (ConcurrentHashMap.this, reducer));
3945 >        }
3946 >
3947 >        /**
3948 >         * Returns the result of accumulating the given transformation
3949 >         * of all values using the given reducer to combine values, or
3950 >         * null if none.
3951 >         *
3952 >         * @param transformer a function returning the transformation
3953 >         * for an element, or null of there is no transformation (in
3954 >         * which case it is not combined).
3955 >         * @param reducer a commutative associative combining function
3956 >         * @return the result of accumulating the given transformation
3957 >         * of all values
3958 >         */
3959 >        public <U> U reduceValues(Fun<? super V, ? extends U> transformer,
3960 >                                  BiFun<? super U, ? super U, ? extends U> reducer) {
3961 >            return fjp.invoke(ForkJoinTasks.reduceValues
3962 >                              (ConcurrentHashMap.this, transformer, reducer));
3963 >        }
3964 >
3965 >        /**
3966 >         * Returns the result of accumulating the given transformation
3967 >         * of all values using the given reducer to combine values,
3968 >         * and the given basis as an identity value.
3969 >         *
3970 >         * @param transformer a function returning the transformation
3971 >         * for an element
3972 >         * @param basis the identity (initial default value) for the reduction
3973 >         * @param reducer a commutative associative combining function
3974 >         * @return the result of accumulating the given transformation
3975 >         * of all values
3976 >         */
3977 >        public double reduceValuesToDouble(ObjectToDouble<? super V> transformer,
3978 >                                           double basis,
3979 >                                           DoubleByDoubleToDouble reducer) {
3980 >            return fjp.invoke(ForkJoinTasks.reduceValuesToDouble
3981 >                              (ConcurrentHashMap.this, transformer, basis, reducer));
3982 >        }
3983 >
3984 >        /**
3985 >         * Returns the result of accumulating the given transformation
3986 >         * of all values using the given reducer to combine values,
3987 >         * and the given basis as an identity value.
3988 >         *
3989 >         * @param transformer a function returning the transformation
3990 >         * for an element
3991 >         * @param basis the identity (initial default value) for the reduction
3992 >         * @param reducer a commutative associative combining function
3993 >         * @return the result of accumulating the given transformation
3994 >         * of all values
3995 >         */
3996 >        public long reduceValuesToLong(ObjectToLong<? super V> transformer,
3997 >                                       long basis,
3998 >                                       LongByLongToLong reducer) {
3999 >            return fjp.invoke(ForkJoinTasks.reduceValuesToLong
4000 >                              (ConcurrentHashMap.this, transformer, basis, reducer));
4001 >        }
4002 >
4003 >        /**
4004 >         * Returns the result of accumulating the given transformation
4005 >         * of all values using the given reducer to combine values,
4006 >         * and the given basis as an identity value.
4007 >         *
4008 >         * @param transformer a function returning the transformation
4009 >         * for an element
4010 >         * @param basis the identity (initial default value) for the reduction
4011 >         * @param reducer a commutative associative combining function
4012 >         * @return the result of accumulating the given transformation
4013 >         * of all values
4014 >         */
4015 >        public int reduceValuesToInt(ObjectToInt<? super V> transformer,
4016 >                                     int basis,
4017 >                                     IntByIntToInt reducer) {
4018 >            return fjp.invoke(ForkJoinTasks.reduceValuesToInt
4019 >                              (ConcurrentHashMap.this, transformer, basis, reducer));
4020 >        }
4021 >
4022 >        /**
4023 >         * Performs the given action for each entry.
4024 >         *
4025 >         * @param action the action
4026 >         */
4027 >        public void forEachEntry(Action<Map.Entry<K,V>> action) {
4028 >            fjp.invoke(ForkJoinTasks.forEachEntry
4029 >                       (ConcurrentHashMap.this, action));
4030 >        }
4031 >
4032 >        /**
4033 >         * Performs the given action for each non-null transformation
4034 >         * of each entry.
4035 >         *
4036 >         * @param transformer a function returning the transformation
4037 >         * for an element, or null of there is no transformation (in
4038 >         * which case the action is not applied).
4039 >         * @param action the action
4040 >         */
4041 >        public <U> void forEachEntry(Fun<Map.Entry<K,V>, ? extends U> transformer,
4042 >                                     Action<U> action) {
4043 >            fjp.invoke(ForkJoinTasks.forEachEntry
4044 >                       (ConcurrentHashMap.this, transformer, action));
4045 >        }
4046 >
4047 >        /**
4048 >         * Returns a non-null result from applying the given search
4049 >         * function on each entry, or null if none.  Upon success,
4050 >         * further element processing is suppressed and the results of
4051 >         * any other parallel invocations of the search function are
4052 >         * ignored.
4053 >         *
4054 >         * @param searchFunction a function returning a non-null
4055 >         * result on success, else null
4056 >         * @return a non-null result from applying the given search
4057 >         * function on each entry, or null if none
4058 >         */
4059 >        public <U> U searchEntries(Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
4060 >            return fjp.invoke(ForkJoinTasks.searchEntries
4061 >                              (ConcurrentHashMap.this, searchFunction));
4062 >        }
4063 >
4064 >        /**
4065 >         * Returns the result of accumulating all entries using the
4066 >         * given reducer to combine values, or null if none.
4067 >         *
4068 >         * @param reducer a commutative associative combining function
4069 >         * @return the result of accumulating all entries
4070 >         */
4071 >        public Map.Entry<K,V> reduceEntries(BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4072 >            return fjp.invoke(ForkJoinTasks.reduceEntries
4073 >                              (ConcurrentHashMap.this, reducer));
4074 >        }
4075 >
4076 >        /**
4077 >         * Returns the result of accumulating the given transformation
4078 >         * of all entries using the given reducer to combine values,
4079 >         * or null if none.
4080 >         *
4081 >         * @param transformer a function returning the transformation
4082 >         * for an element, or null of there is no transformation (in
4083 >         * which case it is not combined).
4084 >         * @param reducer a commutative associative combining function
4085 >         * @return the result of accumulating the given transformation
4086 >         * of all entries
4087 >         */
4088 >        public <U> U reduceEntries(Fun<Map.Entry<K,V>, ? extends U> transformer,
4089 >                                   BiFun<? super U, ? super U, ? extends U> reducer) {
4090 >            return fjp.invoke(ForkJoinTasks.reduceEntries
4091 >                              (ConcurrentHashMap.this, transformer, reducer));
4092 >        }
4093 >
4094 >        /**
4095 >         * Returns the result of accumulating the given transformation
4096 >         * of all entries using the given reducer to combine values,
4097 >         * and the given basis as an identity value.
4098 >         *
4099 >         * @param transformer a function returning the transformation
4100 >         * for an element
4101 >         * @param basis the identity (initial default value) for the reduction
4102 >         * @param reducer a commutative associative combining function
4103 >         * @return the result of accumulating the given transformation
4104 >         * of all entries
4105 >         */
4106 >        public double reduceEntriesToDouble(ObjectToDouble<Map.Entry<K,V>> transformer,
4107 >                                            double basis,
4108 >                                            DoubleByDoubleToDouble reducer) {
4109 >            return fjp.invoke(ForkJoinTasks.reduceEntriesToDouble
4110 >                              (ConcurrentHashMap.this, transformer, basis, reducer));
4111 >        }
4112 >
4113 >        /**
4114 >         * Returns the result of accumulating the given transformation
4115 >         * of all entries using the given reducer to combine values,
4116 >         * and the given basis as an identity value.
4117 >         *
4118 >         * @param transformer a function returning the transformation
4119 >         * for an element
4120 >         * @param basis the identity (initial default value) for the reduction
4121 >         * @param reducer a commutative associative combining function
4122 >         * @return  the result of accumulating the given transformation
4123 >         * of all entries
4124 >         */
4125 >        public long reduceEntriesToLong(ObjectToLong<Map.Entry<K,V>> transformer,
4126 >                                        long basis,
4127 >                                        LongByLongToLong reducer) {
4128 >            return fjp.invoke(ForkJoinTasks.reduceEntriesToLong
4129 >                              (ConcurrentHashMap.this, transformer, basis, reducer));
4130 >        }
4131 >
4132 >        /**
4133 >         * Returns the result of accumulating the given transformation
4134 >         * of all entries using the given reducer to combine values,
4135 >         * and the given basis as an identity value.
4136 >         *
4137 >         * @param transformer a function returning the transformation
4138 >         * for an element
4139 >         * @param basis the identity (initial default value) for the reduction
4140 >         * @param reducer a commutative associative combining function
4141 >         * @return the result of accumulating the given transformation
4142 >         * of all entries
4143 >         */
4144 >        public int reduceEntriesToInt(ObjectToInt<Map.Entry<K,V>> transformer,
4145 >                                      int basis,
4146 >                                      IntByIntToInt reducer) {
4147 >            return fjp.invoke(ForkJoinTasks.reduceEntriesToInt
4148 >                              (ConcurrentHashMap.this, transformer, basis, reducer));
4149 >        }
4150 >    }
4151 >
4152 >    // ---------------------------------------------------------------------
4153 >
4154 >    /**
4155 >     * Predefined tasks for performing bulk parallel operations on
4156 >     * ConcurrentHashMaps. These tasks follow the forms and rules used
4157 >     * in class {@link Parallel}. Each method has the same name, but
4158 >     * returns a task rather than invoking it. These methods may be
4159 >     * useful in custom applications such as submitting a task without
4160 >     * waiting for completion, or combining with other tasks.
4161 >     */
4162 >    public static class ForkJoinTasks {
4163 >        private ForkJoinTasks() {}
4164 >
4165 >        /**
4166 >         * Returns a task that when invoked, performs the given
4167 >         * action for each (key, value)
4168 >         *
4169 >         * @param map the map
4170 >         * @param action the action
4171 >         * @return the task
4172 >         */
4173 >        public static <K,V> ForkJoinTask<Void> forEach
4174 >            (ConcurrentHashMap<K,V> map,
4175 >             BiAction<K,V> action) {
4176 >            if (action == null) throw new NullPointerException();
4177 >            return new ForEachMappingTask<K,V>(map, action);
4178 >        }
4179 >
4180 >        /**
4181 >         * Returns a task that when invoked, performs the given
4182 >         * action for each non-null transformation of each (key, value)
4183 >         *
4184 >         * @param map the map
4185 >         * @param transformer a function returning the transformation
4186 >         * for an element, or null of there is no transformation (in
4187 >         * which case the action is not applied).
4188 >         * @param action the action
4189 >         * @return the task
4190 >         */
4191 >        public static <K,V,U> ForkJoinTask<Void> forEach
4192 >            (ConcurrentHashMap<K,V> map,
4193 >             BiFun<? super K, ? super V, ? extends U> transformer,
4194 >             Action<U> action) {
4195 >            if (transformer == null || action == null)
4196 >                throw new NullPointerException();
4197 >            return new ForEachTransformedMappingTask<K,V,U>
4198 >                (map, transformer, action);
4199 >        }
4200 >
4201 >        /**
4202 >         * Returns a task that when invoked, returns a non-null result
4203 >         * from applying the given search function on each (key,
4204 >         * value), or null if none. Upon success, further element
4205 >         * processing is suppressed and the results of any other
4206 >         * parallel invocations of the search function are ignored.
4207 >         *
4208 >         * @param map the map
4209 >         * @param searchFunction a function returning a non-null
4210 >         * result on success, else null
4211 >         * @return the task
4212 >         */
4213 >        public static <K,V,U> ForkJoinTask<U> search
4214 >            (ConcurrentHashMap<K,V> map,
4215 >             BiFun<? super K, ? super V, ? extends U> searchFunction) {
4216 >            if (searchFunction == null) throw new NullPointerException();
4217 >            return new SearchMappingsTask<K,V,U>
4218 >                (map, searchFunction,
4219 >                 new AtomicReference<U>());
4220 >        }
4221 >
4222 >        /**
4223 >         * Returns a task that when invoked, returns the result of
4224 >         * accumulating the given transformation of all (key, value) pairs
4225 >         * using the given reducer to combine values, or null if none.
4226 >         *
4227 >         * @param map the map
4228 >         * @param transformer a function returning the transformation
4229 >         * for an element, or null of there is no transformation (in
4230 >         * which case it is not combined).
4231 >         * @param reducer a commutative associative combining function
4232 >         * @return the task
4233 >         */
4234 >        public static <K,V,U> ForkJoinTask<U> reduce
4235 >            (ConcurrentHashMap<K,V> map,
4236 >             BiFun<? super K, ? super V, ? extends U> transformer,
4237 >             BiFun<? super U, ? super U, ? extends U> reducer) {
4238 >            if (transformer == null || reducer == null)
4239 >                throw new NullPointerException();
4240 >            return new MapReduceMappingsTask<K,V,U>
4241 >                (map, transformer, reducer);
4242 >        }
4243 >
4244 >        /**
4245 >         * Returns a task that when invoked, returns the result of
4246 >         * accumulating the given transformation of all (key, value) pairs
4247 >         * using the given reducer to combine values, and the given
4248 >         * basis as an identity value.
4249 >         *
4250 >         * @param map the map
4251 >         * @param transformer a function returning the transformation
4252 >         * for an element
4253 >         * @param basis the identity (initial default value) for the reduction
4254 >         * @param reducer a commutative associative combining function
4255 >         * @return the task
4256 >         */
4257 >        public static <K,V> ForkJoinTask<Double> reduceToDouble
4258 >            (ConcurrentHashMap<K,V> map,
4259 >             ObjectByObjectToDouble<? super K, ? super V> transformer,
4260 >             double basis,
4261 >             DoubleByDoubleToDouble reducer) {
4262 >            if (transformer == null || reducer == null)
4263 >                throw new NullPointerException();
4264 >            return new MapReduceMappingsToDoubleTask<K,V>
4265 >                (map, transformer, basis, reducer);
4266 >        }
4267 >
4268 >        /**
4269 >         * Returns a task that when invoked, returns the result of
4270 >         * accumulating the given transformation of all (key, value) pairs
4271 >         * using the given reducer to combine values, and the given
4272 >         * basis as an identity value.
4273 >         *
4274 >         * @param map the map
4275 >         * @param transformer a function returning the transformation
4276 >         * for an element
4277 >         * @param basis the identity (initial default value) for the reduction
4278 >         * @param reducer a commutative associative combining function
4279 >         * @return the task
4280 >         */
4281 >        public static <K,V> ForkJoinTask<Long> reduceToLong
4282 >            (ConcurrentHashMap<K,V> map,
4283 >             ObjectByObjectToLong<? super K, ? super V> transformer,
4284 >             long basis,
4285 >             LongByLongToLong reducer) {
4286 >            if (transformer == null || reducer == null)
4287 >                throw new NullPointerException();
4288 >            return new MapReduceMappingsToLongTask<K,V>
4289 >                (map, transformer, basis, reducer);
4290 >        }
4291 >
4292 >        /**
4293 >         * Returns a task that when invoked, returns the result of
4294 >         * accumulating the given transformation of all (key, value) pairs
4295 >         * using the given reducer to combine values, and the given
4296 >         * basis as an identity value.
4297 >         *
4298 >         * @param transformer a function returning the transformation
4299 >         * for an element
4300 >         * @param basis the identity (initial default value) for the reduction
4301 >         * @param reducer a commutative associative combining function
4302 >         * @return the task
4303 >         */
4304 >        public static <K,V> ForkJoinTask<Integer> reduceToInt
4305 >            (ConcurrentHashMap<K,V> map,
4306 >             ObjectByObjectToInt<? super K, ? super V> transformer,
4307 >             int basis,
4308 >             IntByIntToInt reducer) {
4309 >            if (transformer == null || reducer == null)
4310 >                throw new NullPointerException();
4311 >            return new MapReduceMappingsToIntTask<K,V>
4312 >                (map, transformer, basis, reducer);
4313 >        }
4314 >
4315 >        /**
4316 >         * Returns a task that when invoked, performs the given action
4317 >         * for each key.
4318 >         *
4319 >         * @param map the map
4320 >         * @param action the action
4321 >         * @return the task
4322 >         */
4323 >        public static <K,V> ForkJoinTask<Void> forEachKey
4324 >            (ConcurrentHashMap<K,V> map,
4325 >             Action<K> action) {
4326 >            if (action == null) throw new NullPointerException();
4327 >            return new ForEachKeyTask<K,V>(map, action);
4328 >        }
4329 >
4330 >        /**
4331 >         * Returns a task that when invoked, performs the given action
4332 >         * for each non-null transformation of each key.
4333 >         *
4334 >         * @param map the map
4335 >         * @param transformer a function returning the transformation
4336 >         * for an element, or null of there is no transformation (in
4337 >         * which case the action is not applied).
4338 >         * @param action the action
4339 >         * @return the task
4340 >         */
4341 >        public static <K,V,U> ForkJoinTask<Void> forEachKey
4342 >            (ConcurrentHashMap<K,V> map,
4343 >             Fun<? super K, ? extends U> transformer,
4344 >             Action<U> action) {
4345 >            if (transformer == null || action == null)
4346 >                throw new NullPointerException();
4347 >            return new ForEachTransformedKeyTask<K,V,U>
4348 >                (map, transformer, action);
4349 >        }
4350 >
4351 >        /**
4352 >         * Returns a task that when invoked, returns a non-null result
4353 >         * from applying the given search function on each key, or
4354 >         * null if none.  Upon success, further element processing is
4355 >         * suppressed and the results of any other parallel
4356 >         * invocations of the search function are ignored.
4357 >         *
4358 >         * @param map the map
4359 >         * @param searchFunction a function returning a non-null
4360 >         * result on success, else null
4361 >         * @return the task
4362 >         */
4363 >        public static <K,V,U> ForkJoinTask<U> searchKeys
4364 >            (ConcurrentHashMap<K,V> map,
4365 >             Fun<? super K, ? extends U> searchFunction) {
4366 >            if (searchFunction == null) throw new NullPointerException();
4367 >            return new SearchKeysTask<K,V,U>
4368 >                (map, searchFunction,
4369 >                 new AtomicReference<U>());
4370 >        }
4371 >
4372 >        /**
4373 >         * Returns a task that when invoked, returns the result of
4374 >         * accumulating all keys using the given reducer to combine
4375 >         * values, or null if none.
4376 >         *
4377 >         * @param map the map
4378 >         * @param reducer a commutative associative combining function
4379 >         * @return the task
4380 >         */
4381 >        public static <K,V> ForkJoinTask<K> reduceKeys
4382 >            (ConcurrentHashMap<K,V> map,
4383 >             BiFun<? super K, ? super K, ? extends K> reducer) {
4384 >            if (reducer == null) throw new NullPointerException();
4385 >            return new ReduceKeysTask<K,V>
4386 >                (map, reducer);
4387 >        }
4388 >
4389 >        /**
4390 >         * Returns a task that when invoked, returns the result of
4391 >         * accumulating the given transformation of all keys using the given
4392 >         * reducer to combine values, or null if none.
4393 >         *
4394 >         * @param map the map
4395 >         * @param transformer a function returning the transformation
4396 >         * for an element, or null of there is no transformation (in
4397 >         * which case it is not combined).
4398 >         * @param reducer a commutative associative combining function
4399 >         * @return the task
4400 >         */
4401 >        public static <K,V,U> ForkJoinTask<U> reduceKeys
4402 >            (ConcurrentHashMap<K,V> map,
4403 >             Fun<? super K, ? extends U> transformer,
4404 >             BiFun<? super U, ? super U, ? extends U> reducer) {
4405 >            if (transformer == null || reducer == null)
4406 >                throw new NullPointerException();
4407 >            return new MapReduceKeysTask<K,V,U>
4408 >                (map, transformer, reducer);
4409 >        }
4410 >
4411 >        /**
4412 >         * Returns a task that when invoked, returns the result of
4413 >         * accumulating the given transformation of all keys using the given
4414 >         * reducer to combine values, and the given basis as an
4415 >         * identity value.
4416 >         *
4417 >         * @param map the map
4418 >         * @param transformer a function returning the transformation
4419 >         * for an element
4420 >         * @param basis the identity (initial default value) for the reduction
4421 >         * @param reducer a commutative associative combining function
4422 >         * @return the task
4423 >         */
4424 >        public static <K,V> ForkJoinTask<Double> reduceKeysToDouble
4425 >            (ConcurrentHashMap<K,V> map,
4426 >             ObjectToDouble<? super K> transformer,
4427 >             double basis,
4428 >             DoubleByDoubleToDouble reducer) {
4429 >            if (transformer == null || reducer == null)
4430 >                throw new NullPointerException();
4431 >            return new MapReduceKeysToDoubleTask<K,V>
4432 >                (map, transformer, basis, reducer);
4433 >        }
4434 >
4435 >        /**
4436 >         * Returns a task that when invoked, returns the result of
4437 >         * accumulating the given transformation of all keys using the given
4438 >         * reducer to combine values, and the given basis as an
4439 >         * identity value.
4440 >         *
4441 >         * @param map the map
4442 >         * @param transformer a function returning the transformation
4443 >         * for an element
4444 >         * @param basis the identity (initial default value) for the reduction
4445 >         * @param reducer a commutative associative combining function
4446 >         * @return the task
4447 >         */
4448 >        public static <K,V> ForkJoinTask<Long> reduceKeysToLong
4449 >            (ConcurrentHashMap<K,V> map,
4450 >             ObjectToLong<? super K> transformer,
4451 >             long basis,
4452 >             LongByLongToLong reducer) {
4453 >            if (transformer == null || reducer == null)
4454 >                throw new NullPointerException();
4455 >            return new MapReduceKeysToLongTask<K,V>
4456 >                (map, transformer, basis, reducer);
4457 >        }
4458 >
4459 >        /**
4460 >         * Returns a task that when invoked, returns the result of
4461 >         * accumulating the given transformation of all keys using the given
4462 >         * reducer to combine values, and the given basis as an
4463 >         * identity value.
4464 >         *
4465 >         * @param map the map
4466 >         * @param transformer a function returning the transformation
4467 >         * for an element
4468 >         * @param basis the identity (initial default value) for the reduction
4469 >         * @param reducer a commutative associative combining function
4470 >         * @return the task
4471 >         */
4472 >        public static <K,V> ForkJoinTask<Integer> reduceKeysToInt
4473 >            (ConcurrentHashMap<K,V> map,
4474 >             ObjectToInt<? super K> transformer,
4475 >             int basis,
4476 >             IntByIntToInt reducer) {
4477 >            if (transformer == null || reducer == null)
4478 >                throw new NullPointerException();
4479 >            return new MapReduceKeysToIntTask<K,V>
4480 >                (map, transformer, basis, reducer);
4481 >        }
4482 >
4483 >        /**
4484 >         * Returns a task that when invoked, performs the given action
4485 >         * for each value.
4486 >         *
4487 >         * @param map the map
4488 >         * @param action the action
4489 >         */
4490 >        public static <K,V> ForkJoinTask<Void> forEachValue
4491 >            (ConcurrentHashMap<K,V> map,
4492 >             Action<V> action) {
4493 >            if (action == null) throw new NullPointerException();
4494 >            return new ForEachValueTask<K,V>(map, action);
4495 >        }
4496 >
4497 >        /**
4498 >         * Returns a task that when invoked, performs the given action
4499 >         * for each non-null transformation of each value.
4500 >         *
4501 >         * @param map the map
4502 >         * @param transformer a function returning the transformation
4503 >         * for an element, or null of there is no transformation (in
4504 >         * which case the action is not applied).
4505 >         * @param action the action
4506 >         */
4507 >        public static <K,V,U> ForkJoinTask<Void> forEachValue
4508 >            (ConcurrentHashMap<K,V> map,
4509 >             Fun<? super V, ? extends U> transformer,
4510 >             Action<U> action) {
4511 >            if (transformer == null || action == null)
4512 >                throw new NullPointerException();
4513 >            return new ForEachTransformedValueTask<K,V,U>
4514 >                (map, transformer, action);
4515 >        }
4516 >
4517 >        /**
4518 >         * Returns a task that when invoked, returns a non-null result
4519 >         * from applying the given search function on each value, or
4520 >         * null if none.  Upon success, further element processing is
4521 >         * suppressed and the results of any other parallel
4522 >         * invocations of the search function are ignored.
4523 >         *
4524 >         * @param map the map
4525 >         * @param searchFunction a function returning a non-null
4526 >         * result on success, else null
4527 >         * @return the task
4528 >         *
4529 >         */
4530 >        public static <K,V,U> ForkJoinTask<U> searchValues
4531 >            (ConcurrentHashMap<K,V> map,
4532 >             Fun<? super V, ? extends U> searchFunction) {
4533 >            if (searchFunction == null) throw new NullPointerException();
4534 >            return new SearchValuesTask<K,V,U>
4535 >                (map, searchFunction,
4536 >                 new AtomicReference<U>());
4537 >        }
4538 >
4539 >        /**
4540 >         * Returns a task that when invoked, returns the result of
4541 >         * accumulating all values using the given reducer to combine
4542 >         * values, or null if none.
4543 >         *
4544 >         * @param map the map
4545 >         * @param reducer a commutative associative combining function
4546 >         * @return the task
4547 >         */
4548 >        public static <K,V> ForkJoinTask<V> reduceValues
4549 >            (ConcurrentHashMap<K,V> map,
4550 >             BiFun<? super V, ? super V, ? extends V> reducer) {
4551 >            if (reducer == null) throw new NullPointerException();
4552 >            return new ReduceValuesTask<K,V>
4553 >                (map, reducer);
4554 >        }
4555 >
4556 >        /**
4557 >         * Returns a task that when invoked, returns the result of
4558 >         * accumulating the given transformation of all values using the
4559 >         * given reducer to combine values, or null if none.
4560 >         *
4561 >         * @param map the map
4562 >         * @param transformer a function returning the transformation
4563 >         * for an element, or null of there is no transformation (in
4564 >         * which case it is not combined).
4565 >         * @param reducer a commutative associative combining function
4566 >         * @return the task
4567 >         */
4568 >        public static <K,V,U> ForkJoinTask<U> reduceValues
4569 >            (ConcurrentHashMap<K,V> map,
4570 >             Fun<? super V, ? extends U> transformer,
4571 >             BiFun<? super U, ? super U, ? extends U> reducer) {
4572 >            if (transformer == null || reducer == null)
4573 >                throw new NullPointerException();
4574 >            return new MapReduceValuesTask<K,V,U>
4575 >                (map, transformer, reducer);
4576 >        }
4577 >
4578 >        /**
4579 >         * Returns a task that when invoked, returns the result of
4580 >         * accumulating the given transformation of all values using the
4581 >         * given reducer to combine values, and the given basis as an
4582 >         * identity value.
4583 >         *
4584 >         * @param map the map
4585 >         * @param transformer a function returning the transformation
4586 >         * for an element
4587 >         * @param basis the identity (initial default value) for the reduction
4588 >         * @param reducer a commutative associative combining function
4589 >         * @return the task
4590 >         */
4591 >        public static <K,V> ForkJoinTask<Double> reduceValuesToDouble
4592 >            (ConcurrentHashMap<K,V> map,
4593 >             ObjectToDouble<? super V> transformer,
4594 >             double basis,
4595 >             DoubleByDoubleToDouble reducer) {
4596 >            if (transformer == null || reducer == null)
4597 >                throw new NullPointerException();
4598 >            return new MapReduceValuesToDoubleTask<K,V>
4599 >                (map, transformer, basis, reducer);
4600 >        }
4601 >
4602 >        /**
4603 >         * Returns a task that when invoked, returns the result of
4604 >         * accumulating the given transformation of all values using the
4605 >         * given reducer to combine values, and the given basis as an
4606 >         * identity value.
4607 >         *
4608 >         * @param map the map
4609 >         * @param transformer a function returning the transformation
4610 >         * for an element
4611 >         * @param basis the identity (initial default value) for the reduction
4612 >         * @param reducer a commutative associative combining function
4613 >         * @return the task
4614 >         */
4615 >        public static <K,V> ForkJoinTask<Long> reduceValuesToLong
4616 >            (ConcurrentHashMap<K,V> map,
4617 >             ObjectToLong<? super V> transformer,
4618 >             long basis,
4619 >             LongByLongToLong reducer) {
4620 >            if (transformer == null || reducer == null)
4621 >                throw new NullPointerException();
4622 >            return new MapReduceValuesToLongTask<K,V>
4623 >                (map, transformer, basis, reducer);
4624 >        }
4625 >
4626 >        /**
4627 >         * Returns a task that when invoked, returns the result of
4628 >         * accumulating the given transformation of all values using the
4629 >         * given reducer to combine values, and the given basis as an
4630 >         * identity value.
4631 >         *
4632 >         * @param map the map
4633 >         * @param transformer a function returning the transformation
4634 >         * for an element
4635 >         * @param basis the identity (initial default value) for the reduction
4636 >         * @param reducer a commutative associative combining function
4637 >         * @return the task
4638 >         */
4639 >        public static <K,V> ForkJoinTask<Integer> reduceValuesToInt
4640 >            (ConcurrentHashMap<K,V> map,
4641 >             ObjectToInt<? super V> transformer,
4642 >             int basis,
4643 >             IntByIntToInt reducer) {
4644 >            if (transformer == null || reducer == null)
4645 >                throw new NullPointerException();
4646 >            return new MapReduceValuesToIntTask<K,V>
4647 >                (map, transformer, basis, reducer);
4648 >        }
4649 >
4650 >        /**
4651 >         * Returns a task that when invoked, perform the given action
4652 >         * for each entry.
4653 >         *
4654 >         * @param map the map
4655 >         * @param action the action
4656 >         */
4657 >        public static <K,V> ForkJoinTask<Void> forEachEntry
4658 >            (ConcurrentHashMap<K,V> map,
4659 >             Action<Map.Entry<K,V>> action) {
4660 >            if (action == null) throw new NullPointerException();
4661 >            return new ForEachEntryTask<K,V>(map, action);
4662 >        }
4663 >
4664 >        /**
4665 >         * Returns a task that when invoked, perform the given action
4666 >         * for each non-null transformation of each entry.
4667 >         *
4668 >         * @param map the map
4669 >         * @param transformer a function returning the transformation
4670 >         * for an element, or null of there is no transformation (in
4671 >         * which case the action is not applied).
4672 >         * @param action the action
4673 >         */
4674 >        public static <K,V,U> ForkJoinTask<Void> forEachEntry
4675 >            (ConcurrentHashMap<K,V> map,
4676 >             Fun<Map.Entry<K,V>, ? extends U> transformer,
4677 >             Action<U> action) {
4678 >            if (transformer == null || action == null)
4679 >                throw new NullPointerException();
4680 >            return new ForEachTransformedEntryTask<K,V,U>
4681 >                (map, transformer, action);
4682 >        }
4683 >
4684 >        /**
4685 >         * Returns a task that when invoked, returns a non-null result
4686 >         * from applying the given search function on each entry, or
4687 >         * null if none.  Upon success, further element processing is
4688 >         * suppressed and the results of any other parallel
4689 >         * invocations of the search function are ignored.
4690 >         *
4691 >         * @param map the map
4692 >         * @param searchFunction a function returning a non-null
4693 >         * result on success, else null
4694 >         * @return the task
4695 >         *
4696 >         */
4697 >        public static <K,V,U> ForkJoinTask<U> searchEntries
4698 >            (ConcurrentHashMap<K,V> map,
4699 >             Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
4700 >            if (searchFunction == null) throw new NullPointerException();
4701 >            return new SearchEntriesTask<K,V,U>
4702 >                (map, searchFunction,
4703 >                 new AtomicReference<U>());
4704 >        }
4705 >
4706 >        /**
4707 >         * Returns a task that when invoked, returns the result of
4708 >         * accumulating all entries using the given reducer to combine
4709 >         * values, or null if none.
4710 >         *
4711 >         * @param map the map
4712 >         * @param reducer a commutative associative combining function
4713 >         * @return the task
4714 >         */
4715 >        public static <K,V> ForkJoinTask<Map.Entry<K,V>> reduceEntries
4716 >            (ConcurrentHashMap<K,V> map,
4717 >             BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4718 >            if (reducer == null) throw new NullPointerException();
4719 >            return new ReduceEntriesTask<K,V>
4720 >                (map, reducer);
4721 >        }
4722 >
4723 >        /**
4724 >         * Returns a task that when invoked, returns the result of
4725 >         * accumulating the given transformation of all entries using the
4726 >         * given reducer to combine values, or null if none.
4727 >         *
4728 >         * @param map the map
4729 >         * @param transformer a function returning the transformation
4730 >         * for an element, or null of there is no transformation (in
4731 >         * which case it is not combined).
4732 >         * @param reducer a commutative associative combining function
4733 >         * @return the task
4734 >         */
4735 >        public static <K,V,U> ForkJoinTask<U> reduceEntries
4736 >            (ConcurrentHashMap<K,V> map,
4737 >             Fun<Map.Entry<K,V>, ? extends U> transformer,
4738 >             BiFun<? super U, ? super U, ? extends U> reducer) {
4739 >            if (transformer == null || reducer == null)
4740 >                throw new NullPointerException();
4741 >            return new MapReduceEntriesTask<K,V,U>
4742 >                (map, transformer, reducer);
4743 >        }
4744 >
4745 >        /**
4746 >         * Returns a task that when invoked, returns the result of
4747 >         * accumulating the given transformation of all entries using the
4748 >         * given reducer to combine values, and the given basis as an
4749 >         * identity value.
4750 >         *
4751 >         * @param map the map
4752 >         * @param transformer a function returning the transformation
4753 >         * for an element
4754 >         * @param basis the identity (initial default value) for the reduction
4755 >         * @param reducer a commutative associative combining function
4756 >         * @return the task
4757 >         */
4758 >        public static <K,V> ForkJoinTask<Double> reduceEntriesToDouble
4759 >            (ConcurrentHashMap<K,V> map,
4760 >             ObjectToDouble<Map.Entry<K,V>> transformer,
4761 >             double basis,
4762 >             DoubleByDoubleToDouble reducer) {
4763 >            if (transformer == null || reducer == null)
4764 >                throw new NullPointerException();
4765 >            return new MapReduceEntriesToDoubleTask<K,V>
4766 >                (map, transformer, basis, reducer);
4767 >        }
4768 >
4769 >        /**
4770 >         * Returns a task that when invoked, returns the result of
4771 >         * accumulating the given transformation of all entries using the
4772 >         * given reducer to combine values, and the given basis as an
4773 >         * identity value.
4774 >         *
4775 >         * @param map the map
4776 >         * @param transformer a function returning the transformation
4777 >         * for an element
4778 >         * @param basis the identity (initial default value) for the reduction
4779 >         * @param reducer a commutative associative combining function
4780 >         * @return the task
4781 >         */
4782 >        public static <K,V> ForkJoinTask<Long> reduceEntriesToLong
4783 >            (ConcurrentHashMap<K,V> map,
4784 >             ObjectToLong<Map.Entry<K,V>> transformer,
4785 >             long basis,
4786 >             LongByLongToLong reducer) {
4787 >            if (transformer == null || reducer == null)
4788 >                throw new NullPointerException();
4789 >            return new MapReduceEntriesToLongTask<K,V>
4790 >                (map, transformer, basis, reducer);
4791 >        }
4792 >
4793 >        /**
4794 >         * Returns a task that when invoked, returns the result of
4795 >         * accumulating the given transformation of all entries using the
4796 >         * given reducer to combine values, and the given basis as an
4797 >         * identity value.
4798 >         *
4799 >         * @param map the map
4800 >         * @param transformer a function returning the transformation
4801 >         * for an element
4802 >         * @param basis the identity (initial default value) for the reduction
4803 >         * @param reducer a commutative associative combining function
4804 >         * @return the task
4805 >         */
4806 >        public static <K,V> ForkJoinTask<Integer> reduceEntriesToInt
4807 >            (ConcurrentHashMap<K,V> map,
4808 >             ObjectToInt<Map.Entry<K,V>> transformer,
4809 >             int basis,
4810 >             IntByIntToInt reducer) {
4811 >            if (transformer == null || reducer == null)
4812 >                throw new NullPointerException();
4813 >            return new MapReduceEntriesToIntTask<K,V>
4814 >                (map, transformer, basis, reducer);
4815 >        }
4816 >    }
4817 >
4818 >    // -------------------------------------------------------
4819 >
4820 >    /**
4821 >     * Base for FJ tasks for bulk operations. This adds a variant of
4822 >     * CountedCompleters and some split and merge bookkeeping to
4823 >     * iterator functionality. The forEach and reduce methods are
4824 >     * similar to those illustrated in CountedCompleter documentation,
4825 >     * except that bottom-up reduction completions perform them within
4826 >     * their compute methods. The search methods are like forEach
4827 >     * except they continually poll for success and exit early.  Also,
4828 >     * exceptions are handled in a simpler manner, by just trying to
4829 >     * complete root task exceptionally.
4830 >     */
4831 >    @SuppressWarnings("serial")
4832 >    static abstract class BulkTask<K,V,R> extends Traverser<K,V,R> {
4833 >        final BulkTask<K,V,?> parent;  // completion target
4834 >        int batch;                     // split control
4835 >        int pending;                   // completion control
4836 >
4837 >        /** Constructor for root tasks */
4838 >        BulkTask(ConcurrentHashMap<K,V> map) {
4839 >            super(map);
4840 >            this.parent = null;
4841 >            this.batch = -1; // force call to batch() on execution
4842 >        }
4843 >
4844 >        /** Constructor for subtasks */
4845 >        BulkTask(BulkTask<K,V,?> parent, int batch, boolean split) {
4846 >            super(parent, split);
4847 >            this.parent = parent;
4848 >            this.batch = batch;
4849 >        }
4850 >
4851 >        // FJ methods
4852 >
4853 >        /**
4854 >         * Propagates completion. Note that all reduce actions
4855 >         * bypass this method to combine while completing.
4856 >         */
4857 >        final void tryComplete() {
4858 >            BulkTask<K,V,?> a = this, s = a;
4859 >            for (int c;;) {
4860 >                if ((c = a.pending) == 0) {
4861 >                    if ((a = (s = a).parent) == null) {
4862 >                        s.quietlyComplete();
4863 >                        break;
4864 >                    }
4865 >                }
4866 >                else if (U.compareAndSwapInt(a, PENDING, c, c - 1))
4867 >                    break;
4868 >            }
4869 >        }
4870 >
4871 >        /**
4872 >         * Forces root task to throw exception unless already complete.
4873 >         */
4874 >        final void tryAbortComputation(Throwable ex) {
4875 >            for (BulkTask<K,V,?> a = this;;) {
4876 >                BulkTask<K,V,?> p = a.parent;
4877 >                if (p == null) {
4878 >                    a.completeExceptionally(ex);
4879 >                    break;
4880 >                }
4881 >                a = p;
4882 >            }
4883 >        }
4884 >
4885 >        public final boolean exec() {
4886 >            try {
4887 >                compute();
4888 >            }
4889 >            catch (Throwable ex) {
4890 >                tryAbortComputation(ex);
4891 >            }
4892 >            return false;
4893 >        }
4894 >
4895 >        public abstract void compute();
4896 >
4897 >        // utilities
4898 >
4899 >        /** CompareAndSet pending count */
4900 >        final boolean casPending(int cmp, int val) {
4901 >            return U.compareAndSwapInt(this, PENDING, cmp, val);
4902 >        }
4903 >
4904 >        /**
4905 >         * Returns approx exp2 of the number of times (minus one) to
4906 >         * split task by two before executing leaf action. This value
4907 >         * is faster to compute and more convenient to use as a guide
4908 >         * to splitting than is the depth, since it is used while
4909 >         * dividing by two anyway.
4910 >         */
4911 >        final int batch() {
4912 >            int b = batch;
4913 >            if (b < 0) {
4914 >                long n = map.counter.sum();
4915 >                int sp = getPool().getParallelism() << 3; // slack of 8
4916 >                b = batch = (n <= 0L) ? 0 : (n < (long)sp) ? (int)n : sp;
4917 >            }
4918 >            return b;
4919 >        }
4920 >
4921 >        /**
4922 >         * Error message for hoisted null checks of functions
4923 >         */
4924 >        static final String NullFunctionMessage =
4925 >            "Unexpected null function";
4926 >
4927 >        /**
4928 >         * Returns exportable snapshot entry.
4929 >         */
4930 >        static <K,V> AbstractMap.SimpleEntry<K,V> entryFor(K k, V v) {
4931 >            return new AbstractMap.SimpleEntry<K,V>(k, v);
4932 >        }
4933 >
4934 >        // Unsafe mechanics
4935 >        private static final sun.misc.Unsafe U;
4936 >        private static final long PENDING;
4937 >        static {
4938 >            try {
4939 >                U = sun.misc.Unsafe.getUnsafe();
4940 >                PENDING = U.objectFieldOffset
4941 >                    (BulkTask.class.getDeclaredField("pending"));
4942 >            } catch (Exception e) {
4943 >                throw new Error(e);
4944 >            }
4945 >        }
4946 >    }
4947 >
4948 >    /*
4949 >     * Task classes. Coded in a regular but ugly format/style to
4950 >     * simplify checks that each variant differs in the right way from
4951 >     * others.
4952 >     */
4953 >
4954 >    @SuppressWarnings("serial")
4955 >    static final class ForEachKeyTask<K,V>
4956 >        extends BulkTask<K,V,Void> {
4957 >        final Action<K> action;
4958 >        ForEachKeyTask
4959 >            (ConcurrentHashMap<K,V> m,
4960 >             Action<K> action) {
4961 >            super(m);
4962 >            this.action = action;
4963 >        }
4964 >        ForEachKeyTask
4965 >            (BulkTask<K,V,?> p, int b, boolean split,
4966 >             Action<K> action) {
4967 >            super(p, b, split);
4968 >            this.action = action;
4969 >        }
4970 >        @SuppressWarnings("unchecked") public final void compute() {
4971 >            final Action<K> action = this.action;
4972 >            if (action == null)
4973 >                throw new Error(NullFunctionMessage);
4974 >            int b = batch(), c;
4975 >            while (b > 1 && baseIndex != baseLimit) {
4976 >                do {} while (!casPending(c = pending, c+1));
4977 >                new ForEachKeyTask<K,V>(this, b >>>= 1, true, action).fork();
4978 >            }
4979 >            while (advance() != null)
4980 >                action.apply((K)nextKey);
4981 >            tryComplete();
4982 >        }
4983 >    }
4984 >
4985 >    @SuppressWarnings("serial")
4986 >    static final class ForEachValueTask<K,V>
4987 >        extends BulkTask<K,V,Void> {
4988 >        final Action<V> action;
4989 >        ForEachValueTask
4990 >            (ConcurrentHashMap<K,V> m,
4991 >             Action<V> action) {
4992 >            super(m);
4993 >            this.action = action;
4994 >        }
4995 >        ForEachValueTask
4996 >            (BulkTask<K,V,?> p, int b, boolean split,
4997 >             Action<V> action) {
4998 >            super(p, b, split);
4999 >            this.action = action;
5000 >        }
5001 >        @SuppressWarnings("unchecked") public final void compute() {
5002 >            final Action<V> action = this.action;
5003 >            if (action == null)
5004 >                throw new Error(NullFunctionMessage);
5005 >            int b = batch(), c;
5006 >            while (b > 1 && baseIndex != baseLimit) {
5007 >                do {} while (!casPending(c = pending, c+1));
5008 >                new ForEachValueTask<K,V>(this, b >>>= 1, true, action).fork();
5009 >            }
5010 >            Object v;
5011 >            while ((v = advance()) != null)
5012 >                action.apply((V)v);
5013 >            tryComplete();
5014 >        }
5015 >    }
5016 >
5017 >    @SuppressWarnings("serial")
5018 >    static final class ForEachEntryTask<K,V>
5019 >        extends BulkTask<K,V,Void> {
5020 >        final Action<Entry<K,V>> action;
5021 >        ForEachEntryTask
5022 >            (ConcurrentHashMap<K,V> m,
5023 >             Action<Entry<K,V>> action) {
5024 >            super(m);
5025 >            this.action = action;
5026 >        }
5027 >        ForEachEntryTask
5028 >            (BulkTask<K,V,?> p, int b, boolean split,
5029 >             Action<Entry<K,V>> action) {
5030 >            super(p, b, split);
5031 >            this.action = action;
5032 >        }
5033 >        @SuppressWarnings("unchecked") public final void compute() {
5034 >            final Action<Entry<K,V>> action = this.action;
5035 >            if (action == null)
5036 >                throw new Error(NullFunctionMessage);
5037 >            int b = batch(), c;
5038 >            while (b > 1 && baseIndex != baseLimit) {
5039 >                do {} while (!casPending(c = pending, c+1));
5040 >                new ForEachEntryTask<K,V>(this, b >>>= 1, true, action).fork();
5041 >            }
5042 >            Object v;
5043 >            while ((v = advance()) != null)
5044 >                action.apply(entryFor((K)nextKey, (V)v));
5045 >            tryComplete();
5046 >        }
5047 >    }
5048 >
5049 >    @SuppressWarnings("serial")
5050 >    static final class ForEachMappingTask<K,V>
5051 >        extends BulkTask<K,V,Void> {
5052 >        final BiAction<K,V> action;
5053 >        ForEachMappingTask
5054 >            (ConcurrentHashMap<K,V> m,
5055 >             BiAction<K,V> action) {
5056 >            super(m);
5057 >            this.action = action;
5058 >        }
5059 >        ForEachMappingTask
5060 >            (BulkTask<K,V,?> p, int b, boolean split,
5061 >             BiAction<K,V> action) {
5062 >            super(p, b, split);
5063 >            this.action = action;
5064 >        }
5065 >
5066 >        @SuppressWarnings("unchecked") public final void compute() {
5067 >            final BiAction<K,V> action = this.action;
5068 >            if (action == null)
5069 >                throw new Error(NullFunctionMessage);
5070 >            int b = batch(), c;
5071 >            while (b > 1 && baseIndex != baseLimit) {
5072 >                do {} while (!casPending(c = pending, c+1));
5073 >                new ForEachMappingTask<K,V>(this, b >>>= 1, true,
5074 >                                            action).fork();
5075 >            }
5076 >            Object v;
5077 >            while ((v = advance()) != null)
5078 >                action.apply((K)nextKey, (V)v);
5079 >            tryComplete();
5080 >        }
5081 >    }
5082 >
5083 >    @SuppressWarnings("serial")
5084 >    static final class ForEachTransformedKeyTask<K,V,U>
5085 >        extends BulkTask<K,V,Void> {
5086 >        final Fun<? super K, ? extends U> transformer;
5087 >        final Action<U> action;
5088 >        ForEachTransformedKeyTask
5089 >            (ConcurrentHashMap<K,V> m,
5090 >             Fun<? super K, ? extends U> transformer,
5091 >             Action<U> action) {
5092 >            super(m);
5093 >            this.transformer = transformer;
5094 >            this.action = action;
5095 >
5096 >        }
5097 >        ForEachTransformedKeyTask
5098 >            (BulkTask<K,V,?> p, int b, boolean split,
5099 >             Fun<? super K, ? extends U> transformer,
5100 >             Action<U> action) {
5101 >            super(p, b, split);
5102 >            this.transformer = transformer;
5103 >            this.action = action;
5104 >        }
5105 >        @SuppressWarnings("unchecked") public final void compute() {
5106 >            final Fun<? super K, ? extends U> transformer =
5107 >                this.transformer;
5108 >            final Action<U> action = this.action;
5109 >            if (transformer == null || action == null)
5110 >                throw new Error(NullFunctionMessage);
5111 >            int b = batch(), c;
5112 >            while (b > 1 && baseIndex != baseLimit) {
5113 >                do {} while (!casPending(c = pending, c+1));
5114 >                new ForEachTransformedKeyTask<K,V,U>
5115 >                    (this, b >>>= 1, true, transformer, action).fork();
5116 >            }
5117 >            U u;
5118 >            while (advance() != null) {
5119 >                if ((u = transformer.apply((K)nextKey)) != null)
5120 >                    action.apply(u);
5121 >            }
5122 >            tryComplete();
5123 >        }
5124 >    }
5125 >
5126 >    @SuppressWarnings("serial")
5127 >    static final class ForEachTransformedValueTask<K,V,U>
5128 >        extends BulkTask<K,V,Void> {
5129 >        final Fun<? super V, ? extends U> transformer;
5130 >        final Action<U> action;
5131 >        ForEachTransformedValueTask
5132 >            (ConcurrentHashMap<K,V> m,
5133 >             Fun<? super V, ? extends U> transformer,
5134 >             Action<U> action) {
5135 >            super(m);
5136 >            this.transformer = transformer;
5137 >            this.action = action;
5138 >
5139 >        }
5140 >        ForEachTransformedValueTask
5141 >            (BulkTask<K,V,?> p, int b, boolean split,
5142 >             Fun<? super V, ? extends U> transformer,
5143 >             Action<U> action) {
5144 >            super(p, b, split);
5145 >            this.transformer = transformer;
5146 >            this.action = action;
5147 >        }
5148 >        @SuppressWarnings("unchecked") public final void compute() {
5149 >            final Fun<? super V, ? extends U> transformer =
5150 >                this.transformer;
5151 >            final Action<U> action = this.action;
5152 >            if (transformer == null || action == null)
5153 >                throw new Error(NullFunctionMessage);
5154 >            int b = batch(), c;
5155 >            while (b > 1 && baseIndex != baseLimit) {
5156 >                do {} while (!casPending(c = pending, c+1));
5157 >                new ForEachTransformedValueTask<K,V,U>
5158 >                    (this, b >>>= 1, true, transformer, action).fork();
5159 >            }
5160 >            Object v; U u;
5161 >            while ((v = advance()) != null) {
5162 >                if ((u = transformer.apply((V)v)) != null)
5163 >                    action.apply(u);
5164 >            }
5165 >            tryComplete();
5166 >        }
5167 >    }
5168 >
5169 >    @SuppressWarnings("serial")
5170 >    static final class ForEachTransformedEntryTask<K,V,U>
5171 >        extends BulkTask<K,V,Void> {
5172 >        final Fun<Map.Entry<K,V>, ? extends U> transformer;
5173 >        final Action<U> action;
5174 >        ForEachTransformedEntryTask
5175 >            (ConcurrentHashMap<K,V> m,
5176 >             Fun<Map.Entry<K,V>, ? extends U> transformer,
5177 >             Action<U> action) {
5178 >            super(m);
5179 >            this.transformer = transformer;
5180 >            this.action = action;
5181 >
5182 >        }
5183 >        ForEachTransformedEntryTask
5184 >            (BulkTask<K,V,?> p, int b, boolean split,
5185 >             Fun<Map.Entry<K,V>, ? extends U> transformer,
5186 >             Action<U> action) {
5187 >            super(p, b, split);
5188 >            this.transformer = transformer;
5189 >            this.action = action;
5190 >        }
5191 >        @SuppressWarnings("unchecked") public final void compute() {
5192 >            final Fun<Map.Entry<K,V>, ? extends U> transformer =
5193 >                this.transformer;
5194 >            final Action<U> action = this.action;
5195 >            if (transformer == null || action == null)
5196 >                throw new Error(NullFunctionMessage);
5197 >            int b = batch(), c;
5198 >            while (b > 1 && baseIndex != baseLimit) {
5199 >                do {} while (!casPending(c = pending, c+1));
5200 >                new ForEachTransformedEntryTask<K,V,U>
5201 >                    (this, b >>>= 1, true, transformer, action).fork();
5202 >            }
5203 >            Object v; U u;
5204 >            while ((v = advance()) != null) {
5205 >                if ((u = transformer.apply(entryFor((K)nextKey, (V)v))) != null)
5206 >                    action.apply(u);
5207 >            }
5208 >            tryComplete();
5209 >        }
5210 >    }
5211 >
5212 >    @SuppressWarnings("serial")
5213 >    static final class ForEachTransformedMappingTask<K,V,U>
5214 >        extends BulkTask<K,V,Void> {
5215 >        final BiFun<? super K, ? super V, ? extends U> transformer;
5216 >        final Action<U> action;
5217 >        ForEachTransformedMappingTask
5218 >            (ConcurrentHashMap<K,V> m,
5219 >             BiFun<? super K, ? super V, ? extends U> transformer,
5220 >             Action<U> action) {
5221 >            super(m);
5222 >            this.transformer = transformer;
5223 >            this.action = action;
5224 >
5225 >        }
5226 >        ForEachTransformedMappingTask
5227 >            (BulkTask<K,V,?> p, int b, boolean split,
5228 >             BiFun<? super K, ? super V, ? extends U> transformer,
5229 >             Action<U> action) {
5230 >            super(p, b, split);
5231 >            this.transformer = transformer;
5232 >            this.action = action;
5233 >        }
5234 >        @SuppressWarnings("unchecked") public final void compute() {
5235 >            final BiFun<? super K, ? super V, ? extends U> transformer =
5236 >                this.transformer;
5237 >            final Action<U> action = this.action;
5238 >            if (transformer == null || action == null)
5239 >                throw new Error(NullFunctionMessage);
5240 >            int b = batch(), c;
5241 >            while (b > 1 && baseIndex != baseLimit) {
5242 >                do {} while (!casPending(c = pending, c+1));
5243 >                new ForEachTransformedMappingTask<K,V,U>
5244 >                    (this, b >>>= 1, true, transformer, action).fork();
5245 >            }
5246 >            Object v; U u;
5247 >            while ((v = advance()) != null) {
5248 >                if ((u = transformer.apply((K)nextKey, (V)v)) != null)
5249 >                    action.apply(u);
5250 >            }
5251 >            tryComplete();
5252 >        }
5253 >    }
5254 >
5255 >    @SuppressWarnings("serial")
5256 >    static final class SearchKeysTask<K,V,U>
5257 >        extends BulkTask<K,V,U> {
5258 >        final Fun<? super K, ? extends U> searchFunction;
5259 >        final AtomicReference<U> result;
5260 >        SearchKeysTask
5261 >            (ConcurrentHashMap<K,V> m,
5262 >             Fun<? super K, ? extends U> searchFunction,
5263 >             AtomicReference<U> result) {
5264 >            super(m);
5265 >            this.searchFunction = searchFunction; this.result = result;
5266 >        }
5267 >        SearchKeysTask
5268 >            (BulkTask<K,V,?> p, int b, boolean split,
5269 >             Fun<? super K, ? extends U> searchFunction,
5270 >             AtomicReference<U> result) {
5271 >            super(p, b, split);
5272 >            this.searchFunction = searchFunction; this.result = result;
5273 >        }
5274 >        @SuppressWarnings("unchecked") public final void compute() {
5275 >            AtomicReference<U> result = this.result;
5276 >            final Fun<? super K, ? extends U> searchFunction =
5277 >                this.searchFunction;
5278 >            if (searchFunction == null || result == null)
5279 >                throw new Error(NullFunctionMessage);
5280 >            int b = batch(), c;
5281 >            while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5282 >                do {} while (!casPending(c = pending, c+1));
5283 >                new SearchKeysTask<K,V,U>(this, b >>>= 1, true,
5284 >                                          searchFunction, result).fork();
5285 >            }
5286 >            U u;
5287 >            while (result.get() == null && advance() != null) {
5288 >                if ((u = searchFunction.apply((K)nextKey)) != null) {
5289 >                    if (result.compareAndSet(null, u)) {
5290 >                        for (BulkTask<K,V,?> a = this, p;;) {
5291 >                            if ((p = a.parent) == null) {
5292 >                                a.quietlyComplete();
5293 >                                break;
5294 >                            }
5295 >                            a = p;
5296 >                        }
5297 >                    }
5298 >                    break;
5299 >                }
5300 >            }
5301 >            tryComplete();
5302 >        }
5303 >        public final U getRawResult() { return result.get(); }
5304 >    }
5305 >
5306 >    @SuppressWarnings("serial")
5307 >    static final class SearchValuesTask<K,V,U>
5308 >        extends BulkTask<K,V,U> {
5309 >        final Fun<? super V, ? extends U> searchFunction;
5310 >        final AtomicReference<U> result;
5311 >        SearchValuesTask
5312 >            (ConcurrentHashMap<K,V> m,
5313 >             Fun<? super V, ? extends U> searchFunction,
5314 >             AtomicReference<U> result) {
5315 >            super(m);
5316 >            this.searchFunction = searchFunction; this.result = result;
5317 >        }
5318 >        SearchValuesTask
5319 >            (BulkTask<K,V,?> p, int b, boolean split,
5320 >             Fun<? super V, ? extends U> searchFunction,
5321 >             AtomicReference<U> result) {
5322 >            super(p, b, split);
5323 >            this.searchFunction = searchFunction; this.result = result;
5324 >        }
5325 >        @SuppressWarnings("unchecked") public final void compute() {
5326 >            AtomicReference<U> result = this.result;
5327 >            final Fun<? super V, ? extends U> searchFunction =
5328 >                this.searchFunction;
5329 >            if (searchFunction == null || result == null)
5330 >                throw new Error(NullFunctionMessage);
5331 >            int b = batch(), c;
5332 >            while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5333 >                do {} while (!casPending(c = pending, c+1));
5334 >                new SearchValuesTask<K,V,U>(this, b >>>= 1, true,
5335 >                                            searchFunction, result).fork();
5336 >            }
5337 >            Object v; U u;
5338 >            while (result.get() == null && (v = advance()) != null) {
5339 >                if ((u = searchFunction.apply((V)v)) != null) {
5340 >                    if (result.compareAndSet(null, u)) {
5341 >                        for (BulkTask<K,V,?> a = this, p;;) {
5342 >                            if ((p = a.parent) == null) {
5343 >                                a.quietlyComplete();
5344 >                                break;
5345 >                            }
5346 >                            a = p;
5347 >                        }
5348 >                    }
5349 >                    break;
5350 >                }
5351 >            }
5352 >            tryComplete();
5353 >        }
5354 >        public final U getRawResult() { return result.get(); }
5355 >    }
5356 >
5357 >    @SuppressWarnings("serial")
5358 >    static final class SearchEntriesTask<K,V,U>
5359 >        extends BulkTask<K,V,U> {
5360 >        final Fun<Entry<K,V>, ? extends U> searchFunction;
5361 >        final AtomicReference<U> result;
5362 >        SearchEntriesTask
5363 >            (ConcurrentHashMap<K,V> m,
5364 >             Fun<Entry<K,V>, ? extends U> searchFunction,
5365 >             AtomicReference<U> result) {
5366 >            super(m);
5367 >            this.searchFunction = searchFunction; this.result = result;
5368 >        }
5369 >        SearchEntriesTask
5370 >            (BulkTask<K,V,?> p, int b, boolean split,
5371 >             Fun<Entry<K,V>, ? extends U> searchFunction,
5372 >             AtomicReference<U> result) {
5373 >            super(p, b, split);
5374 >            this.searchFunction = searchFunction; this.result = result;
5375 >        }
5376 >        @SuppressWarnings("unchecked") public final void compute() {
5377 >            AtomicReference<U> result = this.result;
5378 >            final Fun<Entry<K,V>, ? extends U> searchFunction =
5379 >                this.searchFunction;
5380 >            if (searchFunction == null || result == null)
5381 >                throw new Error(NullFunctionMessage);
5382 >            int b = batch(), c;
5383 >            while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5384 >                do {} while (!casPending(c = pending, c+1));
5385 >                new SearchEntriesTask<K,V,U>(this, b >>>= 1, true,
5386 >                                             searchFunction, result).fork();
5387 >            }
5388 >            Object v; U u;
5389 >            while (result.get() == null && (v = advance()) != null) {
5390 >                if ((u = searchFunction.apply(entryFor((K)nextKey, (V)v))) != null) {
5391 >                    if (result.compareAndSet(null, u)) {
5392 >                        for (BulkTask<K,V,?> a = this, p;;) {
5393 >                            if ((p = a.parent) == null) {
5394 >                                a.quietlyComplete();
5395 >                                break;
5396 >                            }
5397 >                            a = p;
5398 >                        }
5399 >                    }
5400 >                    break;
5401 >                }
5402 >            }
5403 >            tryComplete();
5404 >        }
5405 >        public final U getRawResult() { return result.get(); }
5406 >    }
5407 >
5408 >    @SuppressWarnings("serial")
5409 >    static final class SearchMappingsTask<K,V,U>
5410 >        extends BulkTask<K,V,U> {
5411 >        final BiFun<? super K, ? super V, ? extends U> searchFunction;
5412 >        final AtomicReference<U> result;
5413 >        SearchMappingsTask
5414 >            (ConcurrentHashMap<K,V> m,
5415 >             BiFun<? super K, ? super V, ? extends U> searchFunction,
5416 >             AtomicReference<U> result) {
5417 >            super(m);
5418 >            this.searchFunction = searchFunction; this.result = result;
5419 >        }
5420 >        SearchMappingsTask
5421 >            (BulkTask<K,V,?> p, int b, boolean split,
5422 >             BiFun<? super K, ? super V, ? extends U> searchFunction,
5423 >             AtomicReference<U> result) {
5424 >            super(p, b, split);
5425 >            this.searchFunction = searchFunction; this.result = result;
5426 >        }
5427 >        @SuppressWarnings("unchecked") public final void compute() {
5428 >            AtomicReference<U> result = this.result;
5429 >            final BiFun<? super K, ? super V, ? extends U> searchFunction =
5430 >                this.searchFunction;
5431 >            if (searchFunction == null || result == null)
5432 >                throw new Error(NullFunctionMessage);
5433 >            int b = batch(), c;
5434 >            while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5435 >                do {} while (!casPending(c = pending, c+1));
5436 >                new SearchMappingsTask<K,V,U>(this, b >>>= 1, true,
5437 >                                              searchFunction, result).fork();
5438 >            }
5439 >            Object v; U u;
5440 >            while (result.get() == null && (v = advance()) != null) {
5441 >                if ((u = searchFunction.apply((K)nextKey, (V)v)) != null) {
5442 >                    if (result.compareAndSet(null, u)) {
5443 >                        for (BulkTask<K,V,?> a = this, p;;) {
5444 >                            if ((p = a.parent) == null) {
5445 >                                a.quietlyComplete();
5446 >                                break;
5447 >                            }
5448 >                            a = p;
5449 >                        }
5450 >                    }
5451 >                    break;
5452 >                }
5453 >            }
5454 >            tryComplete();
5455 >        }
5456 >        public final U getRawResult() { return result.get(); }
5457 >    }
5458 >
5459 >    @SuppressWarnings("serial")
5460 >    static final class ReduceKeysTask<K,V>
5461 >        extends BulkTask<K,V,K> {
5462 >        final BiFun<? super K, ? super K, ? extends K> reducer;
5463 >        K result;
5464 >        ReduceKeysTask<K,V> sibling;
5465 >        ReduceKeysTask
5466 >            (ConcurrentHashMap<K,V> m,
5467 >             BiFun<? super K, ? super K, ? extends K> reducer) {
5468 >            super(m);
5469 >            this.reducer = reducer;
5470 >        }
5471 >        ReduceKeysTask
5472 >            (BulkTask<K,V,?> p, int b, boolean split,
5473 >             BiFun<? super K, ? super K, ? extends K> reducer) {
5474 >            super(p, b, split);
5475 >            this.reducer = reducer;
5476 >        }
5477 >
5478 >        @SuppressWarnings("unchecked") public final void compute() {
5479 >            ReduceKeysTask<K,V> t = this;
5480 >            final BiFun<? super K, ? super K, ? extends K> reducer =
5481 >                this.reducer;
5482 >            if (reducer == null)
5483 >                throw new Error(NullFunctionMessage);
5484 >            int b = batch();
5485 >            while (b > 1 && t.baseIndex != t.baseLimit) {
5486 >                b >>>= 1;
5487 >                t.pending = 1;
5488 >                ReduceKeysTask<K,V> rt =
5489 >                    new ReduceKeysTask<K,V>
5490 >                    (t, b, true, reducer);
5491 >                t = new ReduceKeysTask<K,V>
5492 >                    (t, b, false, reducer);
5493 >                t.sibling = rt;
5494 >                rt.sibling = t;
5495 >                rt.fork();
5496 >            }
5497 >            K r = null;
5498 >            while (t.advance() != null) {
5499 >                K u = (K)t.nextKey;
5500 >                r = (r == null) ? u : reducer.apply(r, u);
5501 >            }
5502 >            t.result = r;
5503 >            for (;;) {
5504 >                int c; BulkTask<K,V,?> par; ReduceKeysTask<K,V> s, p; K u;
5505 >                if ((par = t.parent) == null ||
5506 >                    !(par instanceof ReduceKeysTask)) {
5507 >                    t.quietlyComplete();
5508 >                    break;
5509 >                }
5510 >                else if ((c = (p = (ReduceKeysTask<K,V>)par).pending) == 0) {
5511 >                    if ((s = t.sibling) != null && (u = s.result) != null)
5512 >                        r = (r == null) ? u : reducer.apply(r, u);
5513 >                    (t = p).result = r;
5514 >                }
5515 >                else if (p.casPending(c, 0))
5516 >                    break;
5517 >            }
5518 >        }
5519 >        public final K getRawResult() { return result; }
5520 >    }
5521 >
5522 >    @SuppressWarnings("serial")
5523 >    static final class ReduceValuesTask<K,V>
5524 >        extends BulkTask<K,V,V> {
5525 >        final BiFun<? super V, ? super V, ? extends V> reducer;
5526 >        V result;
5527 >        ReduceValuesTask<K,V> sibling;
5528 >        ReduceValuesTask
5529 >            (ConcurrentHashMap<K,V> m,
5530 >             BiFun<? super V, ? super V, ? extends V> reducer) {
5531 >            super(m);
5532 >            this.reducer = reducer;
5533 >        }
5534 >        ReduceValuesTask
5535 >            (BulkTask<K,V,?> p, int b, boolean split,
5536 >             BiFun<? super V, ? super V, ? extends V> reducer) {
5537 >            super(p, b, split);
5538 >            this.reducer = reducer;
5539 >        }
5540 >
5541 >        @SuppressWarnings("unchecked") public final void compute() {
5542 >            ReduceValuesTask<K,V> t = this;
5543 >            final BiFun<? super V, ? super V, ? extends V> reducer =
5544 >                this.reducer;
5545 >            if (reducer == null)
5546 >                throw new Error(NullFunctionMessage);
5547 >            int b = batch();
5548 >            while (b > 1 && t.baseIndex != t.baseLimit) {
5549 >                b >>>= 1;
5550 >                t.pending = 1;
5551 >                ReduceValuesTask<K,V> rt =
5552 >                    new ReduceValuesTask<K,V>
5553 >                    (t, b, true, reducer);
5554 >                t = new ReduceValuesTask<K,V>
5555 >                    (t, b, false, reducer);
5556 >                t.sibling = rt;
5557 >                rt.sibling = t;
5558 >                rt.fork();
5559 >            }
5560 >            V r = null;
5561 >            Object v;
5562 >            while ((v = t.advance()) != null) {
5563 >                V u = (V)v;
5564 >                r = (r == null) ? u : reducer.apply(r, u);
5565 >            }
5566 >            t.result = r;
5567 >            for (;;) {
5568 >                int c; BulkTask<K,V,?> par; ReduceValuesTask<K,V> s, p; V u;
5569 >                if ((par = t.parent) == null ||
5570 >                    !(par instanceof ReduceValuesTask)) {
5571 >                    t.quietlyComplete();
5572 >                    break;
5573 >                }
5574 >                else if ((c = (p = (ReduceValuesTask<K,V>)par).pending) == 0) {
5575 >                    if ((s = t.sibling) != null && (u = s.result) != null)
5576 >                        r = (r == null) ? u : reducer.apply(r, u);
5577 >                    (t = p).result = r;
5578 >                }
5579 >                else if (p.casPending(c, 0))
5580 >                    break;
5581 >            }
5582 >        }
5583 >        public final V getRawResult() { return result; }
5584 >    }
5585 >
5586 >    @SuppressWarnings("serial")
5587 >    static final class ReduceEntriesTask<K,V>
5588 >        extends BulkTask<K,V,Map.Entry<K,V>> {
5589 >        final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
5590 >        Map.Entry<K,V> result;
5591 >        ReduceEntriesTask<K,V> sibling;
5592 >        ReduceEntriesTask
5593 >            (ConcurrentHashMap<K,V> m,
5594 >             BiFun<Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
5595 >            super(m);
5596 >            this.reducer = reducer;
5597 >        }
5598 >        ReduceEntriesTask
5599 >            (BulkTask<K,V,?> p, int b, boolean split,
5600 >             BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
5601 >            super(p, b, split);
5602 >            this.reducer = reducer;
5603 >        }
5604 >
5605 >        @SuppressWarnings("unchecked") public final void compute() {
5606 >            ReduceEntriesTask<K,V> t = this;
5607 >            final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer =
5608 >                this.reducer;
5609 >            if (reducer == null)
5610 >                throw new Error(NullFunctionMessage);
5611 >            int b = batch();
5612 >            while (b > 1 && t.baseIndex != t.baseLimit) {
5613 >                b >>>= 1;
5614 >                t.pending = 1;
5615 >                ReduceEntriesTask<K,V> rt =
5616 >                    new ReduceEntriesTask<K,V>
5617 >                    (t, b, true, reducer);
5618 >                t = new ReduceEntriesTask<K,V>
5619 >                    (t, b, false, reducer);
5620 >                t.sibling = rt;
5621 >                rt.sibling = t;
5622 >                rt.fork();
5623 >            }
5624 >            Map.Entry<K,V> r = null;
5625 >            Object v;
5626 >            while ((v = t.advance()) != null) {
5627 >                Map.Entry<K,V> u = entryFor((K)t.nextKey, (V)v);
5628 >                r = (r == null) ? u : reducer.apply(r, u);
5629 >            }
5630 >            t.result = r;
5631 >            for (;;) {
5632 >                int c; BulkTask<K,V,?> par; ReduceEntriesTask<K,V> s, p;
5633 >                Map.Entry<K,V> u;
5634 >                if ((par = t.parent) == null ||
5635 >                    !(par instanceof ReduceEntriesTask)) {
5636 >                    t.quietlyComplete();
5637 >                    break;
5638 >                }
5639 >                else if ((c = (p = (ReduceEntriesTask<K,V>)par).pending) == 0) {
5640 >                    if ((s = t.sibling) != null && (u = s.result) != null)
5641 >                        r = (r == null) ? u : reducer.apply(r, u);
5642 >                    (t = p).result = r;
5643 >                }
5644 >                else if (p.casPending(c, 0))
5645 >                    break;
5646 >            }
5647 >        }
5648 >        public final Map.Entry<K,V> getRawResult() { return result; }
5649 >    }
5650 >
5651 >    @SuppressWarnings("serial")
5652 >    static final class MapReduceKeysTask<K,V,U>
5653 >        extends BulkTask<K,V,U> {
5654 >        final Fun<? super K, ? extends U> transformer;
5655 >        final BiFun<? super U, ? super U, ? extends U> reducer;
5656 >        U result;
5657 >        MapReduceKeysTask<K,V,U> sibling;
5658 >        MapReduceKeysTask
5659 >            (ConcurrentHashMap<K,V> m,
5660 >             Fun<? super K, ? extends U> transformer,
5661 >             BiFun<? super U, ? super U, ? extends U> reducer) {
5662 >            super(m);
5663 >            this.transformer = transformer;
5664 >            this.reducer = reducer;
5665 >        }
5666 >        MapReduceKeysTask
5667 >            (BulkTask<K,V,?> p, int b, boolean split,
5668 >             Fun<? super K, ? extends U> transformer,
5669 >             BiFun<? super U, ? super U, ? extends U> reducer) {
5670 >            super(p, b, split);
5671 >            this.transformer = transformer;
5672 >            this.reducer = reducer;
5673 >        }
5674 >        @SuppressWarnings("unchecked") public final void compute() {
5675 >            MapReduceKeysTask<K,V,U> t = this;
5676 >            final Fun<? super K, ? extends U> transformer =
5677 >                this.transformer;
5678 >            final BiFun<? super U, ? super U, ? extends U> reducer =
5679 >                this.reducer;
5680 >            if (transformer == null || reducer == null)
5681 >                throw new Error(NullFunctionMessage);
5682 >            int b = batch();
5683 >            while (b > 1 && t.baseIndex != t.baseLimit) {
5684 >                b >>>= 1;
5685 >                t.pending = 1;
5686 >                MapReduceKeysTask<K,V,U> rt =
5687 >                    new MapReduceKeysTask<K,V,U>
5688 >                    (t, b, true, transformer, reducer);
5689 >                t = new MapReduceKeysTask<K,V,U>
5690 >                    (t, b, false, transformer, reducer);
5691 >                t.sibling = rt;
5692 >                rt.sibling = t;
5693 >                rt.fork();
5694 >            }
5695 >            U r = null, u;
5696 >            while (t.advance() != null) {
5697 >                if ((u = transformer.apply((K)t.nextKey)) != null)
5698 >                    r = (r == null) ? u : reducer.apply(r, u);
5699 >            }
5700 >            t.result = r;
5701 >            for (;;) {
5702 >                int c; BulkTask<K,V,?> par; MapReduceKeysTask<K,V,U> s, p;
5703 >                if ((par = t.parent) == null ||
5704 >                    !(par instanceof MapReduceKeysTask)) {
5705 >                    t.quietlyComplete();
5706 >                    break;
5707 >                }
5708 >                else if ((c = (p = (MapReduceKeysTask<K,V,U>)par).pending) == 0) {
5709 >                    if ((s = t.sibling) != null && (u = s.result) != null)
5710 >                        r = (r == null) ? u : reducer.apply(r, u);
5711 >                    (t = p).result = r;
5712 >                }
5713 >                else if (p.casPending(c, 0))
5714 >                    break;
5715 >            }
5716          }
5717 +        public final U getRawResult() { return result; }
5718      }
5719  
5720 +    @SuppressWarnings("serial")
5721 +    static final class MapReduceValuesTask<K,V,U>
5722 +        extends BulkTask<K,V,U> {
5723 +        final Fun<? super V, ? extends U> transformer;
5724 +        final BiFun<? super U, ? super U, ? extends U> reducer;
5725 +        U result;
5726 +        MapReduceValuesTask<K,V,U> sibling;
5727 +        MapReduceValuesTask
5728 +            (ConcurrentHashMap<K,V> m,
5729 +             Fun<? super V, ? extends U> transformer,
5730 +             BiFun<? super U, ? super U, ? extends U> reducer) {
5731 +            super(m);
5732 +            this.transformer = transformer;
5733 +            this.reducer = reducer;
5734 +        }
5735 +        MapReduceValuesTask
5736 +            (BulkTask<K,V,?> p, int b, boolean split,
5737 +             Fun<? super V, ? extends U> transformer,
5738 +             BiFun<? super U, ? super U, ? extends U> reducer) {
5739 +            super(p, b, split);
5740 +            this.transformer = transformer;
5741 +            this.reducer = reducer;
5742 +        }
5743 +        @SuppressWarnings("unchecked") public final void compute() {
5744 +            MapReduceValuesTask<K,V,U> t = this;
5745 +            final Fun<? super V, ? extends U> transformer =
5746 +                this.transformer;
5747 +            final BiFun<? super U, ? super U, ? extends U> reducer =
5748 +                this.reducer;
5749 +            if (transformer == null || reducer == null)
5750 +                throw new Error(NullFunctionMessage);
5751 +            int b = batch();
5752 +            while (b > 1 && t.baseIndex != t.baseLimit) {
5753 +                b >>>= 1;
5754 +                t.pending = 1;
5755 +                MapReduceValuesTask<K,V,U> rt =
5756 +                    new MapReduceValuesTask<K,V,U>
5757 +                    (t, b, true, transformer, reducer);
5758 +                t = new MapReduceValuesTask<K,V,U>
5759 +                    (t, b, false, transformer, reducer);
5760 +                t.sibling = rt;
5761 +                rt.sibling = t;
5762 +                rt.fork();
5763 +            }
5764 +            U r = null, u;
5765 +            Object v;
5766 +            while ((v = t.advance()) != null) {
5767 +                if ((u = transformer.apply((V)v)) != null)
5768 +                    r = (r == null) ? u : reducer.apply(r, u);
5769 +            }
5770 +            t.result = r;
5771 +            for (;;) {
5772 +                int c; BulkTask<K,V,?> par; MapReduceValuesTask<K,V,U> s, p;
5773 +                if ((par = t.parent) == null ||
5774 +                    !(par instanceof MapReduceValuesTask)) {
5775 +                    t.quietlyComplete();
5776 +                    break;
5777 +                }
5778 +                else if ((c = (p = (MapReduceValuesTask<K,V,U>)par).pending) == 0) {
5779 +                    if ((s = t.sibling) != null && (u = s.result) != null)
5780 +                        r = (r == null) ? u : reducer.apply(r, u);
5781 +                    (t = p).result = r;
5782 +                }
5783 +                else if (p.casPending(c, 0))
5784 +                    break;
5785 +            }
5786 +        }
5787 +        public final U getRawResult() { return result; }
5788 +    }
5789 +
5790 +    @SuppressWarnings("serial")
5791 +    static final class MapReduceEntriesTask<K,V,U>
5792 +        extends BulkTask<K,V,U> {
5793 +        final Fun<Map.Entry<K,V>, ? extends U> transformer;
5794 +        final BiFun<? super U, ? super U, ? extends U> reducer;
5795 +        U result;
5796 +        MapReduceEntriesTask<K,V,U> sibling;
5797 +        MapReduceEntriesTask
5798 +            (ConcurrentHashMap<K,V> m,
5799 +             Fun<Map.Entry<K,V>, ? extends U> transformer,
5800 +             BiFun<? super U, ? super U, ? extends U> reducer) {
5801 +            super(m);
5802 +            this.transformer = transformer;
5803 +            this.reducer = reducer;
5804 +        }
5805 +        MapReduceEntriesTask
5806 +            (BulkTask<K,V,?> p, int b, boolean split,
5807 +             Fun<Map.Entry<K,V>, ? extends U> transformer,
5808 +             BiFun<? super U, ? super U, ? extends U> reducer) {
5809 +            super(p, b, split);
5810 +            this.transformer = transformer;
5811 +            this.reducer = reducer;
5812 +        }
5813 +        @SuppressWarnings("unchecked") public final void compute() {
5814 +            MapReduceEntriesTask<K,V,U> t = this;
5815 +            final Fun<Map.Entry<K,V>, ? extends U> transformer =
5816 +                this.transformer;
5817 +            final BiFun<? super U, ? super U, ? extends U> reducer =
5818 +                this.reducer;
5819 +            if (transformer == null || reducer == null)
5820 +                throw new Error(NullFunctionMessage);
5821 +            int b = batch();
5822 +            while (b > 1 && t.baseIndex != t.baseLimit) {
5823 +                b >>>= 1;
5824 +                t.pending = 1;
5825 +                MapReduceEntriesTask<K,V,U> rt =
5826 +                    new MapReduceEntriesTask<K,V,U>
5827 +                    (t, b, true, transformer, reducer);
5828 +                t = new MapReduceEntriesTask<K,V,U>
5829 +                    (t, b, false, transformer, reducer);
5830 +                t.sibling = rt;
5831 +                rt.sibling = t;
5832 +                rt.fork();
5833 +            }
5834 +            U r = null, u;
5835 +            Object v;
5836 +            while ((v = t.advance()) != null) {
5837 +                if ((u = transformer.apply(entryFor((K)t.nextKey, (V)v))) != null)
5838 +                    r = (r == null) ? u : reducer.apply(r, u);
5839 +            }
5840 +            t.result = r;
5841 +            for (;;) {
5842 +                int c; BulkTask<K,V,?> par; MapReduceEntriesTask<K,V,U> s, p;
5843 +                if ((par = t.parent) == null ||
5844 +                    !(par instanceof MapReduceEntriesTask)) {
5845 +                    t.quietlyComplete();
5846 +                    break;
5847 +                }
5848 +                else if ((c = (p = (MapReduceEntriesTask<K,V,U>)par).pending) == 0) {
5849 +                    if ((s = t.sibling) != null && (u = s.result) != null)
5850 +                        r = (r == null) ? u : reducer.apply(r, u);
5851 +                    (t = p).result = r;
5852 +                }
5853 +                else if (p.casPending(c, 0))
5854 +                    break;
5855 +            }
5856 +        }
5857 +        public final U getRawResult() { return result; }
5858 +    }
5859 +
5860 +    @SuppressWarnings("serial")
5861 +    static final class MapReduceMappingsTask<K,V,U>
5862 +        extends BulkTask<K,V,U> {
5863 +        final BiFun<? super K, ? super V, ? extends U> transformer;
5864 +        final BiFun<? super U, ? super U, ? extends U> reducer;
5865 +        U result;
5866 +        MapReduceMappingsTask<K,V,U> sibling;
5867 +        MapReduceMappingsTask
5868 +            (ConcurrentHashMap<K,V> m,
5869 +             BiFun<? super K, ? super V, ? extends U> transformer,
5870 +             BiFun<? super U, ? super U, ? extends U> reducer) {
5871 +            super(m);
5872 +            this.transformer = transformer;
5873 +            this.reducer = reducer;
5874 +        }
5875 +        MapReduceMappingsTask
5876 +            (BulkTask<K,V,?> p, int b, boolean split,
5877 +             BiFun<? super K, ? super V, ? extends U> transformer,
5878 +             BiFun<? super U, ? super U, ? extends U> reducer) {
5879 +            super(p, b, split);
5880 +            this.transformer = transformer;
5881 +            this.reducer = reducer;
5882 +        }
5883 +        @SuppressWarnings("unchecked") public final void compute() {
5884 +            MapReduceMappingsTask<K,V,U> t = this;
5885 +            final BiFun<? super K, ? super V, ? extends U> transformer =
5886 +                this.transformer;
5887 +            final BiFun<? super U, ? super U, ? extends U> reducer =
5888 +                this.reducer;
5889 +            if (transformer == null || reducer == null)
5890 +                throw new Error(NullFunctionMessage);
5891 +            int b = batch();
5892 +            while (b > 1 && t.baseIndex != t.baseLimit) {
5893 +                b >>>= 1;
5894 +                t.pending = 1;
5895 +                MapReduceMappingsTask<K,V,U> rt =
5896 +                    new MapReduceMappingsTask<K,V,U>
5897 +                    (t, b, true, transformer, reducer);
5898 +                t = new MapReduceMappingsTask<K,V,U>
5899 +                    (t, b, false, transformer, reducer);
5900 +                t.sibling = rt;
5901 +                rt.sibling = t;
5902 +                rt.fork();
5903 +            }
5904 +            U r = null, u;
5905 +            Object v;
5906 +            while ((v = t.advance()) != null) {
5907 +                if ((u = transformer.apply((K)t.nextKey, (V)v)) != null)
5908 +                    r = (r == null) ? u : reducer.apply(r, u);
5909 +            }
5910 +            for (;;) {
5911 +                int c; BulkTask<K,V,?> par; MapReduceMappingsTask<K,V,U> s, p;
5912 +                if ((par = t.parent) == null ||
5913 +                    !(par instanceof MapReduceMappingsTask)) {
5914 +                    t.quietlyComplete();
5915 +                    break;
5916 +                }
5917 +                else if ((c = (p = (MapReduceMappingsTask<K,V,U>)par).pending) == 0) {
5918 +                    if ((s = t.sibling) != null && (u = s.result) != null)
5919 +                        r = (r == null) ? u : reducer.apply(r, u);
5920 +                    (t = p).result = r;
5921 +                }
5922 +                else if (p.casPending(c, 0))
5923 +                    break;
5924 +            }
5925 +        }
5926 +        public final U getRawResult() { return result; }
5927 +    }
5928 +
5929 +    @SuppressWarnings("serial")
5930 +    static final class MapReduceKeysToDoubleTask<K,V>
5931 +        extends BulkTask<K,V,Double> {
5932 +        final ObjectToDouble<? super K> transformer;
5933 +        final DoubleByDoubleToDouble reducer;
5934 +        final double basis;
5935 +        double result;
5936 +        MapReduceKeysToDoubleTask<K,V> sibling;
5937 +        MapReduceKeysToDoubleTask
5938 +            (ConcurrentHashMap<K,V> m,
5939 +             ObjectToDouble<? super K> transformer,
5940 +             double basis,
5941 +             DoubleByDoubleToDouble reducer) {
5942 +            super(m);
5943 +            this.transformer = transformer;
5944 +            this.basis = basis; this.reducer = reducer;
5945 +        }
5946 +        MapReduceKeysToDoubleTask
5947 +            (BulkTask<K,V,?> p, int b, boolean split,
5948 +             ObjectToDouble<? super K> transformer,
5949 +             double basis,
5950 +             DoubleByDoubleToDouble reducer) {
5951 +            super(p, b, split);
5952 +            this.transformer = transformer;
5953 +            this.basis = basis; this.reducer = reducer;
5954 +        }
5955 +        @SuppressWarnings("unchecked") public final void compute() {
5956 +            MapReduceKeysToDoubleTask<K,V> t = this;
5957 +            final ObjectToDouble<? super K> transformer =
5958 +                this.transformer;
5959 +            final DoubleByDoubleToDouble reducer = this.reducer;
5960 +            if (transformer == null || reducer == null)
5961 +                throw new Error(NullFunctionMessage);
5962 +            final double id = this.basis;
5963 +            int b = batch();
5964 +            while (b > 1 && t.baseIndex != t.baseLimit) {
5965 +                b >>>= 1;
5966 +                t.pending = 1;
5967 +                MapReduceKeysToDoubleTask<K,V> rt =
5968 +                    new MapReduceKeysToDoubleTask<K,V>
5969 +                    (t, b, true, transformer, id, reducer);
5970 +                t = new MapReduceKeysToDoubleTask<K,V>
5971 +                    (t, b, false, transformer, id, reducer);
5972 +                t.sibling = rt;
5973 +                rt.sibling = t;
5974 +                rt.fork();
5975 +            }
5976 +            double r = id;
5977 +            while (t.advance() != null)
5978 +                r = reducer.apply(r, transformer.apply((K)t.nextKey));
5979 +            t.result = r;
5980 +            for (;;) {
5981 +                int c; BulkTask<K,V,?> par; MapReduceKeysToDoubleTask<K,V> s, p;
5982 +                if ((par = t.parent) == null ||
5983 +                    !(par instanceof MapReduceKeysToDoubleTask)) {
5984 +                    t.quietlyComplete();
5985 +                    break;
5986 +                }
5987 +                else if ((c = (p = (MapReduceKeysToDoubleTask<K,V>)par).pending) == 0) {
5988 +                    if ((s = t.sibling) != null)
5989 +                        r = reducer.apply(r, s.result);
5990 +                    (t = p).result = r;
5991 +                }
5992 +                else if (p.casPending(c, 0))
5993 +                    break;
5994 +            }
5995 +        }
5996 +        public final Double getRawResult() { return result; }
5997 +    }
5998 +
5999 +    @SuppressWarnings("serial")
6000 +    static final class MapReduceValuesToDoubleTask<K,V>
6001 +        extends BulkTask<K,V,Double> {
6002 +        final ObjectToDouble<? super V> transformer;
6003 +        final DoubleByDoubleToDouble reducer;
6004 +        final double basis;
6005 +        double result;
6006 +        MapReduceValuesToDoubleTask<K,V> sibling;
6007 +        MapReduceValuesToDoubleTask
6008 +            (ConcurrentHashMap<K,V> m,
6009 +             ObjectToDouble<? super V> transformer,
6010 +             double basis,
6011 +             DoubleByDoubleToDouble reducer) {
6012 +            super(m);
6013 +            this.transformer = transformer;
6014 +            this.basis = basis; this.reducer = reducer;
6015 +        }
6016 +        MapReduceValuesToDoubleTask
6017 +            (BulkTask<K,V,?> p, int b, boolean split,
6018 +             ObjectToDouble<? super V> transformer,
6019 +             double basis,
6020 +             DoubleByDoubleToDouble reducer) {
6021 +            super(p, b, split);
6022 +            this.transformer = transformer;
6023 +            this.basis = basis; this.reducer = reducer;
6024 +        }
6025 +        @SuppressWarnings("unchecked") public final void compute() {
6026 +            MapReduceValuesToDoubleTask<K,V> t = this;
6027 +            final ObjectToDouble<? super V> transformer =
6028 +                this.transformer;
6029 +            final DoubleByDoubleToDouble reducer = this.reducer;
6030 +            if (transformer == null || reducer == null)
6031 +                throw new Error(NullFunctionMessage);
6032 +            final double id = this.basis;
6033 +            int b = batch();
6034 +            while (b > 1 && t.baseIndex != t.baseLimit) {
6035 +                b >>>= 1;
6036 +                t.pending = 1;
6037 +                MapReduceValuesToDoubleTask<K,V> rt =
6038 +                    new MapReduceValuesToDoubleTask<K,V>
6039 +                    (t, b, true, transformer, id, reducer);
6040 +                t = new MapReduceValuesToDoubleTask<K,V>
6041 +                    (t, b, false, transformer, id, reducer);
6042 +                t.sibling = rt;
6043 +                rt.sibling = t;
6044 +                rt.fork();
6045 +            }
6046 +            double r = id;
6047 +            Object v;
6048 +            while ((v = t.advance()) != null)
6049 +                r = reducer.apply(r, transformer.apply((V)v));
6050 +            t.result = r;
6051 +            for (;;) {
6052 +                int c; BulkTask<K,V,?> par; MapReduceValuesToDoubleTask<K,V> s, p;
6053 +                if ((par = t.parent) == null ||
6054 +                    !(par instanceof MapReduceValuesToDoubleTask)) {
6055 +                    t.quietlyComplete();
6056 +                    break;
6057 +                }
6058 +                else if ((c = (p = (MapReduceValuesToDoubleTask<K,V>)par).pending) == 0) {
6059 +                    if ((s = t.sibling) != null)
6060 +                        r = reducer.apply(r, s.result);
6061 +                    (t = p).result = r;
6062 +                }
6063 +                else if (p.casPending(c, 0))
6064 +                    break;
6065 +            }
6066 +        }
6067 +        public final Double getRawResult() { return result; }
6068 +    }
6069 +
6070 +    @SuppressWarnings("serial")
6071 +    static final class MapReduceEntriesToDoubleTask<K,V>
6072 +        extends BulkTask<K,V,Double> {
6073 +        final ObjectToDouble<Map.Entry<K,V>> transformer;
6074 +        final DoubleByDoubleToDouble reducer;
6075 +        final double basis;
6076 +        double result;
6077 +        MapReduceEntriesToDoubleTask<K,V> sibling;
6078 +        MapReduceEntriesToDoubleTask
6079 +            (ConcurrentHashMap<K,V> m,
6080 +             ObjectToDouble<Map.Entry<K,V>> transformer,
6081 +             double basis,
6082 +             DoubleByDoubleToDouble reducer) {
6083 +            super(m);
6084 +            this.transformer = transformer;
6085 +            this.basis = basis; this.reducer = reducer;
6086 +        }
6087 +        MapReduceEntriesToDoubleTask
6088 +            (BulkTask<K,V,?> p, int b, boolean split,
6089 +             ObjectToDouble<Map.Entry<K,V>> transformer,
6090 +             double basis,
6091 +             DoubleByDoubleToDouble reducer) {
6092 +            super(p, b, split);
6093 +            this.transformer = transformer;
6094 +            this.basis = basis; this.reducer = reducer;
6095 +        }
6096 +        @SuppressWarnings("unchecked") public final void compute() {
6097 +            MapReduceEntriesToDoubleTask<K,V> t = this;
6098 +            final ObjectToDouble<Map.Entry<K,V>> transformer =
6099 +                this.transformer;
6100 +            final DoubleByDoubleToDouble reducer = this.reducer;
6101 +            if (transformer == null || reducer == null)
6102 +                throw new Error(NullFunctionMessage);
6103 +            final double id = this.basis;
6104 +            int b = batch();
6105 +            while (b > 1 && t.baseIndex != t.baseLimit) {
6106 +                b >>>= 1;
6107 +                t.pending = 1;
6108 +                MapReduceEntriesToDoubleTask<K,V> rt =
6109 +                    new MapReduceEntriesToDoubleTask<K,V>
6110 +                    (t, b, true, transformer, id, reducer);
6111 +                t = new MapReduceEntriesToDoubleTask<K,V>
6112 +                    (t, b, false, transformer, id, reducer);
6113 +                t.sibling = rt;
6114 +                rt.sibling = t;
6115 +                rt.fork();
6116 +            }
6117 +            double r = id;
6118 +            Object v;
6119 +            while ((v = t.advance()) != null)
6120 +                r = reducer.apply(r, transformer.apply(entryFor((K)t.nextKey, (V)v)));
6121 +            t.result = r;
6122 +            for (;;) {
6123 +                int c; BulkTask<K,V,?> par; MapReduceEntriesToDoubleTask<K,V> s, p;
6124 +                if ((par = t.parent) == null ||
6125 +                    !(par instanceof MapReduceEntriesToDoubleTask)) {
6126 +                    t.quietlyComplete();
6127 +                    break;
6128 +                }
6129 +                else if ((c = (p = (MapReduceEntriesToDoubleTask<K,V>)par).pending) == 0) {
6130 +                    if ((s = t.sibling) != null)
6131 +                        r = reducer.apply(r, s.result);
6132 +                    (t = p).result = r;
6133 +                }
6134 +                else if (p.casPending(c, 0))
6135 +                    break;
6136 +            }
6137 +        }
6138 +        public final Double getRawResult() { return result; }
6139 +    }
6140 +
6141 +    @SuppressWarnings("serial")
6142 +    static final class MapReduceMappingsToDoubleTask<K,V>
6143 +        extends BulkTask<K,V,Double> {
6144 +        final ObjectByObjectToDouble<? super K, ? super V> transformer;
6145 +        final DoubleByDoubleToDouble reducer;
6146 +        final double basis;
6147 +        double result;
6148 +        MapReduceMappingsToDoubleTask<K,V> sibling;
6149 +        MapReduceMappingsToDoubleTask
6150 +            (ConcurrentHashMap<K,V> m,
6151 +             ObjectByObjectToDouble<? super K, ? super V> transformer,
6152 +             double basis,
6153 +             DoubleByDoubleToDouble reducer) {
6154 +            super(m);
6155 +            this.transformer = transformer;
6156 +            this.basis = basis; this.reducer = reducer;
6157 +        }
6158 +        MapReduceMappingsToDoubleTask
6159 +            (BulkTask<K,V,?> p, int b, boolean split,
6160 +             ObjectByObjectToDouble<? super K, ? super V> transformer,
6161 +             double basis,
6162 +             DoubleByDoubleToDouble reducer) {
6163 +            super(p, b, split);
6164 +            this.transformer = transformer;
6165 +            this.basis = basis; this.reducer = reducer;
6166 +        }
6167 +        @SuppressWarnings("unchecked") public final void compute() {
6168 +            MapReduceMappingsToDoubleTask<K,V> t = this;
6169 +            final ObjectByObjectToDouble<? super K, ? super V> transformer =
6170 +                this.transformer;
6171 +            final DoubleByDoubleToDouble reducer = this.reducer;
6172 +            if (transformer == null || reducer == null)
6173 +                throw new Error(NullFunctionMessage);
6174 +            final double id = this.basis;
6175 +            int b = batch();
6176 +            while (b > 1 && t.baseIndex != t.baseLimit) {
6177 +                b >>>= 1;
6178 +                t.pending = 1;
6179 +                MapReduceMappingsToDoubleTask<K,V> rt =
6180 +                    new MapReduceMappingsToDoubleTask<K,V>
6181 +                    (t, b, true, transformer, id, reducer);
6182 +                t = new MapReduceMappingsToDoubleTask<K,V>
6183 +                    (t, b, false, transformer, id, reducer);
6184 +                t.sibling = rt;
6185 +                rt.sibling = t;
6186 +                rt.fork();
6187 +            }
6188 +            double r = id;
6189 +            Object v;
6190 +            while ((v = t.advance()) != null)
6191 +                r = reducer.apply(r, transformer.apply((K)t.nextKey, (V)v));
6192 +            t.result = r;
6193 +            for (;;) {
6194 +                int c; BulkTask<K,V,?> par; MapReduceMappingsToDoubleTask<K,V> s, p;
6195 +                if ((par = t.parent) == null ||
6196 +                    !(par instanceof MapReduceMappingsToDoubleTask)) {
6197 +                    t.quietlyComplete();
6198 +                    break;
6199 +                }
6200 +                else if ((c = (p = (MapReduceMappingsToDoubleTask<K,V>)par).pending) == 0) {
6201 +                    if ((s = t.sibling) != null)
6202 +                        r = reducer.apply(r, s.result);
6203 +                    (t = p).result = r;
6204 +                }
6205 +                else if (p.casPending(c, 0))
6206 +                    break;
6207 +            }
6208 +        }
6209 +        public final Double getRawResult() { return result; }
6210 +    }
6211 +
6212 +    @SuppressWarnings("serial")
6213 +    static final class MapReduceKeysToLongTask<K,V>
6214 +        extends BulkTask<K,V,Long> {
6215 +        final ObjectToLong<? super K> transformer;
6216 +        final LongByLongToLong reducer;
6217 +        final long basis;
6218 +        long result;
6219 +        MapReduceKeysToLongTask<K,V> sibling;
6220 +        MapReduceKeysToLongTask
6221 +            (ConcurrentHashMap<K,V> m,
6222 +             ObjectToLong<? super K> transformer,
6223 +             long basis,
6224 +             LongByLongToLong reducer) {
6225 +            super(m);
6226 +            this.transformer = transformer;
6227 +            this.basis = basis; this.reducer = reducer;
6228 +        }
6229 +        MapReduceKeysToLongTask
6230 +            (BulkTask<K,V,?> p, int b, boolean split,
6231 +             ObjectToLong<? super K> transformer,
6232 +             long basis,
6233 +             LongByLongToLong reducer) {
6234 +            super(p, b, split);
6235 +            this.transformer = transformer;
6236 +            this.basis = basis; this.reducer = reducer;
6237 +        }
6238 +        @SuppressWarnings("unchecked") public final void compute() {
6239 +            MapReduceKeysToLongTask<K,V> t = this;
6240 +            final ObjectToLong<? super K> transformer =
6241 +                this.transformer;
6242 +            final LongByLongToLong reducer = this.reducer;
6243 +            if (transformer == null || reducer == null)
6244 +                throw new Error(NullFunctionMessage);
6245 +            final long id = this.basis;
6246 +            int b = batch();
6247 +            while (b > 1 && t.baseIndex != t.baseLimit) {
6248 +                b >>>= 1;
6249 +                t.pending = 1;
6250 +                MapReduceKeysToLongTask<K,V> rt =
6251 +                    new MapReduceKeysToLongTask<K,V>
6252 +                    (t, b, true, transformer, id, reducer);
6253 +                t = new MapReduceKeysToLongTask<K,V>
6254 +                    (t, b, false, transformer, id, reducer);
6255 +                t.sibling = rt;
6256 +                rt.sibling = t;
6257 +                rt.fork();
6258 +            }
6259 +            long r = id;
6260 +            while (t.advance() != null)
6261 +                r = reducer.apply(r, transformer.apply((K)t.nextKey));
6262 +            t.result = r;
6263 +            for (;;) {
6264 +                int c; BulkTask<K,V,?> par; MapReduceKeysToLongTask<K,V> s, p;
6265 +                if ((par = t.parent) == null ||
6266 +                    !(par instanceof MapReduceKeysToLongTask)) {
6267 +                    t.quietlyComplete();
6268 +                    break;
6269 +                }
6270 +                else if ((c = (p = (MapReduceKeysToLongTask<K,V>)par).pending) == 0) {
6271 +                    if ((s = t.sibling) != null)
6272 +                        r = reducer.apply(r, s.result);
6273 +                    (t = p).result = r;
6274 +                }
6275 +                else if (p.casPending(c, 0))
6276 +                    break;
6277 +            }
6278 +        }
6279 +        public final Long getRawResult() { return result; }
6280 +    }
6281 +
6282 +    @SuppressWarnings("serial")
6283 +    static final class MapReduceValuesToLongTask<K,V>
6284 +        extends BulkTask<K,V,Long> {
6285 +        final ObjectToLong<? super V> transformer;
6286 +        final LongByLongToLong reducer;
6287 +        final long basis;
6288 +        long result;
6289 +        MapReduceValuesToLongTask<K,V> sibling;
6290 +        MapReduceValuesToLongTask
6291 +            (ConcurrentHashMap<K,V> m,
6292 +             ObjectToLong<? super V> transformer,
6293 +             long basis,
6294 +             LongByLongToLong reducer) {
6295 +            super(m);
6296 +            this.transformer = transformer;
6297 +            this.basis = basis; this.reducer = reducer;
6298 +        }
6299 +        MapReduceValuesToLongTask
6300 +            (BulkTask<K,V,?> p, int b, boolean split,
6301 +             ObjectToLong<? super V> transformer,
6302 +             long basis,
6303 +             LongByLongToLong reducer) {
6304 +            super(p, b, split);
6305 +            this.transformer = transformer;
6306 +            this.basis = basis; this.reducer = reducer;
6307 +        }
6308 +        @SuppressWarnings("unchecked") public final void compute() {
6309 +            MapReduceValuesToLongTask<K,V> t = this;
6310 +            final ObjectToLong<? super V> transformer =
6311 +                this.transformer;
6312 +            final LongByLongToLong reducer = this.reducer;
6313 +            if (transformer == null || reducer == null)
6314 +                throw new Error(NullFunctionMessage);
6315 +            final long id = this.basis;
6316 +            int b = batch();
6317 +            while (b > 1 && t.baseIndex != t.baseLimit) {
6318 +                b >>>= 1;
6319 +                t.pending = 1;
6320 +                MapReduceValuesToLongTask<K,V> rt =
6321 +                    new MapReduceValuesToLongTask<K,V>
6322 +                    (t, b, true, transformer, id, reducer);
6323 +                t = new MapReduceValuesToLongTask<K,V>
6324 +                    (t, b, false, transformer, id, reducer);
6325 +                t.sibling = rt;
6326 +                rt.sibling = t;
6327 +                rt.fork();
6328 +            }
6329 +            long r = id;
6330 +            Object v;
6331 +            while ((v = t.advance()) != null)
6332 +                r = reducer.apply(r, transformer.apply((V)v));
6333 +            t.result = r;
6334 +            for (;;) {
6335 +                int c; BulkTask<K,V,?> par; MapReduceValuesToLongTask<K,V> s, p;
6336 +                if ((par = t.parent) == null ||
6337 +                    !(par instanceof MapReduceValuesToLongTask)) {
6338 +                    t.quietlyComplete();
6339 +                    break;
6340 +                }
6341 +                else if ((c = (p = (MapReduceValuesToLongTask<K,V>)par).pending) == 0) {
6342 +                    if ((s = t.sibling) != null)
6343 +                        r = reducer.apply(r, s.result);
6344 +                    (t = p).result = r;
6345 +                }
6346 +                else if (p.casPending(c, 0))
6347 +                    break;
6348 +            }
6349 +        }
6350 +        public final Long getRawResult() { return result; }
6351 +    }
6352 +
6353 +    @SuppressWarnings("serial")
6354 +    static final class MapReduceEntriesToLongTask<K,V>
6355 +        extends BulkTask<K,V,Long> {
6356 +        final ObjectToLong<Map.Entry<K,V>> transformer;
6357 +        final LongByLongToLong reducer;
6358 +        final long basis;
6359 +        long result;
6360 +        MapReduceEntriesToLongTask<K,V> sibling;
6361 +        MapReduceEntriesToLongTask
6362 +            (ConcurrentHashMap<K,V> m,
6363 +             ObjectToLong<Map.Entry<K,V>> transformer,
6364 +             long basis,
6365 +             LongByLongToLong reducer) {
6366 +            super(m);
6367 +            this.transformer = transformer;
6368 +            this.basis = basis; this.reducer = reducer;
6369 +        }
6370 +        MapReduceEntriesToLongTask
6371 +            (BulkTask<K,V,?> p, int b, boolean split,
6372 +             ObjectToLong<Map.Entry<K,V>> transformer,
6373 +             long basis,
6374 +             LongByLongToLong reducer) {
6375 +            super(p, b, split);
6376 +            this.transformer = transformer;
6377 +            this.basis = basis; this.reducer = reducer;
6378 +        }
6379 +        @SuppressWarnings("unchecked") public final void compute() {
6380 +            MapReduceEntriesToLongTask<K,V> t = this;
6381 +            final ObjectToLong<Map.Entry<K,V>> transformer =
6382 +                this.transformer;
6383 +            final LongByLongToLong reducer = this.reducer;
6384 +            if (transformer == null || reducer == null)
6385 +                throw new Error(NullFunctionMessage);
6386 +            final long id = this.basis;
6387 +            int b = batch();
6388 +            while (b > 1 && t.baseIndex != t.baseLimit) {
6389 +                b >>>= 1;
6390 +                t.pending = 1;
6391 +                MapReduceEntriesToLongTask<K,V> rt =
6392 +                    new MapReduceEntriesToLongTask<K,V>
6393 +                    (t, b, true, transformer, id, reducer);
6394 +                t = new MapReduceEntriesToLongTask<K,V>
6395 +                    (t, b, false, transformer, id, reducer);
6396 +                t.sibling = rt;
6397 +                rt.sibling = t;
6398 +                rt.fork();
6399 +            }
6400 +            long r = id;
6401 +            Object v;
6402 +            while ((v = t.advance()) != null)
6403 +                r = reducer.apply(r, transformer.apply(entryFor((K)t.nextKey, (V)v)));
6404 +            t.result = r;
6405 +            for (;;) {
6406 +                int c; BulkTask<K,V,?> par; MapReduceEntriesToLongTask<K,V> s, p;
6407 +                if ((par = t.parent) == null ||
6408 +                    !(par instanceof MapReduceEntriesToLongTask)) {
6409 +                    t.quietlyComplete();
6410 +                    break;
6411 +                }
6412 +                else if ((c = (p = (MapReduceEntriesToLongTask<K,V>)par).pending) == 0) {
6413 +                    if ((s = t.sibling) != null)
6414 +                        r = reducer.apply(r, s.result);
6415 +                    (t = p).result = r;
6416 +                }
6417 +                else if (p.casPending(c, 0))
6418 +                    break;
6419 +            }
6420 +        }
6421 +        public final Long getRawResult() { return result; }
6422 +    }
6423 +
6424 +    @SuppressWarnings("serial")
6425 +    static final class MapReduceMappingsToLongTask<K,V>
6426 +        extends BulkTask<K,V,Long> {
6427 +        final ObjectByObjectToLong<? super K, ? super V> transformer;
6428 +        final LongByLongToLong reducer;
6429 +        final long basis;
6430 +        long result;
6431 +        MapReduceMappingsToLongTask<K,V> sibling;
6432 +        MapReduceMappingsToLongTask
6433 +            (ConcurrentHashMap<K,V> m,
6434 +             ObjectByObjectToLong<? super K, ? super V> transformer,
6435 +             long basis,
6436 +             LongByLongToLong reducer) {
6437 +            super(m);
6438 +            this.transformer = transformer;
6439 +            this.basis = basis; this.reducer = reducer;
6440 +        }
6441 +        MapReduceMappingsToLongTask
6442 +            (BulkTask<K,V,?> p, int b, boolean split,
6443 +             ObjectByObjectToLong<? super K, ? super V> transformer,
6444 +             long basis,
6445 +             LongByLongToLong reducer) {
6446 +            super(p, b, split);
6447 +            this.transformer = transformer;
6448 +            this.basis = basis; this.reducer = reducer;
6449 +        }
6450 +        @SuppressWarnings("unchecked") public final void compute() {
6451 +            MapReduceMappingsToLongTask<K,V> t = this;
6452 +            final ObjectByObjectToLong<? super K, ? super V> transformer =
6453 +                this.transformer;
6454 +            final LongByLongToLong reducer = this.reducer;
6455 +            if (transformer == null || reducer == null)
6456 +                throw new Error(NullFunctionMessage);
6457 +            final long id = this.basis;
6458 +            int b = batch();
6459 +            while (b > 1 && t.baseIndex != t.baseLimit) {
6460 +                b >>>= 1;
6461 +                t.pending = 1;
6462 +                MapReduceMappingsToLongTask<K,V> rt =
6463 +                    new MapReduceMappingsToLongTask<K,V>
6464 +                    (t, b, true, transformer, id, reducer);
6465 +                t = new MapReduceMappingsToLongTask<K,V>
6466 +                    (t, b, false, transformer, id, reducer);
6467 +                t.sibling = rt;
6468 +                rt.sibling = t;
6469 +                rt.fork();
6470 +            }
6471 +            long r = id;
6472 +            Object v;
6473 +            while ((v = t.advance()) != null)
6474 +                r = reducer.apply(r, transformer.apply((K)t.nextKey, (V)v));
6475 +            t.result = r;
6476 +            for (;;) {
6477 +                int c; BulkTask<K,V,?> par; MapReduceMappingsToLongTask<K,V> s, p;
6478 +                if ((par = t.parent) == null ||
6479 +                    !(par instanceof MapReduceMappingsToLongTask)) {
6480 +                    t.quietlyComplete();
6481 +                    break;
6482 +                }
6483 +                else if ((c = (p = (MapReduceMappingsToLongTask<K,V>)par).pending) == 0) {
6484 +                    if ((s = t.sibling) != null)
6485 +                        r = reducer.apply(r, s.result);
6486 +                    (t = p).result = r;
6487 +                }
6488 +                else if (p.casPending(c, 0))
6489 +                    break;
6490 +            }
6491 +        }
6492 +        public final Long getRawResult() { return result; }
6493 +    }
6494 +
6495 +    @SuppressWarnings("serial")
6496 +    static final class MapReduceKeysToIntTask<K,V>
6497 +        extends BulkTask<K,V,Integer> {
6498 +        final ObjectToInt<? super K> transformer;
6499 +        final IntByIntToInt reducer;
6500 +        final int basis;
6501 +        int result;
6502 +        MapReduceKeysToIntTask<K,V> sibling;
6503 +        MapReduceKeysToIntTask
6504 +            (ConcurrentHashMap<K,V> m,
6505 +             ObjectToInt<? super K> transformer,
6506 +             int basis,
6507 +             IntByIntToInt reducer) {
6508 +            super(m);
6509 +            this.transformer = transformer;
6510 +            this.basis = basis; this.reducer = reducer;
6511 +        }
6512 +        MapReduceKeysToIntTask
6513 +            (BulkTask<K,V,?> p, int b, boolean split,
6514 +             ObjectToInt<? super K> transformer,
6515 +             int basis,
6516 +             IntByIntToInt reducer) {
6517 +            super(p, b, split);
6518 +            this.transformer = transformer;
6519 +            this.basis = basis; this.reducer = reducer;
6520 +        }
6521 +        @SuppressWarnings("unchecked") public final void compute() {
6522 +            MapReduceKeysToIntTask<K,V> t = this;
6523 +            final ObjectToInt<? super K> transformer =
6524 +                this.transformer;
6525 +            final IntByIntToInt reducer = this.reducer;
6526 +            if (transformer == null || reducer == null)
6527 +                throw new Error(NullFunctionMessage);
6528 +            final int id = this.basis;
6529 +            int b = batch();
6530 +            while (b > 1 && t.baseIndex != t.baseLimit) {
6531 +                b >>>= 1;
6532 +                t.pending = 1;
6533 +                MapReduceKeysToIntTask<K,V> rt =
6534 +                    new MapReduceKeysToIntTask<K,V>
6535 +                    (t, b, true, transformer, id, reducer);
6536 +                t = new MapReduceKeysToIntTask<K,V>
6537 +                    (t, b, false, transformer, id, reducer);
6538 +                t.sibling = rt;
6539 +                rt.sibling = t;
6540 +                rt.fork();
6541 +            }
6542 +            int r = id;
6543 +            while (t.advance() != null)
6544 +                r = reducer.apply(r, transformer.apply((K)t.nextKey));
6545 +            t.result = r;
6546 +            for (;;) {
6547 +                int c; BulkTask<K,V,?> par; MapReduceKeysToIntTask<K,V> s, p;
6548 +                if ((par = t.parent) == null ||
6549 +                    !(par instanceof MapReduceKeysToIntTask)) {
6550 +                    t.quietlyComplete();
6551 +                    break;
6552 +                }
6553 +                else if ((c = (p = (MapReduceKeysToIntTask<K,V>)par).pending) == 0) {
6554 +                    if ((s = t.sibling) != null)
6555 +                        r = reducer.apply(r, s.result);
6556 +                    (t = p).result = r;
6557 +                }
6558 +                else if (p.casPending(c, 0))
6559 +                    break;
6560 +            }
6561 +        }
6562 +        public final Integer getRawResult() { return result; }
6563 +    }
6564 +
6565 +    @SuppressWarnings("serial")
6566 +    static final class MapReduceValuesToIntTask<K,V>
6567 +        extends BulkTask<K,V,Integer> {
6568 +        final ObjectToInt<? super V> transformer;
6569 +        final IntByIntToInt reducer;
6570 +        final int basis;
6571 +        int result;
6572 +        MapReduceValuesToIntTask<K,V> sibling;
6573 +        MapReduceValuesToIntTask
6574 +            (ConcurrentHashMap<K,V> m,
6575 +             ObjectToInt<? super V> transformer,
6576 +             int basis,
6577 +             IntByIntToInt reducer) {
6578 +            super(m);
6579 +            this.transformer = transformer;
6580 +            this.basis = basis; this.reducer = reducer;
6581 +        }
6582 +        MapReduceValuesToIntTask
6583 +            (BulkTask<K,V,?> p, int b, boolean split,
6584 +             ObjectToInt<? super V> transformer,
6585 +             int basis,
6586 +             IntByIntToInt reducer) {
6587 +            super(p, b, split);
6588 +            this.transformer = transformer;
6589 +            this.basis = basis; this.reducer = reducer;
6590 +        }
6591 +        @SuppressWarnings("unchecked") public final void compute() {
6592 +            MapReduceValuesToIntTask<K,V> t = this;
6593 +            final ObjectToInt<? super V> transformer =
6594 +                this.transformer;
6595 +            final IntByIntToInt reducer = this.reducer;
6596 +            if (transformer == null || reducer == null)
6597 +                throw new Error(NullFunctionMessage);
6598 +            final int id = this.basis;
6599 +            int b = batch();
6600 +            while (b > 1 && t.baseIndex != t.baseLimit) {
6601 +                b >>>= 1;
6602 +                t.pending = 1;
6603 +                MapReduceValuesToIntTask<K,V> rt =
6604 +                    new MapReduceValuesToIntTask<K,V>
6605 +                    (t, b, true, transformer, id, reducer);
6606 +                t = new MapReduceValuesToIntTask<K,V>
6607 +                    (t, b, false, transformer, id, reducer);
6608 +                t.sibling = rt;
6609 +                rt.sibling = t;
6610 +                rt.fork();
6611 +            }
6612 +            int r = id;
6613 +            Object v;
6614 +            while ((v = t.advance()) != null)
6615 +                r = reducer.apply(r, transformer.apply((V)v));
6616 +            t.result = r;
6617 +            for (;;) {
6618 +                int c; BulkTask<K,V,?> par; MapReduceValuesToIntTask<K,V> s, p;
6619 +                if ((par = t.parent) == null ||
6620 +                    !(par instanceof MapReduceValuesToIntTask)) {
6621 +                    t.quietlyComplete();
6622 +                    break;
6623 +                }
6624 +                else if ((c = (p = (MapReduceValuesToIntTask<K,V>)par).pending) == 0) {
6625 +                    if ((s = t.sibling) != null)
6626 +                        r = reducer.apply(r, s.result);
6627 +                    (t = p).result = r;
6628 +                }
6629 +                else if (p.casPending(c, 0))
6630 +                    break;
6631 +            }
6632 +        }
6633 +        public final Integer getRawResult() { return result; }
6634 +    }
6635 +
6636 +    @SuppressWarnings("serial")
6637 +    static final class MapReduceEntriesToIntTask<K,V>
6638 +        extends BulkTask<K,V,Integer> {
6639 +        final ObjectToInt<Map.Entry<K,V>> transformer;
6640 +        final IntByIntToInt reducer;
6641 +        final int basis;
6642 +        int result;
6643 +        MapReduceEntriesToIntTask<K,V> sibling;
6644 +        MapReduceEntriesToIntTask
6645 +            (ConcurrentHashMap<K,V> m,
6646 +             ObjectToInt<Map.Entry<K,V>> transformer,
6647 +             int basis,
6648 +             IntByIntToInt reducer) {
6649 +            super(m);
6650 +            this.transformer = transformer;
6651 +            this.basis = basis; this.reducer = reducer;
6652 +        }
6653 +        MapReduceEntriesToIntTask
6654 +            (BulkTask<K,V,?> p, int b, boolean split,
6655 +             ObjectToInt<Map.Entry<K,V>> transformer,
6656 +             int basis,
6657 +             IntByIntToInt reducer) {
6658 +            super(p, b, split);
6659 +            this.transformer = transformer;
6660 +            this.basis = basis; this.reducer = reducer;
6661 +        }
6662 +        @SuppressWarnings("unchecked") public final void compute() {
6663 +            MapReduceEntriesToIntTask<K,V> t = this;
6664 +            final ObjectToInt<Map.Entry<K,V>> transformer =
6665 +                this.transformer;
6666 +            final IntByIntToInt reducer = this.reducer;
6667 +            if (transformer == null || reducer == null)
6668 +                throw new Error(NullFunctionMessage);
6669 +            final int id = this.basis;
6670 +            int b = batch();
6671 +            while (b > 1 && t.baseIndex != t.baseLimit) {
6672 +                b >>>= 1;
6673 +                t.pending = 1;
6674 +                MapReduceEntriesToIntTask<K,V> rt =
6675 +                    new MapReduceEntriesToIntTask<K,V>
6676 +                    (t, b, true, transformer, id, reducer);
6677 +                t = new MapReduceEntriesToIntTask<K,V>
6678 +                    (t, b, false, transformer, id, reducer);
6679 +                t.sibling = rt;
6680 +                rt.sibling = t;
6681 +                rt.fork();
6682 +            }
6683 +            int r = id;
6684 +            Object v;
6685 +            while ((v = t.advance()) != null)
6686 +                r = reducer.apply(r, transformer.apply(entryFor((K)t.nextKey, (V)v)));
6687 +            t.result = r;
6688 +            for (;;) {
6689 +                int c; BulkTask<K,V,?> par; MapReduceEntriesToIntTask<K,V> s, p;
6690 +                if ((par = t.parent) == null ||
6691 +                    !(par instanceof MapReduceEntriesToIntTask)) {
6692 +                    t.quietlyComplete();
6693 +                    break;
6694 +                }
6695 +                else if ((c = (p = (MapReduceEntriesToIntTask<K,V>)par).pending) == 0) {
6696 +                    if ((s = t.sibling) != null)
6697 +                        r = reducer.apply(r, s.result);
6698 +                    (t = p).result = r;
6699 +                }
6700 +                else if (p.casPending(c, 0))
6701 +                    break;
6702 +            }
6703 +        }
6704 +        public final Integer getRawResult() { return result; }
6705 +    }
6706 +
6707 +    @SuppressWarnings("serial")
6708 +    static final class MapReduceMappingsToIntTask<K,V>
6709 +        extends BulkTask<K,V,Integer> {
6710 +        final ObjectByObjectToInt<? super K, ? super V> transformer;
6711 +        final IntByIntToInt reducer;
6712 +        final int basis;
6713 +        int result;
6714 +        MapReduceMappingsToIntTask<K,V> sibling;
6715 +        MapReduceMappingsToIntTask
6716 +            (ConcurrentHashMap<K,V> m,
6717 +             ObjectByObjectToInt<? super K, ? super V> transformer,
6718 +             int basis,
6719 +             IntByIntToInt reducer) {
6720 +            super(m);
6721 +            this.transformer = transformer;
6722 +            this.basis = basis; this.reducer = reducer;
6723 +        }
6724 +        MapReduceMappingsToIntTask
6725 +            (BulkTask<K,V,?> p, int b, boolean split,
6726 +             ObjectByObjectToInt<? super K, ? super V> transformer,
6727 +             int basis,
6728 +             IntByIntToInt reducer) {
6729 +            super(p, b, split);
6730 +            this.transformer = transformer;
6731 +            this.basis = basis; this.reducer = reducer;
6732 +        }
6733 +        @SuppressWarnings("unchecked") public final void compute() {
6734 +            MapReduceMappingsToIntTask<K,V> t = this;
6735 +            final ObjectByObjectToInt<? super K, ? super V> transformer =
6736 +                this.transformer;
6737 +            final IntByIntToInt reducer = this.reducer;
6738 +            if (transformer == null || reducer == null)
6739 +                throw new Error(NullFunctionMessage);
6740 +            final int id = this.basis;
6741 +            int b = batch();
6742 +            while (b > 1 && t.baseIndex != t.baseLimit) {
6743 +                b >>>= 1;
6744 +                t.pending = 1;
6745 +                MapReduceMappingsToIntTask<K,V> rt =
6746 +                    new MapReduceMappingsToIntTask<K,V>
6747 +                    (t, b, true, transformer, id, reducer);
6748 +                t = new MapReduceMappingsToIntTask<K,V>
6749 +                    (t, b, false, transformer, id, reducer);
6750 +                t.sibling = rt;
6751 +                rt.sibling = t;
6752 +                rt.fork();
6753 +            }
6754 +            int r = id;
6755 +            Object v;
6756 +            while ((v = t.advance()) != null)
6757 +                r = reducer.apply(r, transformer.apply((K)t.nextKey, (V)v));
6758 +            t.result = r;
6759 +            for (;;) {
6760 +                int c; BulkTask<K,V,?> par; MapReduceMappingsToIntTask<K,V> s, p;
6761 +                if ((par = t.parent) == null ||
6762 +                    !(par instanceof MapReduceMappingsToIntTask)) {
6763 +                    t.quietlyComplete();
6764 +                    break;
6765 +                }
6766 +                else if ((c = (p = (MapReduceMappingsToIntTask<K,V>)par).pending) == 0) {
6767 +                    if ((s = t.sibling) != null)
6768 +                        r = reducer.apply(r, s.result);
6769 +                    (t = p).result = r;
6770 +                }
6771 +                else if (p.casPending(c, 0))
6772 +                    break;
6773 +            }
6774 +        }
6775 +        public final Integer getRawResult() { return result; }
6776 +    }
6777 +
6778 +
6779      // Unsafe mechanics
6780      private static final sun.misc.Unsafe UNSAFE;
6781 <    private static final long SBASE;
6782 <    private static final int SSHIFT;
6783 <    private static final long TBASE;
6784 <    private static final int TSHIFT;
6781 >    private static final long counterOffset;
6782 >    private static final long sizeCtlOffset;
6783 >    private static final long ABASE;
6784 >    private static final int ASHIFT;
6785  
6786      static {
6787 <        int ss, ts;
6787 >        int ss;
6788          try {
6789              UNSAFE = sun.misc.Unsafe.getUnsafe();
6790 <            Class<?> tc = HashEntry[].class;
6791 <            Class<?> sc = Segment[].class;
6792 <            TBASE = UNSAFE.arrayBaseOffset(tc);
6793 <            SBASE = UNSAFE.arrayBaseOffset(sc);
6794 <            ts = UNSAFE.arrayIndexScale(tc);
6790 >            Class<?> k = ConcurrentHashMap.class;
6791 >            counterOffset = UNSAFE.objectFieldOffset
6792 >                (k.getDeclaredField("counter"));
6793 >            sizeCtlOffset = UNSAFE.objectFieldOffset
6794 >                (k.getDeclaredField("sizeCtl"));
6795 >            Class<?> sc = Node[].class;
6796 >            ABASE = UNSAFE.arrayBaseOffset(sc);
6797              ss = UNSAFE.arrayIndexScale(sc);
6798          } catch (Exception e) {
6799              throw new Error(e);
6800          }
6801 <        if ((ss & (ss-1)) != 0 || (ts & (ts-1)) != 0)
6801 >        if ((ss & (ss-1)) != 0)
6802              throw new Error("data type scale not a power of two");
6803 <        SSHIFT = 31 - Integer.numberOfLeadingZeros(ss);
1481 <        TSHIFT = 31 - Integer.numberOfLeadingZeros(ts);
6803 >        ASHIFT = 31 - Integer.numberOfLeadingZeros(ss);
6804      }
6805 <
1484 < }
6805 > }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines