440 |
|
final CountDownLatch aboutToWait = new CountDownLatch(1); |
441 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
442 |
|
public void realRun() throws InterruptedException { |
443 |
+ |
long startTime = System.nanoTime(); |
444 |
|
for (int i = 0; i < SIZE; ++i) { |
444 |
– |
long t0 = System.nanoTime(); |
445 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
446 |
– |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
446 |
|
} |
448 |
– |
long t0 = System.nanoTime(); |
447 |
|
aboutToWait.countDown(); |
448 |
|
try { |
449 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
449 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
450 |
|
shouldThrow(); |
451 |
|
} catch (InterruptedException success) { |
452 |
< |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
452 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
453 |
|
} |
454 |
|
}}); |
455 |
|
|
456 |
< |
aboutToWait.await(); |
457 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
456 |
> |
await(aboutToWait); |
457 |
> |
waitForThreadToEnterWaitState(t); |
458 |
|
t.interrupt(); |
459 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
459 |
> |
awaitTermination(t); |
460 |
|
checkEmpty(q); |
461 |
|
} |
462 |
|
|
720 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
721 |
|
q.add(one); |
722 |
|
q.add(two); |
725 |
– |
ExecutorService executor = Executors.newFixedThreadPool(2); |
723 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
724 |
< |
executor.execute(new CheckedRunnable() { |
725 |
< |
public void realRun() throws InterruptedException { |
726 |
< |
assertFalse(q.offer(three)); |
727 |
< |
threadsStarted.await(); |
728 |
< |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
729 |
< |
assertEquals(0, q.remainingCapacity()); |
730 |
< |
}}); |
731 |
< |
|
732 |
< |
executor.execute(new CheckedRunnable() { |
733 |
< |
public void realRun() throws InterruptedException { |
734 |
< |
threadsStarted.await(); |
735 |
< |
assertSame(one, q.take()); |
736 |
< |
}}); |
737 |
< |
|
738 |
< |
joinPool(executor); |
724 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
725 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
726 |
> |
executor.execute(new CheckedRunnable() { |
727 |
> |
public void realRun() throws InterruptedException { |
728 |
> |
assertFalse(q.offer(three)); |
729 |
> |
threadsStarted.await(); |
730 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
731 |
> |
assertEquals(0, q.remainingCapacity()); |
732 |
> |
}}); |
733 |
> |
|
734 |
> |
executor.execute(new CheckedRunnable() { |
735 |
> |
public void realRun() throws InterruptedException { |
736 |
> |
threadsStarted.await(); |
737 |
> |
assertSame(one, q.take()); |
738 |
> |
}}); |
739 |
> |
} |
740 |
|
} |
741 |
|
|
742 |
|
/** |
745 |
|
public void testPollInExecutor() { |
746 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
747 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
748 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
749 |
< |
executor.execute(new CheckedRunnable() { |
750 |
< |
public void realRun() throws InterruptedException { |
751 |
< |
assertNull(q.poll()); |
752 |
< |
threadsStarted.await(); |
753 |
< |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
754 |
< |
checkEmpty(q); |
755 |
< |
}}); |
756 |
< |
|
757 |
< |
executor.execute(new CheckedRunnable() { |
758 |
< |
public void realRun() throws InterruptedException { |
759 |
< |
threadsStarted.await(); |
760 |
< |
q.put(one); |
761 |
< |
}}); |
762 |
< |
|
763 |
< |
joinPool(executor); |
748 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
749 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
750 |
> |
executor.execute(new CheckedRunnable() { |
751 |
> |
public void realRun() throws InterruptedException { |
752 |
> |
assertNull(q.poll()); |
753 |
> |
threadsStarted.await(); |
754 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
755 |
> |
checkEmpty(q); |
756 |
> |
}}); |
757 |
> |
|
758 |
> |
executor.execute(new CheckedRunnable() { |
759 |
> |
public void realRun() throws InterruptedException { |
760 |
> |
threadsStarted.await(); |
761 |
> |
q.put(one); |
762 |
> |
}}); |
763 |
> |
} |
764 |
|
} |
765 |
|
|
766 |
|
/** |