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 |
|
|
542 |
|
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
543 |
|
*/ |
544 |
|
public void testGetTaskCount() throws InterruptedException { |
545 |
+ |
final int TASKS = 3; |
546 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
547 |
|
final ThreadPoolExecutor p = |
548 |
|
new CustomTPE(1, 1, |
549 |
|
LONG_DELAY_MS, MILLISECONDS, |
550 |
|
new ArrayBlockingQueue<Runnable>(10)); |
551 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
551 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
552 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
554 |
– |
final CountDownLatch done = new CountDownLatch(1); |
553 |
|
assertEquals(0, p.getTaskCount()); |
554 |
+ |
assertEquals(0, p.getCompletedTaskCount()); |
555 |
|
p.execute(new CheckedRunnable() { |
556 |
|
public void realRun() throws InterruptedException { |
557 |
|
threadStarted.countDown(); |
558 |
< |
assertEquals(1, p.getTaskCount()); |
560 |
< |
done.await(); |
558 |
> |
await(done); |
559 |
|
}}); |
560 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
560 |
> |
await(threadStarted); |
561 |
|
assertEquals(1, p.getTaskCount()); |
562 |
< |
done.countDown(); |
562 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
563 |
> |
for (int i = 0; i < TASKS; i++) { |
564 |
> |
assertEquals(1 + i, p.getTaskCount()); |
565 |
> |
p.execute(new CheckedRunnable() { |
566 |
> |
public void realRun() throws InterruptedException { |
567 |
> |
threadStarted.countDown(); |
568 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
569 |
> |
await(done); |
570 |
> |
}}); |
571 |
> |
} |
572 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
573 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
574 |
|
} |
575 |
+ |
assertEquals(1 + TASKS, p.getTaskCount()); |
576 |
+ |
assertEquals(1 + TASKS, p.getCompletedTaskCount()); |
577 |
|
} |
578 |
|
|
579 |
|
/** |
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); |
650 |
– |
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()); |
668 |
– |
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); |
684 |
– |
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])); |
703 |
– |
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()); |
743 |
– |
done.countDown(); |
752 |
|
} |
753 |
|
} |
754 |
|
|
764 |
|
new CustomTPE(poolSize, poolSize, |
765 |
|
LONG_DELAY_MS, MILLISECONDS, |
766 |
|
new ArrayBlockingQueue<Runnable>(10)); |
767 |
< |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
767 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
768 |
|
Runnable waiter = new CheckedRunnable() { public void realRun() { |
769 |
|
threadsStarted.countDown(); |
770 |
|
try { |
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)) { |
1133 |
< |
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 |
|
} |
1147 |
– |
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)) { |
1161 |
< |
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 |
1175 |
– |
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)) { |
1192 |
< |
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); |
1199 |
– |
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)); |
1229 |
– |
done.countDown(); |
1234 |
|
} |
1235 |
|
assertEquals(LatchAwaiter.DONE, r1.state); |
1236 |
|
assertEquals(LatchAwaiter.NEW, r2.state); |
1347 |
|
new CustomTPE(2, 3, |
1348 |
|
LONG_DELAY_MS, MILLISECONDS, |
1349 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1350 |
< |
try { |
1351 |
< |
p.setMaximumPoolSize(1); |
1352 |
< |
shouldThrow(); |
1353 |
< |
} catch (IllegalArgumentException success) { |
1354 |
< |
} finally { |
1351 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1350 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1351 |
> |
try { |
1352 |
> |
p.setMaximumPoolSize(1); |
1353 |
> |
shouldThrow(); |
1354 |
> |
} catch (IllegalArgumentException success) {} |
1355 |
|
} |
1353 |
– |
joinPool(p); |
1356 |
|
} |
1357 |
|
|
1358 |
|
/** |
1364 |
|
new CustomTPE(2, 3, |
1365 |
|
LONG_DELAY_MS, |
1366 |
|
MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1367 |
< |
try { |
1368 |
< |
p.setMaximumPoolSize(-1); |
1369 |
< |
shouldThrow(); |
1370 |
< |
} catch (IllegalArgumentException success) { |
1371 |
< |
} finally { |
1370 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1367 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1368 |
> |
try { |
1369 |
> |
p.setMaximumPoolSize(-1); |
1370 |
> |
shouldThrow(); |
1371 |
> |
} catch (IllegalArgumentException success) {} |
1372 |
|
} |
1372 |
– |
joinPool(p); |
1373 |
|
} |
1374 |
|
|
1375 |
|
/** |
1381 |
|
new CustomTPE(2, 3, |
1382 |
|
LONG_DELAY_MS, MILLISECONDS, |
1383 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1384 |
< |
|
1385 |
< |
try { |
1386 |
< |
p.setKeepAliveTime(-1,MILLISECONDS); |
1387 |
< |
shouldThrow(); |
1388 |
< |
} catch (IllegalArgumentException success) { |
1389 |
< |
} finally { |
1390 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1384 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1385 |
> |
try { |
1386 |
> |
p.setKeepAliveTime(-1, MILLISECONDS); |
1387 |
> |
shouldThrow(); |
1388 |
> |
} catch (IllegalArgumentException success) {} |
1389 |
|
} |
1392 |
– |
joinPool(p); |
1390 |
|
} |
1391 |
|
|
1392 |
|
/** |
1394 |
|
*/ |
1395 |
|
public void testTerminated() { |
1396 |
|
CustomTPE p = new CustomTPE(); |
1397 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1398 |
< |
assertTrue(p.terminatedCalled()); |
1399 |
< |
joinPool(p); |
1397 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1398 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1399 |
> |
assertTrue(p.terminatedCalled()); |
1400 |
> |
assertTrue(p.isShutdown()); |
1401 |
> |
} |
1402 |
|
} |
1403 |
|
|
1404 |
|
/** |
1406 |
|
*/ |
1407 |
|
public void testBeforeAfter() throws InterruptedException { |
1408 |
|
CustomTPE p = new CustomTPE(); |
1409 |
< |
try { |
1409 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1410 |
|
final CountDownLatch done = new CountDownLatch(1); |
1411 |
|
p.execute(new CheckedRunnable() { |
1412 |
|
public void realRun() { |
1416 |
|
assertEquals(0, done.getCount()); |
1417 |
|
assertTrue(p.afterCalled()); |
1418 |
|
assertTrue(p.beforeCalled()); |
1420 |
– |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1421 |
– |
} finally { |
1422 |
– |
joinPool(p); |
1419 |
|
} |
1420 |
|
} |
1421 |
|
|
1427 |
|
new CustomTPE(2, 2, |
1428 |
|
LONG_DELAY_MS, MILLISECONDS, |
1429 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1430 |
< |
try { |
1430 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1431 |
|
Future<String> future = e.submit(new StringTask()); |
1432 |
|
String result = future.get(); |
1433 |
|
assertSame(TEST_STRING, result); |
1438 |
– |
} finally { |
1439 |
– |
joinPool(e); |
1434 |
|
} |
1435 |
|
} |
1436 |
|
|
1442 |
|
new CustomTPE(2, 2, |
1443 |
|
LONG_DELAY_MS, MILLISECONDS, |
1444 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1445 |
< |
try { |
1445 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1446 |
|
Future<?> future = e.submit(new NoOpRunnable()); |
1447 |
|
future.get(); |
1448 |
|
assertTrue(future.isDone()); |
1455 |
– |
} finally { |
1456 |
– |
joinPool(e); |
1449 |
|
} |
1450 |
|
} |
1451 |
|
|
1457 |
|
new CustomTPE(2, 2, |
1458 |
|
LONG_DELAY_MS, MILLISECONDS, |
1459 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1460 |
< |
try { |
1460 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1461 |
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
1462 |
|
String result = future.get(); |
1463 |
|
assertSame(TEST_STRING, result); |
1472 |
– |
} finally { |
1473 |
– |
joinPool(e); |
1464 |
|
} |
1465 |
|
} |
1466 |
|
|
1472 |
|
new CustomTPE(2, 2, |
1473 |
|
LONG_DELAY_MS, MILLISECONDS, |
1474 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1475 |
< |
try { |
1476 |
< |
e.invokeAny(null); |
1477 |
< |
shouldThrow(); |
1478 |
< |
} catch (NullPointerException success) { |
1479 |
< |
} finally { |
1490 |
< |
joinPool(e); |
1475 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1476 |
> |
try { |
1477 |
> |
e.invokeAny(null); |
1478 |
> |
shouldThrow(); |
1479 |
> |
} catch (NullPointerException success) {} |
1480 |
|
} |
1481 |
|
} |
1482 |
|
|
1488 |
|
new CustomTPE(2, 2, |
1489 |
|
LONG_DELAY_MS, MILLISECONDS, |
1490 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1491 |
< |
try { |
1492 |
< |
e.invokeAny(new ArrayList<Callable<String>>()); |
1493 |
< |
shouldThrow(); |
1494 |
< |
} catch (IllegalArgumentException success) { |
1495 |
< |
} finally { |
1507 |
< |
joinPool(e); |
1491 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1492 |
> |
try { |
1493 |
> |
e.invokeAny(new ArrayList<Callable<String>>()); |
1494 |
> |
shouldThrow(); |
1495 |
> |
} catch (IllegalArgumentException success) {} |
1496 |
|
} |
1497 |
|
} |
1498 |
|
|
1505 |
|
new CustomTPE(2, 2, |
1506 |
|
LONG_DELAY_MS, MILLISECONDS, |
1507 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1508 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1509 |
< |
l.add(latchAwaitingStringTask(latch)); |
1510 |
< |
l.add(null); |
1511 |
< |
try { |
1512 |
< |
e.invokeAny(l); |
1513 |
< |
shouldThrow(); |
1514 |
< |
} catch (NullPointerException success) { |
1515 |
< |
} finally { |
1508 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
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 |
|
latch.countDown(); |
1529 |
– |
joinPool(e); |
1517 |
|
} |
1518 |
|
} |
1519 |
|
|
1525 |
|
new CustomTPE(2, 2, |
1526 |
|
LONG_DELAY_MS, MILLISECONDS, |
1527 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1528 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1529 |
< |
l.add(new NPETask()); |
1530 |
< |
try { |
1531 |
< |
e.invokeAny(l); |
1532 |
< |
shouldThrow(); |
1533 |
< |
} catch (ExecutionException success) { |
1534 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1535 |
< |
} finally { |
1536 |
< |
joinPool(e); |
1528 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
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 |
> |
} |
1537 |
|
} |
1538 |
|
} |
1539 |
|
|
1545 |
|
new CustomTPE(2, 2, |
1546 |
|
LONG_DELAY_MS, MILLISECONDS, |
1547 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1548 |
< |
try { |
1548 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1549 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1550 |
|
l.add(new StringTask()); |
1551 |
|
l.add(new StringTask()); |
1552 |
|
String result = e.invokeAny(l); |
1553 |
|
assertSame(TEST_STRING, result); |
1567 |
– |
} finally { |
1568 |
– |
joinPool(e); |
1554 |
|
} |
1555 |
|
} |
1556 |
|
|
1562 |
|
new CustomTPE(2, 2, |
1563 |
|
LONG_DELAY_MS, MILLISECONDS, |
1564 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1565 |
< |
try { |
1566 |
< |
e.invokeAll(null); |
1567 |
< |
shouldThrow(); |
1568 |
< |
} catch (NullPointerException success) { |
1569 |
< |
} finally { |
1585 |
< |
joinPool(e); |
1565 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1566 |
> |
try { |
1567 |
> |
e.invokeAll(null); |
1568 |
> |
shouldThrow(); |
1569 |
> |
} catch (NullPointerException success) {} |
1570 |
|
} |
1571 |
|
} |
1572 |
|
|
1578 |
|
new CustomTPE(2, 2, |
1579 |
|
LONG_DELAY_MS, MILLISECONDS, |
1580 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1581 |
< |
try { |
1581 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1582 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1583 |
|
assertTrue(r.isEmpty()); |
1600 |
– |
} finally { |
1601 |
– |
joinPool(e); |
1584 |
|
} |
1585 |
|
} |
1586 |
|
|
1592 |
|
new CustomTPE(2, 2, |
1593 |
|
LONG_DELAY_MS, MILLISECONDS, |
1594 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1595 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1596 |
< |
l.add(new StringTask()); |
1597 |
< |
l.add(null); |
1598 |
< |
try { |
1599 |
< |
e.invokeAll(l); |
1600 |
< |
shouldThrow(); |
1601 |
< |
} catch (NullPointerException success) { |
1602 |
< |
} finally { |
1621 |
< |
joinPool(e); |
1595 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1596 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1597 |
> |
l.add(new StringTask()); |
1598 |
> |
l.add(null); |
1599 |
> |
try { |
1600 |
> |
e.invokeAll(l); |
1601 |
> |
shouldThrow(); |
1602 |
> |
} catch (NullPointerException success) {} |
1603 |
|
} |
1604 |
|
} |
1605 |
|
|
1611 |
|
new CustomTPE(2, 2, |
1612 |
|
LONG_DELAY_MS, MILLISECONDS, |
1613 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1614 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1615 |
< |
l.add(new NPETask()); |
1616 |
< |
List<Future<String>> futures = e.invokeAll(l); |
1617 |
< |
assertEquals(1, futures.size()); |
1618 |
< |
try { |
1619 |
< |
futures.get(0).get(); |
1620 |
< |
shouldThrow(); |
1621 |
< |
} catch (ExecutionException success) { |
1622 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1623 |
< |
} finally { |
1624 |
< |
joinPool(e); |
1614 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1615 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1616 |
> |
l.add(new NPETask()); |
1617 |
> |
List<Future<String>> futures = e.invokeAll(l); |
1618 |
> |
assertEquals(1, futures.size()); |
1619 |
> |
try { |
1620 |
> |
futures.get(0).get(); |
1621 |
> |
shouldThrow(); |
1622 |
> |
} catch (ExecutionException success) { |
1623 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1624 |
> |
} |
1625 |
|
} |
1626 |
|
} |
1627 |
|
|
1633 |
|
new CustomTPE(2, 2, |
1634 |
|
LONG_DELAY_MS, MILLISECONDS, |
1635 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1636 |
< |
try { |
1636 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1637 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1638 |
|
l.add(new StringTask()); |
1639 |
|
l.add(new StringTask()); |
1641 |
|
assertEquals(2, futures.size()); |
1642 |
|
for (Future<String> future : futures) |
1643 |
|
assertSame(TEST_STRING, future.get()); |
1663 |
– |
} finally { |
1664 |
– |
joinPool(e); |
1644 |
|
} |
1645 |
|
} |
1646 |
|
|
1652 |
|
new CustomTPE(2, 2, |
1653 |
|
LONG_DELAY_MS, MILLISECONDS, |
1654 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1655 |
< |
try { |
1656 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1657 |
< |
shouldThrow(); |
1658 |
< |
} catch (NullPointerException success) { |
1659 |
< |
} finally { |
1681 |
< |
joinPool(e); |
1655 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1656 |
> |
try { |
1657 |
> |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1658 |
> |
shouldThrow(); |
1659 |
> |
} catch (NullPointerException success) {} |
1660 |
|
} |
1661 |
|
} |
1662 |
|
|
1668 |
|
new CustomTPE(2, 2, |
1669 |
|
LONG_DELAY_MS, MILLISECONDS, |
1670 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1671 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1672 |
< |
l.add(new StringTask()); |
1673 |
< |
try { |
1674 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1675 |
< |
shouldThrow(); |
1676 |
< |
} catch (NullPointerException success) { |
1677 |
< |
} finally { |
1700 |
< |
joinPool(e); |
1671 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1672 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1673 |
> |
l.add(new StringTask()); |
1674 |
> |
try { |
1675 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1676 |
> |
shouldThrow(); |
1677 |
> |
} catch (NullPointerException success) {} |
1678 |
|
} |
1679 |
|
} |
1680 |
|
|
1686 |
|
new CustomTPE(2, 2, |
1687 |
|
LONG_DELAY_MS, MILLISECONDS, |
1688 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1689 |
< |
try { |
1690 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1691 |
< |
shouldThrow(); |
1692 |
< |
} catch (IllegalArgumentException success) { |
1693 |
< |
} finally { |
1694 |
< |
joinPool(e); |
1689 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1690 |
> |
try { |
1691 |
> |
e.invokeAny(new ArrayList<Callable<String>>(), |
1692 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
1693 |
> |
shouldThrow(); |
1694 |
> |
} catch (IllegalArgumentException success) {} |
1695 |
|
} |
1696 |
|
} |
1697 |
|
|
1704 |
|
new CustomTPE(2, 2, |
1705 |
|
LONG_DELAY_MS, MILLISECONDS, |
1706 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1707 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1708 |
< |
l.add(latchAwaitingStringTask(latch)); |
1709 |
< |
l.add(null); |
1710 |
< |
try { |
1711 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1712 |
< |
shouldThrow(); |
1713 |
< |
} catch (NullPointerException success) { |
1714 |
< |
} finally { |
1707 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1708 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1709 |
> |
l.add(latchAwaitingStringTask(latch)); |
1710 |
> |
l.add(null); |
1711 |
> |
try { |
1712 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1713 |
> |
shouldThrow(); |
1714 |
> |
} catch (NullPointerException success) {} |
1715 |
|
latch.countDown(); |
1739 |
– |
joinPool(e); |
1716 |
|
} |
1717 |
|
} |
1718 |
|
|
1724 |
|
new CustomTPE(2, 2, |
1725 |
|
LONG_DELAY_MS, MILLISECONDS, |
1726 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1727 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1728 |
< |
l.add(new NPETask()); |
1729 |
< |
try { |
1730 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1731 |
< |
shouldThrow(); |
1732 |
< |
} catch (ExecutionException success) { |
1733 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1734 |
< |
} finally { |
1735 |
< |
joinPool(e); |
1727 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1728 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1729 |
> |
l.add(new NPETask()); |
1730 |
> |
try { |
1731 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1732 |
> |
shouldThrow(); |
1733 |
> |
} catch (ExecutionException success) { |
1734 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1735 |
> |
} |
1736 |
|
} |
1737 |
|
} |
1738 |
|
|
1744 |
|
new CustomTPE(2, 2, |
1745 |
|
LONG_DELAY_MS, MILLISECONDS, |
1746 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1747 |
< |
try { |
1747 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1748 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1749 |
|
l.add(new StringTask()); |
1750 |
|
l.add(new StringTask()); |
1751 |
|
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1752 |
|
assertSame(TEST_STRING, result); |
1777 |
– |
} finally { |
1778 |
– |
joinPool(e); |
1753 |
|
} |
1754 |
|
} |
1755 |
|
|
1761 |
|
new CustomTPE(2, 2, |
1762 |
|
LONG_DELAY_MS, MILLISECONDS, |
1763 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1764 |
< |
try { |
1765 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1766 |
< |
shouldThrow(); |
1767 |
< |
} catch (NullPointerException success) { |
1768 |
< |
} finally { |
1795 |
< |
joinPool(e); |
1764 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1765 |
> |
try { |
1766 |
> |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1767 |
> |
shouldThrow(); |
1768 |
> |
} catch (NullPointerException success) {} |
1769 |
|
} |
1770 |
|
} |
1771 |
|
|
1777 |
|
new CustomTPE(2, 2, |
1778 |
|
LONG_DELAY_MS, MILLISECONDS, |
1779 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1780 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1781 |
< |
l.add(new StringTask()); |
1782 |
< |
try { |
1783 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1784 |
< |
shouldThrow(); |
1785 |
< |
} catch (NullPointerException success) { |
1786 |
< |
} finally { |
1814 |
< |
joinPool(e); |
1780 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1781 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1782 |
> |
l.add(new StringTask()); |
1783 |
> |
try { |
1784 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1785 |
> |
shouldThrow(); |
1786 |
> |
} catch (NullPointerException success) {} |
1787 |
|
} |
1788 |
|
} |
1789 |
|
|
1795 |
|
new CustomTPE(2, 2, |
1796 |
|
LONG_DELAY_MS, MILLISECONDS, |
1797 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1798 |
< |
try { |
1799 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1798 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1799 |
> |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), |
1800 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
1801 |
|
assertTrue(r.isEmpty()); |
1829 |
– |
} finally { |
1830 |
– |
joinPool(e); |
1802 |
|
} |
1803 |
|
} |
1804 |
|
|
1810 |
|
new CustomTPE(2, 2, |
1811 |
|
LONG_DELAY_MS, MILLISECONDS, |
1812 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1813 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1814 |
< |
l.add(new StringTask()); |
1815 |
< |
l.add(null); |
1816 |
< |
try { |
1817 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1818 |
< |
shouldThrow(); |
1819 |
< |
} catch (NullPointerException success) { |
1820 |
< |
} finally { |
1850 |
< |
joinPool(e); |
1813 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1814 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1815 |
> |
l.add(new StringTask()); |
1816 |
> |
l.add(null); |
1817 |
> |
try { |
1818 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1819 |
> |
shouldThrow(); |
1820 |
> |
} catch (NullPointerException success) {} |
1821 |
|
} |
1822 |
|
} |
1823 |
|
|
1829 |
|
new CustomTPE(2, 2, |
1830 |
|
LONG_DELAY_MS, MILLISECONDS, |
1831 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1832 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1833 |
< |
l.add(new NPETask()); |
1834 |
< |
List<Future<String>> futures = |
1835 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1836 |
< |
assertEquals(1, futures.size()); |
1837 |
< |
try { |
1838 |
< |
futures.get(0).get(); |
1839 |
< |
shouldThrow(); |
1840 |
< |
} catch (ExecutionException success) { |
1841 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1842 |
< |
} finally { |
1843 |
< |
joinPool(e); |
1832 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1833 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1834 |
> |
l.add(new NPETask()); |
1835 |
> |
List<Future<String>> futures = |
1836 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1837 |
> |
assertEquals(1, futures.size()); |
1838 |
> |
try { |
1839 |
> |
futures.get(0).get(); |
1840 |
> |
shouldThrow(); |
1841 |
> |
} catch (ExecutionException success) { |
1842 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1843 |
> |
} |
1844 |
|
} |
1845 |
|
} |
1846 |
|
|
1852 |
|
new CustomTPE(2, 2, |
1853 |
|
LONG_DELAY_MS, MILLISECONDS, |
1854 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1855 |
< |
try { |
1855 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1856 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1857 |
|
l.add(new StringTask()); |
1858 |
|
l.add(new StringTask()); |
1861 |
|
assertEquals(2, futures.size()); |
1862 |
|
for (Future<String> future : futures) |
1863 |
|
assertSame(TEST_STRING, future.get()); |
1894 |
– |
} finally { |
1895 |
– |
joinPool(e); |
1864 |
|
} |
1865 |
|
} |
1866 |
|
|
1868 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1869 |
|
*/ |
1870 |
|
public void testTimedInvokeAll6() throws Exception { |
1871 |
< |
final ExecutorService e = |
1872 |
< |
new CustomTPE(2, 2, |
1873 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1874 |
< |
new ArrayBlockingQueue<Runnable>(10)); |
1875 |
< |
try { |
1876 |
< |
for (long timeout = timeoutMillis();;) { |
1871 |
> |
for (long timeout = timeoutMillis();;) { |
1872 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1873 |
> |
final Callable<String> waiter = new CheckedCallable<String>() { |
1874 |
> |
public String realCall() { |
1875 |
> |
try { done.await(LONG_DELAY_MS, MILLISECONDS); } |
1876 |
> |
catch (InterruptedException ok) {} |
1877 |
> |
return "1"; }}; |
1878 |
> |
final ExecutorService p = |
1879 |
> |
new CustomTPE(2, 2, |
1880 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1881 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1882 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1883 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1884 |
|
tasks.add(new StringTask("0")); |
1885 |
< |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1885 |
> |
tasks.add(waiter); |
1886 |
|
tasks.add(new StringTask("2")); |
1887 |
|
long startTime = System.nanoTime(); |
1888 |
|
List<Future<String>> futures = |
1889 |
< |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1889 |
> |
p.invokeAll(tasks, timeout, MILLISECONDS); |
1890 |
|
assertEquals(tasks.size(), futures.size()); |
1891 |
|
assertTrue(millisElapsedSince(startTime) >= timeout); |
1892 |
|
for (Future future : futures) |
1902 |
|
fail("expected exactly one task to be cancelled"); |
1903 |
|
} |
1904 |
|
} |
1931 |
– |
} finally { |
1932 |
– |
joinPool(e); |
1905 |
|
} |
1906 |
|
} |
1907 |
|
|
1915 |
|
LONG_DELAY_MS, MILLISECONDS, |
1916 |
|
new LinkedBlockingQueue<Runnable>(), |
1917 |
|
new FailingThreadFactory()); |
1918 |
< |
try { |
1918 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1919 |
|
final int TASKS = 100; |
1920 |
|
final CountDownLatch done = new CountDownLatch(TASKS); |
1921 |
|
for (int k = 0; k < TASKS; ++k) |
1924 |
|
done.countDown(); |
1925 |
|
}}); |
1926 |
|
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
1955 |
– |
} finally { |
1956 |
– |
joinPool(e); |
1927 |
|
} |
1928 |
|
} |
1929 |
|
|
1935 |
|
new CustomTPE(2, 2, |
1936 |
|
1000, MILLISECONDS, |
1937 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1938 |
< |
assertFalse(p.allowsCoreThreadTimeOut()); |
1939 |
< |
joinPool(p); |
1938 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1939 |
> |
assertFalse(p.allowsCoreThreadTimeOut()); |
1940 |
> |
} |
1941 |
|
} |
1942 |
|
|
1943 |
|
/** |
1949 |
|
new CustomTPE(2, 10, |
1950 |
|
keepAliveTime, MILLISECONDS, |
1951 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1952 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1953 |
< |
try { |
1952 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1953 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1954 |
|
p.allowCoreThreadTimeOut(true); |
1955 |
|
p.execute(new CheckedRunnable() { |
1956 |
|
public void realRun() { |
1965 |
|
Thread.yield(); |
1966 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1967 |
|
assertEquals(0, p.getPoolSize()); |
1997 |
– |
} finally { |
1998 |
– |
joinPool(p); |
1968 |
|
} |
1969 |
|
} |
1970 |
|
|
1977 |
|
new CustomTPE(2, 10, |
1978 |
|
keepAliveTime, MILLISECONDS, |
1979 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1980 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1981 |
< |
try { |
1980 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1981 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1982 |
|
p.allowCoreThreadTimeOut(false); |
1983 |
|
p.execute(new CheckedRunnable() { |
1984 |
|
public void realRun() throws InterruptedException { |
1987 |
|
}}); |
1988 |
|
delay(2 * keepAliveTime); |
1989 |
|
assertTrue(p.getPoolSize() >= 1); |
2021 |
– |
} finally { |
2022 |
– |
joinPool(p); |
1990 |
|
} |
1991 |
|
} |
1992 |
|
|
1995 |
|
* (in part, a test of CustomTPE itself) |
1996 |
|
*/ |
1997 |
|
public void testGet_cancelled() throws Exception { |
1998 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1999 |
|
final ExecutorService e = |
2000 |
|
new CustomTPE(1, 1, |
2001 |
|
LONG_DELAY_MS, MILLISECONDS, |
2002 |
|
new LinkedBlockingQueue<Runnable>()); |
2003 |
< |
try { |
2003 |
> |
try (PoolCleaner cleaner = cleaner(e, done)) { |
2004 |
|
final CountDownLatch blockerStarted = new CountDownLatch(1); |
2037 |
– |
final CountDownLatch done = new CountDownLatch(1); |
2005 |
|
final List<Future<?>> futures = new ArrayList<>(); |
2006 |
|
for (int i = 0; i < 2; i++) { |
2007 |
|
Runnable r = new CheckedRunnable() { public void realRun() |
2011 |
|
}}; |
2012 |
|
futures.add(e.submit(r)); |
2013 |
|
} |
2014 |
< |
assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
2014 |
> |
await(blockerStarted); |
2015 |
|
for (Future<?> future : futures) future.cancel(false); |
2016 |
|
for (Future<?> future : futures) { |
2017 |
|
try { |
2025 |
|
assertTrue(future.isCancelled()); |
2026 |
|
assertTrue(future.isDone()); |
2027 |
|
} |
2061 |
– |
done.countDown(); |
2062 |
– |
} finally { |
2063 |
– |
joinPool(e); |
2028 |
|
} |
2029 |
|
} |
2030 |
|
|