195 |
|
* timed tryAcquire times out |
196 |
|
*/ |
197 |
|
public void testTryAcquire_timeout() { |
198 |
< |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
199 |
< |
final boolean fair = rnd.nextBoolean(); |
198 |
> |
final boolean fair = ThreadLocalRandom.current().nextBoolean(); |
199 |
|
final Semaphore s = new Semaphore(0, fair); |
200 |
|
final long startTime = System.nanoTime(); |
201 |
|
try { assertFalse(s.tryAcquire(timeoutMillis(), MILLISECONDS)); } |
207 |
|
* timed tryAcquire(N) times out |
208 |
|
*/ |
209 |
|
public void testTryAcquireN_timeout() { |
210 |
< |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
212 |
< |
final boolean fair = rnd.nextBoolean(); |
210 |
> |
final boolean fair = ThreadLocalRandom.current().nextBoolean(); |
211 |
|
final Semaphore s = new Semaphore(2, fair); |
212 |
|
final long startTime = System.nanoTime(); |
213 |
|
try { assertFalse(s.tryAcquire(3, timeoutMillis(), MILLISECONDS)); } |
271 |
|
assertThreadBlocks(t, acquirer.parkedState()); |
272 |
|
t.interrupt(); |
273 |
|
} |
274 |
< |
|
274 |
> |
|
275 |
|
awaitTermination(t); |
276 |
|
} |
277 |
|
|
608 |
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
609 |
|
public void realRun() throws InterruptedException { |
610 |
|
// Will fail, even though 1 permit is available |
611 |
< |
assertFalse(s.tryAcquire(0L, MILLISECONDS)); |
612 |
< |
assertFalse(s.tryAcquire(1, 0L, MILLISECONDS)); |
611 |
> |
assertFalse( |
612 |
> |
s.tryAcquire(randomExpiredTimeout(), randomTimeUnit())); |
613 |
> |
assertFalse( |
614 |
> |
s.tryAcquire(1, randomExpiredTimeout(), randomTimeUnit())); |
615 |
|
|
616 |
|
// untimed tryAcquire will barge and succeed |
617 |
|
assertTrue(s.tryAcquire()); |