101 |
|
* thread becomes active |
102 |
|
*/ |
103 |
|
public void testGetActiveCount() throws InterruptedException { |
104 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
105 |
|
final ThreadPoolExecutor p = |
106 |
|
new ThreadPoolExecutor(2, 2, |
107 |
|
LONG_DELAY_MS, MILLISECONDS, |
108 |
|
new ArrayBlockingQueue<Runnable>(10)); |
109 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
109 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
110 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
110 |
– |
final CountDownLatch done = new CountDownLatch(1); |
111 |
|
assertEquals(0, p.getActiveCount()); |
112 |
|
p.execute(new CheckedRunnable() { |
113 |
|
public void realRun() throws InterruptedException { |
114 |
|
threadStarted.countDown(); |
115 |
|
assertEquals(1, p.getActiveCount()); |
116 |
< |
done.await(); |
116 |
> |
await(done); |
117 |
|
}}); |
118 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
118 |
> |
await(threadStarted); |
119 |
|
assertEquals(1, p.getActiveCount()); |
120 |
– |
done.countDown(); |
120 |
|
} |
121 |
|
} |
122 |
|
|
328 |
|
*/ |
329 |
|
public void testGetLargestPoolSize() throws InterruptedException { |
330 |
|
final int THREADS = 3; |
331 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
332 |
|
final ThreadPoolExecutor p = |
333 |
|
new ThreadPoolExecutor(THREADS, THREADS, |
334 |
|
LONG_DELAY_MS, MILLISECONDS, |
335 |
|
new ArrayBlockingQueue<Runnable>(10)); |
336 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
337 |
< |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
338 |
< |
final CountDownLatch done = new CountDownLatch(1); |
336 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
337 |
|
assertEquals(0, p.getLargestPoolSize()); |
338 |
+ |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
339 |
|
for (int i = 0; i < THREADS; i++) |
340 |
|
p.execute(new CheckedRunnable() { |
341 |
|
public void realRun() throws InterruptedException { |
342 |
|
threadsStarted.countDown(); |
343 |
< |
done.await(); |
343 |
> |
await(done); |
344 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
345 |
|
}}); |
346 |
< |
assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
346 |
> |
await(threadsStarted); |
347 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
349 |
– |
done.countDown(); // release pool |
348 |
|
} |
349 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
350 |
|
} |
372 |
|
* become active |
373 |
|
*/ |
374 |
|
public void testGetPoolSize() throws InterruptedException { |
375 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
376 |
|
final ThreadPoolExecutor p = |
377 |
|
new ThreadPoolExecutor(1, 1, |
378 |
|
LONG_DELAY_MS, MILLISECONDS, |
379 |
|
new ArrayBlockingQueue<Runnable>(10)); |
380 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
382 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
383 |
< |
final CountDownLatch done = new CountDownLatch(1); |
380 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
381 |
|
assertEquals(0, p.getPoolSize()); |
382 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
383 |
|
p.execute(new CheckedRunnable() { |
384 |
|
public void realRun() throws InterruptedException { |
385 |
|
threadStarted.countDown(); |
386 |
|
assertEquals(1, p.getPoolSize()); |
387 |
< |
done.await(); |
387 |
> |
await(done); |
388 |
|
}}); |
389 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
389 |
> |
await(threadStarted); |
390 |
|
assertEquals(1, p.getPoolSize()); |
393 |
– |
done.countDown(); // release pool |
391 |
|
} |
392 |
|
} |
393 |
|
|
408 |
|
p.execute(new CheckedRunnable() { |
409 |
|
public void realRun() throws InterruptedException { |
410 |
|
threadStarted.countDown(); |
411 |
< |
done.await(); |
411 |
> |
await(done); |
412 |
|
}}); |
413 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
413 |
> |
await(threadStarted); |
414 |
|
assertEquals(1, p.getTaskCount()); |
415 |
|
assertEquals(0, p.getCompletedTaskCount()); |
416 |
|
for (int i = 0; i < TASKS; i++) { |
419 |
|
public void realRun() throws InterruptedException { |
420 |
|
threadStarted.countDown(); |
421 |
|
assertEquals(1 + TASKS, p.getTaskCount()); |
422 |
< |
done.await(); |
422 |
> |
await(done); |
423 |
|
}}); |
424 |
|
} |
425 |
|
assertEquals(1 + TASKS, p.getTaskCount()); |
492 |
|
public void realRun() throws InterruptedException { |
493 |
|
assertFalse(p.isTerminating()); |
494 |
|
threadStarted.countDown(); |
495 |
< |
done.await(); |
495 |
> |
await(done); |
496 |
|
}}); |
497 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
497 |
> |
await(threadStarted); |
498 |
|
assertFalse(p.isTerminating()); |
499 |
|
done.countDown(); |
500 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
520 |
|
public void realRun() throws InterruptedException { |
521 |
|
assertFalse(p.isTerminating()); |
522 |
|
threadStarted.countDown(); |
523 |
< |
done.await(); |
523 |
> |
await(done); |
524 |
|
}}); |
525 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
525 |
> |
await(threadStarted); |
526 |
|
assertFalse(p.isTerminating()); |
527 |
|
done.countDown(); |
528 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
536 |
|
* getQueue returns the work queue, which contains queued tasks |
537 |
|
*/ |
538 |
|
public void testGetQueue() throws InterruptedException { |
539 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
540 |
|
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
541 |
|
final ThreadPoolExecutor p = |
542 |
|
new ThreadPoolExecutor(1, 1, |
543 |
|
LONG_DELAY_MS, MILLISECONDS, |
544 |
|
q); |
545 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
545 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
546 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
549 |
– |
final CountDownLatch done = new CountDownLatch(1); |
547 |
|
FutureTask[] tasks = new FutureTask[5]; |
548 |
|
for (int i = 0; i < tasks.length; i++) { |
549 |
|
Callable task = new CheckedCallable<Boolean>() { |
550 |
|
public Boolean realCall() throws InterruptedException { |
551 |
|
threadStarted.countDown(); |
552 |
|
assertSame(q, p.getQueue()); |
553 |
< |
done.await(); |
553 |
> |
await(done); |
554 |
|
return Boolean.TRUE; |
555 |
|
}}; |
556 |
|
tasks[i] = new FutureTask(task); |
557 |
|
p.execute(tasks[i]); |
558 |
|
} |
559 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
559 |
> |
await(threadStarted); |
560 |
|
assertSame(q, p.getQueue()); |
561 |
|
assertFalse(q.contains(tasks[0])); |
562 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
563 |
|
assertEquals(tasks.length - 1, q.size()); |
567 |
– |
done.countDown(); |
564 |
|
} |
565 |
|
} |
566 |
|
|
568 |
|
* remove(task) removes queued task, and fails to remove active task |
569 |
|
*/ |
570 |
|
public void testRemove() throws InterruptedException { |
571 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
572 |
|
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
573 |
|
final ThreadPoolExecutor p = |
574 |
|
new ThreadPoolExecutor(1, 1, |
575 |
|
LONG_DELAY_MS, MILLISECONDS, |
576 |
|
q); |
577 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
577 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
578 |
|
Runnable[] tasks = new Runnable[6]; |
579 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
583 |
– |
final CountDownLatch done = new CountDownLatch(1); |
580 |
|
for (int i = 0; i < tasks.length; i++) { |
581 |
|
tasks[i] = new CheckedRunnable() { |
582 |
|
public void realRun() throws InterruptedException { |
583 |
|
threadStarted.countDown(); |
584 |
< |
done.await(); |
584 |
> |
await(done); |
585 |
|
}}; |
586 |
|
p.execute(tasks[i]); |
587 |
|
} |
588 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
588 |
> |
await(threadStarted); |
589 |
|
assertFalse(p.remove(tasks[0])); |
590 |
|
assertTrue(q.contains(tasks[4])); |
591 |
|
assertTrue(q.contains(tasks[3])); |
595 |
|
assertTrue(q.contains(tasks[3])); |
596 |
|
assertTrue(p.remove(tasks[3])); |
597 |
|
assertFalse(q.contains(tasks[3])); |
602 |
– |
done.countDown(); |
598 |
|
} |
599 |
|
} |
600 |
|
|
615 |
|
Callable task = new CheckedCallable<Boolean>() { |
616 |
|
public Boolean realCall() throws InterruptedException { |
617 |
|
threadStarted.countDown(); |
618 |
< |
done.await(); |
618 |
> |
await(done); |
619 |
|
return Boolean.TRUE; |
620 |
|
}}; |
621 |
|
tasks[i] = new FutureTask(task); |
622 |
|
p.execute(tasks[i]); |
623 |
|
} |
624 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
624 |
> |
await(threadStarted); |
625 |
|
assertEquals(tasks.length, p.getTaskCount()); |
626 |
|
assertEquals(tasks.length - 1, q.size()); |
627 |
|
assertEquals(1L, p.getActiveCount()); |
659 |
|
}}; |
660 |
|
for (int i = 0; i < count; i++) |
661 |
|
p.execute(waiter); |
662 |
< |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
662 |
> |
await(threadsStarted); |
663 |
|
assertEquals(poolSize, p.getActiveCount()); |
664 |
|
assertEquals(0, p.getCompletedTaskCount()); |
665 |
|
final List<Runnable> queuedTasks; |
1021 |
|
* get of submitted callable throws InterruptedException if interrupted |
1022 |
|
*/ |
1023 |
|
public void testInterruptedSubmit() throws InterruptedException { |
1024 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1025 |
|
final ThreadPoolExecutor p = |
1026 |
|
new ThreadPoolExecutor(1, 1, |
1027 |
|
60, SECONDS, |
1028 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1029 |
|
|
1030 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1030 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1031 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1036 |
– |
final CountDownLatch done = new CountDownLatch(1); |
1032 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
1033 |
|
public void realRun() throws Exception { |
1034 |
|
Callable task = new CheckedCallable<Boolean>() { |
1035 |
|
public Boolean realCall() throws InterruptedException { |
1036 |
|
threadStarted.countDown(); |
1037 |
< |
done.await(); |
1037 |
> |
await(done); |
1038 |
|
return Boolean.TRUE; |
1039 |
|
}}; |
1040 |
|
p.submit(task).get(); |
1041 |
|
}}); |
1042 |
|
|
1043 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
1043 |
> |
await(threadStarted); |
1044 |
|
t.interrupt(); |
1045 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
1051 |
< |
done.countDown(); |
1045 |
> |
awaitTermination(t); |
1046 |
|
} |
1047 |
|
} |
1048 |
|
|
1050 |
|
* execute throws RejectedExecutionException if saturated. |
1051 |
|
*/ |
1052 |
|
public void testSaturatedExecute() { |
1053 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1054 |
|
final ThreadPoolExecutor p = |
1055 |
|
new ThreadPoolExecutor(1, 1, |
1056 |
|
LONG_DELAY_MS, MILLISECONDS, |
1057 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1058 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1064 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1058 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1059 |
|
Runnable task = new CheckedRunnable() { |
1060 |
|
public void realRun() throws InterruptedException { |
1061 |
< |
done.await(); |
1061 |
> |
await(done); |
1062 |
|
}}; |
1063 |
|
for (int i = 0; i < 2; ++i) |
1064 |
|
p.execute(task); |
1069 |
|
} catch (RejectedExecutionException success) {} |
1070 |
|
assertTrue(p.getTaskCount() <= 2); |
1071 |
|
} |
1078 |
– |
done.countDown(); |
1072 |
|
} |
1073 |
|
} |
1074 |
|
|
1076 |
|
* submit(runnable) throws RejectedExecutionException if saturated. |
1077 |
|
*/ |
1078 |
|
public void testSaturatedSubmitRunnable() { |
1079 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1080 |
|
final ThreadPoolExecutor p = |
1081 |
|
new ThreadPoolExecutor(1, 1, |
1082 |
|
LONG_DELAY_MS, MILLISECONDS, |
1083 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1084 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1091 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1084 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1085 |
|
Runnable task = new CheckedRunnable() { |
1086 |
|
public void realRun() throws InterruptedException { |
1087 |
< |
done.await(); |
1087 |
> |
await(done); |
1088 |
|
}}; |
1089 |
|
for (int i = 0; i < 2; ++i) |
1090 |
|
p.submit(task); |
1095 |
|
} catch (RejectedExecutionException success) {} |
1096 |
|
assertTrue(p.getTaskCount() <= 2); |
1097 |
|
} |
1105 |
– |
done.countDown(); |
1098 |
|
} |
1099 |
|
} |
1100 |
|
|
1102 |
|
* submit(callable) throws RejectedExecutionException if saturated. |
1103 |
|
*/ |
1104 |
|
public void testSaturatedSubmitCallable() { |
1105 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1106 |
|
final ThreadPoolExecutor p = |
1107 |
|
new ThreadPoolExecutor(1, 1, |
1108 |
|
LONG_DELAY_MS, MILLISECONDS, |
1109 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1110 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1118 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1110 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1111 |
|
Runnable task = new CheckedRunnable() { |
1112 |
|
public void realRun() throws InterruptedException { |
1113 |
< |
done.await(); |
1113 |
> |
await(done); |
1114 |
|
}}; |
1115 |
|
for (int i = 0; i < 2; ++i) |
1116 |
|
p.submit(Executors.callable(task)); |
1121 |
|
} catch (RejectedExecutionException success) {} |
1122 |
|
assertTrue(p.getTaskCount() <= 2); |
1123 |
|
} |
1132 |
– |
done.countDown(); |
1124 |
|
} |
1125 |
|
} |
1126 |
|
|
1138 |
|
final CountDownLatch done = new CountDownLatch(1); |
1139 |
|
Runnable blocker = new CheckedRunnable() { |
1140 |
|
public void realRun() throws InterruptedException { |
1141 |
< |
done.await(); |
1141 |
> |
await(done); |
1142 |
|
}}; |
1143 |
|
p.execute(blocker); |
1144 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1157 |
|
* executor using DiscardPolicy drops task if saturated. |
1158 |
|
*/ |
1159 |
|
public void testSaturatedExecute3() { |
1160 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1161 |
|
final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1162 |
|
for (int i = 0; i < tasks.length; ++i) |
1163 |
|
tasks[i] = new TrackedNoOpRunnable(); |
1166 |
|
LONG_DELAY_MS, MILLISECONDS, |
1167 |
|
new ArrayBlockingQueue<Runnable>(1), |
1168 |
|
new ThreadPoolExecutor.DiscardPolicy()); |
1169 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1178 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1169 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1170 |
|
p.execute(awaiter(done)); |
1171 |
|
|
1172 |
|
for (TrackedNoOpRunnable task : tasks) |
1173 |
|
p.execute(task); |
1174 |
|
for (int i = 1; i < tasks.length; i++) |
1175 |
|
assertFalse(tasks[i].done); |
1185 |
– |
done.countDown(); |
1176 |
|
} |
1177 |
|
for (int i = 1; i < tasks.length; i++) |
1178 |
|
assertFalse(tasks[i].done); |
1192 |
|
LONG_DELAY_MS, MILLISECONDS, |
1193 |
|
new ArrayBlockingQueue<Runnable>(1), |
1194 |
|
new ThreadPoolExecutor.DiscardOldestPolicy()); |
1195 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1195 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1196 |
|
assertEquals(LatchAwaiter.NEW, r1.state); |
1197 |
|
assertEquals(LatchAwaiter.NEW, r2.state); |
1198 |
|
assertEquals(LatchAwaiter.NEW, r3.state); |
1202 |
|
p.execute(r3); |
1203 |
|
assertFalse(p.getQueue().contains(r2)); |
1204 |
|
assertTrue(p.getQueue().contains(r3)); |
1215 |
– |
done.countDown(); |
1205 |
|
} |
1206 |
|
assertEquals(LatchAwaiter.DONE, r1.state); |
1207 |
|
assertEquals(LatchAwaiter.NEW, r2.state); |
1835 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1836 |
|
l.add(new NPETask()); |
1837 |
|
List<Future<String>> futures = |
1838 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1838 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1839 |
|
assertEquals(1, futures.size()); |
1840 |
|
try { |
1841 |
|
futures.get(0).get(); |
2020 |
|
* get(cancelled task) throws CancellationException |
2021 |
|
*/ |
2022 |
|
public void testGet_cancelled() throws Exception { |
2023 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
2024 |
|
final ExecutorService e = |
2025 |
|
new ThreadPoolExecutor(1, 1, |
2026 |
|
LONG_DELAY_MS, MILLISECONDS, |
2027 |
|
new LinkedBlockingQueue<Runnable>()); |
2028 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
2028 |
> |
try (PoolCleaner cleaner = cleaner(e, done)) { |
2029 |
|
final CountDownLatch blockerStarted = new CountDownLatch(1); |
2040 |
– |
final CountDownLatch done = new CountDownLatch(1); |
2030 |
|
final List<Future<?>> futures = new ArrayList<>(); |
2031 |
|
for (int i = 0; i < 2; i++) { |
2032 |
|
Runnable r = new CheckedRunnable() { public void realRun() |
2036 |
|
}}; |
2037 |
|
futures.add(e.submit(r)); |
2038 |
|
} |
2039 |
< |
assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
2039 |
> |
await(blockerStarted); |
2040 |
|
for (Future<?> future : futures) future.cancel(false); |
2041 |
|
for (Future<?> future : futures) { |
2042 |
|
try { |
2050 |
|
assertTrue(future.isCancelled()); |
2051 |
|
assertTrue(future.isDone()); |
2052 |
|
} |
2064 |
– |
done.countDown(); |
2053 |
|
} |
2054 |
|
} |
2055 |
|
|