20 |
|
import java.util.ConcurrentModificationException; |
21 |
|
import java.util.NoSuchElementException; |
22 |
|
import java.util.concurrent.ConcurrentMap; |
23 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
24 |
|
import java.util.concurrent.locks.LockSupport; |
25 |
|
import java.io.Serializable; |
26 |
|
|
436 |
|
*/ |
437 |
|
final void tryAwaitLock(Node[] tab, int i) { |
438 |
|
if (tab != null && i >= 0 && i < tab.length) { // bounds check |
439 |
+ |
int r = ThreadLocalRandom.current().nextInt(); // randomize spins |
440 |
|
int spins = MAX_SPINS, h; |
441 |
|
while (tabAt(tab, i) == this && ((h = hash) & LOCKED) != 0) { |
442 |
|
if (spins >= 0) { |
443 |
< |
if (--spins == MAX_SPINS >>> 1) |
444 |
< |
Thread.yield(); // heuristically yield mid-way |
443 |
> |
r ^= r << 1; r ^= r >>> 3; r ^= r << 10; // xorshift |
444 |
> |
if (r >= 0 && --spins == 0) |
445 |
> |
Thread.yield(); // yield before block |
446 |
|
} |
447 |
|
else if (casHash(h, h | WAITING)) { |
448 |
|
synchronized (this) { |