12 |
|
import java.util.Arrays; |
13 |
|
import java.util.Collection; |
14 |
|
import java.util.HashSet; |
15 |
– |
import java.util.concurrent.ThreadLocalRandom; |
15 |
|
import java.util.concurrent.locks.AbstractQueuedLongSynchronizer; |
16 |
|
import java.util.concurrent.locks.AbstractQueuedLongSynchronizer.ConditionObject; |
17 |
|
|
19 |
– |
import junit.framework.AssertionFailedError; |
18 |
|
import junit.framework.Test; |
19 |
|
import junit.framework.TestSuite; |
20 |
|
|
139 |
|
long startTime = System.nanoTime(); |
140 |
|
while (!sync.isQueued(t)) { |
141 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
142 |
< |
throw new AssertionFailedError("timed out"); |
142 |
> |
throw new AssertionError("timed out"); |
143 |
|
Thread.yield(); |
144 |
|
} |
145 |
|
assertTrue(t.isAlive()); |
223 |
|
assertTrue(c.await(timeoutMillis, MILLISECONDS)); |
224 |
|
break; |
225 |
|
case awaitNanos: |
226 |
< |
long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis); |
227 |
< |
long nanosRemaining = c.awaitNanos(nanosTimeout); |
226 |
> |
long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); |
227 |
> |
long nanosRemaining = c.awaitNanos(timeoutNanos); |
228 |
|
assertTrue(nanosRemaining > 0); |
229 |
|
break; |
230 |
|
case awaitUntil: |
251 |
|
break; |
252 |
|
case awaitNanos: |
253 |
|
startTime = System.nanoTime(); |
254 |
< |
long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis); |
255 |
< |
long nanosRemaining = c.awaitNanos(nanosTimeout); |
254 |
> |
long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); |
255 |
> |
long nanosRemaining = c.awaitNanos(timeoutNanos); |
256 |
|
assertTrue(nanosRemaining <= 0); |
257 |
|
assertTrue(nanosRemaining > -MILLISECONDS.toNanos(LONG_DELAY_MS)); |
258 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
1264 |
|
|
1265 |
|
// A synchronizer only offering a choice of failure modes |
1266 |
|
class Sync extends AbstractQueuedLongSynchronizer { |
1267 |
< |
boolean pleaseThrow; |
1267 |
> |
volatile boolean pleaseThrow; |
1268 |
|
@Override protected boolean tryAcquire(long ignored) { |
1269 |
|
if (pleaseThrow) throw ex; |
1270 |
|
return false; |
1286 |
|
final Thread thread = newStartedThread(new CheckedRunnable() { |
1287 |
|
public void realRun() { |
1288 |
|
try { |
1289 |
< |
if (ThreadLocalRandom.current().nextBoolean()) |
1289 |
> |
if (randomBoolean()) |
1290 |
|
s.acquire(1); |
1291 |
|
else |
1292 |
|
s.acquireShared(1); |