365 |
|
*/ |
366 |
|
public void testTimedOffer() throws InterruptedException { |
367 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
368 |
< |
Thread t = new Thread(new CheckedRunnable() { |
368 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
369 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
370 |
|
public void realRun() throws InterruptedException { |
371 |
|
q.put(new Object()); |
372 |
|
q.put(new Object()); |
373 |
< |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS)); |
373 |
> |
long startTime = System.nanoTime(); |
374 |
> |
assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); |
375 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
376 |
> |
pleaseInterrupt.countDown(); |
377 |
|
try { |
378 |
< |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
378 |
> |
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
379 |
|
shouldThrow(); |
380 |
|
} catch (InterruptedException success) {} |
381 |
|
}}); |
382 |
|
|
383 |
< |
t.start(); |
380 |
< |
delay(SHORT_DELAY_MS); |
383 |
> |
await(pleaseInterrupt); |
384 |
|
t.interrupt(); |
385 |
< |
t.join(); |
385 |
> |
awaitTermination(t); |
386 |
|
} |
387 |
|
|
388 |
|
/** |