28 |
|
import java.util.concurrent.SynchronousQueue; |
29 |
|
import java.util.concurrent.ThreadFactory; |
30 |
|
import java.util.concurrent.ThreadPoolExecutor; |
31 |
– |
import java.util.concurrent.TimeUnit; |
31 |
|
import java.util.concurrent.atomic.AtomicInteger; |
32 |
|
|
33 |
|
import junit.framework.Test; |
536 |
|
*/ |
537 |
|
public void testGetQueue() throws InterruptedException { |
538 |
|
final CountDownLatch done = new CountDownLatch(1); |
539 |
< |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
539 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
540 |
|
final ThreadPoolExecutor p = |
541 |
|
new ThreadPoolExecutor(1, 1, |
542 |
|
LONG_DELAY_MS, MILLISECONDS, |
568 |
|
*/ |
569 |
|
public void testRemove() throws InterruptedException { |
570 |
|
final CountDownLatch done = new CountDownLatch(1); |
571 |
< |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
571 |
> |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
572 |
|
final ThreadPoolExecutor p = |
573 |
|
new ThreadPoolExecutor(1, 1, |
574 |
|
LONG_DELAY_MS, MILLISECONDS, |
603 |
|
public void testPurge() throws InterruptedException { |
604 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
605 |
|
final CountDownLatch done = new CountDownLatch(1); |
606 |
< |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
606 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
607 |
|
final ThreadPoolExecutor p = |
608 |
|
new ThreadPoolExecutor(1, 1, |
609 |
|
LONG_DELAY_MS, MILLISECONDS, |
1507 |
|
LONG_DELAY_MS, MILLISECONDS, |
1508 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1509 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1510 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1510 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1511 |
|
l.add(latchAwaitingStringTask(latch)); |
1512 |
|
l.add(null); |
1513 |
|
try { |
1527 |
|
LONG_DELAY_MS, MILLISECONDS, |
1528 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1529 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1530 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1530 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1531 |
|
l.add(new NPETask()); |
1532 |
|
try { |
1533 |
|
e.invokeAny(l); |
1547 |
|
LONG_DELAY_MS, MILLISECONDS, |
1548 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1549 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1550 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1550 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1551 |
|
l.add(new StringTask()); |
1552 |
|
l.add(new StringTask()); |
1553 |
|
String result = e.invokeAny(l); |
1594 |
|
LONG_DELAY_MS, MILLISECONDS, |
1595 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1596 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1597 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1597 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1598 |
|
l.add(new StringTask()); |
1599 |
|
l.add(null); |
1600 |
|
try { |
1613 |
|
LONG_DELAY_MS, MILLISECONDS, |
1614 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1615 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1616 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1616 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1617 |
|
l.add(new NPETask()); |
1618 |
|
List<Future<String>> futures = e.invokeAll(l); |
1619 |
|
assertEquals(1, futures.size()); |
1635 |
|
LONG_DELAY_MS, MILLISECONDS, |
1636 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1637 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1638 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1638 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1639 |
|
l.add(new StringTask()); |
1640 |
|
l.add(new StringTask()); |
1641 |
|
List<Future<String>> futures = e.invokeAll(l); |
1670 |
|
LONG_DELAY_MS, MILLISECONDS, |
1671 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1672 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1673 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1673 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1674 |
|
l.add(new StringTask()); |
1675 |
|
try { |
1676 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1706 |
|
LONG_DELAY_MS, MILLISECONDS, |
1707 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1708 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1709 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1709 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1710 |
|
l.add(latchAwaitingStringTask(latch)); |
1711 |
|
l.add(null); |
1712 |
|
try { |
1726 |
|
LONG_DELAY_MS, MILLISECONDS, |
1727 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1728 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1729 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1729 |
> |
long startTime = System.nanoTime(); |
1730 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1731 |
|
l.add(new NPETask()); |
1732 |
|
try { |
1733 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1733 |
> |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1734 |
|
shouldThrow(); |
1735 |
|
} catch (ExecutionException success) { |
1736 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1737 |
|
} |
1738 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1739 |
|
} |
1740 |
|
} |
1741 |
|
|
1748 |
|
LONG_DELAY_MS, MILLISECONDS, |
1749 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1750 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1751 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1751 |
> |
long startTime = System.nanoTime(); |
1752 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1753 |
|
l.add(new StringTask()); |
1754 |
|
l.add(new StringTask()); |
1755 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1755 |
> |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1756 |
|
assertSame(TEST_STRING, result); |
1757 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1758 |
|
} |
1759 |
|
} |
1760 |
|
|
1783 |
|
LONG_DELAY_MS, MILLISECONDS, |
1784 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1785 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1786 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1786 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1787 |
|
l.add(new StringTask()); |
1788 |
|
try { |
1789 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1816 |
|
LONG_DELAY_MS, MILLISECONDS, |
1817 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1818 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1819 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1819 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1820 |
|
l.add(new StringTask()); |
1821 |
|
l.add(null); |
1822 |
|
try { |
1835 |
|
LONG_DELAY_MS, MILLISECONDS, |
1836 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1837 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1838 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1838 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1839 |
|
l.add(new NPETask()); |
1840 |
|
List<Future<String>> futures = |
1841 |
|
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1858 |
|
LONG_DELAY_MS, MILLISECONDS, |
1859 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1860 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1861 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1861 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1862 |
|
l.add(new StringTask()); |
1863 |
|
l.add(new StringTask()); |
1864 |
|
List<Future<String>> futures = |
1873 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1874 |
|
*/ |
1875 |
|
public void testTimedInvokeAll6() throws Exception { |
1876 |
< |
final ExecutorService e = |
1877 |
< |
new ThreadPoolExecutor(2, 2, |
1878 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1879 |
< |
new ArrayBlockingQueue<Runnable>(10)); |
1880 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
1881 |
< |
for (long timeout = timeoutMillis();;) { |
1876 |
> |
for (long timeout = timeoutMillis();;) { |
1877 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1878 |
> |
final Callable<String> waiter = new CheckedCallable<String>() { |
1879 |
> |
public String realCall() { |
1880 |
> |
try { done.await(LONG_DELAY_MS, MILLISECONDS); } |
1881 |
> |
catch (InterruptedException ok) {} |
1882 |
> |
return "1"; }}; |
1883 |
> |
final ExecutorService p = |
1884 |
> |
new ThreadPoolExecutor(2, 2, |
1885 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1886 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1887 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1888 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1889 |
|
tasks.add(new StringTask("0")); |
1890 |
< |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1890 |
> |
tasks.add(waiter); |
1891 |
|
tasks.add(new StringTask("2")); |
1892 |
|
long startTime = System.nanoTime(); |
1893 |
|
List<Future<String>> futures = |
1894 |
< |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1894 |
> |
p.invokeAll(tasks, timeout, MILLISECONDS); |
1895 |
|
assertEquals(tasks.size(), futures.size()); |
1896 |
|
assertTrue(millisElapsedSince(startTime) >= timeout); |
1897 |
|
for (Future future : futures) |