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.59 by jsr166, Tue Apr 26 01:54:09 2005 UTC vs.
Revision 1.110 by jsr166, Wed Apr 27 14:06:30 2011 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7   package java.util.concurrent;
# Line 33 | Line 33 | import java.io.ObjectOutputStream;
33   * removal of only some entries.  Similarly, Iterators and
34   * Enumerations return elements reflecting the state of the hash table
35   * at some point at or since the creation of the iterator/enumeration.
36 < * They do <em>not</em> throw
37 < * {@link ConcurrentModificationException}.  However, iterators are
38 < * designed to be used by only one thread at a time.
36 > * They do <em>not</em> throw {@link ConcurrentModificationException}.
37 > * However, iterators are designed to be used by only one thread at a time.
38   *
39   * <p> The allowed concurrency among update operations is guided by
40   * the optional <tt>concurrencyLevel</tt> constructor argument
# Line 59 | Line 58 | import java.io.ObjectOutputStream;
58   * <em>optional</em> methods of the {@link Map} and {@link Iterator}
59   * interfaces.
60   *
61 < * <p> Like {@link java.util.Hashtable} but unlike {@link
62 < * java.util.HashMap}, this class does NOT allow <tt>null</tt> to be
64 < * used as a key or value.
61 > * <p> Like {@link Hashtable} but unlike {@link HashMap}, this class
62 > * does <em>not</em> allow <tt>null</tt> to be used as a key or value.
63   *
64   * <p>This class is a member of the
65 < * <a href="{@docRoot}/../guide/collections/index.html">
65 > * <a href="{@docRoot}/../technotes/guides/collections/index.html">
66   * Java Collections Framework</a>.
67   *
68   * @since 1.5
69   * @author Doug Lea
70   * @param <K> the type of keys maintained by this map
71 < * @param <V> the type of mapped values
71 > * @param <V> the type of mapped values
72   */
73   public class ConcurrentHashMap<K, V> extends AbstractMap<K, V>
74          implements ConcurrentMap<K, V>, Serializable {
# Line 78 | Line 76 | public class ConcurrentHashMap<K, V> ext
76  
77      /*
78       * The basic strategy is to subdivide the table among Segments,
79 <     * each of which itself is a concurrently readable hash table.
79 >     * each of which itself is a concurrently readable hash table.  To
80 >     * reduce footprint, all but one segments are constructed only
81 >     * when first needed (see ensureSegment). To maintain visibility
82 >     * in the presence of lazy construction, accesses to segments as
83 >     * well as elements of segment's table must use volatile access,
84 >     * which is done via Unsafe within methods segmentAt etc
85 >     * below. These provide the functionality of AtomicReferenceArrays
86 >     * but reduce the levels of indirection. Additionally,
87 >     * volatile-writes of table elements and entry "next" fields
88 >     * within locked operations use the cheaper "lazySet" forms of
89 >     * writes (via putOrderedObject) because these writes are always
90 >     * followed by lock releases that maintain sequential consistency
91 >     * of table updates.
92 >     *
93 >     * Historical note: The previous version of this class relied
94 >     * heavily on "final" fields, which avoided some volatile reads at
95 >     * the expense of a large initial footprint.  Some remnants of
96 >     * that design (including forced construction of segment 0) exist
97 >     * to ensure serialization compatibility.
98       */
99  
100      /* ---------------- Constants -------------- */
# Line 104 | Line 120 | public class ConcurrentHashMap<K, V> ext
120      /**
121       * The maximum capacity, used if a higher value is implicitly
122       * specified by either of the constructors with arguments.  MUST
123 <     * be a power of two <= 1<<30 to ensure that entries are indexible
123 >     * be a power of two <= 1<<30 to ensure that entries are indexable
124       * using ints.
125       */
126 <    static final int MAXIMUM_CAPACITY = 1 << 30;
126 >    static final int MAXIMUM_CAPACITY = 1 << 30;
127 >
128 >    /**
129 >     * The minimum capacity for per-segment tables.  Must be a power
130 >     * of two, at least two to avoid immediate resizing on next use
131 >     * after lazy construction.
132 >     */
133 >    static final int MIN_SEGMENT_TABLE_CAPACITY = 2;
134  
135      /**
136       * The maximum number of segments to allow; used to bound
137 <     * constructor arguments.
137 >     * constructor arguments. Must be power of two less than 1 << 24.
138       */
139      static final int MAX_SEGMENTS = 1 << 16; // slightly conservative
140  
# Line 137 | Line 160 | public class ConcurrentHashMap<K, V> ext
160      final int segmentShift;
161  
162      /**
163 <     * The segments, each of which is a specialized hash table
163 >     * The segments, each of which is a specialized hash table.
164       */
165 <    final Segment[] segments;
165 >    final Segment<K,V>[] segments;
166  
167      transient Set<K> keySet;
168      transient Set<Map.Entry<K,V>> entrySet;
169      transient Collection<V> values;
170  
148    /* ---------------- Small Utilities -------------- */
149
150    /**
151     * Returns a hash code for non-null Object x.
152     * Uses the same hash code spreader as most other java.util hash tables.
153     * @param x the object serving as a key
154     * @return the hash code
155     */
156    static int hash(Object x) {
157        int h = x.hashCode();
158        h += ~(h << 9);
159        h ^=  (h >>> 14);
160        h +=  (h << 4);
161        h ^=  (h >>> 10);
162        return h;
163    }
164
165    /**
166     * Returns the segment that should be used for key with given hash
167     * @param hash the hash code for the key
168     * @return the segment
169     */
170    final Segment<K,V> segmentFor(int hash) {
171        return (Segment<K,V>) segments[(hash >>> segmentShift) & segmentMask];
172    }
173
174    /* ---------------- Inner Classes -------------- */
175
171      /**
172       * ConcurrentHashMap list entry. Note that this is never exported
173 <     * out as a user-visible Map.Entry.
179 <     *
180 <     * Because the value field is volatile, not final, it is legal wrt
181 <     * the Java Memory Model for an unsynchronized reader to see null
182 <     * instead of initial value when read via a data race.  Although a
183 <     * reordering leading to this is not likely to ever actually
184 <     * occur, the Segment.readValueUnderLock method is used as a
185 <     * backup in case a null (pre-initialized) value is ever seen in
186 <     * an unsynchronized access method.
173 >     * out as a user-visible Map.Entry.
174       */
175      static final class HashEntry<K,V> {
189        final K key;
176          final int hash;
177 +        final K key;
178          volatile V value;
179 <        final HashEntry<K,V> next;
179 >        volatile HashEntry<K,V> next;
180  
181 <        HashEntry(K key, int hash, HashEntry<K,V> next, V value) {
195 <            this.key = key;
181 >        HashEntry(int hash, K key, V value, HashEntry<K,V> next) {
182              this.hash = hash;
183 <            this.next = next;
183 >            this.key = key;
184              this.value = value;
185 +            this.next = next;
186 +        }
187 +
188 +        /**
189 +         * Sets next field with volatile write semantics.  (See above
190 +         * about use of putOrderedObject.)
191 +         */
192 +        final void setNext(HashEntry<K,V> n) {
193 +            UNSAFE.putOrderedObject(this, nextOffset, n);
194          }
195 +
196 +        // Unsafe mechanics
197 +        static final sun.misc.Unsafe UNSAFE;
198 +        static final long nextOffset;
199 +        static {
200 +            try {
201 +                UNSAFE = sun.misc.Unsafe.getUnsafe();
202 +                Class k = HashEntry.class;
203 +                nextOffset = UNSAFE.objectFieldOffset
204 +                    (k.getDeclaredField("next"));
205 +            } catch (Exception e) {
206 +                throw new Error(e);
207 +            }
208 +        }
209 +    }
210 +
211 +    /**
212 +     * 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.
215 +     */
216 +    @SuppressWarnings("unchecked")
217 +    static final <K,V> HashEntry<K,V> entryAt(HashEntry<K,V>[] tab, int i) {
218 +        return (tab == null) ? null :
219 +            (HashEntry<K,V>) UNSAFE.getObjectVolatile
220 +            (tab, ((long)i << TSHIFT) + TBASE);
221 +    }
222 +
223 +    /**
224 +     * Sets the ith element of given table, with volatile write
225 +     * semantics. (See above about use of putOrderedObject.)
226 +     */
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);
230 +    }
231 +
232 +    /**
233 +     * Applies a supplemental hash function to a given hashCode, which
234 +     * defends against poor quality hash functions.  This is critical
235 +     * because ConcurrentHashMap uses power-of-two length hash tables,
236 +     * that otherwise encounter collisions for hashCodes that do not
237 +     * differ in lower or upper bits.
238 +     */
239 +    private static int hash(int h) {
240 +        // Spread bits to regularize both segment and index locations,
241 +        // using variant of single-word Wang/Jenkins hash.
242 +        h += (h <<  15) ^ 0xffffcd7d;
243 +        h ^= (h >>> 10);
244 +        h += (h <<   3);
245 +        h ^= (h >>>  6);
246 +        h += (h <<   2) + (h << 14);
247 +        return h ^ (h >>> 16);
248      }
249  
250      /**
# Line 206 | Line 254 | public class ConcurrentHashMap<K, V> ext
254       */
255      static final class Segment<K,V> extends ReentrantLock implements Serializable {
256          /*
257 <         * Segments maintain a table of entry lists that are ALWAYS
258 <         * kept in a consistent state, so can be read without locking.
259 <         * Next fields of nodes are immutable (final).  All list
260 <         * additions are performed at the front of each bin. This
261 <         * makes it easy to check changes, and also fast to traverse.
262 <         * When nodes would otherwise be changed, new nodes are
215 <         * created to replace them. This works well for hash tables
216 <         * since the bin lists tend to be short. (The average length
217 <         * is less than two for the default load factor threshold.)
218 <         *
219 <         * Read operations can thus proceed without locking, but rely
220 <         * on selected uses of volatiles to ensure that completed
221 <         * write operations performed by other threads are
222 <         * noticed. For most purposes, the "count" field, tracking the
223 <         * number of elements, serves as that volatile variable
224 <         * ensuring visibility.  This is convenient because this field
225 <         * needs to be read in many read operations anyway:
226 <         *
227 <         *   - All (unsynchronized) read operations must first read the
228 <         *     "count" field, and should not look at table entries if
229 <         *     it is 0.
257 >         * Segments maintain a table of entry lists that are always
258 >         * kept in a consistent state, so can be read (via volatile
259 >         * reads of segments and tables) without locking.  This
260 >         * requires replicating nodes when necessary during table
261 >         * resizing, so the old lists can be traversed by readers
262 >         * still using old version of table.
263           *
264 <         *   - All (synchronized) write operations should write to
265 <         *     the "count" field after structurally changing any bin.
266 <         *     The operations must not take any action that could even
267 <         *     momentarily cause a concurrent read operation to see
268 <         *     inconsistent data. This is made easier by the nature of
269 <         *     the read operations in Map. For example, no operation
270 <         *     can reveal that the table has grown but the threshold
271 <         *     has not yet been updated, so there are no atomicity
272 <         *     requirements for this with respect to reads.
273 <         *
274 <         * As a guide, all critical volatile reads and writes to the
275 <         * count field are marked in code comments.
264 >         * This class defines only mutative methods requiring locking.
265 >         * Except as noted, the methods of this class perform the
266 >         * per-segment versions of ConcurrentHashMap methods.  (Other
267 >         * methods are integrated directly into ConcurrentHashMap
268 >         * methods.) These mutative methods use a form of controlled
269 >         * spinning on contention via methods scanAndLock and
270 >         * scanAndLockForPut. These intersperse tryLocks with
271 >         * traversals to locate nodes.  The main benefit is to absorb
272 >         * cache misses (which are very common for hash tables) while
273 >         * obtaining locks so that traversal is faster once
274 >         * acquired. We do not actually use the found nodes since they
275 >         * must be re-acquired under lock anyway to ensure sequential
276 >         * consistency of updates (and in any case may be undetectably
277 >         * stale), but they will normally be much faster to re-locate.
278 >         * Also, scanAndLockForPut speculatively creates a fresh node
279 >         * to use in put if no node is found.
280           */
281  
282          private static final long serialVersionUID = 2249069246763182397L;
283  
284          /**
285 <         * The number of elements in this segment's region.
285 >         * The maximum number of times to tryLock in a prescan before
286 >         * 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.
290           */
291 <        transient volatile int count;
291 >        static final int MAX_SCAN_RETRIES =
292 >            Runtime.getRuntime().availableProcessors() > 1 ? 64 : 1;
293  
294          /**
295 <         * Number of updates that alter the size of the table. This is
296 <         * used during bulk-read methods to make sure they see a
255 <         * consistent snapshot: If modCounts change during a traversal
256 <         * of segments computing size or checking containsValue, then
257 <         * we might have an inconsistent view of state so (usually)
258 <         * must retry.
295 >         * The per-segment table. Elements are accessed via
296 >         * entryAt/setEntryAt providing volatile semantics.
297           */
298 <        transient int modCount;
298 >        transient volatile HashEntry<K,V>[] table;
299  
300          /**
301 <         * The table is rehashed when its size exceeds this threshold.
302 <         * (The value of this field is always (int)(capacity *
265 <         * loadFactor).)
301 >         * The number of elements. Accessed only either within locks
302 >         * or among other volatile reads that maintain visibility.
303           */
304 <        transient int threshold;
304 >        transient int count;
305 >
306 >        /**
307 >         * The total number of mutative operations in this segment.
308 >         * Even though this may overflows 32 bits, it provides
309 >         * sufficient accuracy for stability checks in CHM isEmpty()
310 >         * and size() methods.  Accessed only either within locks or
311 >         * among other volatile reads that maintain visibility.
312 >         */
313 >        transient int modCount;
314  
315          /**
316 <         * The per-segment table. Declared as a raw type, casted
317 <         * to HashEntry<K,V> on each use.
316 >         * The table is rehashed when its size exceeds this threshold.
317 >         * (The value of this field is always <tt>(int)(capacity *
318 >         * loadFactor)</tt>.)
319           */
320 <        transient volatile HashEntry[] table;
320 >        transient int threshold;
321  
322          /**
323           * The load factor for the hash table.  Even though this value
# Line 280 | Line 327 | public class ConcurrentHashMap<K, V> ext
327           */
328          final float loadFactor;
329  
330 <        Segment(int initialCapacity, float lf) {
331 <            loadFactor = lf;
332 <            setTable(new HashEntry[initialCapacity]);
330 >        Segment(float lf, int threshold, HashEntry<K,V>[] tab) {
331 >            this.loadFactor = lf;
332 >            this.threshold = threshold;
333 >            this.table = tab;
334          }
335  
336 <        /**
337 <         * Set table to new HashEntry array.
338 <         * Call only while holding lock or in constructor.
339 <         */
292 <        void setTable(HashEntry[] newTable) {
293 <            threshold = (int)(newTable.length * loadFactor);
294 <            table = newTable;
295 <        }
296 <
297 <        /**
298 <         * Return properly casted first entry of bin for given hash
299 <         */
300 <        HashEntry<K,V> getFirst(int hash) {
301 <            HashEntry[] tab = table;
302 <            return (HashEntry<K,V>) tab[hash & (tab.length - 1)];
303 <        }
304 <
305 <        /**
306 <         * Read value field of an entry under lock. Called if value
307 <         * field ever appears to be null. This is possible only if a
308 <         * compiler happens to reorder a HashEntry initialization with
309 <         * its table assignment, which is legal under memory model
310 <         * but is not known to ever occur.
311 <         */
312 <        V readValueUnderLock(HashEntry<K,V> e) {
313 <            lock();
336 >        final V put(K key, int hash, V value, boolean onlyIfAbsent) {
337 >            HashEntry<K,V> node = tryLock() ? null :
338 >                scanAndLockForPut(key, hash, value);
339 >            V oldValue;
340              try {
341 <                return e.value;
342 <            } finally {
343 <                unlock();
344 <            }
345 <        }
346 <
347 <        /* Specialized implementations of map methods */
348 <
349 <        V get(Object key, int hash) {
350 <            if (count != 0) { // read-volatile
351 <                HashEntry<K,V> e = getFirst(hash);
352 <                while (e != null) {
353 <                    if (e.hash == hash && key.equals(e.key)) {
354 <                        V v = e.value;
355 <                        if (v != null)
356 <                            return v;
331 <                        return readValueUnderLock(e); // recheck
341 >                HashEntry<K,V>[] tab = table;
342 >                int index = (tab.length - 1) & hash;
343 >                HashEntry<K,V> first = entryAt(tab, index);
344 >                for (HashEntry<K,V> e = first;;) {
345 >                    if (e != null) {
346 >                        K k;
347 >                        if ((k = e.key) == key ||
348 >                            (e.hash == hash && key.equals(k))) {
349 >                            oldValue = e.value;
350 >                            if (!onlyIfAbsent) {
351 >                                e.value = value;
352 >                                ++modCount;
353 >                            }
354 >                            break;
355 >                        }
356 >                        e = e.next;
357                      }
358 <                    e = e.next;
359 <                }
360 <            }
361 <            return null;
362 <        }
363 <
364 <        boolean containsKey(Object key, int hash) {
365 <            if (count != 0) { // read-volatile
366 <                HashEntry<K,V> e = getFirst(hash);
367 <                while (e != null) {
368 <                    if (e.hash == hash && key.equals(e.key))
369 <                        return true;
370 <                    e = e.next;
371 <                }
347 <            }
348 <            return false;
349 <        }
350 <
351 <        boolean containsValue(Object value) {
352 <            if (count != 0) { // read-volatile
353 <                HashEntry[] tab = table;
354 <                int len = tab.length;
355 <                for (int i = 0 ; i < len; i++) {
356 <                    for (HashEntry<K,V> e = (HashEntry<K,V>)tab[i];
357 <                         e != null ;
358 <                         e = e.next) {
359 <                        V v = e.value;
360 <                        if (v == null) // recheck
361 <                            v = readValueUnderLock(e);
362 <                        if (value.equals(v))
363 <                            return true;
358 >                    else {
359 >                        if (node != null)
360 >                            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;
371 >                        break;
372                      }
373                  }
366            }
367            return false;
368        }
369
370        boolean replace(K key, int hash, V oldValue, V newValue) {
371            lock();
372            try {
373                HashEntry<K,V> e = getFirst(hash);
374                while (e != null && (e.hash != hash || !key.equals(e.key)))
375                    e = e.next;
376
377                boolean replaced = false;
378                if (e != null && oldValue.equals(e.value)) {
379                    replaced = true;
380                    e.value = newValue;
381                }
382                return replaced;
383            } finally {
384                unlock();
385            }
386        }
387
388        V replace(K key, int hash, V newValue) {
389            lock();
390            try {
391                HashEntry<K,V> e = getFirst(hash);
392                while (e != null && (e.hash != hash || !key.equals(e.key)))
393                    e = e.next;
394
395                V oldValue = null;
396                if (e != null) {
397                    oldValue = e.value;
398                    e.value = newValue;
399                }
400                return oldValue;
401            } finally {
402                unlock();
403            }
404        }
405
406
407        V put(K key, int hash, V value, boolean onlyIfAbsent) {
408            lock();
409            try {
410                int c = count;
411                if (c++ > threshold) // ensure capacity
412                    rehash();
413                HashEntry[] tab = table;
414                int index = hash & (tab.length - 1);
415                HashEntry<K,V> first = (HashEntry<K,V>) tab[index];
416                HashEntry<K,V> e = first;
417                while (e != null && (e.hash != hash || !key.equals(e.key)))
418                    e = e.next;
419
420                V oldValue;
421                if (e != null) {
422                    oldValue = e.value;
423                    if (!onlyIfAbsent)
424                        e.value = value;
425                }
426                else {
427                    oldValue = null;
428                    ++modCount;
429                    tab[index] = new HashEntry<K,V>(key, hash, first, value);
430                    count = c; // write-volatile
431                }
432                return oldValue;
374              } finally {
375                  unlock();
376              }
377 +            return oldValue;
378          }
379  
380 <        void rehash() {
381 <            HashEntry[] oldTable = table;            
382 <            int oldCapacity = oldTable.length;
383 <            if (oldCapacity >= MAXIMUM_CAPACITY)
384 <                return;
385 <
380 >        /**
381 >         * Doubles size of table and repacks entries, also adding the
382 >         * given node to new table
383 >         */
384 >        @SuppressWarnings("unchecked")
385 >        private void rehash(HashEntry<K,V> node) {
386              /*
387 <             * Reclassify nodes in each list to new Map.  Because we are
388 <             * using power-of-two expansion, the elements from each bin
389 <             * must either stay at same index, or move with a power of two
390 <             * offset. We eliminate unnecessary node creation by catching
391 <             * cases where old nodes can be reused because their next
392 <             * fields won't change. Statistically, at the default
393 <             * threshold, only about one-sixth of them need cloning when
394 <             * a table doubles. The nodes they replace will be garbage
395 <             * collectable as soon as they are no longer referenced by any
396 <             * reader thread that may be in the midst of traversing table
397 <             * right now.
387 >             * Reclassify nodes in each list to new table.  Because we
388 >             * are using power-of-two expansion, the elements from
389 >             * each bin must either stay at same index, or move with a
390 >             * power of two offset. We eliminate unnecessary node
391 >             * creation by catching cases where old nodes can be
392 >             * reused because their next fields won't change.
393 >             * Statistically, at the default threshold, only about
394 >             * one-sixth of them need cloning when a table
395 >             * doubles. The nodes they replace will be garbage
396 >             * collectable as soon as they are no longer referenced by
397 >             * any reader thread that may be in the midst of
398 >             * concurrently traversing table. Entry accesses use plain
399 >             * array indexing because they are followed by volatile
400 >             * table write.
401               */
402 <
403 <            HashEntry[] newTable = new HashEntry[oldCapacity << 1];
404 <            threshold = (int)(newTable.length * loadFactor);
405 <            int sizeMask = newTable.length - 1;
402 >            HashEntry<K,V>[] oldTable = table;
403 >            int oldCapacity = oldTable.length;
404 >            int newCapacity = oldCapacity << 1;
405 >            threshold = (int)(newCapacity * loadFactor);
406 >            HashEntry<K,V>[] newTable =
407 >                (HashEntry<K,V>[]) new HashEntry[newCapacity];
408 >            int sizeMask = newCapacity - 1;
409              for (int i = 0; i < oldCapacity ; i++) {
410 <                // We need to guarantee that any existing reads of old Map can
463 <                //  proceed. So we cannot yet null out each bin.
464 <                HashEntry<K,V> e = (HashEntry<K,V>)oldTable[i];
465 <
410 >                HashEntry<K,V> e = oldTable[i];
411                  if (e != null) {
412                      HashEntry<K,V> next = e.next;
413                      int idx = e.hash & sizeMask;
414 <
470 <                    //  Single node on list
471 <                    if (next == null)
414 >                    if (next == null)   //  Single node on list
415                          newTable[idx] = e;
416 <
474 <                    else {
475 <                        // Reuse trailing consecutive sequence at same slot
416 >                    else { // Reuse consecutive sequence at same slot
417                          HashEntry<K,V> lastRun = e;
418                          int lastIdx = idx;
419                          for (HashEntry<K,V> last = next;
# Line 485 | Line 426 | public class ConcurrentHashMap<K, V> ext
426                              }
427                          }
428                          newTable[lastIdx] = lastRun;
429 <
489 <                        // Clone all remaining nodes
429 >                        // Clone remaining nodes
430                          for (HashEntry<K,V> p = e; p != lastRun; p = p.next) {
431 <                            int k = p.hash & sizeMask;
432 <                            HashEntry<K,V> n = (HashEntry<K,V>)newTable[k];
433 <                            newTable[k] = new HashEntry<K,V>(p.key, p.hash,
434 <                                                             n, p.value);
431 >                            V v = p.value;
432 >                            int h = p.hash;
433 >                            int k = h & sizeMask;
434 >                            HashEntry<K,V> n = newTable[k];
435 >                            newTable[k] = new HashEntry<K,V>(h, p.key, v, n);
436                          }
437                      }
438                  }
439              }
440 +            int nodeIndex = node.hash & sizeMask; // add the new node
441 +            node.setNext(newTable[nodeIndex]);
442 +            newTable[nodeIndex] = node;
443              table = newTable;
444          }
445  
446          /**
447 +         * Scans for a node containing given key while trying to
448 +         * acquire lock, creating and returning one if not found. Upon
449 +         * return, guarantees that lock is held. Unlike in most
450 +         * methods, calls to method equals are not screened: Since
451 +         * traversal speed doesn't matter, we might as well help warm
452 +         * up the associated code and accesses as well.
453 +         *
454 +         * @return a new node if key not found, else null
455 +         */
456 +        private HashEntry<K,V> scanAndLockForPut(K key, int hash, V value) {
457 +            HashEntry<K,V> first = entryForHash(this, hash);
458 +            HashEntry<K,V> e = first;
459 +            HashEntry<K,V> node = null;
460 +            int retries = -1; // negative while locating node
461 +            while (!tryLock()) {
462 +                HashEntry<K,V> f; // to recheck first below
463 +                if (retries < 0) {
464 +                    if (e == null) {
465 +                        if (node == null) // speculatively create node
466 +                            node = new HashEntry<K,V>(hash, key, value, null);
467 +                        retries = 0;
468 +                    }
469 +                    else if (key.equals(e.key))
470 +                        retries = 0;
471 +                    else
472 +                        e = e.next;
473 +                }
474 +                else if (++retries > MAX_SCAN_RETRIES) {
475 +                    lock();
476 +                    break;
477 +                }
478 +                else if ((retries & 1) == 0 &&
479 +                         (f = entryForHash(this, hash)) != first) {
480 +                    e = first = f; // re-traverse if entry changed
481 +                    retries = -1;
482 +                }
483 +            }
484 +            return node;
485 +        }
486 +
487 +        /**
488 +         * Scans for a node containing the given key while trying to
489 +         * acquire lock for a remove or replace operation. Upon
490 +         * return, guarantees that lock is held.  Note that we must
491 +         * lock even if the key is not found, to ensure sequential
492 +         * consistency of updates.
493 +         */
494 +        private void scanAndLock(Object key, int hash) {
495 +            // similar to but simpler than scanAndLockForPut
496 +            HashEntry<K,V> first = entryForHash(this, hash);
497 +            HashEntry<K,V> e = first;
498 +            int retries = -1;
499 +            while (!tryLock()) {
500 +                HashEntry<K,V> f;
501 +                if (retries < 0) {
502 +                    if (e == null || key.equals(e.key))
503 +                        retries = 0;
504 +                    else
505 +                        e = e.next;
506 +                }
507 +                else if (++retries > MAX_SCAN_RETRIES) {
508 +                    lock();
509 +                    break;
510 +                }
511 +                else if ((retries & 1) == 0 &&
512 +                         (f = entryForHash(this, hash)) != first) {
513 +                    e = first = f;
514 +                    retries = -1;
515 +                }
516 +            }
517 +        }
518 +
519 +        /**
520           * Remove; match on key only if value null, else match both.
521           */
522 <        V remove(Object key, int hash, Object value) {
523 <            lock();
522 >        final V remove(Object key, int hash, Object value) {
523 >            if (!tryLock())
524 >                scanAndLock(key, hash);
525 >            V oldValue = null;
526              try {
527 <                int c = count - 1;
528 <                HashEntry[] tab = table;
529 <                int index = hash & (tab.length - 1);
530 <                HashEntry<K,V> first = (HashEntry<K,V>)tab[index];
531 <                HashEntry<K,V> e = first;
532 <                while (e != null && (e.hash != hash || !key.equals(e.key)))
533 <                    e = e.next;
527 >                HashEntry<K,V>[] tab = table;
528 >                int index = (tab.length - 1) & hash;
529 >                HashEntry<K,V> e = entryAt(tab, index);
530 >                HashEntry<K,V> pred = null;
531 >                while (e != null) {
532 >                    K k;
533 >                    HashEntry<K,V> next = e.next;
534 >                    if ((k = e.key) == key ||
535 >                        (e.hash == hash && key.equals(k))) {
536 >                        V v = e.value;
537 >                        if (value == null || value == v || value.equals(v)) {
538 >                            if (pred == null)
539 >                                setEntryAt(tab, index, next);
540 >                            else
541 >                                pred.setNext(next);
542 >                            ++modCount;
543 >                            --count;
544 >                            oldValue = v;
545 >                        }
546 >                        break;
547 >                    }
548 >                    pred = e;
549 >                    e = next;
550 >                }
551 >            } finally {
552 >                unlock();
553 >            }
554 >            return oldValue;
555 >        }
556  
557 <                V oldValue = null;
558 <                if (e != null) {
559 <                    V v = e.value;
560 <                    if (value == null || value.equals(v)) {
561 <                        oldValue = v;
562 <                        // All entries following removed node can stay
563 <                        // in list, but all preceding ones need to be
564 <                        // cloned.
557 >        final boolean replace(K key, int hash, V oldValue, V newValue) {
558 >            if (!tryLock())
559 >                scanAndLock(key, hash);
560 >            boolean replaced = false;
561 >            try {
562 >                HashEntry<K,V> e;
563 >                for (e = entryForHash(this, hash); e != null; e = e.next) {
564 >                    K k;
565 >                    if ((k = e.key) == key ||
566 >                        (e.hash == hash && key.equals(k))) {
567 >                        if (oldValue.equals(e.value)) {
568 >                            e.value = newValue;
569 >                            ++modCount;
570 >                            replaced = true;
571 >                        }
572 >                        break;
573 >                    }
574 >                }
575 >            } finally {
576 >                unlock();
577 >            }
578 >            return replaced;
579 >        }
580 >
581 >        final V replace(K key, int hash, V value) {
582 >            if (!tryLock())
583 >                scanAndLock(key, hash);
584 >            V oldValue = null;
585 >            try {
586 >                HashEntry<K,V> e;
587 >                for (e = entryForHash(this, hash); e != null; e = e.next) {
588 >                    K k;
589 >                    if ((k = e.key) == key ||
590 >                        (e.hash == hash && key.equals(k))) {
591 >                        oldValue = e.value;
592 >                        e.value = value;
593                          ++modCount;
594 <                        HashEntry<K,V> newFirst = e.next;
526 <                        for (HashEntry<K,V> p = first; p != e; p = p.next)
527 <                            newFirst = new HashEntry<K,V>(p.key, p.hash,  
528 <                                                          newFirst, p.value);
529 <                        tab[index] = newFirst;
530 <                        count = c; // write-volatile
594 >                        break;
595                      }
596                  }
597 <                return oldValue;
597 >            } finally {
598 >                unlock();
599 >            }
600 >            return oldValue;
601 >        }
602 >
603 >        final void clear() {
604 >            lock();
605 >            try {
606 >                HashEntry<K,V>[] tab = table;
607 >                for (int i = 0; i < tab.length ; i++)
608 >                    setEntryAt(tab, i, null);
609 >                ++modCount;
610 >                count = 0;
611              } finally {
612                  unlock();
613              }
614          }
615 +    }
616 +
617 +    // Accessing segments
618  
619 <        void clear() {
620 <            if (count != 0) {
621 <                lock();
622 <                try {
623 <                    HashEntry[] tab = table;
624 <                    for (int i = 0; i < tab.length ; i++)
625 <                        tab[i] = null;
626 <                    ++modCount;
627 <                    count = 0; // write-volatile
628 <                } finally {
629 <                    unlock();
619 >    /**
620 >     * Gets the jth element of given segment array (if nonnull) with
621 >     * volatile element access semantics via Unsafe. (The null check
622 >     * 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.
626 >     */
627 >    @SuppressWarnings("unchecked")
628 >    static final <K,V> Segment<K,V> segmentAt(Segment<K,V>[] ss, int j) {
629 >        long u = (j << SSHIFT) + SBASE;
630 >        return ss == null ? null :
631 >            (Segment<K,V>) UNSAFE.getObjectVolatile(ss, u);
632 >    }
633 >
634 >    /**
635 >     * Returns the segment for the given index, creating it and
636 >     * recording in segment table (via CAS) if not already present.
637 >     *
638 >     * @param k the index
639 >     * @return the segment
640 >     */
641 >    @SuppressWarnings("unchecked")
642 >    private Segment<K,V> ensureSegment(int k) {
643 >        final Segment<K,V>[] ss = this.segments;
644 >        long u = (k << SSHIFT) + SBASE; // raw offset
645 >        Segment<K,V> seg;
646 >        if ((seg = (Segment<K,V>)UNSAFE.getObjectVolatile(ss, u)) == null) {
647 >            Segment<K,V> proto = ss[0]; // use segment 0 as prototype
648 >            int cap = proto.table.length;
649 >            float lf = proto.loadFactor;
650 >            int threshold = (int)(cap * lf);
651 >            HashEntry<K,V>[] tab = (HashEntry<K,V>[])new HashEntry[cap];
652 >            if ((seg = (Segment<K,V>)UNSAFE.getObjectVolatile(ss, u))
653 >                == null) { // recheck
654 >                Segment<K,V> s = new Segment<K,V>(lf, threshold, tab);
655 >                while ((seg = (Segment<K,V>)UNSAFE.getObjectVolatile(ss, u))
656 >                       == null) {
657 >                    if (UNSAFE.compareAndSwapObject(ss, u, null, seg = s))
658 >                        break;
659                  }
660              }
661          }
662 +        return seg;
663      }
664  
665 +    // Hash-based segment and entry accesses
666 +
667 +    /**
668 +     * Gets the segment for the given hash code.
669 +     */
670 +    @SuppressWarnings("unchecked")
671 +    private Segment<K,V> segmentForHash(int h) {
672 +        long u = (((h >>> segmentShift) & segmentMask) << SSHIFT) + SBASE;
673 +        return (Segment<K,V>) UNSAFE.getObjectVolatile(segments, u);
674 +    }
675  
676 +    /**
677 +     * Gets the table entry for the given segment and hash code.
678 +     */
679 +    @SuppressWarnings("unchecked")
680 +    static final <K,V> HashEntry<K,V> entryForHash(Segment<K,V> seg, int h) {
681 +        HashEntry<K,V>[] tab;
682 +        return (seg == null || (tab = seg.table) == null) ? null :
683 +            (HashEntry<K,V>) UNSAFE.getObjectVolatile
684 +            (tab, ((long)(((tab.length - 1) & h)) << TSHIFT) + TBASE);
685 +    }
686  
687      /* ---------------- Public operations -------------- */
688  
# Line 567 | Line 697 | public class ConcurrentHashMap<K, V> ext
697       * bin exceeds this threshold.
698       * @param concurrencyLevel the estimated number of concurrently
699       * updating threads. The implementation performs internal sizing
700 <     * to try to accommodate this many threads.  
700 >     * to try to accommodate this many threads.
701       * @throws IllegalArgumentException if the initial capacity is
702       * negative or the load factor or concurrencyLevel are
703       * nonpositive.
704       */
705 +    @SuppressWarnings("unchecked")
706      public ConcurrentHashMap(int initialCapacity,
707                               float loadFactor, int concurrencyLevel) {
708          if (!(loadFactor > 0) || initialCapacity < 0 || concurrencyLevel <= 0)
709              throw new IllegalArgumentException();
579
710          if (concurrencyLevel > MAX_SEGMENTS)
711              concurrencyLevel = MAX_SEGMENTS;
582
712          // Find power-of-two sizes best matching arguments
713          int sshift = 0;
714          int ssize = 1;
# Line 587 | Line 716 | public class ConcurrentHashMap<K, V> ext
716              ++sshift;
717              ssize <<= 1;
718          }
719 <        segmentShift = 32 - sshift;
720 <        segmentMask = ssize - 1;
592 <        this.segments = new Segment[ssize];
593 <
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 = 1;
726 >        int cap = MIN_SEGMENT_TABLE_CAPACITY;
727          while (cap < c)
728              cap <<= 1;
729 <
730 <        for (int i = 0; i < this.segments.length; ++i)
731 <            this.segments[i] = new Segment<K,V>(cap, loadFactor);
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;
736      }
737  
738      /**
739       * Creates a new, empty map with the specified initial capacity
740 <     * and load factor and with the default concurrencyLevel
610 <     * (<tt>16</tt>).
740 >     * and load factor and with the default concurrencyLevel (16).
741       *
742       * @param initialCapacity The implementation performs internal
743       * 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.
747       * @throws IllegalArgumentException if the initial capacity of
748       * elements is negative or the load factor is nonpositive
749 +     *
750 +     * @since 1.6
751       */
752      public ConcurrentHashMap(int initialCapacity, float loadFactor) {
753          this(initialCapacity, loadFactor, DEFAULT_CONCURRENCY_LEVEL);
# Line 621 | Line 755 | public class ConcurrentHashMap<K, V> ext
755  
756      /**
757       * Creates a new, empty map with the specified initial capacity,
758 <     * and with default load factor (<tt>0.75f</tt>)
625 <     * and concurrencyLevel (<tt>16</tt>).
758 >     * and with default load factor (0.75) and concurrencyLevel (16).
759       *
760       * @param initialCapacity the initial capacity. The implementation
761       * performs internal sizing to accommodate this many elements.
# Line 634 | Line 767 | public class ConcurrentHashMap<K, V> ext
767      }
768  
769      /**
770 <     * Creates a new, empty map with a default initial capacity
771 <     * (<tt>16</tt>), load factor
639 <     * (<tt>0.75f</tt>), and concurrencyLevel
640 <     * (<tt>16</tt>).
770 >     * Creates a new, empty map with a default initial capacity (16),
771 >     * load factor (0.75) and concurrencyLevel (16).
772       */
773      public ConcurrentHashMap() {
774          this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
775      }
776  
777      /**
778 <     * Creates a new map with the same mappings as the given map.  The
779 <     * map is created with a capacity of 1.5 times the number of
780 <     * mappings in the given map or <tt>16</tt>
781 <     * (whichever is greater), and a default load factor
782 <     * (<tt>0.75f</tt>) and concurrencyLevel
783 <     * (<tt>16</tt>).
653 <     * @param t the map
778 >     * Creates a new map with the same mappings as the given map.
779 >     * The map is created with a capacity of 1.5 times the number
780 >     * of mappings in the given map or 16 (whichever is greater),
781 >     * and a default load factor (0.75) and concurrencyLevel (16).
782 >     *
783 >     * @param m the map
784       */
785 <    public ConcurrentHashMap(Map<? extends K, ? extends V> t) {
786 <        this(Math.max((int) (t.size() / DEFAULT_LOAD_FACTOR) + 1,
785 >    public ConcurrentHashMap(Map<? extends K, ? extends V> m) {
786 >        this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
787                        DEFAULT_INITIAL_CAPACITY),
788               DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
789 <        putAll(t);
789 >        putAll(m);
790      }
791  
792      /**
793       * 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.
795 >     * @return <tt>true</tt> if this map contains no key-value mappings
796       */
797      public boolean isEmpty() {
668        final Segment[] segments = this.segments;
798          /*
799 <         * We keep track of per-segment modCounts to avoid ABA
800 <         * problems in which an element in one segment was added and
801 <         * in another removed during traversal, in which case the
802 <         * table was never actually empty at any point. Note the
803 <         * similar use of modCounts in the size() and containsValue()
804 <         * methods, which are the only other methods also susceptible
805 <         * to ABA problems.
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 <        int[] mc = new int[segments.length];
808 <        int mcsum = 0;
809 <        for (int i = 0; i < segments.length; ++i) {
810 <            if (segments[i].count != 0)
811 <                return false;
812 <            else
684 <                mcsum += mc[i] = segments[i].modCount;
685 <        }
686 <        // If mcsum happens to be zero, then we know we got a snapshot
687 <        // before any modifications at all were made.  This is
688 <        // probably common enough to bother tracking.
689 <        if (mcsum != 0) {
690 <            for (int i = 0; i < segments.length; ++i) {
691 <                if (segments[i].count != 0 ||
692 <                    mc[i] != segments[i].modCount)
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;
830      }
831  
# Line 701 | Line 834 | public class ConcurrentHashMap<K, V> ext
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.
837 >     * @return the number of key-value mappings in this map
838       */
839      public int size() {
707        final Segment[] segments = this.segments;
708        long sum = 0;
709        long check = 0;
710        int[] mc = new int[segments.length];
840          // Try a few times to get accurate count. On failure due to
841          // continuous async changes in table, resort to locking.
842 <        for (int k = 0; k < RETRIES_BEFORE_LOCK; ++k) {
843 <            check = 0;
844 <            sum = 0;
845 <            int mcsum = 0;
846 <            for (int i = 0; i < segments.length; ++i) {
847 <                sum += segments[i].count;
848 <                mcsum += mc[i] = segments[i].modCount;
849 <            }
850 <            if (mcsum != 0) {
851 <                for (int i = 0; i < segments.length; ++i) {
852 <                    check += segments[i].count;
853 <                    if (mc[i] != segments[i].modCount) {
854 <                        check = -1; // force retry
855 <                        break;
842 >        final Segment<K,V>[] segments = this.segments;
843 >        int size;
844 >        boolean overflow; // true if size overflows 32 bits
845 >        long sum;         // sum of modCounts
846 >        long last = 0L;   // previous sum
847 >        int retries = -1; // first iteration isn't retry
848 >        try {
849 >            for (;;) {
850 >                if (retries++ == RETRIES_BEFORE_LOCK) {
851 >                    for (int j = 0; j < segments.length; ++j)
852 >                        ensureSegment(j).lock(); // force creation
853 >                }
854 >                sum = 0L;
855 >                size = 0;
856 >                overflow = false;
857 >                for (int j = 0; j < segments.length; ++j) {
858 >                    Segment<K,V> seg = segmentAt(segments, j);
859 >                    if (seg != null) {
860 >                        sum += seg.modCount;
861 >                        int c = seg.count;
862 >                        if (c < 0 || (size += c) < 0)
863 >                            overflow = true;
864                      }
865                  }
866 +                if (sum == last)
867 +                    break;
868 +                last = sum;
869 +            }
870 +        } finally {
871 +            if (retries > RETRIES_BEFORE_LOCK) {
872 +                for (int j = 0; j < segments.length; ++j)
873 +                    segmentAt(segments, j).unlock();
874              }
730            if (check == sum)
731                break;
875          }
876 <        if (check != sum) { // Resort to locking all segments
734 <            sum = 0;
735 <            for (int i = 0; i < segments.length; ++i)
736 <                segments[i].lock();
737 <            for (int i = 0; i < segments.length; ++i)
738 <                sum += segments[i].count;
739 <            for (int i = 0; i < segments.length; ++i)
740 <                segments[i].unlock();
741 <        }
742 <        if (sum > Integer.MAX_VALUE)
743 <            return Integer.MAX_VALUE;
744 <        else
745 <            return (int)sum;
876 >        return overflow ? Integer.MAX_VALUE : size;
877      }
878  
748
879      /**
880 <     * Returns the value to which the specified key is mapped in this table.
881 <     *
882 <     * @param   key   a key in the table.
883 <     * @return  the value to which the key is mapped in this table;
884 <     *          <tt>null</tt> if the key is not mapped to any value in
885 <     *          this table.
886 <     * @throws  NullPointerException  if the key is
887 <     *               <tt>null</tt>.
880 >     * Returns the value to which the specified key is mapped,
881 >     * or {@code null} if this map contains no mapping for the key.
882 >     *
883 >     * <p>More formally, if this map contains a mapping from a key
884 >     * {@code k} to a value {@code v} such that {@code key.equals(k)},
885 >     * then this method returns {@code v}; otherwise it returns
886 >     * {@code null}.  (There can be at most one such mapping.)
887 >     *
888 >     * @throws NullPointerException if the specified key is null
889       */
890      public V get(Object key) {
891 <        int hash = hash(key); // throws NullPointerException if key null
892 <        return segmentFor(hash).get(key, hash);
891 >        Segment<K,V> s; // manually integrate access methods to reduce overhead
892 >        HashEntry<K,V>[] tab;
893 >        int h = hash(key.hashCode());
894 >        long u = (((h >>> segmentShift) & segmentMask) << SSHIFT) + SBASE;
895 >        if ((s = (Segment<K,V>)UNSAFE.getObjectVolatile(segments, u)) != null &&
896 >            (tab = s.table) != null) {
897 >            for (HashEntry<K,V> e = (HashEntry<K,V>) UNSAFE.getObjectVolatile
898 >                     (tab, ((long)(((tab.length - 1) & h)) << TSHIFT) + TBASE);
899 >                 e != null; e = e.next) {
900 >                K k;
901 >                if ((k = e.key) == key || (e.hash == h && key.equals(k)))
902 >                    return e.value;
903 >            }
904 >        }
905 >        return null;
906      }
907  
908      /**
909       * Tests if the specified object is a key in this table.
910       *
911 <     * @param   key   possible key.
912 <     * @return  <tt>true</tt> if and only if the specified object
913 <     *          is a key in this table, as determined by the
914 <     *          <tt>equals</tt> method; <tt>false</tt> otherwise.
915 <     * @throws  NullPointerException  if the key is
772 <     *               <tt>null</tt>.
911 >     * @param  key   possible key
912 >     * @return <tt>true</tt> if and only if the specified object
913 >     *         is a key in this table, as determined by the
914 >     *         <tt>equals</tt> method; <tt>false</tt> otherwise.
915 >     * @throws NullPointerException if the specified key is null
916       */
917 +    @SuppressWarnings("unchecked")
918      public boolean containsKey(Object key) {
919 <        int hash = hash(key); // throws NullPointerException if key null
920 <        return segmentFor(hash).containsKey(key, hash);
919 >        Segment<K,V> s; // same as get() except no need for volatile value read
920 >        HashEntry<K,V>[] tab;
921 >        int h = hash(key.hashCode());
922 >        long u = (((h >>> segmentShift) & segmentMask) << SSHIFT) + SBASE;
923 >        if ((s = (Segment<K,V>)UNSAFE.getObjectVolatile(segments, u)) != null &&
924 >            (tab = s.table) != null) {
925 >            for (HashEntry<K,V> e = (HashEntry<K,V>) UNSAFE.getObjectVolatile
926 >                     (tab, ((long)(((tab.length - 1) & h)) << TSHIFT) + TBASE);
927 >                 e != null; e = e.next) {
928 >                K k;
929 >                if ((k = e.key) == key || (e.hash == h && key.equals(k)))
930 >                    return true;
931 >            }
932 >        }
933 >        return false;
934      }
935  
936      /**
# Line 782 | Line 939 | public class ConcurrentHashMap<K, V> ext
939       * traversal of the hash table, and so is much slower than
940       * method <tt>containsKey</tt>.
941       *
942 <     * @param value value whose presence in this map is to be tested.
942 >     * @param value value whose presence in this map is to be tested
943       * @return <tt>true</tt> if this map maps one or more keys to the
944 <     * specified value.
945 <     * @throws  NullPointerException  if the value is <tt>null</tt>.
944 >     *         specified value
945 >     * @throws NullPointerException if the specified value is null
946       */
947      public boolean containsValue(Object value) {
948 +        // Same idea as size()
949          if (value == null)
950              throw new NullPointerException();
951 <        
794 <        // See explanation of modCount use above
795 <
796 <        final Segment[] segments = this.segments;
797 <        int[] mc = new int[segments.length];
798 <
799 <        // Try a few times without locking
800 <        for (int k = 0; k < RETRIES_BEFORE_LOCK; ++k) {
801 <            int sum = 0;
802 <            int mcsum = 0;
803 <            for (int i = 0; i < segments.length; ++i) {
804 <                int c = segments[i].count;
805 <                mcsum += mc[i] = segments[i].modCount;
806 <                if (segments[i].containsValue(value))
807 <                    return true;
808 <            }
809 <            boolean cleanSweep = true;
810 <            if (mcsum != 0) {
811 <                for (int i = 0; i < segments.length; ++i) {
812 <                    int c = segments[i].count;
813 <                    if (mc[i] != segments[i].modCount) {
814 <                        cleanSweep = false;
815 <                        break;
816 <                    }
817 <                }
818 <            }
819 <            if (cleanSweep)
820 <                return false;
821 <        }
822 <        // Resort to locking all segments
823 <        for (int i = 0; i < segments.length; ++i)
824 <            segments[i].lock();
951 >        final Segment<K,V>[] segments = this.segments;
952          boolean found = false;
953 +        long last = 0L;   // previous sum
954 +        int retries = -1;
955          try {
956 <            for (int i = 0; i < segments.length; ++i) {
957 <                if (segments[i].containsValue(value)) {
958 <                    found = true;
959 <                    break;
956 >            outer: for (;;) {
957 >                if (retries++ == RETRIES_BEFORE_LOCK) {
958 >                    for (int j = 0; j < segments.length; ++j)
959 >                        ensureSegment(j).lock(); // force creation
960 >                }
961 >                long sum = 0L;
962 >                for (int j = 0; j < segments.length; ++j) {
963 >                    HashEntry<K,V>[] tab;
964 >                    Segment<K,V> seg = segmentAt(segments, j);
965 >                    if (seg != null && (tab = seg.table) != null) {
966 >                        for (int i = 0 ; i < tab.length; i++) {
967 >                            HashEntry<K,V> e;
968 >                            for (e = entryAt(tab, i); e != null; e = e.next) {
969 >                                V v = e.value;
970 >                                if (v != null && value.equals(v)) {
971 >                                    found = true;
972 >                                    break outer;
973 >                                }
974 >                            }
975 >                        }
976 >                        sum += seg.modCount;
977 >                    }
978                  }
979 +                if (retries > 0 && sum == last)
980 +                    break;
981 +                last = sum;
982              }
983          } finally {
984 <            for (int i = 0; i < segments.length; ++i)
985 <                segments[i].unlock();
984 >            if (retries > RETRIES_BEFORE_LOCK) {
985 >                for (int j = 0; j < segments.length; ++j)
986 >                    segmentAt(segments, j).unlock();
987 >            }
988          }
989          return found;
990      }
# Line 840 | Line 992 | public class ConcurrentHashMap<K, V> ext
992      /**
993       * Legacy method testing if some key maps into the specified value
994       * in this table.  This method is identical in functionality to
995 <     * {@link #containsValue}, and  exists solely to ensure
995 >     * {@link #containsValue}, and exists solely to ensure
996       * full compatibility with class {@link java.util.Hashtable},
997       * which supported this method prior to introduction of the
998       * Java Collections framework.
999 <
1000 <     * @param      value   a value to search for.
1001 <     * @return     <tt>true</tt> if and only if some key maps to the
1002 <     *             <tt>value</tt> argument in this table as
1003 <     *             determined by the <tt>equals</tt> method;
1004 <     *             <tt>false</tt> otherwise.
1005 <     * @throws  NullPointerException  if the value is <tt>null</tt>.
999 >     *
1000 >     * @param  value a value to search for
1001 >     * @return <tt>true</tt> if and only if some key maps to the
1002 >     *         <tt>value</tt> argument in this table as
1003 >     *         determined by the <tt>equals</tt> method;
1004 >     *         <tt>false</tt> otherwise
1005 >     * @throws NullPointerException if the specified value is null
1006       */
1007      public boolean contains(Object value) {
1008          return containsValue(value);
1009      }
1010  
1011      /**
1012 <     * Maps the specified <tt>key</tt> to the specified
1013 <     * <tt>value</tt> in this table. Neither the key nor the
862 <     * value can be <tt>null</tt>.
1012 >     * Maps the specified key to the specified value in this table.
1013 >     * Neither the key nor the value can be null.
1014       *
1015       * <p> The value can be retrieved by calling the <tt>get</tt> method
1016       * with a key that is equal to the original key.
1017       *
1018 <     * @param      key     the table key.
1019 <     * @param      value   the value.
1020 <     * @return     the previous value of the specified key in this table,
1021 <     *             or <tt>null</tt> if it did not have one.
1022 <     * @throws  NullPointerException  if the key or value is
872 <     *               <tt>null</tt>.
1018 >     * @param key key with which the specified value is to be associated
1019 >     * @param value value to be associated with the specified key
1020 >     * @return the previous value associated with <tt>key</tt>, or
1021 >     *         <tt>null</tt> if there was no mapping for <tt>key</tt>
1022 >     * @throws NullPointerException if the specified key or value is null
1023       */
1024 +    @SuppressWarnings("unchecked")
1025      public V put(K key, V value) {
1026 +        Segment<K,V> s;
1027          if (value == null)
1028              throw new NullPointerException();
1029 <        int hash = hash(key);
1030 <        return segmentFor(hash).put(key, hash, value, false);
1029 >        int hash = hash(key.hashCode());
1030 >        int j = (hash >>> segmentShift) & segmentMask;
1031 >        if ((s = (Segment<K,V>)UNSAFE.getObject          // nonvolatile; recheck
1032 >             (segments, (j << SSHIFT) + SBASE)) == null) //  in ensureSegment
1033 >            s = ensureSegment(j);
1034 >        return s.put(key, hash, value, false);
1035      }
1036  
1037      /**
1038 <     * If the specified key is not already associated
1039 <     * with a value, associate it with the given value.
1040 <     * This is equivalent to
1041 <     * <pre>
1042 <     *   if (!map.containsKey(key))
887 <     *      return map.put(key, value);
888 <     *   else
889 <     *      return map.get(key);
890 <     * </pre>
891 <     * Except that the action is performed atomically.
892 <     * @param key key with which the specified value is to be associated.
893 <     * @param value value to be associated with the specified key.
894 <     * @return previous value associated with specified key, or <tt>null</tt>
895 <     *         if there was no mapping for key.
896 <     * @throws NullPointerException if the specified key or value is
897 <     *            <tt>null</tt>.
1038 >     * {@inheritDoc}
1039 >     *
1040 >     * @return the previous value associated with the specified key,
1041 >     *         or <tt>null</tt> if there was no mapping for the key
1042 >     * @throws NullPointerException if the specified key or value is null
1043       */
1044 +    @SuppressWarnings("unchecked")
1045      public V putIfAbsent(K key, V value) {
1046 +        Segment<K,V> s;
1047          if (value == null)
1048              throw new NullPointerException();
1049 <        int hash = hash(key);
1050 <        return segmentFor(hash).put(key, hash, value, true);
1049 >        int hash = hash(key.hashCode());
1050 >        int j = (hash >>> segmentShift) & segmentMask;
1051 >        if ((s = (Segment<K,V>)UNSAFE.getObject
1052 >             (segments, (j << SSHIFT) + SBASE)) == null)
1053 >            s = ensureSegment(j);
1054 >        return s.put(key, hash, value, true);
1055      }
1056  
906
1057      /**
1058       * Copies all of the mappings from the specified map to this one.
909     *
1059       * These mappings replace any mappings that this map had for any of the
1060 <     * keys currently in the specified Map.
1060 >     * keys currently in the specified map.
1061       *
1062 <     * @param t Mappings to be stored in this map.
1062 >     * @param m mappings to be stored in this map
1063       */
1064 <    public void putAll(Map<? extends K, ? extends V> t) {
1065 <        for (Iterator<? extends Map.Entry<? extends K, ? extends V>> it = (Iterator<? extends Map.Entry<? extends K, ? extends V>>) t.entrySet().iterator(); it.hasNext(); ) {
917 <            Entry<? extends K, ? extends V> e = it.next();
1064 >    public void putAll(Map<? extends K, ? extends V> m) {
1065 >        for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
1066              put(e.getKey(), e.getValue());
919        }
1067      }
1068  
1069      /**
1070 <     * Removes the key (and its corresponding value) from this
1071 <     * table. This method does nothing if the key is not in the table.
1070 >     * Removes the key (and its corresponding value) from this map.
1071 >     * This method does nothing if the key is not in the map.
1072       *
1073 <     * @param   key   the key that needs to be removed.
1074 <     * @return  the value to which the key had been mapped in this table,
1075 <     *          or <tt>null</tt> if the key did not have a mapping.
1076 <     * @throws  NullPointerException  if the key is
930 <     *               <tt>null</tt>.
1073 >     * @param  key the key that needs to be removed
1074 >     * @return the previous value associated with <tt>key</tt>, or
1075 >     *         <tt>null</tt> if there was no mapping for <tt>key</tt>
1076 >     * @throws NullPointerException if the specified key is null
1077       */
1078      public V remove(Object key) {
1079 <        int hash = hash(key);
1080 <        return segmentFor(hash).remove(key, hash, null);
1079 >        int hash = hash(key.hashCode());
1080 >        Segment<K,V> s = segmentForHash(hash);
1081 >        return s == null ? null : s.remove(key, hash, null);
1082      }
1083  
1084      /**
1085 <     * Remove entry for key only if currently mapped to given value.
1086 <     * Acts as
1087 <     * <pre>
941 <     *  if (map.get(key).equals(value)) {
942 <     *     map.remove(key);
943 <     *     return true;
944 <     * } else return false;
945 <     * </pre>
946 <     * except that the action is performed atomically.
947 <     * @param key key with which the specified value is associated.
948 <     * @param value value associated with the specified key.
949 <     * @return true if the value was removed
950 <     * @throws NullPointerException if the specified key is
951 <     *            <tt>null</tt>.
1085 >     * {@inheritDoc}
1086 >     *
1087 >     * @throws NullPointerException if the specified key is null
1088       */
1089      public boolean remove(Object key, Object value) {
1090 <        int hash = hash(key);
1091 <        return segmentFor(hash).remove(key, hash, value) != null;
1090 >        int hash = hash(key.hashCode());
1091 >        Segment<K,V> s;
1092 >        return value != null && (s = segmentForHash(hash)) != null &&
1093 >            s.remove(key, hash, value) != null;
1094      }
1095  
958
1096      /**
1097 <     * Replace entry for key only if currently mapped to given value.
1098 <     * Acts as
1099 <     * <pre>
963 <     *  if (map.get(key).equals(oldValue)) {
964 <     *     map.put(key, newValue);
965 <     *     return true;
966 <     * } else return false;
967 <     * </pre>
968 <     * except that the action is performed atomically.
969 <     * @param key key with which the specified value is associated.
970 <     * @param oldValue value expected to be associated with the specified key.
971 <     * @param newValue value to be associated with the specified key.
972 <     * @return true if the value was replaced
973 <     * @throws NullPointerException if the specified key or values are
974 <     * <tt>null</tt>.
1097 >     * {@inheritDoc}
1098 >     *
1099 >     * @throws NullPointerException if any of the arguments are null
1100       */
1101      public boolean replace(K key, V oldValue, V newValue) {
1102 +        int hash = hash(key.hashCode());
1103          if (oldValue == null || newValue == null)
1104              throw new NullPointerException();
1105 <        int hash = hash(key);
1106 <        return segmentFor(hash).replace(key, hash, oldValue, newValue);
1105 >        Segment<K,V> s = segmentForHash(hash);
1106 >        return s != null && s.replace(key, hash, oldValue, newValue);
1107      }
1108  
1109      /**
1110 <     * Replace entry for key only if currently mapped to some value.
1111 <     * Acts as
1112 <     * <pre>
1113 <     *  if ((map.containsKey(key)) {
1114 <     *     return map.put(key, value);
989 <     * } else return null;
990 <     * </pre>
991 <     * except that the action is performed atomically.
992 <     * @param key key with which the specified value is associated.
993 <     * @param value value to be associated with the specified key.
994 <     * @return previous value associated with specified key, or <tt>null</tt>
995 <     *         if there was no mapping for key.  
996 <     * @throws NullPointerException if the specified key or value is
997 <     *            <tt>null</tt>.
1110 >     * {@inheritDoc}
1111 >     *
1112 >     * @return the previous value associated with the specified key,
1113 >     *         or <tt>null</tt> if there was no mapping for the key
1114 >     * @throws NullPointerException if the specified key or value is null
1115       */
1116      public V replace(K key, V value) {
1117 +        int hash = hash(key.hashCode());
1118          if (value == null)
1119              throw new NullPointerException();
1120 <        int hash = hash(key);
1121 <        return segmentFor(hash).replace(key, hash, value);
1120 >        Segment<K,V> s = segmentForHash(hash);
1121 >        return s == null ? null : s.replace(key, hash, value);
1122      }
1123  
1006
1124      /**
1125 <     * Removes all mappings from this map.
1125 >     * Removes all of the mappings from this map.
1126       */
1127      public void clear() {
1128 <        for (int i = 0; i < segments.length; ++i)
1129 <            segments[i].clear();
1128 >        final Segment<K,V>[] segments = this.segments;
1129 >        for (int j = 0; j < segments.length; ++j) {
1130 >            Segment<K,V> s = segmentAt(segments, j);
1131 >            if (s != null)
1132 >                s.clear();
1133 >        }
1134      }
1135  
1136      /**
1137 <     * Returns a set view of the keys contained in this map.  The set is
1138 <     * backed by the map, so changes to the map are reflected in the set, and
1139 <     * vice-versa.  The set supports element removal, which removes the
1140 <     * corresponding mapping from this map, via the <tt>Iterator.remove</tt>,
1141 <     * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt>, and
1142 <     * <tt>clear</tt> operations.  It does not support the <tt>add</tt> or
1137 >     * Returns a {@link Set} view of the keys contained in this map.
1138 >     * The set is backed by the map, so changes to the map are
1139 >     * reflected in the set, and vice-versa.  The set supports element
1140 >     * removal, which removes the corresponding mapping from this map,
1141 >     * via the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
1142 >     * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
1143 >     * operations.  It does not support the <tt>add</tt> or
1144       * <tt>addAll</tt> operations.
1145 <     * The view's returned <tt>iterator</tt> is a "weakly consistent" iterator that
1146 <     * will never throw {@link java.util.ConcurrentModificationException},
1145 >     *
1146 >     * <p>The view's <tt>iterator</tt> is a "weakly consistent" iterator
1147 >     * that will never throw {@link ConcurrentModificationException},
1148       * and guarantees to traverse elements as they existed upon
1149       * construction of the iterator, and may (but is not guaranteed to)
1150       * reflect any modifications subsequent to construction.
1028     *
1029     * @return a set view of the keys contained in this map.
1151       */
1152      public Set<K> keySet() {
1153          Set<K> ks = keySet;
1154          return (ks != null) ? ks : (keySet = new KeySet());
1155      }
1156  
1036
1157      /**
1158 <     * Returns a collection view of the values contained in this map.  The
1159 <     * collection is backed by the map, so changes to the map are reflected in
1160 <     * the collection, and vice-versa.  The collection supports element
1161 <     * removal, which removes the corresponding mapping from this map, via the
1162 <     * <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>,
1163 <     * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> operations.
1164 <     * It does not support the <tt>add</tt> or <tt>addAll</tt> operations.
1165 <     * The view's returned <tt>iterator</tt> is a "weakly consistent" iterator that
1166 <     * will never throw {@link java.util.ConcurrentModificationException},
1158 >     * Returns a {@link Collection} view of the values contained in this map.
1159 >     * The collection is backed by the map, so changes to the map are
1160 >     * reflected in the collection, and vice-versa.  The collection
1161 >     * supports element removal, which removes the corresponding
1162 >     * mapping from this map, via the <tt>Iterator.remove</tt>,
1163 >     * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
1164 >     * <tt>retainAll</tt>, and <tt>clear</tt> operations.  It does not
1165 >     * support the <tt>add</tt> or <tt>addAll</tt> operations.
1166 >     *
1167 >     * <p>The view's <tt>iterator</tt> is a "weakly consistent" iterator
1168 >     * that will never throw {@link ConcurrentModificationException},
1169       * and guarantees to traverse elements as they existed upon
1170       * construction of the iterator, and may (but is not guaranteed to)
1171       * reflect any modifications subsequent to construction.
1050     *
1051     * @return a collection view of the values contained in this map.
1172       */
1173      public Collection<V> values() {
1174          Collection<V> vs = values;
1175          return (vs != null) ? vs : (values = new Values());
1176      }
1177  
1058
1178      /**
1179 <     * Returns a collection view of the mappings contained in this map.  Each
1180 <     * element in the returned collection is a <tt>Map.Entry</tt>.  The
1181 <     * collection is backed by the map, so changes to the map are reflected in
1182 <     * the collection, and vice-versa.  The collection supports element
1183 <     * removal, which removes the corresponding mapping from the map, via the
1184 <     * <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>,
1185 <     * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> operations.
1186 <     * It does not support the <tt>add</tt> or <tt>addAll</tt> operations.
1187 <     * The view's returned <tt>iterator</tt> is a "weakly consistent" iterator that
1188 <     * will never throw {@link java.util.ConcurrentModificationException},
1179 >     * Returns a {@link Set} view of the mappings contained in this map.
1180 >     * The set is backed by the map, so changes to the map are
1181 >     * reflected in the set, and vice-versa.  The set supports element
1182 >     * removal, which removes the corresponding mapping from the map,
1183 >     * via the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
1184 >     * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
1185 >     * operations.  It does not support the <tt>add</tt> or
1186 >     * <tt>addAll</tt> operations.
1187 >     *
1188 >     * <p>The view's <tt>iterator</tt> is a "weakly consistent" iterator
1189 >     * that will never throw {@link ConcurrentModificationException},
1190       * and guarantees to traverse elements as they existed upon
1191       * construction of the iterator, and may (but is not guaranteed to)
1192       * reflect any modifications subsequent to construction.
1073     *
1074     * @return a collection view of the mappings contained in this map.
1193       */
1194      public Set<Map.Entry<K,V>> entrySet() {
1195          Set<Map.Entry<K,V>> es = entrySet;
1196 <        return (es != null) ? es : (entrySet = (Set<Map.Entry<K,V>>) (Set) new EntrySet());
1196 >        return (es != null) ? es : (entrySet = new EntrySet());
1197      }
1198  
1081
1199      /**
1200       * Returns an enumeration of the keys in this table.
1201       *
1202 <     * @return  an enumeration of the keys in this table.
1203 <     * @see     #keySet
1202 >     * @return an enumeration of the keys in this table
1203 >     * @see #keySet()
1204       */
1205      public Enumeration<K> keys() {
1206          return new KeyIterator();
# Line 1092 | Line 1209 | public class ConcurrentHashMap<K, V> ext
1209      /**
1210       * Returns an enumeration of the values in this table.
1211       *
1212 <     * @return  an enumeration of the values in this table.
1213 <     * @see     #values
1212 >     * @return an enumeration of the values in this table
1213 >     * @see #values()
1214       */
1215      public Enumeration<V> elements() {
1216          return new ValueIterator();
# Line 1104 | Line 1221 | public class ConcurrentHashMap<K, V> ext
1221      abstract class HashIterator {
1222          int nextSegmentIndex;
1223          int nextTableIndex;
1224 <        HashEntry[] currentTable;
1224 >        HashEntry<K,V>[] currentTable;
1225          HashEntry<K, V> nextEntry;
1226          HashEntry<K, V> lastReturned;
1227  
# Line 1114 | Line 1231 | public class ConcurrentHashMap<K, V> ext
1231              advance();
1232          }
1233  
1234 <        public boolean hasMoreElements() { return hasNext(); }
1235 <
1234 >        /**
1235 >         * Sets nextEntry to first node of next non-empty table
1236 >         * (in backwards order, to simplify checks).
1237 >         */
1238          final void advance() {
1239 <            if (nextEntry != null && (nextEntry = nextEntry.next) != null)
1240 <                return;
1241 <
1242 <            while (nextTableIndex >= 0) {
1243 <                if ( (nextEntry = (HashEntry<K,V>)currentTable[nextTableIndex--]) != null)
1244 <                    return;
1245 <            }
1246 <
1247 <            while (nextSegmentIndex >= 0) {
1248 <                Segment<K,V> seg = (Segment<K,V>)segments[nextSegmentIndex--];
1130 <                if (seg.count != 0) {
1131 <                    currentTable = seg.table;
1132 <                    for (int j = currentTable.length - 1; j >= 0; --j) {
1133 <                        if ( (nextEntry = (HashEntry<K,V>)currentTable[j]) != null) {
1134 <                            nextTableIndex = j - 1;
1135 <                            return;
1136 <                        }
1137 <                    }
1239 >            for (;;) {
1240 >                if (nextTableIndex >= 0) {
1241 >                    if ((nextEntry = entryAt(currentTable,
1242 >                                             nextTableIndex--)) != null)
1243 >                        break;
1244 >                }
1245 >                else if (nextSegmentIndex >= 0) {
1246 >                    Segment<K,V> seg = segmentAt(segments, nextSegmentIndex--);
1247 >                    if (seg != null && (currentTable = seg.table) != null)
1248 >                        nextTableIndex = currentTable.length - 1;
1249                  }
1250 +                else
1251 +                    break;
1252              }
1253          }
1254  
1255 <        public boolean hasNext() { return nextEntry != null; }
1256 <
1257 <        HashEntry<K,V> nextEntry() {
1145 <            if (nextEntry == null)
1255 >        final HashEntry<K,V> nextEntry() {
1256 >            HashEntry<K,V> e = nextEntry;
1257 >            if (e == null)
1258                  throw new NoSuchElementException();
1259 <            lastReturned = nextEntry;
1260 <            advance();
1261 <            return lastReturned;
1259 >            lastReturned = e; // cannot assign until after null check
1260 >            if ((nextEntry = e.next) == null)
1261 >                advance();
1262 >            return e;
1263          }
1264  
1265 <        public void remove() {
1265 >        public final boolean hasNext() { return nextEntry != null; }
1266 >        public final boolean hasMoreElements() { return nextEntry != null; }
1267 >
1268 >        public final void remove() {
1269              if (lastReturned == null)
1270                  throw new IllegalStateException();
1271              ConcurrentHashMap.this.remove(lastReturned.key);
# Line 1157 | Line 1273 | public class ConcurrentHashMap<K, V> ext
1273          }
1274      }
1275  
1276 <    final class KeyIterator extends HashIterator implements Iterator<K>, Enumeration<K> {
1277 <        public K next() { return super.nextEntry().key; }
1278 <        public K nextElement() { return super.nextEntry().key; }
1279 <    }
1280 <
1281 <    final class ValueIterator extends HashIterator implements Iterator<V>, Enumeration<V> {
1282 <        public V next() { return super.nextEntry().value; }
1283 <        public V nextElement() { return super.nextEntry().value; }
1276 >    final class KeyIterator
1277 >        extends HashIterator
1278 >        implements Iterator<K>, Enumeration<K>
1279 >    {
1280 >        public final K next()        { return super.nextEntry().key; }
1281 >        public final K nextElement() { return super.nextEntry().key; }
1282 >    }
1283 >
1284 >    final class ValueIterator
1285 >        extends HashIterator
1286 >        implements Iterator<V>, Enumeration<V>
1287 >    {
1288 >        public final V next()        { return super.nextEntry().value; }
1289 >        public final V nextElement() { return super.nextEntry().value; }
1290      }
1291  
1170    
1171
1292      /**
1293 <     * Entry iterator. Exported Entry objects must write-through
1294 <     * changes in setValue, even if the nodes have been cloned. So we
1295 <     * cannot return internal HashEntry objects. Instead, the iterator
1296 <     * itself acts as a forwarding pseudo-entry.
1297 <     */
1298 <    final class EntryIterator extends HashIterator implements Map.Entry<K,V>, Iterator<Entry<K,V>> {
1299 <        public Map.Entry<K,V> next() {
1300 <            nextEntry();
1181 <            return this;
1182 <        }
1183 <
1184 <        public K getKey() {
1185 <            if (lastReturned == null)
1186 <                throw new IllegalStateException("Entry was removed");
1187 <            return lastReturned.key;
1188 <        }
1189 <
1190 <        public V getValue() {
1191 <            if (lastReturned == null)
1192 <                throw new IllegalStateException("Entry was removed");
1193 <            return ConcurrentHashMap.this.get(lastReturned.key);
1293 >     * Custom Entry class used by EntryIterator.next(), that relays
1294 >     * setValue changes to the underlying map.
1295 >     */
1296 >    final class WriteThroughEntry
1297 >        extends AbstractMap.SimpleEntry<K,V>
1298 >    {
1299 >        WriteThroughEntry(K k, V v) {
1300 >            super(k,v);
1301          }
1302  
1303 +        /**
1304 +         * Sets our entry's value and writes through to the map. The
1305 +         * value to return is somewhat arbitrary here. Since a
1306 +         * WriteThroughEntry does not necessarily track asynchronous
1307 +         * changes, the most recent "previous" value could be
1308 +         * different from what we return (or could even have been
1309 +         * removed in which case the put will re-establish). We do not
1310 +         * and cannot guarantee more.
1311 +         */
1312          public V setValue(V value) {
1313 <            if (lastReturned == null)
1314 <                throw new IllegalStateException("Entry was removed");
1315 <            return ConcurrentHashMap.this.put(lastReturned.key, value);
1316 <        }
1201 <
1202 <        public boolean equals(Object o) {
1203 <            // If not acting as entry, just use default.
1204 <            if (lastReturned == null)
1205 <                return super.equals(o);
1206 <            if (!(o instanceof Map.Entry))
1207 <                return false;
1208 <            Map.Entry e = (Map.Entry)o;
1209 <            return eq(getKey(), e.getKey()) && eq(getValue(), e.getValue());
1210 <        }
1211 <
1212 <        public int hashCode() {
1213 <            // If not acting as entry, just use default.
1214 <            if (lastReturned == null)
1215 <                return super.hashCode();
1216 <
1217 <            Object k = getKey();
1218 <            Object v = getValue();
1219 <            return ((k == null) ? 0 : k.hashCode()) ^
1220 <                   ((v == null) ? 0 : v.hashCode());
1221 <        }
1222 <
1223 <        public String toString() {
1224 <            // If not acting as entry, just use default.
1225 <            if (lastReturned == null)
1226 <                return super.toString();
1227 <            else
1228 <                return getKey() + "=" + getValue();
1313 >            if (value == null) throw new NullPointerException();
1314 >            V v = super.setValue(value);
1315 >            ConcurrentHashMap.this.put(getKey(), value);
1316 >            return v;
1317          }
1318 +    }
1319  
1320 <        boolean eq(Object o1, Object o2) {
1321 <            return (o1 == null ? o2 == null : o1.equals(o2));
1320 >    final class EntryIterator
1321 >        extends HashIterator
1322 >        implements Iterator<Entry<K,V>>
1323 >    {
1324 >        public Map.Entry<K,V> next() {
1325 >            HashEntry<K,V> e = super.nextEntry();
1326 >            return new WriteThroughEntry(e.key, e.value);
1327          }
1234
1328      }
1329  
1330      final class KeySet extends AbstractSet<K> {
# Line 1241 | Line 1334 | public class ConcurrentHashMap<K, V> ext
1334          public int size() {
1335              return ConcurrentHashMap.this.size();
1336          }
1337 +        public boolean isEmpty() {
1338 +            return ConcurrentHashMap.this.isEmpty();
1339 +        }
1340          public boolean contains(Object o) {
1341              return ConcurrentHashMap.this.containsKey(o);
1342          }
# Line 1250 | Line 1346 | public class ConcurrentHashMap<K, V> ext
1346          public void clear() {
1347              ConcurrentHashMap.this.clear();
1348          }
1253        public Object[] toArray() {
1254            Collection<K> c = new ArrayList<K>();
1255            for (Iterator<K> i = iterator(); i.hasNext(); )
1256                c.add(i.next());
1257            return c.toArray();
1258        }
1259        public <T> T[] toArray(T[] a) {
1260            Collection<K> c = new ArrayList<K>();
1261            for (Iterator<K> i = iterator(); i.hasNext(); )
1262                c.add(i.next());
1263            return c.toArray(a);
1264        }
1349      }
1350  
1351      final class Values extends AbstractCollection<V> {
# Line 1271 | Line 1355 | public class ConcurrentHashMap<K, V> ext
1355          public int size() {
1356              return ConcurrentHashMap.this.size();
1357          }
1358 +        public boolean isEmpty() {
1359 +            return ConcurrentHashMap.this.isEmpty();
1360 +        }
1361          public boolean contains(Object o) {
1362              return ConcurrentHashMap.this.containsValue(o);
1363          }
1364          public void clear() {
1365              ConcurrentHashMap.this.clear();
1366          }
1280        public Object[] toArray() {
1281            Collection<V> c = new ArrayList<V>();
1282            for (Iterator<V> i = iterator(); i.hasNext(); )
1283                c.add(i.next());
1284            return c.toArray();
1285        }
1286        public <T> T[] toArray(T[] a) {
1287            Collection<V> c = new ArrayList<V>();
1288            for (Iterator<V> i = iterator(); i.hasNext(); )
1289                c.add(i.next());
1290            return c.toArray(a);
1291        }
1367      }
1368  
1369      final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
# Line 1298 | Line 1373 | public class ConcurrentHashMap<K, V> ext
1373          public boolean contains(Object o) {
1374              if (!(o instanceof Map.Entry))
1375                  return false;
1376 <            Map.Entry<K,V> e = (Map.Entry<K,V>)o;
1376 >            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
1377              V v = ConcurrentHashMap.this.get(e.getKey());
1378              return v != null && v.equals(e.getValue());
1379          }
1380          public boolean remove(Object o) {
1381              if (!(o instanceof Map.Entry))
1382                  return false;
1383 <            Map.Entry<K,V> e = (Map.Entry<K,V>)o;
1383 >            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
1384              return ConcurrentHashMap.this.remove(e.getKey(), e.getValue());
1385          }
1386          public int size() {
1387              return ConcurrentHashMap.this.size();
1388          }
1389 +        public boolean isEmpty() {
1390 +            return ConcurrentHashMap.this.isEmpty();
1391 +        }
1392          public void clear() {
1393              ConcurrentHashMap.this.clear();
1394          }
1317        public Object[] toArray() {
1318            // Since we don't ordinarily have distinct Entry objects, we
1319            // must pack elements using exportable SimpleEntry
1320            Collection<Map.Entry<K,V>> c = new ArrayList<Map.Entry<K,V>>(size());
1321            for (Iterator<Map.Entry<K,V>> i = iterator(); i.hasNext(); )
1322                c.add(new AbstractMap.SimpleEntry<K,V>(i.next()));
1323            return c.toArray();
1324        }
1325        public <T> T[] toArray(T[] a) {
1326            Collection<Map.Entry<K,V>> c = new ArrayList<Map.Entry<K,V>>(size());
1327            for (Iterator<Map.Entry<K,V>> i = iterator(); i.hasNext(); )
1328                c.add(new AbstractMap.SimpleEntry<K,V>(i.next()));
1329            return c.toArray(a);
1330        }
1331
1395      }
1396  
1397      /* ---------------- Serialization Support -------------- */
1398  
1399      /**
1400 <     * Save the state of the <tt>ConcurrentHashMap</tt>
1401 <     * instance to a stream (i.e.,
1339 <     * serialize it).
1400 >     * Saves the state of the <tt>ConcurrentHashMap</tt> instance to a
1401 >     * stream (i.e., serializes it).
1402       * @param s the stream
1403       * @serialData
1404       * the key (Object) and value (Object)
1405       * for each key-value mapping, followed by a null pair.
1406       * The key-value mappings are emitted in no particular order.
1407       */
1408 <    private void writeObject(java.io.ObjectOutputStream s) throws IOException  {
1408 >    private void writeObject(java.io.ObjectOutputStream s) throws IOException {
1409 >        // force all segments for serialization compatibility
1410 >        for (int k = 0; k < segments.length; ++k)
1411 >            ensureSegment(k);
1412          s.defaultWriteObject();
1413  
1414 +        final Segment<K,V>[] segments = this.segments;
1415          for (int k = 0; k < segments.length; ++k) {
1416 <            Segment<K,V> seg = (Segment<K,V>)segments[k];
1416 >            Segment<K,V> seg = segmentAt(segments, k);
1417              seg.lock();
1418              try {
1419 <                HashEntry[] tab = seg.table;
1419 >                HashEntry<K,V>[] tab = seg.table;
1420                  for (int i = 0; i < tab.length; ++i) {
1421 <                    for (HashEntry<K,V> e = (HashEntry<K,V>)tab[i]; e != null; e = e.next) {
1421 >                    HashEntry<K,V> e;
1422 >                    for (e = entryAt(tab, i); e != null; e = e.next) {
1423                          s.writeObject(e.key);
1424                          s.writeObject(e.value);
1425                      }
# Line 1366 | Line 1433 | public class ConcurrentHashMap<K, V> ext
1433      }
1434  
1435      /**
1436 <     * Reconstitute the <tt>ConcurrentHashMap</tt>
1437 <     * instance from a stream (i.e.,
1371 <     * deserialize it).
1436 >     * Reconstitutes the <tt>ConcurrentHashMap</tt> instance from a
1437 >     * stream (i.e., deserializes it).
1438       * @param s the stream
1439       */
1440 +    @SuppressWarnings("unchecked")
1441      private void readObject(java.io.ObjectInputStream s)
1442 <        throws IOException, ClassNotFoundException  {
1442 >        throws IOException, ClassNotFoundException {
1443          s.defaultReadObject();
1444  
1445 <        // Initialize each segment to be minimally sized, and let grow.
1446 <        for (int i = 0; i < segments.length; ++i) {
1447 <            segments[i].setTable(new HashEntry[1]);
1445 >        // Re-initialize segments to be minimally sized, and let grow.
1446 >        int cap = MIN_SEGMENT_TABLE_CAPACITY;
1447 >        final Segment<K,V>[] segments = this.segments;
1448 >        for (int k = 0; k < segments.length; ++k) {
1449 >            Segment<K,V> seg = segments[k];
1450 >            if (seg != null) {
1451 >                seg.threshold = (int)(cap * seg.loadFactor);
1452 >                seg.table = (HashEntry<K,V>[]) new HashEntry[cap];
1453 >            }
1454          }
1455  
1456          // Read the keys and values, and put the mappings in the table
# Line 1389 | Line 1462 | public class ConcurrentHashMap<K, V> ext
1462              put(key, value);
1463          }
1464      }
1392 }
1465  
1466 +    // Unsafe mechanics
1467 +    private static final sun.misc.Unsafe UNSAFE;
1468 +    private static final long SBASE;
1469 +    private static final int SSHIFT;
1470 +    private static final long TBASE;
1471 +    private static final int TSHIFT;
1472 +
1473 +    static {
1474 +        int ss, ts;
1475 +        try {
1476 +            UNSAFE = sun.misc.Unsafe.getUnsafe();
1477 +            Class tc = HashEntry[].class;
1478 +            Class sc = Segment[].class;
1479 +            TBASE = UNSAFE.arrayBaseOffset(tc);
1480 +            SBASE = UNSAFE.arrayBaseOffset(sc);
1481 +            ts = UNSAFE.arrayIndexScale(tc);
1482 +            ss = UNSAFE.arrayIndexScale(sc);
1483 +        } catch (Exception e) {
1484 +            throw new Error(e);
1485 +        }
1486 +        if ((ss & (ss-1)) != 0 || (ts & (ts-1)) != 0)
1487 +            throw new Error("data type scale not a power of two");
1488 +        SSHIFT = 31 - Integer.numberOfLeadingZeros(ss);
1489 +        TSHIFT = 31 - Integer.numberOfLeadingZeros(ts);
1490 +    }
1491 +
1492 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines