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 { |
262 |
|
assertEquals(1, p.getActiveCount()); |
263 |
|
await(done); |
264 |
|
}}); |
265 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
265 |
> |
await(threadStarted); |
266 |
|
assertEquals(1, p.getActiveCount()); |
267 |
– |
done.countDown(); |
267 |
|
} |
268 |
|
} |
269 |
|
|
533 |
|
assertEquals(1, p.getPoolSize()); |
534 |
|
await(done); |
535 |
|
}}); |
536 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
536 |
> |
await(threadStarted); |
537 |
|
assertEquals(1, p.getPoolSize()); |
538 |
|
} |
539 |
|
} |
557 |
|
threadStarted.countDown(); |
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++) { |
609 |
|
threadStarted.countDown(); |
610 |
|
await(done); |
611 |
|
}}); |
612 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
612 |
> |
await(threadStarted); |
613 |
|
assertFalse(p.isTerminating()); |
614 |
|
done.countDown(); |
615 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
637 |
|
threadStarted.countDown(); |
638 |
|
await(done); |
639 |
|
}}); |
640 |
< |
assertTrue(threadStarted.await(LONG_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); |
662 |
– |
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>() { |
671 |
|
tasks[i] = new FutureTask(task); |
672 |
|
p.execute(tasks[i]); |
673 |
|
} |
674 |
< |
assertTrue(threadStarted.await(LONG_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()); |
680 |
– |
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); |
696 |
– |
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 { |
700 |
|
}}; |
701 |
|
p.execute(tasks[i]); |
702 |
|
} |
703 |
< |
assertTrue(threadStarted.await(LONG_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])); |
715 |
– |
done.countDown(); |
713 |
|
} |
714 |
|
} |
715 |
|
|
736 |
|
tasks[i] = new FutureTask(task); |
737 |
|
p.execute(tasks[i]); |
738 |
|
} |
739 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
739 |
> |
await(threadStarted); |
740 |
|
assertEquals(tasks.length, p.getTaskCount()); |
741 |
|
assertEquals(tasks.length - 1, q.size()); |
742 |
|
assertEquals(1L, p.getActiveCount()); |
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)) { |
1144 |
< |
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 |
|
await(done); |
1152 |
|
} catch (RejectedExecutionException success) {} |
1153 |
|
assertTrue(p.getTaskCount() <= 2); |
1154 |
|
} |
1158 |
– |
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)) { |
1172 |
< |
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 |
|
await(done); |
1179 |
|
for (int i = 1; i < tasks.length; i++) |
1180 |
|
assertTrue(tasks[i].done); |
1181 |
|
assertFalse(tasks[0].done); // waiting in queue |
1186 |
– |
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)) { |
1203 |
< |
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); |
1210 |
– |
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)); |
1240 |
– |
done.countDown(); |
1234 |
|
} |
1235 |
|
assertEquals(LatchAwaiter.DONE, r1.state); |
1236 |
|
assertEquals(LatchAwaiter.NEW, r2.state); |
1989 |
|
* (in part, a test of CustomTPE itself) |
1990 |
|
*/ |
1991 |
|
public void testGet_cancelled() throws Exception { |
1992 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1993 |
|
final ExecutorService e = |
1994 |
|
new CustomTPE(1, 1, |
1995 |
|
LONG_DELAY_MS, MILLISECONDS, |
1996 |
|
new LinkedBlockingQueue<Runnable>()); |
1997 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
1997 |
> |
try (PoolCleaner cleaner = cleaner(e, done)) { |
1998 |
|
final CountDownLatch blockerStarted = new CountDownLatch(1); |
2005 |
– |
final CountDownLatch done = new CountDownLatch(1); |
1999 |
|
final List<Future<?>> futures = new ArrayList<>(); |
2000 |
|
for (int i = 0; i < 2; i++) { |
2001 |
|
Runnable r = new CheckedRunnable() { public void realRun() |
2005 |
|
}}; |
2006 |
|
futures.add(e.submit(r)); |
2007 |
|
} |
2008 |
< |
assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
2008 |
> |
await(blockerStarted); |
2009 |
|
for (Future<?> future : futures) future.cancel(false); |
2010 |
|
for (Future<?> future : futures) { |
2011 |
|
try { |
2019 |
|
assertTrue(future.isCancelled()); |
2020 |
|
assertTrue(future.isDone()); |
2021 |
|
} |
2029 |
– |
done.countDown(); |
2022 |
|
} |
2023 |
|
} |
2024 |
|
|