283 |
|
final CountDownLatch aboutToWait = new CountDownLatch(1); |
284 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
285 |
|
public void realRun() throws InterruptedException { |
286 |
+ |
long startTime = System.nanoTime(); |
287 |
|
for (int i = 0; i < SIZE; ++i) { |
287 |
– |
long t0 = System.nanoTime(); |
288 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
289 |
– |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
289 |
|
} |
291 |
– |
long t0 = System.nanoTime(); |
290 |
|
aboutToWait.countDown(); |
291 |
|
try { |
292 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
292 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
293 |
|
shouldThrow(); |
294 |
|
} catch (InterruptedException success) { |
295 |
< |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
295 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
296 |
|
} |
297 |
|
}}); |
298 |
|
|
299 |
|
aboutToWait.await(); |
300 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
300 |
> |
waitForThreadToEnterWaitState(t, LONG_DELAY_MS); |
301 |
|
t.interrupt(); |
302 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
302 |
> |
awaitTermination(t); |
303 |
|
checkEmpty(q); |
304 |
|
} |
305 |
|
|
594 |
|
public void testOfferInExecutor() { |
595 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
596 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
597 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
598 |
< |
|
601 |
< |
executor.execute(new CheckedRunnable() { |
602 |
< |
public void realRun() throws InterruptedException { |
603 |
< |
threadsStarted.await(); |
604 |
< |
assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); |
605 |
< |
}}); |
606 |
< |
|
607 |
< |
executor.execute(new CheckedRunnable() { |
608 |
< |
public void realRun() throws InterruptedException { |
609 |
< |
threadsStarted.await(); |
610 |
< |
assertSame(one, q.take()); |
611 |
< |
checkEmpty(q); |
612 |
< |
}}); |
597 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
598 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
599 |
|
|
600 |
< |
joinPool(executor); |
600 |
> |
executor.execute(new CheckedRunnable() { |
601 |
> |
public void realRun() throws InterruptedException { |
602 |
> |
threadsStarted.await(); |
603 |
> |
assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); |
604 |
> |
}}); |
605 |
> |
|
606 |
> |
executor.execute(new CheckedRunnable() { |
607 |
> |
public void realRun() throws InterruptedException { |
608 |
> |
threadsStarted.await(); |
609 |
> |
assertSame(one, q.take()); |
610 |
> |
checkEmpty(q); |
611 |
> |
}}); |
612 |
> |
} |
613 |
|
} |
614 |
|
|
615 |
|
/** |
618 |
|
public void testPollInExecutor() { |
619 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
620 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
621 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
622 |
< |
|
625 |
< |
executor.execute(new CheckedRunnable() { |
626 |
< |
public void realRun() throws InterruptedException { |
627 |
< |
assertNull(q.poll()); |
628 |
< |
threadsStarted.await(); |
629 |
< |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
630 |
< |
checkEmpty(q); |
631 |
< |
}}); |
621 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
622 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
623 |
|
|
624 |
< |
executor.execute(new CheckedRunnable() { |
625 |
< |
public void realRun() throws InterruptedException { |
626 |
< |
threadsStarted.await(); |
627 |
< |
q.put(one); |
628 |
< |
}}); |
629 |
< |
|
630 |
< |
joinPool(executor); |
624 |
> |
executor.execute(new CheckedRunnable() { |
625 |
> |
public void realRun() throws InterruptedException { |
626 |
> |
assertNull(q.poll()); |
627 |
> |
threadsStarted.await(); |
628 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
629 |
> |
checkEmpty(q); |
630 |
> |
}}); |
631 |
> |
|
632 |
> |
executor.execute(new CheckedRunnable() { |
633 |
> |
public void realRun() throws InterruptedException { |
634 |
> |
threadsStarted.await(); |
635 |
> |
q.put(one); |
636 |
> |
}}); |
637 |
> |
} |
638 |
|
} |
639 |
|
|
640 |
|
/** |