--- jsr166/src/jsr166e/ConcurrentHashMapV8.java 2011/12/05 04:50:19 1.33 +++ jsr166/src/jsr166e/ConcurrentHashMapV8.java 2012/03/04 20:34:27 1.37 @@ -20,6 +20,7 @@ import java.util.Enumeration; import java.util.ConcurrentModificationException; import java.util.NoSuchElementException; import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.locks.LockSupport; import java.io.Serializable; @@ -351,7 +352,7 @@ public class ConcurrentHashMapV8 * Encodings for special uses of Node hash fields. See above for * explanation. */ - static final int MOVED = 0x80000000; // hash field for fowarding nodes + static final int MOVED = 0x80000000; // hash field for forwarding nodes static final int LOCKED = 0x40000000; // set/tested only as a bit static final int WAITING = 0xc0000000; // both bits set/tested together static final int HASH_BITS = 0x3fffffff; // usable bits of normal node hash @@ -435,11 +436,13 @@ public class ConcurrentHashMapV8 */ final void tryAwaitLock(Node[] tab, int i) { if (tab != null && i >= 0 && i < tab.length) { // bounds check + int r = ThreadLocalRandom.current().nextInt(); // randomize spins int spins = MAX_SPINS, h; while (tabAt(tab, i) == this && ((h = hash) & LOCKED) != 0) { if (spins >= 0) { - if (--spins == MAX_SPINS >>> 1) - Thread.yield(); // heuristically yield mid-way + r ^= r << 1; r ^= r >>> 3; r ^= r << 10; // xorshift + if (r >= 0 && --spins == 0) + Thread.yield(); // yield before block } else if (casHash(h, h | WAITING)) { synchronized (this) { @@ -1291,7 +1294,7 @@ public class ConcurrentHashMapV8 * * At each step, the iterator snapshots the key ("nextKey") and * value ("nextVal") of a valid node (i.e., one that, at point of - * snapshot, has a nonnull user value). Because val fields can + * snapshot, has a non-null user value). Because val fields can * change (including to null, indicating deletion), field nextVal * might not be accurate at point of use, but still maintains the * weak consistency property of holding a value that was once @@ -2237,7 +2240,7 @@ public class ConcurrentHashMapV8 } static final class Values extends MapView - implements Collection { + implements Collection { Values(ConcurrentHashMapV8 map) { super(map); } public final boolean contains(Object o) { return map.containsValue(o); }