248 |
|
* thread becomes active |
249 |
|
*/ |
250 |
|
public void testGetActiveCount() throws InterruptedException { |
251 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
252 |
|
final ThreadPoolExecutor p = |
253 |
|
new CustomTPE(2, 2, |
254 |
|
LONG_DELAY_MS, MILLISECONDS, |
255 |
|
new ArrayBlockingQueue<Runnable>(10)); |
256 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
256 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
257 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
257 |
– |
final CountDownLatch done = new CountDownLatch(1); |
258 |
|
assertEquals(0, p.getActiveCount()); |
259 |
|
p.execute(new CheckedRunnable() { |
260 |
|
public void realRun() throws InterruptedException { |
261 |
|
threadStarted.countDown(); |
262 |
|
assertEquals(1, p.getActiveCount()); |
263 |
< |
done.await(); |
263 |
> |
await(done); |
264 |
|
}}); |
265 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
265 |
> |
await(threadStarted); |
266 |
|
assertEquals(1, p.getActiveCount()); |
267 |
– |
done.countDown(); |
267 |
|
} |
268 |
|
} |
269 |
|
|
475 |
|
*/ |
476 |
|
public void testGetLargestPoolSize() throws InterruptedException { |
477 |
|
final int THREADS = 3; |
478 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
479 |
|
final ThreadPoolExecutor p = |
480 |
|
new CustomTPE(THREADS, THREADS, |
481 |
|
LONG_DELAY_MS, MILLISECONDS, |
482 |
|
new ArrayBlockingQueue<Runnable>(10)); |
483 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
484 |
< |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
485 |
< |
final CountDownLatch done = new CountDownLatch(1); |
483 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
484 |
|
assertEquals(0, p.getLargestPoolSize()); |
485 |
+ |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
486 |
|
for (int i = 0; i < THREADS; i++) |
487 |
|
p.execute(new CheckedRunnable() { |
488 |
|
public void realRun() throws InterruptedException { |
489 |
|
threadsStarted.countDown(); |
490 |
< |
done.await(); |
490 |
> |
await(done); |
491 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
492 |
|
}}); |
493 |
< |
assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
493 |
> |
await(threadsStarted); |
494 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
496 |
– |
done.countDown(); // release pool |
495 |
|
} |
496 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
497 |
|
} |
519 |
|
* become active |
520 |
|
*/ |
521 |
|
public void testGetPoolSize() throws InterruptedException { |
522 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
523 |
|
final ThreadPoolExecutor p = |
524 |
|
new CustomTPE(1, 1, |
525 |
|
LONG_DELAY_MS, MILLISECONDS, |
526 |
|
new ArrayBlockingQueue<Runnable>(10)); |
527 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
529 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
530 |
< |
final CountDownLatch done = new CountDownLatch(1); |
527 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
528 |
|
assertEquals(0, p.getPoolSize()); |
529 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
530 |
|
p.execute(new CheckedRunnable() { |
531 |
|
public void realRun() throws InterruptedException { |
532 |
|
threadStarted.countDown(); |
533 |
|
assertEquals(1, p.getPoolSize()); |
534 |
< |
done.await(); |
534 |
> |
await(done); |
535 |
|
}}); |
536 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
536 |
> |
await(threadStarted); |
537 |
|
assertEquals(1, p.getPoolSize()); |
540 |
– |
done.countDown(); // release pool |
538 |
|
} |
539 |
|
} |
540 |
|
|
555 |
|
p.execute(new CheckedRunnable() { |
556 |
|
public void realRun() throws InterruptedException { |
557 |
|
threadStarted.countDown(); |
558 |
< |
done.await(); |
558 |
> |
await(done); |
559 |
|
}}); |
560 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
560 |
> |
await(threadStarted); |
561 |
|
assertEquals(1, p.getTaskCount()); |
562 |
|
assertEquals(0, p.getCompletedTaskCount()); |
563 |
|
for (int i = 0; i < TASKS; i++) { |
566 |
|
public void realRun() throws InterruptedException { |
567 |
|
threadStarted.countDown(); |
568 |
|
assertEquals(1 + TASKS, p.getTaskCount()); |
569 |
< |
done.await(); |
569 |
> |
await(done); |
570 |
|
}}); |
571 |
|
} |
572 |
|
assertEquals(1 + TASKS, p.getTaskCount()); |
607 |
|
public void realRun() throws InterruptedException { |
608 |
|
assertFalse(p.isTerminating()); |
609 |
|
threadStarted.countDown(); |
610 |
< |
done.await(); |
610 |
> |
await(done); |
611 |
|
}}); |
612 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
612 |
> |
await(threadStarted); |
613 |
|
assertFalse(p.isTerminating()); |
614 |
|
done.countDown(); |
615 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
635 |
|
public void realRun() throws InterruptedException { |
636 |
|
assertFalse(p.isTerminating()); |
637 |
|
threadStarted.countDown(); |
638 |
< |
done.await(); |
638 |
> |
await(done); |
639 |
|
}}); |
640 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
640 |
> |
await(threadStarted); |
641 |
|
assertFalse(p.isTerminating()); |
642 |
|
done.countDown(); |
643 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
651 |
|
* getQueue returns the work queue, which contains queued tasks |
652 |
|
*/ |
653 |
|
public void testGetQueue() throws InterruptedException { |
654 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
655 |
|
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
656 |
|
final ThreadPoolExecutor p = |
657 |
|
new CustomTPE(1, 1, |
658 |
|
LONG_DELAY_MS, MILLISECONDS, |
659 |
|
q); |
660 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
660 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
661 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
664 |
– |
final CountDownLatch done = new CountDownLatch(1); |
662 |
|
FutureTask[] tasks = new FutureTask[5]; |
663 |
|
for (int i = 0; i < tasks.length; i++) { |
664 |
|
Callable task = new CheckedCallable<Boolean>() { |
665 |
|
public Boolean realCall() throws InterruptedException { |
666 |
|
threadStarted.countDown(); |
667 |
|
assertSame(q, p.getQueue()); |
668 |
< |
done.await(); |
668 |
> |
await(done); |
669 |
|
return Boolean.TRUE; |
670 |
|
}}; |
671 |
|
tasks[i] = new FutureTask(task); |
672 |
|
p.execute(tasks[i]); |
673 |
|
} |
674 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
674 |
> |
await(threadStarted); |
675 |
|
assertSame(q, p.getQueue()); |
676 |
|
assertFalse(q.contains(tasks[0])); |
677 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
678 |
|
assertEquals(tasks.length - 1, q.size()); |
682 |
– |
done.countDown(); |
679 |
|
} |
680 |
|
} |
681 |
|
|
683 |
|
* remove(task) removes queued task, and fails to remove active task |
684 |
|
*/ |
685 |
|
public void testRemove() throws InterruptedException { |
686 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
687 |
|
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
688 |
|
final ThreadPoolExecutor p = |
689 |
|
new CustomTPE(1, 1, |
690 |
|
LONG_DELAY_MS, MILLISECONDS, |
691 |
|
q); |
692 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
692 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
693 |
|
Runnable[] tasks = new Runnable[6]; |
694 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
698 |
– |
final CountDownLatch done = new CountDownLatch(1); |
695 |
|
for (int i = 0; i < tasks.length; i++) { |
696 |
|
tasks[i] = new CheckedRunnable() { |
697 |
|
public void realRun() throws InterruptedException { |
698 |
|
threadStarted.countDown(); |
699 |
< |
done.await(); |
699 |
> |
await(done); |
700 |
|
}}; |
701 |
|
p.execute(tasks[i]); |
702 |
|
} |
703 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
703 |
> |
await(threadStarted); |
704 |
|
assertFalse(p.remove(tasks[0])); |
705 |
|
assertTrue(q.contains(tasks[4])); |
706 |
|
assertTrue(q.contains(tasks[3])); |
710 |
|
assertTrue(q.contains(tasks[3])); |
711 |
|
assertTrue(p.remove(tasks[3])); |
712 |
|
assertFalse(q.contains(tasks[3])); |
717 |
– |
done.countDown(); |
713 |
|
} |
714 |
|
} |
715 |
|
|
724 |
|
new CustomTPE(1, 1, |
725 |
|
LONG_DELAY_MS, MILLISECONDS, |
726 |
|
q); |
727 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
727 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
728 |
|
FutureTask[] tasks = new FutureTask[5]; |
729 |
|
for (int i = 0; i < tasks.length; i++) { |
730 |
|
Callable task = new CheckedCallable<Boolean>() { |
731 |
|
public Boolean realCall() throws InterruptedException { |
732 |
|
threadStarted.countDown(); |
733 |
< |
done.await(); |
733 |
> |
await(done); |
734 |
|
return Boolean.TRUE; |
735 |
|
}}; |
736 |
|
tasks[i] = new FutureTask(task); |
737 |
|
p.execute(tasks[i]); |
738 |
|
} |
739 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
739 |
> |
await(threadStarted); |
740 |
|
assertEquals(tasks.length, p.getTaskCount()); |
741 |
|
assertEquals(tasks.length - 1, q.size()); |
742 |
|
assertEquals(1L, p.getActiveCount()); |
749 |
|
p.purge(); // Nothing to do |
750 |
|
assertEquals(tasks.length - 3, q.size()); |
751 |
|
assertEquals(tasks.length - 2, p.getTaskCount()); |
757 |
– |
done.countDown(); |
752 |
|
} |
753 |
|
} |
754 |
|
|
774 |
|
}}; |
775 |
|
for (int i = 0; i < count; i++) |
776 |
|
p.execute(waiter); |
777 |
< |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
777 |
> |
await(threadsStarted); |
778 |
|
assertEquals(poolSize, p.getActiveCount()); |
779 |
|
assertEquals(0, p.getCompletedTaskCount()); |
780 |
|
final List<Runnable> queuedTasks; |
1133 |
|
* execute throws RejectedExecutionException if saturated. |
1134 |
|
*/ |
1135 |
|
public void testSaturatedExecute() { |
1136 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1137 |
|
final ThreadPoolExecutor p = |
1138 |
|
new CustomTPE(1, 1, |
1139 |
|
LONG_DELAY_MS, MILLISECONDS, |
1140 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1141 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1147 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1141 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1142 |
|
Runnable task = new CheckedRunnable() { |
1143 |
|
public void realRun() throws InterruptedException { |
1144 |
< |
done.await(); |
1144 |
> |
await(done); |
1145 |
|
}}; |
1146 |
|
for (int i = 0; i < 2; ++i) |
1147 |
|
p.execute(task); |
1152 |
|
} catch (RejectedExecutionException success) {} |
1153 |
|
assertTrue(p.getTaskCount() <= 2); |
1154 |
|
} |
1161 |
– |
done.countDown(); |
1155 |
|
} |
1156 |
|
} |
1157 |
|
|
1159 |
|
* executor using CallerRunsPolicy runs task if saturated. |
1160 |
|
*/ |
1161 |
|
public void testSaturatedExecute2() { |
1162 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1163 |
|
final ThreadPoolExecutor p = |
1164 |
|
new CustomTPE(1, 1, |
1165 |
|
LONG_DELAY_MS, MILLISECONDS, |
1166 |
|
new ArrayBlockingQueue<Runnable>(1), |
1167 |
|
new CustomTPE.CallerRunsPolicy()); |
1168 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1175 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1168 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1169 |
|
Runnable blocker = new CheckedRunnable() { |
1170 |
|
public void realRun() throws InterruptedException { |
1171 |
< |
done.await(); |
1171 |
> |
await(done); |
1172 |
|
}}; |
1173 |
|
p.execute(blocker); |
1174 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1179 |
|
for (int i = 1; i < tasks.length; i++) |
1180 |
|
assertTrue(tasks[i].done); |
1181 |
|
assertFalse(tasks[0].done); // waiting in queue |
1189 |
– |
done.countDown(); |
1182 |
|
} |
1183 |
|
} |
1184 |
|
|
1189 |
|
final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1190 |
|
for (int i = 0; i < tasks.length; ++i) |
1191 |
|
tasks[i] = new TrackedNoOpRunnable(); |
1192 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1193 |
|
final ThreadPoolExecutor p = |
1194 |
|
new CustomTPE(1, 1, |
1195 |
|
LONG_DELAY_MS, MILLISECONDS, |
1196 |
|
new ArrayBlockingQueue<Runnable>(1), |
1197 |
|
new CustomTPE.DiscardPolicy()); |
1198 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1206 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1198 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1199 |
|
p.execute(awaiter(done)); |
1200 |
|
|
1201 |
|
for (TrackedNoOpRunnable task : tasks) |
1202 |
|
p.execute(task); |
1203 |
|
for (int i = 1; i < tasks.length; i++) |
1204 |
|
assertFalse(tasks[i].done); |
1213 |
– |
done.countDown(); |
1205 |
|
} |
1206 |
|
for (int i = 1; i < tasks.length; i++) |
1207 |
|
assertFalse(tasks[i].done); |
1221 |
|
LONG_DELAY_MS, MILLISECONDS, |
1222 |
|
new ArrayBlockingQueue<Runnable>(1), |
1223 |
|
new CustomTPE.DiscardOldestPolicy()); |
1224 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1224 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1225 |
|
assertEquals(LatchAwaiter.NEW, r1.state); |
1226 |
|
assertEquals(LatchAwaiter.NEW, r2.state); |
1227 |
|
assertEquals(LatchAwaiter.NEW, r3.state); |
1231 |
|
p.execute(r3); |
1232 |
|
assertFalse(p.getQueue().contains(r2)); |
1233 |
|
assertTrue(p.getQueue().contains(r3)); |
1243 |
– |
done.countDown(); |
1234 |
|
} |
1235 |
|
assertEquals(LatchAwaiter.DONE, r1.state); |
1236 |
|
assertEquals(LatchAwaiter.NEW, r2.state); |
1725 |
|
LONG_DELAY_MS, MILLISECONDS, |
1726 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1727 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1728 |
+ |
long startTime = System.nanoTime(); |
1729 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1730 |
|
l.add(new NPETask()); |
1731 |
|
try { |
1732 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1732 |
> |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1733 |
|
shouldThrow(); |
1734 |
|
} catch (ExecutionException success) { |
1735 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1736 |
|
} |
1737 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1738 |
|
} |
1739 |
|
} |
1740 |
|
|
1747 |
|
LONG_DELAY_MS, MILLISECONDS, |
1748 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1749 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1750 |
+ |
long startTime = System.nanoTime(); |
1751 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1752 |
|
l.add(new StringTask()); |
1753 |
|
l.add(new StringTask()); |
1754 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1754 |
> |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1755 |
|
assertSame(TEST_STRING, result); |
1756 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1757 |
|
} |
1758 |
|
} |
1759 |
|
|
1837 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1838 |
|
l.add(new NPETask()); |
1839 |
|
List<Future<String>> futures = |
1840 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1840 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1841 |
|
assertEquals(1, futures.size()); |
1842 |
|
try { |
1843 |
|
futures.get(0).get(); |
1872 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1873 |
|
*/ |
1874 |
|
public void testTimedInvokeAll6() throws Exception { |
1875 |
< |
final ExecutorService e = |
1876 |
< |
new CustomTPE(2, 2, |
1877 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1878 |
< |
new ArrayBlockingQueue<Runnable>(10)); |
1879 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
1880 |
< |
for (long timeout = timeoutMillis();;) { |
1875 |
> |
for (long timeout = timeoutMillis();;) { |
1876 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1877 |
> |
final Callable<String> waiter = new CheckedCallable<String>() { |
1878 |
> |
public String realCall() { |
1879 |
> |
try { done.await(LONG_DELAY_MS, MILLISECONDS); } |
1880 |
> |
catch (InterruptedException ok) {} |
1881 |
> |
return "1"; }}; |
1882 |
> |
final ExecutorService p = |
1883 |
> |
new CustomTPE(2, 2, |
1884 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1885 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1886 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1887 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1888 |
|
tasks.add(new StringTask("0")); |
1889 |
< |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1889 |
> |
tasks.add(waiter); |
1890 |
|
tasks.add(new StringTask("2")); |
1891 |
|
long startTime = System.nanoTime(); |
1892 |
|
List<Future<String>> futures = |
1893 |
< |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1893 |
> |
p.invokeAll(tasks, timeout, MILLISECONDS); |
1894 |
|
assertEquals(tasks.size(), futures.size()); |
1895 |
|
assertTrue(millisElapsedSince(startTime) >= timeout); |
1896 |
|
for (Future future : futures) |
1999 |
|
* (in part, a test of CustomTPE itself) |
2000 |
|
*/ |
2001 |
|
public void testGet_cancelled() throws Exception { |
2002 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
2003 |
|
final ExecutorService e = |
2004 |
|
new CustomTPE(1, 1, |
2005 |
|
LONG_DELAY_MS, MILLISECONDS, |
2006 |
|
new LinkedBlockingQueue<Runnable>()); |
2007 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
2007 |
> |
try (PoolCleaner cleaner = cleaner(e, done)) { |
2008 |
|
final CountDownLatch blockerStarted = new CountDownLatch(1); |
2008 |
– |
final CountDownLatch done = new CountDownLatch(1); |
2009 |
|
final List<Future<?>> futures = new ArrayList<>(); |
2010 |
|
for (int i = 0; i < 2; i++) { |
2011 |
|
Runnable r = new CheckedRunnable() { public void realRun() |
2015 |
|
}}; |
2016 |
|
futures.add(e.submit(r)); |
2017 |
|
} |
2018 |
< |
assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
2018 |
> |
await(blockerStarted); |
2019 |
|
for (Future<?> future : futures) future.cancel(false); |
2020 |
|
for (Future<?> future : futures) { |
2021 |
|
try { |
2029 |
|
assertTrue(future.isCancelled()); |
2030 |
|
assertTrue(future.isDone()); |
2031 |
|
} |
2032 |
– |
done.countDown(); |
2032 |
|
} |
2033 |
|
} |
2034 |
|
|