10 |
|
import static java.util.concurrent.TimeUnit.SECONDS; |
11 |
|
|
12 |
|
import java.util.ArrayList; |
13 |
+ |
import java.util.Collection; |
14 |
+ |
import java.util.Collections; |
15 |
|
import java.util.List; |
16 |
|
import java.util.concurrent.ArrayBlockingQueue; |
17 |
|
import java.util.concurrent.BlockingQueue; |
19 |
|
import java.util.concurrent.CancellationException; |
20 |
|
import java.util.concurrent.CountDownLatch; |
21 |
|
import java.util.concurrent.ExecutionException; |
20 |
– |
import java.util.concurrent.Executors; |
22 |
|
import java.util.concurrent.ExecutorService; |
23 |
|
import java.util.concurrent.Future; |
24 |
|
import java.util.concurrent.FutureTask; |
240 |
|
final Runnable task = new CheckedRunnable() { |
241 |
|
public void realRun() { done.countDown(); }}; |
242 |
|
p.execute(task); |
243 |
< |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
243 |
> |
await(done); |
244 |
|
} |
245 |
|
} |
246 |
|
|
249 |
|
* thread becomes active |
250 |
|
*/ |
251 |
|
public void testGetActiveCount() throws InterruptedException { |
252 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
253 |
|
final ThreadPoolExecutor p = |
254 |
|
new CustomTPE(2, 2, |
255 |
|
LONG_DELAY_MS, MILLISECONDS, |
256 |
|
new ArrayBlockingQueue<Runnable>(10)); |
257 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
257 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
258 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
257 |
– |
final CountDownLatch done = new CountDownLatch(1); |
259 |
|
assertEquals(0, p.getActiveCount()); |
260 |
|
p.execute(new CheckedRunnable() { |
261 |
|
public void realRun() throws InterruptedException { |
262 |
|
threadStarted.countDown(); |
263 |
|
assertEquals(1, p.getActiveCount()); |
264 |
< |
done.await(); |
264 |
> |
await(done); |
265 |
|
}}); |
266 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
266 |
> |
await(threadStarted); |
267 |
|
assertEquals(1, p.getActiveCount()); |
267 |
– |
done.countDown(); |
268 |
|
} |
269 |
|
} |
270 |
|
|
334 |
|
public void realRun() throws InterruptedException { |
335 |
|
threadStarted.countDown(); |
336 |
|
assertEquals(0, p.getCompletedTaskCount()); |
337 |
< |
threadProceed.await(); |
337 |
> |
await(threadProceed); |
338 |
|
threadDone.countDown(); |
339 |
|
}}); |
340 |
|
await(threadStarted); |
341 |
|
assertEquals(0, p.getCompletedTaskCount()); |
342 |
|
threadProceed.countDown(); |
343 |
< |
threadDone.await(); |
343 |
> |
await(threadDone); |
344 |
|
long startTime = System.nanoTime(); |
345 |
|
while (p.getCompletedTaskCount() != 1) { |
346 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
476 |
|
*/ |
477 |
|
public void testGetLargestPoolSize() throws InterruptedException { |
478 |
|
final int THREADS = 3; |
479 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
480 |
|
final ThreadPoolExecutor p = |
481 |
|
new CustomTPE(THREADS, THREADS, |
482 |
|
LONG_DELAY_MS, MILLISECONDS, |
483 |
|
new ArrayBlockingQueue<Runnable>(10)); |
484 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
484 |
< |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
485 |
< |
final CountDownLatch done = new CountDownLatch(1); |
484 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
485 |
|
assertEquals(0, p.getLargestPoolSize()); |
486 |
+ |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
487 |
|
for (int i = 0; i < THREADS; i++) |
488 |
|
p.execute(new CheckedRunnable() { |
489 |
|
public void realRun() throws InterruptedException { |
490 |
|
threadsStarted.countDown(); |
491 |
< |
done.await(); |
491 |
> |
await(done); |
492 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
493 |
|
}}); |
494 |
< |
assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
494 |
> |
await(threadsStarted); |
495 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
496 |
– |
done.countDown(); // release pool |
496 |
|
} |
497 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
498 |
|
} |
520 |
|
* become active |
521 |
|
*/ |
522 |
|
public void testGetPoolSize() throws InterruptedException { |
523 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
524 |
|
final ThreadPoolExecutor p = |
525 |
|
new CustomTPE(1, 1, |
526 |
|
LONG_DELAY_MS, MILLISECONDS, |
527 |
|
new ArrayBlockingQueue<Runnable>(10)); |
528 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
529 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
530 |
< |
final CountDownLatch done = new CountDownLatch(1); |
528 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
529 |
|
assertEquals(0, p.getPoolSize()); |
530 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
531 |
|
p.execute(new CheckedRunnable() { |
532 |
|
public void realRun() throws InterruptedException { |
533 |
|
threadStarted.countDown(); |
534 |
|
assertEquals(1, p.getPoolSize()); |
535 |
< |
done.await(); |
535 |
> |
await(done); |
536 |
|
}}); |
537 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
537 |
> |
await(threadStarted); |
538 |
|
assertEquals(1, p.getPoolSize()); |
540 |
– |
done.countDown(); // release pool |
539 |
|
} |
540 |
|
} |
541 |
|
|
543 |
|
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
544 |
|
*/ |
545 |
|
public void testGetTaskCount() throws InterruptedException { |
546 |
+ |
final int TASKS = 3; |
547 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
548 |
|
final ThreadPoolExecutor p = |
549 |
|
new CustomTPE(1, 1, |
550 |
|
LONG_DELAY_MS, MILLISECONDS, |
551 |
|
new ArrayBlockingQueue<Runnable>(10)); |
552 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
552 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
553 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
554 |
– |
final CountDownLatch done = new CountDownLatch(1); |
554 |
|
assertEquals(0, p.getTaskCount()); |
555 |
+ |
assertEquals(0, p.getCompletedTaskCount()); |
556 |
|
p.execute(new CheckedRunnable() { |
557 |
|
public void realRun() throws InterruptedException { |
558 |
|
threadStarted.countDown(); |
559 |
< |
assertEquals(1, p.getTaskCount()); |
560 |
< |
done.await(); |
559 |
> |
await(done); |
560 |
|
}}); |
561 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
561 |
> |
await(threadStarted); |
562 |
|
assertEquals(1, p.getTaskCount()); |
563 |
< |
done.countDown(); |
563 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
564 |
> |
for (int i = 0; i < TASKS; i++) { |
565 |
> |
assertEquals(1 + i, p.getTaskCount()); |
566 |
> |
p.execute(new CheckedRunnable() { |
567 |
> |
public void realRun() throws InterruptedException { |
568 |
> |
threadStarted.countDown(); |
569 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
570 |
> |
await(done); |
571 |
> |
}}); |
572 |
> |
} |
573 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
574 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
575 |
|
} |
576 |
+ |
assertEquals(1 + TASKS, p.getTaskCount()); |
577 |
+ |
assertEquals(1 + TASKS, p.getCompletedTaskCount()); |
578 |
|
} |
579 |
|
|
580 |
|
/** |
608 |
|
public void realRun() throws InterruptedException { |
609 |
|
assertFalse(p.isTerminating()); |
610 |
|
threadStarted.countDown(); |
611 |
< |
done.await(); |
611 |
> |
await(done); |
612 |
|
}}); |
613 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
613 |
> |
await(threadStarted); |
614 |
|
assertFalse(p.isTerminating()); |
615 |
|
done.countDown(); |
616 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
636 |
|
public void realRun() throws InterruptedException { |
637 |
|
assertFalse(p.isTerminating()); |
638 |
|
threadStarted.countDown(); |
639 |
< |
done.await(); |
639 |
> |
await(done); |
640 |
|
}}); |
641 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
641 |
> |
await(threadStarted); |
642 |
|
assertFalse(p.isTerminating()); |
643 |
|
done.countDown(); |
644 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
652 |
|
* getQueue returns the work queue, which contains queued tasks |
653 |
|
*/ |
654 |
|
public void testGetQueue() throws InterruptedException { |
655 |
< |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
655 |
> |
final CountDownLatch done = new CountDownLatch(1); |
656 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
657 |
|
final ThreadPoolExecutor p = |
658 |
|
new CustomTPE(1, 1, |
659 |
|
LONG_DELAY_MS, MILLISECONDS, |
660 |
|
q); |
661 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
661 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
662 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
650 |
– |
final CountDownLatch done = new CountDownLatch(1); |
663 |
|
FutureTask[] tasks = new FutureTask[5]; |
664 |
|
for (int i = 0; i < tasks.length; i++) { |
665 |
< |
Callable task = new CheckedCallable<Boolean>() { |
665 |
> |
Callable<Boolean> task = new CheckedCallable<Boolean>() { |
666 |
|
public Boolean realCall() throws InterruptedException { |
667 |
|
threadStarted.countDown(); |
668 |
|
assertSame(q, p.getQueue()); |
669 |
< |
done.await(); |
669 |
> |
await(done); |
670 |
|
return Boolean.TRUE; |
671 |
|
}}; |
672 |
|
tasks[i] = new FutureTask(task); |
673 |
|
p.execute(tasks[i]); |
674 |
|
} |
675 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
675 |
> |
await(threadStarted); |
676 |
|
assertSame(q, p.getQueue()); |
677 |
|
assertFalse(q.contains(tasks[0])); |
678 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
679 |
|
assertEquals(tasks.length - 1, q.size()); |
668 |
– |
done.countDown(); |
680 |
|
} |
681 |
|
} |
682 |
|
|
684 |
|
* remove(task) removes queued task, and fails to remove active task |
685 |
|
*/ |
686 |
|
public void testRemove() throws InterruptedException { |
687 |
< |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
687 |
> |
final CountDownLatch done = new CountDownLatch(1); |
688 |
> |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
689 |
|
final ThreadPoolExecutor p = |
690 |
|
new CustomTPE(1, 1, |
691 |
|
LONG_DELAY_MS, MILLISECONDS, |
692 |
|
q); |
693 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
693 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
694 |
|
Runnable[] tasks = new Runnable[6]; |
695 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
684 |
– |
final CountDownLatch done = new CountDownLatch(1); |
696 |
|
for (int i = 0; i < tasks.length; i++) { |
697 |
|
tasks[i] = new CheckedRunnable() { |
698 |
|
public void realRun() throws InterruptedException { |
699 |
|
threadStarted.countDown(); |
700 |
< |
done.await(); |
700 |
> |
await(done); |
701 |
|
}}; |
702 |
|
p.execute(tasks[i]); |
703 |
|
} |
704 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
704 |
> |
await(threadStarted); |
705 |
|
assertFalse(p.remove(tasks[0])); |
706 |
|
assertTrue(q.contains(tasks[4])); |
707 |
|
assertTrue(q.contains(tasks[3])); |
711 |
|
assertTrue(q.contains(tasks[3])); |
712 |
|
assertTrue(p.remove(tasks[3])); |
713 |
|
assertFalse(q.contains(tasks[3])); |
703 |
– |
done.countDown(); |
714 |
|
} |
715 |
|
} |
716 |
|
|
720 |
|
public void testPurge() throws InterruptedException { |
721 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
722 |
|
final CountDownLatch done = new CountDownLatch(1); |
723 |
< |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
723 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
724 |
|
final ThreadPoolExecutor p = |
725 |
|
new CustomTPE(1, 1, |
726 |
|
LONG_DELAY_MS, MILLISECONDS, |
727 |
|
q); |
728 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
728 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
729 |
|
FutureTask[] tasks = new FutureTask[5]; |
730 |
|
for (int i = 0; i < tasks.length; i++) { |
731 |
< |
Callable task = new CheckedCallable<Boolean>() { |
731 |
> |
Callable<Boolean> task = new CheckedCallable<Boolean>() { |
732 |
|
public Boolean realCall() throws InterruptedException { |
733 |
|
threadStarted.countDown(); |
734 |
< |
done.await(); |
734 |
> |
await(done); |
735 |
|
return Boolean.TRUE; |
736 |
|
}}; |
737 |
|
tasks[i] = new FutureTask(task); |
738 |
|
p.execute(tasks[i]); |
739 |
|
} |
740 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
740 |
> |
await(threadStarted); |
741 |
|
assertEquals(tasks.length, p.getTaskCount()); |
742 |
|
assertEquals(tasks.length - 1, q.size()); |
743 |
|
assertEquals(1L, p.getActiveCount()); |
750 |
|
p.purge(); // Nothing to do |
751 |
|
assertEquals(tasks.length - 3, q.size()); |
752 |
|
assertEquals(tasks.length - 2, p.getTaskCount()); |
743 |
– |
done.countDown(); |
753 |
|
} |
754 |
|
} |
755 |
|
|
765 |
|
new CustomTPE(poolSize, poolSize, |
766 |
|
LONG_DELAY_MS, MILLISECONDS, |
767 |
|
new ArrayBlockingQueue<Runnable>(10)); |
768 |
< |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
768 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
769 |
|
Runnable waiter = new CheckedRunnable() { public void realRun() { |
770 |
|
threadsStarted.countDown(); |
771 |
|
try { |
775 |
|
}}; |
776 |
|
for (int i = 0; i < count; i++) |
777 |
|
p.execute(waiter); |
778 |
< |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
778 |
> |
await(threadsStarted); |
779 |
|
assertEquals(poolSize, p.getActiveCount()); |
780 |
|
assertEquals(0, p.getCompletedTaskCount()); |
781 |
|
final List<Runnable> queuedTasks; |
1134 |
|
* execute throws RejectedExecutionException if saturated. |
1135 |
|
*/ |
1136 |
|
public void testSaturatedExecute() { |
1137 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1138 |
|
final ThreadPoolExecutor p = |
1139 |
|
new CustomTPE(1, 1, |
1140 |
|
LONG_DELAY_MS, MILLISECONDS, |
1141 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1142 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1133 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1142 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1143 |
|
Runnable task = new CheckedRunnable() { |
1144 |
|
public void realRun() throws InterruptedException { |
1145 |
< |
done.await(); |
1145 |
> |
await(done); |
1146 |
|
}}; |
1147 |
|
for (int i = 0; i < 2; ++i) |
1148 |
|
p.execute(task); |
1153 |
|
} catch (RejectedExecutionException success) {} |
1154 |
|
assertTrue(p.getTaskCount() <= 2); |
1155 |
|
} |
1147 |
– |
done.countDown(); |
1156 |
|
} |
1157 |
|
} |
1158 |
|
|
1160 |
|
* executor using CallerRunsPolicy runs task if saturated. |
1161 |
|
*/ |
1162 |
|
public void testSaturatedExecute2() { |
1163 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1164 |
|
final ThreadPoolExecutor p = |
1165 |
|
new CustomTPE(1, 1, |
1166 |
|
LONG_DELAY_MS, MILLISECONDS, |
1167 |
|
new ArrayBlockingQueue<Runnable>(1), |
1168 |
|
new CustomTPE.CallerRunsPolicy()); |
1169 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1161 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1169 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1170 |
|
Runnable blocker = new CheckedRunnable() { |
1171 |
|
public void realRun() throws InterruptedException { |
1172 |
< |
done.await(); |
1172 |
> |
await(done); |
1173 |
|
}}; |
1174 |
|
p.execute(blocker); |
1175 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1180 |
|
for (int i = 1; i < tasks.length; i++) |
1181 |
|
assertTrue(tasks[i].done); |
1182 |
|
assertFalse(tasks[0].done); // waiting in queue |
1175 |
– |
done.countDown(); |
1183 |
|
} |
1184 |
|
} |
1185 |
|
|
1190 |
|
final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1191 |
|
for (int i = 0; i < tasks.length; ++i) |
1192 |
|
tasks[i] = new TrackedNoOpRunnable(); |
1193 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1194 |
|
final ThreadPoolExecutor p = |
1195 |
|
new CustomTPE(1, 1, |
1196 |
|
LONG_DELAY_MS, MILLISECONDS, |
1197 |
|
new ArrayBlockingQueue<Runnable>(1), |
1198 |
|
new CustomTPE.DiscardPolicy()); |
1199 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1192 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1199 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1200 |
|
p.execute(awaiter(done)); |
1201 |
|
|
1202 |
|
for (TrackedNoOpRunnable task : tasks) |
1203 |
|
p.execute(task); |
1204 |
|
for (int i = 1; i < tasks.length; i++) |
1205 |
|
assertFalse(tasks[i].done); |
1199 |
– |
done.countDown(); |
1206 |
|
} |
1207 |
|
for (int i = 1; i < tasks.length; i++) |
1208 |
|
assertFalse(tasks[i].done); |
1222 |
|
LONG_DELAY_MS, MILLISECONDS, |
1223 |
|
new ArrayBlockingQueue<Runnable>(1), |
1224 |
|
new CustomTPE.DiscardOldestPolicy()); |
1225 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1225 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1226 |
|
assertEquals(LatchAwaiter.NEW, r1.state); |
1227 |
|
assertEquals(LatchAwaiter.NEW, r2.state); |
1228 |
|
assertEquals(LatchAwaiter.NEW, r3.state); |
1232 |
|
p.execute(r3); |
1233 |
|
assertFalse(p.getQueue().contains(r2)); |
1234 |
|
assertTrue(p.getQueue().contains(r3)); |
1229 |
– |
done.countDown(); |
1235 |
|
} |
1236 |
|
assertEquals(LatchAwaiter.DONE, r1.state); |
1237 |
|
assertEquals(LatchAwaiter.NEW, r2.state); |
1348 |
|
new CustomTPE(2, 3, |
1349 |
|
LONG_DELAY_MS, MILLISECONDS, |
1350 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1351 |
< |
try { |
1352 |
< |
p.setMaximumPoolSize(1); |
1353 |
< |
shouldThrow(); |
1354 |
< |
} catch (IllegalArgumentException success) { |
1355 |
< |
} finally { |
1351 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1351 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1352 |
> |
try { |
1353 |
> |
p.setMaximumPoolSize(1); |
1354 |
> |
shouldThrow(); |
1355 |
> |
} catch (IllegalArgumentException success) {} |
1356 |
|
} |
1353 |
– |
joinPool(p); |
1357 |
|
} |
1358 |
|
|
1359 |
|
/** |
1365 |
|
new CustomTPE(2, 3, |
1366 |
|
LONG_DELAY_MS, |
1367 |
|
MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1368 |
< |
try { |
1369 |
< |
p.setMaximumPoolSize(-1); |
1370 |
< |
shouldThrow(); |
1371 |
< |
} catch (IllegalArgumentException success) { |
1372 |
< |
} finally { |
1370 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1368 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1369 |
> |
try { |
1370 |
> |
p.setMaximumPoolSize(-1); |
1371 |
> |
shouldThrow(); |
1372 |
> |
} catch (IllegalArgumentException success) {} |
1373 |
|
} |
1372 |
– |
joinPool(p); |
1374 |
|
} |
1375 |
|
|
1376 |
|
/** |
1382 |
|
new CustomTPE(2, 3, |
1383 |
|
LONG_DELAY_MS, MILLISECONDS, |
1384 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1385 |
< |
|
1386 |
< |
try { |
1387 |
< |
p.setKeepAliveTime(-1,MILLISECONDS); |
1388 |
< |
shouldThrow(); |
1389 |
< |
} catch (IllegalArgumentException success) { |
1389 |
< |
} finally { |
1390 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1385 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1386 |
> |
try { |
1387 |
> |
p.setKeepAliveTime(-1, MILLISECONDS); |
1388 |
> |
shouldThrow(); |
1389 |
> |
} catch (IllegalArgumentException success) {} |
1390 |
|
} |
1392 |
– |
joinPool(p); |
1391 |
|
} |
1392 |
|
|
1393 |
|
/** |
1395 |
|
*/ |
1396 |
|
public void testTerminated() { |
1397 |
|
CustomTPE p = new CustomTPE(); |
1398 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1399 |
< |
assertTrue(p.terminatedCalled()); |
1400 |
< |
joinPool(p); |
1398 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1399 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1400 |
> |
assertTrue(p.terminatedCalled()); |
1401 |
> |
assertTrue(p.isShutdown()); |
1402 |
> |
} |
1403 |
|
} |
1404 |
|
|
1405 |
|
/** |
1407 |
|
*/ |
1408 |
|
public void testBeforeAfter() throws InterruptedException { |
1409 |
|
CustomTPE p = new CustomTPE(); |
1410 |
< |
try { |
1410 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1411 |
|
final CountDownLatch done = new CountDownLatch(1); |
1412 |
|
p.execute(new CheckedRunnable() { |
1413 |
|
public void realRun() { |
1417 |
|
assertEquals(0, done.getCount()); |
1418 |
|
assertTrue(p.afterCalled()); |
1419 |
|
assertTrue(p.beforeCalled()); |
1420 |
– |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1421 |
– |
} finally { |
1422 |
– |
joinPool(p); |
1420 |
|
} |
1421 |
|
} |
1422 |
|
|
1428 |
|
new CustomTPE(2, 2, |
1429 |
|
LONG_DELAY_MS, MILLISECONDS, |
1430 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1431 |
< |
try { |
1431 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1432 |
|
Future<String> future = e.submit(new StringTask()); |
1433 |
|
String result = future.get(); |
1434 |
|
assertSame(TEST_STRING, result); |
1438 |
– |
} finally { |
1439 |
– |
joinPool(e); |
1435 |
|
} |
1436 |
|
} |
1437 |
|
|
1443 |
|
new CustomTPE(2, 2, |
1444 |
|
LONG_DELAY_MS, MILLISECONDS, |
1445 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1446 |
< |
try { |
1446 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1447 |
|
Future<?> future = e.submit(new NoOpRunnable()); |
1448 |
|
future.get(); |
1449 |
|
assertTrue(future.isDone()); |
1455 |
– |
} finally { |
1456 |
– |
joinPool(e); |
1450 |
|
} |
1451 |
|
} |
1452 |
|
|
1458 |
|
new CustomTPE(2, 2, |
1459 |
|
LONG_DELAY_MS, MILLISECONDS, |
1460 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1461 |
< |
try { |
1461 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1462 |
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
1463 |
|
String result = future.get(); |
1464 |
|
assertSame(TEST_STRING, result); |
1472 |
– |
} finally { |
1473 |
– |
joinPool(e); |
1465 |
|
} |
1466 |
|
} |
1467 |
|
|
1468 |
|
/** |
1469 |
< |
* invokeAny(null) throws NPE |
1469 |
> |
* invokeAny(null) throws NullPointerException |
1470 |
|
*/ |
1471 |
|
public void testInvokeAny1() throws Exception { |
1472 |
|
final ExecutorService e = |
1473 |
|
new CustomTPE(2, 2, |
1474 |
|
LONG_DELAY_MS, MILLISECONDS, |
1475 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1476 |
< |
try { |
1477 |
< |
e.invokeAny(null); |
1478 |
< |
shouldThrow(); |
1479 |
< |
} catch (NullPointerException success) { |
1480 |
< |
} finally { |
1490 |
< |
joinPool(e); |
1476 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1477 |
> |
try { |
1478 |
> |
e.invokeAny(null); |
1479 |
> |
shouldThrow(); |
1480 |
> |
} catch (NullPointerException success) {} |
1481 |
|
} |
1482 |
|
} |
1483 |
|
|
1484 |
|
/** |
1485 |
< |
* invokeAny(empty collection) throws IAE |
1485 |
> |
* invokeAny(empty collection) throws IllegalArgumentException |
1486 |
|
*/ |
1487 |
|
public void testInvokeAny2() throws Exception { |
1488 |
|
final ExecutorService e = |
1489 |
|
new CustomTPE(2, 2, |
1490 |
|
LONG_DELAY_MS, MILLISECONDS, |
1491 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1492 |
< |
try { |
1493 |
< |
e.invokeAny(new ArrayList<Callable<String>>()); |
1494 |
< |
shouldThrow(); |
1495 |
< |
} catch (IllegalArgumentException success) { |
1496 |
< |
} finally { |
1507 |
< |
joinPool(e); |
1492 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1493 |
> |
try { |
1494 |
> |
e.invokeAny(new ArrayList<Callable<String>>()); |
1495 |
> |
shouldThrow(); |
1496 |
> |
} catch (IllegalArgumentException success) {} |
1497 |
|
} |
1498 |
|
} |
1499 |
|
|
1506 |
|
new CustomTPE(2, 2, |
1507 |
|
LONG_DELAY_MS, MILLISECONDS, |
1508 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1509 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1510 |
< |
l.add(latchAwaitingStringTask(latch)); |
1511 |
< |
l.add(null); |
1512 |
< |
try { |
1513 |
< |
e.invokeAny(l); |
1514 |
< |
shouldThrow(); |
1515 |
< |
} catch (NullPointerException success) { |
1516 |
< |
} finally { |
1509 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1510 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1511 |
> |
l.add(latchAwaitingStringTask(latch)); |
1512 |
> |
l.add(null); |
1513 |
> |
try { |
1514 |
> |
e.invokeAny(l); |
1515 |
> |
shouldThrow(); |
1516 |
> |
} catch (NullPointerException success) {} |
1517 |
|
latch.countDown(); |
1529 |
– |
joinPool(e); |
1518 |
|
} |
1519 |
|
} |
1520 |
|
|
1526 |
|
new CustomTPE(2, 2, |
1527 |
|
LONG_DELAY_MS, MILLISECONDS, |
1528 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1529 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1530 |
< |
l.add(new NPETask()); |
1531 |
< |
try { |
1532 |
< |
e.invokeAny(l); |
1533 |
< |
shouldThrow(); |
1534 |
< |
} catch (ExecutionException success) { |
1535 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1536 |
< |
} finally { |
1537 |
< |
joinPool(e); |
1529 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1530 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1531 |
> |
l.add(new NPETask()); |
1532 |
> |
try { |
1533 |
> |
e.invokeAny(l); |
1534 |
> |
shouldThrow(); |
1535 |
> |
} catch (ExecutionException success) { |
1536 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1537 |
> |
} |
1538 |
|
} |
1539 |
|
} |
1540 |
|
|
1546 |
|
new CustomTPE(2, 2, |
1547 |
|
LONG_DELAY_MS, MILLISECONDS, |
1548 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1549 |
< |
try { |
1550 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1549 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1550 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1551 |
|
l.add(new StringTask()); |
1552 |
|
l.add(new StringTask()); |
1553 |
|
String result = e.invokeAny(l); |
1554 |
|
assertSame(TEST_STRING, result); |
1567 |
– |
} finally { |
1568 |
– |
joinPool(e); |
1555 |
|
} |
1556 |
|
} |
1557 |
|
|
1563 |
|
new CustomTPE(2, 2, |
1564 |
|
LONG_DELAY_MS, MILLISECONDS, |
1565 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1566 |
< |
try { |
1567 |
< |
e.invokeAll(null); |
1568 |
< |
shouldThrow(); |
1569 |
< |
} catch (NullPointerException success) { |
1570 |
< |
} finally { |
1585 |
< |
joinPool(e); |
1566 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1567 |
> |
try { |
1568 |
> |
e.invokeAll(null); |
1569 |
> |
shouldThrow(); |
1570 |
> |
} catch (NullPointerException success) {} |
1571 |
|
} |
1572 |
|
} |
1573 |
|
|
1574 |
|
/** |
1575 |
< |
* invokeAll(empty collection) returns empty collection |
1575 |
> |
* invokeAll(empty collection) returns empty list |
1576 |
|
*/ |
1577 |
|
public void testInvokeAll2() throws Exception { |
1578 |
|
final ExecutorService e = |
1579 |
|
new CustomTPE(2, 2, |
1580 |
|
LONG_DELAY_MS, MILLISECONDS, |
1581 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1582 |
< |
try { |
1583 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1582 |
> |
final Collection<Callable<String>> emptyCollection |
1583 |
> |
= Collections.emptyList(); |
1584 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1585 |
> |
List<Future<String>> r = e.invokeAll(emptyCollection); |
1586 |
|
assertTrue(r.isEmpty()); |
1600 |
– |
} finally { |
1601 |
– |
joinPool(e); |
1587 |
|
} |
1588 |
|
} |
1589 |
|
|
1595 |
|
new CustomTPE(2, 2, |
1596 |
|
LONG_DELAY_MS, MILLISECONDS, |
1597 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1598 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1599 |
< |
l.add(new StringTask()); |
1600 |
< |
l.add(null); |
1601 |
< |
try { |
1602 |
< |
e.invokeAll(l); |
1603 |
< |
shouldThrow(); |
1604 |
< |
} catch (NullPointerException success) { |
1605 |
< |
} finally { |
1621 |
< |
joinPool(e); |
1598 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1599 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1600 |
> |
l.add(new StringTask()); |
1601 |
> |
l.add(null); |
1602 |
> |
try { |
1603 |
> |
e.invokeAll(l); |
1604 |
> |
shouldThrow(); |
1605 |
> |
} catch (NullPointerException success) {} |
1606 |
|
} |
1607 |
|
} |
1608 |
|
|
1614 |
|
new CustomTPE(2, 2, |
1615 |
|
LONG_DELAY_MS, MILLISECONDS, |
1616 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1617 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1618 |
< |
l.add(new NPETask()); |
1619 |
< |
List<Future<String>> futures = e.invokeAll(l); |
1620 |
< |
assertEquals(1, futures.size()); |
1621 |
< |
try { |
1622 |
< |
futures.get(0).get(); |
1623 |
< |
shouldThrow(); |
1624 |
< |
} catch (ExecutionException success) { |
1625 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1626 |
< |
} finally { |
1627 |
< |
joinPool(e); |
1617 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1618 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1619 |
> |
l.add(new NPETask()); |
1620 |
> |
List<Future<String>> futures = e.invokeAll(l); |
1621 |
> |
assertEquals(1, futures.size()); |
1622 |
> |
try { |
1623 |
> |
futures.get(0).get(); |
1624 |
> |
shouldThrow(); |
1625 |
> |
} catch (ExecutionException success) { |
1626 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1627 |
> |
} |
1628 |
|
} |
1629 |
|
} |
1630 |
|
|
1636 |
|
new CustomTPE(2, 2, |
1637 |
|
LONG_DELAY_MS, MILLISECONDS, |
1638 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1639 |
< |
try { |
1640 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1639 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1640 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1641 |
|
l.add(new StringTask()); |
1642 |
|
l.add(new StringTask()); |
1643 |
|
List<Future<String>> futures = e.invokeAll(l); |
1644 |
|
assertEquals(2, futures.size()); |
1645 |
|
for (Future<String> future : futures) |
1646 |
|
assertSame(TEST_STRING, future.get()); |
1663 |
– |
} finally { |
1664 |
– |
joinPool(e); |
1647 |
|
} |
1648 |
|
} |
1649 |
|
|
1655 |
|
new CustomTPE(2, 2, |
1656 |
|
LONG_DELAY_MS, MILLISECONDS, |
1657 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1658 |
< |
try { |
1659 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1660 |
< |
shouldThrow(); |
1661 |
< |
} catch (NullPointerException success) { |
1662 |
< |
} finally { |
1681 |
< |
joinPool(e); |
1658 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1659 |
> |
try { |
1660 |
> |
e.invokeAny(null, randomTimeout(), randomTimeUnit()); |
1661 |
> |
shouldThrow(); |
1662 |
> |
} catch (NullPointerException success) {} |
1663 |
|
} |
1664 |
|
} |
1665 |
|
|
1671 |
|
new CustomTPE(2, 2, |
1672 |
|
LONG_DELAY_MS, MILLISECONDS, |
1673 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1674 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1675 |
< |
l.add(new StringTask()); |
1676 |
< |
try { |
1677 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1678 |
< |
shouldThrow(); |
1679 |
< |
} catch (NullPointerException success) { |
1680 |
< |
} finally { |
1700 |
< |
joinPool(e); |
1674 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1675 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1676 |
> |
l.add(new StringTask()); |
1677 |
> |
try { |
1678 |
> |
e.invokeAny(l, randomTimeout(), null); |
1679 |
> |
shouldThrow(); |
1680 |
> |
} catch (NullPointerException success) {} |
1681 |
|
} |
1682 |
|
} |
1683 |
|
|
1684 |
|
/** |
1685 |
< |
* timed invokeAny(empty collection) throws IAE |
1685 |
> |
* timed invokeAny(empty collection) throws IllegalArgumentException |
1686 |
|
*/ |
1687 |
|
public void testTimedInvokeAny2() throws Exception { |
1688 |
|
final ExecutorService e = |
1689 |
|
new CustomTPE(2, 2, |
1690 |
|
LONG_DELAY_MS, MILLISECONDS, |
1691 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1692 |
< |
try { |
1693 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1694 |
< |
shouldThrow(); |
1695 |
< |
} catch (IllegalArgumentException success) { |
1696 |
< |
} finally { |
1697 |
< |
joinPool(e); |
1692 |
> |
final Collection<Callable<String>> emptyCollection |
1693 |
> |
= Collections.emptyList(); |
1694 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1695 |
> |
try { |
1696 |
> |
e.invokeAny(emptyCollection, randomTimeout(), randomTimeUnit()); |
1697 |
> |
shouldThrow(); |
1698 |
> |
} catch (IllegalArgumentException success) {} |
1699 |
|
} |
1700 |
|
} |
1701 |
|
|
1708 |
|
new CustomTPE(2, 2, |
1709 |
|
LONG_DELAY_MS, MILLISECONDS, |
1710 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1711 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1712 |
< |
l.add(latchAwaitingStringTask(latch)); |
1713 |
< |
l.add(null); |
1714 |
< |
try { |
1715 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1716 |
< |
shouldThrow(); |
1717 |
< |
} catch (NullPointerException success) { |
1718 |
< |
} finally { |
1711 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1712 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1713 |
> |
l.add(latchAwaitingStringTask(latch)); |
1714 |
> |
l.add(null); |
1715 |
> |
try { |
1716 |
> |
e.invokeAny(l, randomTimeout(), MILLISECONDS); |
1717 |
> |
shouldThrow(); |
1718 |
> |
} catch (NullPointerException success) {} |
1719 |
|
latch.countDown(); |
1739 |
– |
joinPool(e); |
1720 |
|
} |
1721 |
|
} |
1722 |
|
|
1728 |
|
new CustomTPE(2, 2, |
1729 |
|
LONG_DELAY_MS, MILLISECONDS, |
1730 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1731 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1732 |
< |
l.add(new NPETask()); |
1733 |
< |
try { |
1734 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1735 |
< |
shouldThrow(); |
1736 |
< |
} catch (ExecutionException success) { |
1737 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1738 |
< |
} finally { |
1739 |
< |
joinPool(e); |
1731 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1732 |
> |
long startTime = System.nanoTime(); |
1733 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1734 |
> |
l.add(new NPETask()); |
1735 |
> |
try { |
1736 |
> |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1737 |
> |
shouldThrow(); |
1738 |
> |
} catch (ExecutionException success) { |
1739 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1740 |
> |
} |
1741 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1742 |
|
} |
1743 |
|
} |
1744 |
|
|
1750 |
|
new CustomTPE(2, 2, |
1751 |
|
LONG_DELAY_MS, MILLISECONDS, |
1752 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1753 |
< |
try { |
1754 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1753 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1754 |
> |
long startTime = System.nanoTime(); |
1755 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1756 |
|
l.add(new StringTask()); |
1757 |
|
l.add(new StringTask()); |
1758 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1758 |
> |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1759 |
|
assertSame(TEST_STRING, result); |
1760 |
< |
} finally { |
1778 |
< |
joinPool(e); |
1760 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1761 |
|
} |
1762 |
|
} |
1763 |
|
|
1769 |
|
new CustomTPE(2, 2, |
1770 |
|
LONG_DELAY_MS, MILLISECONDS, |
1771 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1772 |
< |
try { |
1773 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1774 |
< |
shouldThrow(); |
1775 |
< |
} catch (NullPointerException success) { |
1776 |
< |
} finally { |
1795 |
< |
joinPool(e); |
1772 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1773 |
> |
try { |
1774 |
> |
e.invokeAll(null, randomTimeout(), randomTimeUnit()); |
1775 |
> |
shouldThrow(); |
1776 |
> |
} catch (NullPointerException success) {} |
1777 |
|
} |
1778 |
|
} |
1779 |
|
|
1785 |
|
new CustomTPE(2, 2, |
1786 |
|
LONG_DELAY_MS, MILLISECONDS, |
1787 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1788 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1789 |
< |
l.add(new StringTask()); |
1790 |
< |
try { |
1791 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1792 |
< |
shouldThrow(); |
1793 |
< |
} catch (NullPointerException success) { |
1794 |
< |
} finally { |
1814 |
< |
joinPool(e); |
1788 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1789 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1790 |
> |
l.add(new StringTask()); |
1791 |
> |
try { |
1792 |
> |
e.invokeAll(l, randomTimeout(), null); |
1793 |
> |
shouldThrow(); |
1794 |
> |
} catch (NullPointerException success) {} |
1795 |
|
} |
1796 |
|
} |
1797 |
|
|
1798 |
|
/** |
1799 |
< |
* timed invokeAll(empty collection) returns empty collection |
1799 |
> |
* timed invokeAll(empty collection) returns empty list |
1800 |
|
*/ |
1801 |
|
public void testTimedInvokeAll2() throws Exception { |
1802 |
|
final ExecutorService e = |
1803 |
|
new CustomTPE(2, 2, |
1804 |
|
LONG_DELAY_MS, MILLISECONDS, |
1805 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1806 |
< |
try { |
1807 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1806 |
> |
final Collection<Callable<String>> emptyCollection |
1807 |
> |
= Collections.emptyList(); |
1808 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1809 |
> |
List<Future<String>> r = |
1810 |
> |
e.invokeAll(emptyCollection, randomTimeout(), randomTimeUnit()); |
1811 |
|
assertTrue(r.isEmpty()); |
1829 |
– |
} finally { |
1830 |
– |
joinPool(e); |
1812 |
|
} |
1813 |
|
} |
1814 |
|
|
1820 |
|
new CustomTPE(2, 2, |
1821 |
|
LONG_DELAY_MS, MILLISECONDS, |
1822 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1823 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1824 |
< |
l.add(new StringTask()); |
1825 |
< |
l.add(null); |
1826 |
< |
try { |
1827 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1828 |
< |
shouldThrow(); |
1829 |
< |
} catch (NullPointerException success) { |
1830 |
< |
} finally { |
1850 |
< |
joinPool(e); |
1823 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1824 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1825 |
> |
l.add(new StringTask()); |
1826 |
> |
l.add(null); |
1827 |
> |
try { |
1828 |
> |
e.invokeAll(l, randomTimeout(), randomTimeUnit()); |
1829 |
> |
shouldThrow(); |
1830 |
> |
} catch (NullPointerException success) {} |
1831 |
|
} |
1832 |
|
} |
1833 |
|
|
1839 |
|
new CustomTPE(2, 2, |
1840 |
|
LONG_DELAY_MS, MILLISECONDS, |
1841 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1842 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1843 |
< |
l.add(new NPETask()); |
1844 |
< |
List<Future<String>> futures = |
1845 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1846 |
< |
assertEquals(1, futures.size()); |
1847 |
< |
try { |
1848 |
< |
futures.get(0).get(); |
1849 |
< |
shouldThrow(); |
1850 |
< |
} catch (ExecutionException success) { |
1851 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1852 |
< |
} finally { |
1853 |
< |
joinPool(e); |
1842 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1843 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1844 |
> |
l.add(new NPETask()); |
1845 |
> |
List<Future<String>> futures = |
1846 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1847 |
> |
assertEquals(1, futures.size()); |
1848 |
> |
try { |
1849 |
> |
futures.get(0).get(); |
1850 |
> |
shouldThrow(); |
1851 |
> |
} catch (ExecutionException success) { |
1852 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1853 |
> |
} |
1854 |
|
} |
1855 |
|
} |
1856 |
|
|
1862 |
|
new CustomTPE(2, 2, |
1863 |
|
LONG_DELAY_MS, MILLISECONDS, |
1864 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1865 |
< |
try { |
1866 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1865 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1866 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1867 |
|
l.add(new StringTask()); |
1868 |
|
l.add(new StringTask()); |
1869 |
|
List<Future<String>> futures = |
1871 |
|
assertEquals(2, futures.size()); |
1872 |
|
for (Future<String> future : futures) |
1873 |
|
assertSame(TEST_STRING, future.get()); |
1894 |
– |
} finally { |
1895 |
– |
joinPool(e); |
1874 |
|
} |
1875 |
|
} |
1876 |
|
|
1878 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1879 |
|
*/ |
1880 |
|
public void testTimedInvokeAll6() throws Exception { |
1881 |
< |
final ExecutorService e = |
1882 |
< |
new CustomTPE(2, 2, |
1883 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1884 |
< |
new ArrayBlockingQueue<Runnable>(10)); |
1885 |
< |
try { |
1886 |
< |
for (long timeout = timeoutMillis();;) { |
1881 |
> |
for (long timeout = timeoutMillis();;) { |
1882 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1883 |
> |
final Callable<String> waiter = new CheckedCallable<String>() { |
1884 |
> |
public String realCall() { |
1885 |
> |
try { done.await(LONG_DELAY_MS, MILLISECONDS); } |
1886 |
> |
catch (InterruptedException ok) {} |
1887 |
> |
return "1"; }}; |
1888 |
> |
final ExecutorService p = |
1889 |
> |
new CustomTPE(2, 2, |
1890 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1891 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1892 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1893 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1894 |
|
tasks.add(new StringTask("0")); |
1895 |
< |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1895 |
> |
tasks.add(waiter); |
1896 |
|
tasks.add(new StringTask("2")); |
1897 |
|
long startTime = System.nanoTime(); |
1898 |
|
List<Future<String>> futures = |
1899 |
< |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1899 |
> |
p.invokeAll(tasks, timeout, MILLISECONDS); |
1900 |
|
assertEquals(tasks.size(), futures.size()); |
1901 |
|
assertTrue(millisElapsedSince(startTime) >= timeout); |
1902 |
|
for (Future future : futures) |
1912 |
|
fail("expected exactly one task to be cancelled"); |
1913 |
|
} |
1914 |
|
} |
1931 |
– |
} finally { |
1932 |
– |
joinPool(e); |
1915 |
|
} |
1916 |
|
} |
1917 |
|
|
1925 |
|
LONG_DELAY_MS, MILLISECONDS, |
1926 |
|
new LinkedBlockingQueue<Runnable>(), |
1927 |
|
new FailingThreadFactory()); |
1928 |
< |
try { |
1928 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1929 |
|
final int TASKS = 100; |
1930 |
|
final CountDownLatch done = new CountDownLatch(TASKS); |
1931 |
|
for (int k = 0; k < TASKS; ++k) |
1933 |
|
public void realRun() { |
1934 |
|
done.countDown(); |
1935 |
|
}}); |
1936 |
< |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
1955 |
< |
} finally { |
1956 |
< |
joinPool(e); |
1936 |
> |
await(done); |
1937 |
|
} |
1938 |
|
} |
1939 |
|
|
1945 |
|
new CustomTPE(2, 2, |
1946 |
|
1000, MILLISECONDS, |
1947 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1948 |
< |
assertFalse(p.allowsCoreThreadTimeOut()); |
1949 |
< |
joinPool(p); |
1948 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1949 |
> |
assertFalse(p.allowsCoreThreadTimeOut()); |
1950 |
> |
} |
1951 |
|
} |
1952 |
|
|
1953 |
|
/** |
1959 |
|
new CustomTPE(2, 10, |
1960 |
|
keepAliveTime, MILLISECONDS, |
1961 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1962 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1963 |
< |
try { |
1962 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1963 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1964 |
|
p.allowCoreThreadTimeOut(true); |
1965 |
|
p.execute(new CheckedRunnable() { |
1966 |
|
public void realRun() { |
1975 |
|
Thread.yield(); |
1976 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1977 |
|
assertEquals(0, p.getPoolSize()); |
1997 |
– |
} finally { |
1998 |
– |
joinPool(p); |
1978 |
|
} |
1979 |
|
} |
1980 |
|
|
1987 |
|
new CustomTPE(2, 10, |
1988 |
|
keepAliveTime, MILLISECONDS, |
1989 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1990 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1991 |
< |
try { |
1990 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1991 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1992 |
|
p.allowCoreThreadTimeOut(false); |
1993 |
|
p.execute(new CheckedRunnable() { |
1994 |
|
public void realRun() throws InterruptedException { |
1997 |
|
}}); |
1998 |
|
delay(2 * keepAliveTime); |
1999 |
|
assertTrue(p.getPoolSize() >= 1); |
2021 |
– |
} finally { |
2022 |
– |
joinPool(p); |
2000 |
|
} |
2001 |
|
} |
2002 |
|
|
2005 |
|
* (in part, a test of CustomTPE itself) |
2006 |
|
*/ |
2007 |
|
public void testGet_cancelled() throws Exception { |
2008 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
2009 |
|
final ExecutorService e = |
2010 |
|
new CustomTPE(1, 1, |
2011 |
|
LONG_DELAY_MS, MILLISECONDS, |
2012 |
|
new LinkedBlockingQueue<Runnable>()); |
2013 |
< |
try { |
2013 |
> |
try (PoolCleaner cleaner = cleaner(e, done)) { |
2014 |
|
final CountDownLatch blockerStarted = new CountDownLatch(1); |
2037 |
– |
final CountDownLatch done = new CountDownLatch(1); |
2015 |
|
final List<Future<?>> futures = new ArrayList<>(); |
2016 |
|
for (int i = 0; i < 2; i++) { |
2017 |
|
Runnable r = new CheckedRunnable() { public void realRun() |
2021 |
|
}}; |
2022 |
|
futures.add(e.submit(r)); |
2023 |
|
} |
2024 |
< |
assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
2024 |
> |
await(blockerStarted); |
2025 |
|
for (Future<?> future : futures) future.cancel(false); |
2026 |
|
for (Future<?> future : futures) { |
2027 |
|
try { |
2035 |
|
assertTrue(future.isCancelled()); |
2036 |
|
assertTrue(future.isDone()); |
2037 |
|
} |
2061 |
– |
done.countDown(); |
2062 |
– |
} finally { |
2063 |
– |
joinPool(e); |
2038 |
|
} |
2039 |
|
} |
2040 |
|
|