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; |
247 |
|
* thread becomes active |
248 |
|
*/ |
249 |
|
public void testGetActiveCount() throws InterruptedException { |
250 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
251 |
|
final ThreadPoolExecutor p = |
252 |
|
new CustomTPE(2, 2, |
253 |
|
LONG_DELAY_MS, MILLISECONDS, |
254 |
|
new ArrayBlockingQueue<Runnable>(10)); |
255 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
255 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
256 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
257 |
– |
final CountDownLatch done = new CountDownLatch(1); |
257 |
|
assertEquals(0, p.getActiveCount()); |
258 |
|
p.execute(new CheckedRunnable() { |
259 |
|
public void realRun() throws InterruptedException { |
260 |
|
threadStarted.countDown(); |
261 |
|
assertEquals(1, p.getActiveCount()); |
262 |
< |
done.await(); |
262 |
> |
await(done); |
263 |
|
}}); |
264 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
264 |
> |
await(threadStarted); |
265 |
|
assertEquals(1, p.getActiveCount()); |
267 |
– |
done.countDown(); |
266 |
|
} |
267 |
|
} |
268 |
|
|
486 |
|
p.execute(new CheckedRunnable() { |
487 |
|
public void realRun() throws InterruptedException { |
488 |
|
threadsStarted.countDown(); |
489 |
< |
done.await(); |
489 |
> |
await(done); |
490 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
491 |
|
}}); |
492 |
< |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
492 |
> |
await(threadsStarted); |
493 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
494 |
|
} |
495 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
530 |
|
public void realRun() throws InterruptedException { |
531 |
|
threadStarted.countDown(); |
532 |
|
assertEquals(1, p.getPoolSize()); |
533 |
< |
done.await(); |
533 |
> |
await(done); |
534 |
|
}}); |
535 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
535 |
> |
await(threadStarted); |
536 |
|
assertEquals(1, p.getPoolSize()); |
537 |
|
} |
538 |
|
} |
554 |
|
p.execute(new CheckedRunnable() { |
555 |
|
public void realRun() throws InterruptedException { |
556 |
|
threadStarted.countDown(); |
557 |
< |
done.await(); |
557 |
> |
await(done); |
558 |
|
}}); |
559 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
559 |
> |
await(threadStarted); |
560 |
|
assertEquals(1, p.getTaskCount()); |
561 |
|
assertEquals(0, p.getCompletedTaskCount()); |
562 |
|
for (int i = 0; i < TASKS; i++) { |
565 |
|
public void realRun() throws InterruptedException { |
566 |
|
threadStarted.countDown(); |
567 |
|
assertEquals(1 + TASKS, p.getTaskCount()); |
568 |
< |
done.await(); |
568 |
> |
await(done); |
569 |
|
}}); |
570 |
|
} |
571 |
|
assertEquals(1 + TASKS, p.getTaskCount()); |
606 |
|
public void realRun() throws InterruptedException { |
607 |
|
assertFalse(p.isTerminating()); |
608 |
|
threadStarted.countDown(); |
609 |
< |
done.await(); |
609 |
> |
await(done); |
610 |
|
}}); |
611 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
611 |
> |
await(threadStarted); |
612 |
|
assertFalse(p.isTerminating()); |
613 |
|
done.countDown(); |
614 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
634 |
|
public void realRun() throws InterruptedException { |
635 |
|
assertFalse(p.isTerminating()); |
636 |
|
threadStarted.countDown(); |
637 |
< |
done.await(); |
637 |
> |
await(done); |
638 |
|
}}); |
639 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
639 |
> |
await(threadStarted); |
640 |
|
assertFalse(p.isTerminating()); |
641 |
|
done.countDown(); |
642 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
650 |
|
* getQueue returns the work queue, which contains queued tasks |
651 |
|
*/ |
652 |
|
public void testGetQueue() throws InterruptedException { |
653 |
< |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
653 |
> |
final CountDownLatch done = new CountDownLatch(1); |
654 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
655 |
|
final ThreadPoolExecutor p = |
656 |
|
new CustomTPE(1, 1, |
657 |
|
LONG_DELAY_MS, MILLISECONDS, |
658 |
|
q); |
659 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
659 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
660 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
662 |
– |
final CountDownLatch done = 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()); |
667 |
< |
done.await(); |
667 |
> |
await(done); |
668 |
|
return Boolean.TRUE; |
669 |
|
}}; |
670 |
|
tasks[i] = new FutureTask(task); |
671 |
|
p.execute(tasks[i]); |
672 |
|
} |
673 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
673 |
> |
await(threadStarted); |
674 |
|
assertSame(q, p.getQueue()); |
675 |
|
assertFalse(q.contains(tasks[0])); |
676 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
677 |
|
assertEquals(tasks.length - 1, q.size()); |
680 |
– |
done.countDown(); |
678 |
|
} |
679 |
|
} |
680 |
|
|
682 |
|
* remove(task) removes queued task, and fails to remove active task |
683 |
|
*/ |
684 |
|
public void testRemove() throws InterruptedException { |
685 |
< |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
685 |
> |
final CountDownLatch done = new CountDownLatch(1); |
686 |
> |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
687 |
|
final ThreadPoolExecutor p = |
688 |
|
new CustomTPE(1, 1, |
689 |
|
LONG_DELAY_MS, MILLISECONDS, |
690 |
|
q); |
691 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
691 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
692 |
|
Runnable[] tasks = new Runnable[6]; |
693 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
696 |
– |
final CountDownLatch done = new CountDownLatch(1); |
694 |
|
for (int i = 0; i < tasks.length; i++) { |
695 |
|
tasks[i] = new CheckedRunnable() { |
696 |
|
public void realRun() throws InterruptedException { |
697 |
|
threadStarted.countDown(); |
698 |
< |
done.await(); |
698 |
> |
await(done); |
699 |
|
}}; |
700 |
|
p.execute(tasks[i]); |
701 |
|
} |
702 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
702 |
> |
await(threadStarted); |
703 |
|
assertFalse(p.remove(tasks[0])); |
704 |
|
assertTrue(q.contains(tasks[4])); |
705 |
|
assertTrue(q.contains(tasks[3])); |
709 |
|
assertTrue(q.contains(tasks[3])); |
710 |
|
assertTrue(p.remove(tasks[3])); |
711 |
|
assertFalse(q.contains(tasks[3])); |
715 |
– |
done.countDown(); |
712 |
|
} |
713 |
|
} |
714 |
|
|
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 |
< |
done.await(); |
732 |
> |
await(done); |
733 |
|
return Boolean.TRUE; |
734 |
|
}}; |
735 |
|
tasks[i] = new FutureTask(task); |
736 |
|
p.execute(tasks[i]); |
737 |
|
} |
738 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
738 |
> |
await(threadStarted); |
739 |
|
assertEquals(tasks.length, p.getTaskCount()); |
740 |
|
assertEquals(tasks.length - 1, q.size()); |
741 |
|
assertEquals(1L, p.getActiveCount()); |
773 |
|
}}; |
774 |
|
for (int i = 0; i < count; i++) |
775 |
|
p.execute(waiter); |
776 |
< |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
776 |
> |
await(threadsStarted); |
777 |
|
assertEquals(poolSize, p.getActiveCount()); |
778 |
|
assertEquals(0, p.getCompletedTaskCount()); |
779 |
|
final List<Runnable> queuedTasks; |
1132 |
|
* execute throws RejectedExecutionException if saturated. |
1133 |
|
*/ |
1134 |
|
public void testSaturatedExecute() { |
1135 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1136 |
|
final ThreadPoolExecutor p = |
1137 |
|
new CustomTPE(1, 1, |
1138 |
|
LONG_DELAY_MS, MILLISECONDS, |
1139 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1140 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1144 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1140 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1141 |
|
Runnable task = new CheckedRunnable() { |
1142 |
|
public void realRun() throws InterruptedException { |
1143 |
< |
done.await(); |
1143 |
> |
await(done); |
1144 |
|
}}; |
1145 |
|
for (int i = 0; i < 2; ++i) |
1146 |
|
p.execute(task); |
1151 |
|
} catch (RejectedExecutionException success) {} |
1152 |
|
assertTrue(p.getTaskCount() <= 2); |
1153 |
|
} |
1158 |
– |
done.countDown(); |
1154 |
|
} |
1155 |
|
} |
1156 |
|
|
1158 |
|
* executor using CallerRunsPolicy runs task if saturated. |
1159 |
|
*/ |
1160 |
|
public void testSaturatedExecute2() { |
1161 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1162 |
|
final ThreadPoolExecutor p = |
1163 |
|
new CustomTPE(1, 1, |
1164 |
|
LONG_DELAY_MS, MILLISECONDS, |
1165 |
|
new ArrayBlockingQueue<Runnable>(1), |
1166 |
|
new CustomTPE.CallerRunsPolicy()); |
1167 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1172 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1167 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1168 |
|
Runnable blocker = new CheckedRunnable() { |
1169 |
|
public void realRun() throws InterruptedException { |
1170 |
< |
done.await(); |
1170 |
> |
await(done); |
1171 |
|
}}; |
1172 |
|
p.execute(blocker); |
1173 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1178 |
|
for (int i = 1; i < tasks.length; i++) |
1179 |
|
assertTrue(tasks[i].done); |
1180 |
|
assertFalse(tasks[0].done); // waiting in queue |
1186 |
– |
done.countDown(); |
1181 |
|
} |
1182 |
|
} |
1183 |
|
|
1188 |
|
final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1189 |
|
for (int i = 0; i < tasks.length; ++i) |
1190 |
|
tasks[i] = new TrackedNoOpRunnable(); |
1191 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1192 |
|
final ThreadPoolExecutor p = |
1193 |
|
new CustomTPE(1, 1, |
1194 |
|
LONG_DELAY_MS, MILLISECONDS, |
1195 |
|
new ArrayBlockingQueue<Runnable>(1), |
1196 |
|
new CustomTPE.DiscardPolicy()); |
1197 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1203 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1197 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1198 |
|
p.execute(awaiter(done)); |
1199 |
|
|
1200 |
|
for (TrackedNoOpRunnable task : tasks) |
1201 |
|
p.execute(task); |
1202 |
|
for (int i = 1; i < tasks.length; i++) |
1203 |
|
assertFalse(tasks[i].done); |
1210 |
– |
done.countDown(); |
1204 |
|
} |
1205 |
|
for (int i = 1; i < tasks.length; i++) |
1206 |
|
assertFalse(tasks[i].done); |
1220 |
|
LONG_DELAY_MS, MILLISECONDS, |
1221 |
|
new ArrayBlockingQueue<Runnable>(1), |
1222 |
|
new CustomTPE.DiscardOldestPolicy()); |
1223 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1223 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1224 |
|
assertEquals(LatchAwaiter.NEW, r1.state); |
1225 |
|
assertEquals(LatchAwaiter.NEW, r2.state); |
1226 |
|
assertEquals(LatchAwaiter.NEW, r3.state); |
1230 |
|
p.execute(r3); |
1231 |
|
assertFalse(p.getQueue().contains(r2)); |
1232 |
|
assertTrue(p.getQueue().contains(r3)); |
1240 |
– |
done.countDown(); |
1233 |
|
} |
1234 |
|
assertEquals(LatchAwaiter.DONE, r1.state); |
1235 |
|
assertEquals(LatchAwaiter.NEW, r2.state); |
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, MEDIUM_DELAY_MS, MILLISECONDS); |
1839 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1840 |
|
assertEquals(1, futures.size()); |
1841 |
|
try { |
1842 |
|
futures.get(0).get(); |
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) |
1998 |
|
* (in part, a test of CustomTPE itself) |
1999 |
|
*/ |
2000 |
|
public void testGet_cancelled() throws Exception { |
2001 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
2002 |
|
final ExecutorService e = |
2003 |
|
new CustomTPE(1, 1, |
2004 |
|
LONG_DELAY_MS, MILLISECONDS, |
2005 |
|
new LinkedBlockingQueue<Runnable>()); |
2006 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
2006 |
> |
try (PoolCleaner cleaner = cleaner(e, done)) { |
2007 |
|
final CountDownLatch blockerStarted = new CountDownLatch(1); |
2005 |
– |
final CountDownLatch done = new CountDownLatch(1); |
2008 |
|
final List<Future<?>> futures = new ArrayList<>(); |
2009 |
|
for (int i = 0; i < 2; i++) { |
2010 |
|
Runnable r = new CheckedRunnable() { public void realRun() |
2014 |
|
}}; |
2015 |
|
futures.add(e.submit(r)); |
2016 |
|
} |
2017 |
< |
assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
2017 |
> |
await(blockerStarted); |
2018 |
|
for (Future<?> future : futures) future.cancel(false); |
2019 |
|
for (Future<?> future : futures) { |
2020 |
|
try { |
2028 |
|
assertTrue(future.isCancelled()); |
2029 |
|
assertTrue(future.isDone()); |
2030 |
|
} |
2029 |
– |
done.countDown(); |
2031 |
|
} |
2032 |
|
} |
2033 |
|
|