722 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
723 |
|
q.add(one); |
724 |
|
q.add(two); |
725 |
– |
ExecutorService executor = Executors.newFixedThreadPool(2); |
725 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
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 |
< |
joinPool(executor); |
726 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
727 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
728 |
> |
executor.execute(new CheckedRunnable() { |
729 |
> |
public void realRun() throws InterruptedException { |
730 |
> |
assertFalse(q.offer(three)); |
731 |
> |
threadsStarted.await(); |
732 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
733 |
> |
assertEquals(0, q.remainingCapacity()); |
734 |
> |
}}); |
735 |
> |
|
736 |
> |
executor.execute(new CheckedRunnable() { |
737 |
> |
public void realRun() throws InterruptedException { |
738 |
> |
threadsStarted.await(); |
739 |
> |
assertSame(one, q.take()); |
740 |
> |
}}); |
741 |
> |
} |
742 |
|
} |
743 |
|
|
744 |
|
/** |
747 |
|
public void testPollInExecutor() { |
748 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
749 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
750 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
751 |
< |
executor.execute(new CheckedRunnable() { |
752 |
< |
public void realRun() throws InterruptedException { |
753 |
< |
assertNull(q.poll()); |
754 |
< |
threadsStarted.await(); |
755 |
< |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
756 |
< |
checkEmpty(q); |
757 |
< |
}}); |
758 |
< |
|
759 |
< |
executor.execute(new CheckedRunnable() { |
760 |
< |
public void realRun() throws InterruptedException { |
761 |
< |
threadsStarted.await(); |
762 |
< |
q.put(one); |
763 |
< |
}}); |
764 |
< |
|
765 |
< |
joinPool(executor); |
750 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
751 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
752 |
> |
executor.execute(new CheckedRunnable() { |
753 |
> |
public void realRun() throws InterruptedException { |
754 |
> |
assertNull(q.poll()); |
755 |
> |
threadsStarted.await(); |
756 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
757 |
> |
checkEmpty(q); |
758 |
> |
}}); |
759 |
> |
|
760 |
> |
executor.execute(new CheckedRunnable() { |
761 |
> |
public void realRun() throws InterruptedException { |
762 |
> |
threadsStarted.await(); |
763 |
> |
q.put(one); |
764 |
> |
}}); |
765 |
> |
} |
766 |
|
} |
767 |
|
|
768 |
|
/** |