17 |
|
import java.util.concurrent.CancellationException; |
18 |
|
import java.util.concurrent.CountDownLatch; |
19 |
|
import java.util.concurrent.ExecutionException; |
20 |
– |
import java.util.concurrent.Executors; |
20 |
|
import java.util.concurrent.ExecutorService; |
21 |
|
import java.util.concurrent.Future; |
22 |
|
import java.util.concurrent.FutureTask; |
238 |
|
final Runnable task = new CheckedRunnable() { |
239 |
|
public void realRun() { done.countDown(); }}; |
240 |
|
p.execute(task); |
241 |
< |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
241 |
> |
await(done); |
242 |
|
} |
243 |
|
} |
244 |
|
|
332 |
|
public void realRun() throws InterruptedException { |
333 |
|
threadStarted.countDown(); |
334 |
|
assertEquals(0, p.getCompletedTaskCount()); |
335 |
< |
threadProceed.await(); |
335 |
> |
await(threadProceed); |
336 |
|
threadDone.countDown(); |
337 |
|
}}); |
338 |
|
await(threadStarted); |
339 |
|
assertEquals(0, p.getCompletedTaskCount()); |
340 |
|
threadProceed.countDown(); |
341 |
< |
threadDone.await(); |
341 |
> |
await(threadDone); |
342 |
|
long startTime = System.nanoTime(); |
343 |
|
while (p.getCompletedTaskCount() != 1) { |
344 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
651 |
|
*/ |
652 |
|
public void testGetQueue() throws InterruptedException { |
653 |
|
final CountDownLatch done = new CountDownLatch(1); |
654 |
< |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
654 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
655 |
|
final ThreadPoolExecutor p = |
656 |
|
new CustomTPE(1, 1, |
657 |
|
LONG_DELAY_MS, MILLISECONDS, |
660 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
661 |
|
FutureTask[] tasks = new FutureTask[5]; |
662 |
|
for (int i = 0; i < tasks.length; i++) { |
663 |
< |
Callable task = new CheckedCallable<Boolean>() { |
663 |
> |
Callable<Boolean> task = new CheckedCallable<Boolean>() { |
664 |
|
public Boolean realCall() throws InterruptedException { |
665 |
|
threadStarted.countDown(); |
666 |
|
assertSame(q, p.getQueue()); |
683 |
|
*/ |
684 |
|
public void testRemove() throws InterruptedException { |
685 |
|
final CountDownLatch done = new CountDownLatch(1); |
686 |
< |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
686 |
> |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
687 |
|
final ThreadPoolExecutor p = |
688 |
|
new CustomTPE(1, 1, |
689 |
|
LONG_DELAY_MS, MILLISECONDS, |
718 |
|
public void testPurge() throws InterruptedException { |
719 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
720 |
|
final CountDownLatch done = new CountDownLatch(1); |
721 |
< |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
721 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
722 |
|
final ThreadPoolExecutor p = |
723 |
|
new CustomTPE(1, 1, |
724 |
|
LONG_DELAY_MS, MILLISECONDS, |
726 |
|
try (PoolCleaner cleaner = cleaner(p, done)) { |
727 |
|
FutureTask[] tasks = new FutureTask[5]; |
728 |
|
for (int i = 0; i < tasks.length; i++) { |
729 |
< |
Callable task = new CheckedCallable<Boolean>() { |
729 |
> |
Callable<Boolean> task = new CheckedCallable<Boolean>() { |
730 |
|
public Boolean realCall() throws InterruptedException { |
731 |
|
threadStarted.countDown(); |
732 |
|
await(done); |
1505 |
|
LONG_DELAY_MS, MILLISECONDS, |
1506 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1507 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1508 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1508 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1509 |
|
l.add(latchAwaitingStringTask(latch)); |
1510 |
|
l.add(null); |
1511 |
|
try { |
1525 |
|
LONG_DELAY_MS, MILLISECONDS, |
1526 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1527 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1528 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1528 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1529 |
|
l.add(new NPETask()); |
1530 |
|
try { |
1531 |
|
e.invokeAny(l); |
1545 |
|
LONG_DELAY_MS, MILLISECONDS, |
1546 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1547 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1548 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1548 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1549 |
|
l.add(new StringTask()); |
1550 |
|
l.add(new StringTask()); |
1551 |
|
String result = e.invokeAny(l); |
1592 |
|
LONG_DELAY_MS, MILLISECONDS, |
1593 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1594 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1595 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1595 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1596 |
|
l.add(new StringTask()); |
1597 |
|
l.add(null); |
1598 |
|
try { |
1611 |
|
LONG_DELAY_MS, MILLISECONDS, |
1612 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1613 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1614 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1614 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1615 |
|
l.add(new NPETask()); |
1616 |
|
List<Future<String>> futures = e.invokeAll(l); |
1617 |
|
assertEquals(1, futures.size()); |
1633 |
|
LONG_DELAY_MS, MILLISECONDS, |
1634 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1635 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1636 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1636 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1637 |
|
l.add(new StringTask()); |
1638 |
|
l.add(new StringTask()); |
1639 |
|
List<Future<String>> futures = e.invokeAll(l); |
1668 |
|
LONG_DELAY_MS, MILLISECONDS, |
1669 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1670 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1671 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1671 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1672 |
|
l.add(new StringTask()); |
1673 |
|
try { |
1674 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1704 |
|
LONG_DELAY_MS, MILLISECONDS, |
1705 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1706 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1707 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1707 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1708 |
|
l.add(latchAwaitingStringTask(latch)); |
1709 |
|
l.add(null); |
1710 |
|
try { |
1724 |
|
LONG_DELAY_MS, MILLISECONDS, |
1725 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1726 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1727 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1727 |
> |
long startTime = System.nanoTime(); |
1728 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1729 |
|
l.add(new NPETask()); |
1730 |
|
try { |
1731 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1731 |
> |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1732 |
|
shouldThrow(); |
1733 |
|
} catch (ExecutionException success) { |
1734 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1735 |
|
} |
1736 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1737 |
|
} |
1738 |
|
} |
1739 |
|
|
1746 |
|
LONG_DELAY_MS, MILLISECONDS, |
1747 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1748 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1749 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1749 |
> |
long startTime = System.nanoTime(); |
1750 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1751 |
|
l.add(new StringTask()); |
1752 |
|
l.add(new StringTask()); |
1753 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1753 |
> |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1754 |
|
assertSame(TEST_STRING, result); |
1755 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1756 |
|
} |
1757 |
|
} |
1758 |
|
|
1781 |
|
LONG_DELAY_MS, MILLISECONDS, |
1782 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1783 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1784 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1784 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1785 |
|
l.add(new StringTask()); |
1786 |
|
try { |
1787 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1814 |
|
LONG_DELAY_MS, MILLISECONDS, |
1815 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1816 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1817 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1817 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1818 |
|
l.add(new StringTask()); |
1819 |
|
l.add(null); |
1820 |
|
try { |
1833 |
|
LONG_DELAY_MS, MILLISECONDS, |
1834 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1835 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1836 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1836 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1837 |
|
l.add(new NPETask()); |
1838 |
|
List<Future<String>> futures = |
1839 |
|
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1856 |
|
LONG_DELAY_MS, MILLISECONDS, |
1857 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1858 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1859 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1859 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1860 |
|
l.add(new StringTask()); |
1861 |
|
l.add(new StringTask()); |
1862 |
|
List<Future<String>> futures = |
1871 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1872 |
|
*/ |
1873 |
|
public void testTimedInvokeAll6() throws Exception { |
1874 |
< |
final ExecutorService e = |
1875 |
< |
new CustomTPE(2, 2, |
1876 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1877 |
< |
new ArrayBlockingQueue<Runnable>(10)); |
1878 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
1879 |
< |
for (long timeout = timeoutMillis();;) { |
1874 |
> |
for (long timeout = timeoutMillis();;) { |
1875 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1876 |
> |
final Callable<String> waiter = new CheckedCallable<String>() { |
1877 |
> |
public String realCall() { |
1878 |
> |
try { done.await(LONG_DELAY_MS, MILLISECONDS); } |
1879 |
> |
catch (InterruptedException ok) {} |
1880 |
> |
return "1"; }}; |
1881 |
> |
final ExecutorService p = |
1882 |
> |
new CustomTPE(2, 2, |
1883 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1884 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1885 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1886 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1887 |
|
tasks.add(new StringTask("0")); |
1888 |
< |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1888 |
> |
tasks.add(waiter); |
1889 |
|
tasks.add(new StringTask("2")); |
1890 |
|
long startTime = System.nanoTime(); |
1891 |
|
List<Future<String>> futures = |
1892 |
< |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1892 |
> |
p.invokeAll(tasks, timeout, MILLISECONDS); |
1893 |
|
assertEquals(tasks.size(), futures.size()); |
1894 |
|
assertTrue(millisElapsedSince(startTime) >= timeout); |
1895 |
|
for (Future future : futures) |
1926 |
|
public void realRun() { |
1927 |
|
done.countDown(); |
1928 |
|
}}); |
1929 |
< |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
1929 |
> |
await(done); |
1930 |
|
} |
1931 |
|
} |
1932 |
|
|