ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ThreadPoolExecutorTest.java (file contents):
Revision 1.81 by jsr166, Sun Oct 4 02:24:49 2015 UTC vs.
Revision 1.115 by jsr166, Mon Mar 20 00:21:54 2017 UTC

# Line 18 | Line 18 | import java.util.concurrent.Callable;
18   import java.util.concurrent.CancellationException;
19   import java.util.concurrent.CountDownLatch;
20   import java.util.concurrent.ExecutionException;
21 import java.util.concurrent.Executors;
21   import java.util.concurrent.ExecutorService;
22   import java.util.concurrent.Future;
23   import java.util.concurrent.FutureTask;
# Line 28 | Line 27 | import java.util.concurrent.RejectedExec
27   import java.util.concurrent.SynchronousQueue;
28   import java.util.concurrent.ThreadFactory;
29   import java.util.concurrent.ThreadPoolExecutor;
31 import java.util.concurrent.TimeUnit;
30   import java.util.concurrent.atomic.AtomicInteger;
31  
32   import junit.framework.Test;
# Line 101 | Line 99 | public class ThreadPoolExecutorTest exte
99       * thread becomes active
100       */
101      public void testGetActiveCount() throws InterruptedException {
102 +        final CountDownLatch done = new CountDownLatch(1);
103          final ThreadPoolExecutor p =
104              new ThreadPoolExecutor(2, 2,
105                                     LONG_DELAY_MS, MILLISECONDS,
106                                     new ArrayBlockingQueue<Runnable>(10));
107 <        try (PoolCleaner cleaner = cleaner(p)) {
107 >        try (PoolCleaner cleaner = cleaner(p, done)) {
108              final CountDownLatch threadStarted = new CountDownLatch(1);
110            final CountDownLatch done = new CountDownLatch(1);
109              assertEquals(0, p.getActiveCount());
110              p.execute(new CheckedRunnable() {
111                  public void realRun() throws InterruptedException {
112                      threadStarted.countDown();
113                      assertEquals(1, p.getActiveCount());
114 <                    done.await();
114 >                    await(done);
115                  }});
116 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
116 >            await(threadStarted);
117              assertEquals(1, p.getActiveCount());
120            done.countDown();
118          }
119      }
120  
# Line 329 | Line 326 | public class ThreadPoolExecutorTest exte
326       */
327      public void testGetLargestPoolSize() throws InterruptedException {
328          final int THREADS = 3;
329 +        final CountDownLatch done = new CountDownLatch(1);
330          final ThreadPoolExecutor p =
331              new ThreadPoolExecutor(THREADS, THREADS,
332                                     LONG_DELAY_MS, MILLISECONDS,
333                                     new ArrayBlockingQueue<Runnable>(10));
334 <        try (PoolCleaner cleaner = cleaner(p)) {
337 <            final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
338 <            final CountDownLatch done = new CountDownLatch(1);
334 >        try (PoolCleaner cleaner = cleaner(p, done)) {
335              assertEquals(0, p.getLargestPoolSize());
336 +            final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
337              for (int i = 0; i < THREADS; i++)
338                  p.execute(new CheckedRunnable() {
339                      public void realRun() throws InterruptedException {
340                          threadsStarted.countDown();
341 <                        done.await();
341 >                        await(done);
342                          assertEquals(THREADS, p.getLargestPoolSize());
343                      }});
344 <            assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
344 >            await(threadsStarted);
345              assertEquals(THREADS, p.getLargestPoolSize());
349            done.countDown();   // release pool
346          }
347          assertEquals(THREADS, p.getLargestPoolSize());
348      }
# Line 374 | Line 370 | public class ThreadPoolExecutorTest exte
370       * become active
371       */
372      public void testGetPoolSize() throws InterruptedException {
373 +        final CountDownLatch done = new CountDownLatch(1);
374          final ThreadPoolExecutor p =
375              new ThreadPoolExecutor(1, 1,
376                                     LONG_DELAY_MS, MILLISECONDS,
377                                     new ArrayBlockingQueue<Runnable>(10));
378 <        try (PoolCleaner cleaner = cleaner(p)) {
382 <            final CountDownLatch threadStarted = new CountDownLatch(1);
383 <            final CountDownLatch done = new CountDownLatch(1);
378 >        try (PoolCleaner cleaner = cleaner(p, done)) {
379              assertEquals(0, p.getPoolSize());
380 +            final CountDownLatch threadStarted = new CountDownLatch(1);
381              p.execute(new CheckedRunnable() {
382                  public void realRun() throws InterruptedException {
383                      threadStarted.countDown();
384                      assertEquals(1, p.getPoolSize());
385 <                    done.await();
385 >                    await(done);
386                  }});
387 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
387 >            await(threadStarted);
388              assertEquals(1, p.getPoolSize());
393            done.countDown();   // release pool
389          }
390      }
391  
# Line 398 | Line 393 | public class ThreadPoolExecutorTest exte
393       * getTaskCount increases, but doesn't overestimate, when tasks submitted
394       */
395      public void testGetTaskCount() throws InterruptedException {
396 +        final int TASKS = 3;
397 +        final CountDownLatch done = new CountDownLatch(1);
398          final ThreadPoolExecutor p =
399              new ThreadPoolExecutor(1, 1,
400                                     LONG_DELAY_MS, MILLISECONDS,
401                                     new ArrayBlockingQueue<Runnable>(10));
402 <        try (PoolCleaner cleaner = cleaner(p)) {
402 >        try (PoolCleaner cleaner = cleaner(p, done)) {
403              final CountDownLatch threadStarted = new CountDownLatch(1);
407            final CountDownLatch done = new CountDownLatch(1);
404              assertEquals(0, p.getTaskCount());
405 +            assertEquals(0, p.getCompletedTaskCount());
406              p.execute(new CheckedRunnable() {
407                  public void realRun() throws InterruptedException {
408                      threadStarted.countDown();
409 <                    assertEquals(1, p.getTaskCount());
413 <                    done.await();
409 >                    await(done);
410                  }});
411 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
411 >            await(threadStarted);
412              assertEquals(1, p.getTaskCount());
413 <            done.countDown();
413 >            assertEquals(0, p.getCompletedTaskCount());
414 >            for (int i = 0; i < TASKS; i++) {
415 >                assertEquals(1 + i, p.getTaskCount());
416 >                p.execute(new CheckedRunnable() {
417 >                    public void realRun() throws InterruptedException {
418 >                        threadStarted.countDown();
419 >                        assertEquals(1 + TASKS, p.getTaskCount());
420 >                        await(done);
421 >                    }});
422 >            }
423 >            assertEquals(1 + TASKS, p.getTaskCount());
424 >            assertEquals(0, p.getCompletedTaskCount());
425          }
426 +        assertEquals(1 + TASKS, p.getTaskCount());
427 +        assertEquals(1 + TASKS, p.getCompletedTaskCount());
428      }
429  
430      /**
# Line 426 | Line 435 | public class ThreadPoolExecutorTest exte
435              new ThreadPoolExecutor(1, 1,
436                                     LONG_DELAY_MS, MILLISECONDS,
437                                     new ArrayBlockingQueue<Runnable>(10));
438 <        assertFalse(p.isShutdown());
439 <        try { p.shutdown(); } catch (SecurityException ok) { return; }
440 <        assertTrue(p.isShutdown());
441 <        joinPool(p);
438 >        try (PoolCleaner cleaner = cleaner(p)) {
439 >            assertFalse(p.isShutdown());
440 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
441 >            assertTrue(p.isShutdown());
442 >        }
443      }
444  
445      /**
# Line 440 | Line 450 | public class ThreadPoolExecutorTest exte
450              new ThreadPoolExecutor(1, 1,
451                                     LONG_DELAY_MS, MILLISECONDS,
452                                     new ArrayBlockingQueue<Runnable>(10));
453 <        assertFalse(p.isTerminated());
454 <        assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS));
455 <        assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
456 <        assertFalse(p.awaitTermination(-1L, NANOSECONDS));
457 <        assertFalse(p.awaitTermination(-1L, MILLISECONDS));
458 <        assertFalse(p.awaitTermination(0L, NANOSECONDS));
459 <        assertFalse(p.awaitTermination(0L, MILLISECONDS));
460 <        long timeoutNanos = 999999L;
461 <        long startTime = System.nanoTime();
462 <        assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
463 <        assertTrue(System.nanoTime() - startTime >= timeoutNanos);
464 <        assertFalse(p.isTerminated());
465 <        startTime = System.nanoTime();
466 <        long timeoutMillis = timeoutMillis();
467 <        assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS));
468 <        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
469 <        assertFalse(p.isTerminated());
470 <        p.shutdown();
471 <        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
472 <        assertTrue(p.isTerminated());
453 >        try (PoolCleaner cleaner = cleaner(p)) {
454 >            assertFalse(p.isTerminated());
455 >            assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS));
456 >            assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
457 >            assertFalse(p.awaitTermination(-1L, NANOSECONDS));
458 >            assertFalse(p.awaitTermination(-1L, MILLISECONDS));
459 >            assertFalse(p.awaitTermination(0L, NANOSECONDS));
460 >            assertFalse(p.awaitTermination(0L, MILLISECONDS));
461 >            long timeoutNanos = 999999L;
462 >            long startTime = System.nanoTime();
463 >            assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
464 >            assertTrue(System.nanoTime() - startTime >= timeoutNanos);
465 >            assertFalse(p.isTerminated());
466 >            startTime = System.nanoTime();
467 >            long timeoutMillis = timeoutMillis();
468 >            assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS));
469 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
470 >            assertFalse(p.isTerminated());
471 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
472 >            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
473 >            assertTrue(p.isTerminated());
474 >        }
475      }
476  
477      /**
# Line 470 | Line 482 | public class ThreadPoolExecutorTest exte
482              new ThreadPoolExecutor(1, 1,
483                                     LONG_DELAY_MS, MILLISECONDS,
484                                     new ArrayBlockingQueue<Runnable>(10));
485 <        final CountDownLatch threadStarted = new CountDownLatch(1);
486 <        final CountDownLatch done = new CountDownLatch(1);
487 <        assertFalse(p.isTerminated());
488 <        try {
485 >        try (PoolCleaner cleaner = cleaner(p)) {
486 >            final CountDownLatch threadStarted = new CountDownLatch(1);
487 >            final CountDownLatch done = new CountDownLatch(1);
488 >            assertFalse(p.isTerminating());
489              p.execute(new CheckedRunnable() {
490                  public void realRun() throws InterruptedException {
491 <                    assertFalse(p.isTerminated());
491 >                    assertFalse(p.isTerminating());
492                      threadStarted.countDown();
493 <                    done.await();
493 >                    await(done);
494                  }});
495 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
495 >            await(threadStarted);
496              assertFalse(p.isTerminating());
497              done.countDown();
486        } finally {
498              try { p.shutdown(); } catch (SecurityException ok) { return; }
499 +            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
500 +            assertTrue(p.isTerminated());
501 +            assertFalse(p.isTerminating());
502          }
489        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
490        assertTrue(p.isTerminated());
503      }
504  
505      /**
# Line 498 | Line 510 | public class ThreadPoolExecutorTest exte
510              new ThreadPoolExecutor(1, 1,
511                                     LONG_DELAY_MS, MILLISECONDS,
512                                     new ArrayBlockingQueue<Runnable>(10));
513 <        final CountDownLatch threadStarted = new CountDownLatch(1);
514 <        final CountDownLatch done = new CountDownLatch(1);
515 <        try {
513 >        try (PoolCleaner cleaner = cleaner(p)) {
514 >            final CountDownLatch threadStarted = new CountDownLatch(1);
515 >            final CountDownLatch done = new CountDownLatch(1);
516              assertFalse(p.isTerminating());
517              p.execute(new CheckedRunnable() {
518                  public void realRun() throws InterruptedException {
519                      assertFalse(p.isTerminating());
520                      threadStarted.countDown();
521 <                    done.await();
521 >                    await(done);
522                  }});
523 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
523 >            await(threadStarted);
524              assertFalse(p.isTerminating());
525              done.countDown();
514        } finally {
526              try { p.shutdown(); } catch (SecurityException ok) { return; }
527 +            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
528 +            assertTrue(p.isTerminated());
529 +            assertFalse(p.isTerminating());
530          }
517        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
518        assertTrue(p.isTerminated());
519        assertFalse(p.isTerminating());
531      }
532  
533      /**
534       * getQueue returns the work queue, which contains queued tasks
535       */
536      public void testGetQueue() throws InterruptedException {
537 <        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
537 >        final CountDownLatch done = new CountDownLatch(1);
538 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
539          final ThreadPoolExecutor p =
540              new ThreadPoolExecutor(1, 1,
541                                     LONG_DELAY_MS, MILLISECONDS,
542                                     q);
543 <        final CountDownLatch threadStarted = new CountDownLatch(1);
544 <        final CountDownLatch done = new CountDownLatch(1);
533 <        try {
543 >        try (PoolCleaner cleaner = cleaner(p, done)) {
544 >            final CountDownLatch threadStarted = new CountDownLatch(1);
545              FutureTask[] tasks = new FutureTask[5];
546              for (int i = 0; i < tasks.length; i++) {
547                  Callable task = new CheckedCallable<Boolean>() {
548                      public Boolean realCall() throws InterruptedException {
549                          threadStarted.countDown();
550                          assertSame(q, p.getQueue());
551 <                        done.await();
551 >                        await(done);
552                          return Boolean.TRUE;
553                      }};
554                  tasks[i] = new FutureTask(task);
555                  p.execute(tasks[i]);
556              }
557 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
557 >            await(threadStarted);
558              assertSame(q, p.getQueue());
559              assertFalse(q.contains(tasks[0]));
560              assertTrue(q.contains(tasks[tasks.length - 1]));
561              assertEquals(tasks.length - 1, q.size());
551        } finally {
552            done.countDown();
553            joinPool(p);
562          }
563      }
564  
# Line 558 | Line 566 | public class ThreadPoolExecutorTest exte
566       * remove(task) removes queued task, and fails to remove active task
567       */
568      public void testRemove() throws InterruptedException {
569 <        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
569 >        final CountDownLatch done = new CountDownLatch(1);
570 >        BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
571          final ThreadPoolExecutor p =
572              new ThreadPoolExecutor(1, 1,
573                                     LONG_DELAY_MS, MILLISECONDS,
574                                     q);
575 <        Runnable[] tasks = new Runnable[5];
576 <        final CountDownLatch threadStarted = new CountDownLatch(1);
577 <        final CountDownLatch done = new CountDownLatch(1);
569 <        try {
575 >        try (PoolCleaner cleaner = cleaner(p, done)) {
576 >            Runnable[] tasks = new Runnable[6];
577 >            final CountDownLatch threadStarted = new CountDownLatch(1);
578              for (int i = 0; i < tasks.length; i++) {
579                  tasks[i] = new CheckedRunnable() {
580                      public void realRun() throws InterruptedException {
581                          threadStarted.countDown();
582 <                        done.await();
582 >                        await(done);
583                      }};
584                  p.execute(tasks[i]);
585              }
586 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
586 >            await(threadStarted);
587              assertFalse(p.remove(tasks[0]));
588              assertTrue(q.contains(tasks[4]));
589              assertTrue(q.contains(tasks[3]));
# Line 585 | Line 593 | public class ThreadPoolExecutorTest exte
593              assertTrue(q.contains(tasks[3]));
594              assertTrue(p.remove(tasks[3]));
595              assertFalse(q.contains(tasks[3]));
588        } finally {
589            done.countDown();
590            joinPool(p);
596          }
597      }
598  
# Line 597 | Line 602 | public class ThreadPoolExecutorTest exte
602      public void testPurge() throws InterruptedException {
603          final CountDownLatch threadStarted = new CountDownLatch(1);
604          final CountDownLatch done = new CountDownLatch(1);
605 <        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
605 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
606          final ThreadPoolExecutor p =
607              new ThreadPoolExecutor(1, 1,
608                                     LONG_DELAY_MS, MILLISECONDS,
609                                     q);
610 <        FutureTask[] tasks = new FutureTask[5];
611 <        try {
610 >        try (PoolCleaner cleaner = cleaner(p, done)) {
611 >            FutureTask[] tasks = new FutureTask[5];
612              for (int i = 0; i < tasks.length; i++) {
613                  Callable task = new CheckedCallable<Boolean>() {
614                      public Boolean realCall() throws InterruptedException {
615                          threadStarted.countDown();
616 <                        done.await();
616 >                        await(done);
617                          return Boolean.TRUE;
618                      }};
619                  tasks[i] = new FutureTask(task);
620                  p.execute(tasks[i]);
621              }
622 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
622 >            await(threadStarted);
623              assertEquals(tasks.length, p.getTaskCount());
624              assertEquals(tasks.length - 1, q.size());
625              assertEquals(1L, p.getActiveCount());
# Line 627 | Line 632 | public class ThreadPoolExecutorTest exte
632              p.purge();         // Nothing to do
633              assertEquals(tasks.length - 3, q.size());
634              assertEquals(tasks.length - 2, p.getTaskCount());
630        } finally {
631            done.countDown();
632            joinPool(p);
635          }
636      }
637  
# Line 645 | Line 647 | public class ThreadPoolExecutorTest exte
647              new ThreadPoolExecutor(poolSize, poolSize,
648                                     LONG_DELAY_MS, MILLISECONDS,
649                                     new ArrayBlockingQueue<Runnable>(10));
650 <        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
650 >        final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
651          Runnable waiter = new CheckedRunnable() { public void realRun() {
652              threadsStarted.countDown();
653              try {
# Line 655 | Line 657 | public class ThreadPoolExecutorTest exte
657          }};
658          for (int i = 0; i < count; i++)
659              p.execute(waiter);
660 <        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
660 >        await(threadsStarted);
661          assertEquals(poolSize, p.getActiveCount());
662          assertEquals(0, p.getCompletedTaskCount());
663          final List<Runnable> queuedTasks;
# Line 1017 | Line 1019 | public class ThreadPoolExecutorTest exte
1019       * get of submitted callable throws InterruptedException if interrupted
1020       */
1021      public void testInterruptedSubmit() throws InterruptedException {
1022 +        final CountDownLatch done = new CountDownLatch(1);
1023          final ThreadPoolExecutor p =
1024              new ThreadPoolExecutor(1, 1,
1025                                     60, SECONDS,
1026                                     new ArrayBlockingQueue<Runnable>(10));
1027  
1028 <        final CountDownLatch threadStarted = new CountDownLatch(1);
1029 <        final CountDownLatch done = new CountDownLatch(1);
1027 <        try {
1028 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1029 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1030              Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1031                  public void realRun() throws Exception {
1032                      Callable task = new CheckedCallable<Boolean>() {
1033                          public Boolean realCall() throws InterruptedException {
1034                              threadStarted.countDown();
1035 <                            done.await();
1035 >                            await(done);
1036                              return Boolean.TRUE;
1037                          }};
1038                      p.submit(task).get();
1039                  }});
1040  
1041 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
1041 >            await(threadStarted);
1042              t.interrupt();
1043 <            awaitTermination(t, MEDIUM_DELAY_MS);
1042 <        } finally {
1043 <            done.countDown();
1044 <            joinPool(p);
1043 >            awaitTermination(t);
1044          }
1045      }
1046  
# Line 1049 | Line 1048 | public class ThreadPoolExecutorTest exte
1048       * execute throws RejectedExecutionException if saturated.
1049       */
1050      public void testSaturatedExecute() {
1051 <        ThreadPoolExecutor p =
1051 >        final CountDownLatch done = new CountDownLatch(1);
1052 >        final ThreadPoolExecutor p =
1053              new ThreadPoolExecutor(1, 1,
1054                                     LONG_DELAY_MS, MILLISECONDS,
1055                                     new ArrayBlockingQueue<Runnable>(1));
1056 <        final CountDownLatch done = new CountDownLatch(1);
1057 <        try {
1056 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1057              Runnable task = new CheckedRunnable() {
1058                  public void realRun() throws InterruptedException {
1059 <                    done.await();
1059 >                    await(done);
1060                  }};
1061              for (int i = 0; i < 2; ++i)
1062                  p.execute(task);
# Line 1068 | Line 1067 | public class ThreadPoolExecutorTest exte
1067                  } catch (RejectedExecutionException success) {}
1068                  assertTrue(p.getTaskCount() <= 2);
1069              }
1071        } finally {
1072            done.countDown();
1073            joinPool(p);
1070          }
1071      }
1072  
# Line 1078 | Line 1074 | public class ThreadPoolExecutorTest exte
1074       * submit(runnable) throws RejectedExecutionException if saturated.
1075       */
1076      public void testSaturatedSubmitRunnable() {
1077 <        ThreadPoolExecutor p =
1077 >        final CountDownLatch done = new CountDownLatch(1);
1078 >        final ThreadPoolExecutor p =
1079              new ThreadPoolExecutor(1, 1,
1080                                     LONG_DELAY_MS, MILLISECONDS,
1081                                     new ArrayBlockingQueue<Runnable>(1));
1082 <        final CountDownLatch done = new CountDownLatch(1);
1086 <        try {
1082 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1083              Runnable task = new CheckedRunnable() {
1084                  public void realRun() throws InterruptedException {
1085 <                    done.await();
1085 >                    await(done);
1086                  }};
1087              for (int i = 0; i < 2; ++i)
1088                  p.submit(task);
# Line 1097 | Line 1093 | public class ThreadPoolExecutorTest exte
1093                  } catch (RejectedExecutionException success) {}
1094                  assertTrue(p.getTaskCount() <= 2);
1095              }
1100        } finally {
1101            done.countDown();
1102            joinPool(p);
1096          }
1097      }
1098  
# Line 1107 | Line 1100 | public class ThreadPoolExecutorTest exte
1100       * submit(callable) throws RejectedExecutionException if saturated.
1101       */
1102      public void testSaturatedSubmitCallable() {
1103 <        ThreadPoolExecutor p =
1103 >        final CountDownLatch done = new CountDownLatch(1);
1104 >        final ThreadPoolExecutor p =
1105              new ThreadPoolExecutor(1, 1,
1106                                     LONG_DELAY_MS, MILLISECONDS,
1107                                     new ArrayBlockingQueue<Runnable>(1));
1108 <        final CountDownLatch done = new CountDownLatch(1);
1115 <        try {
1108 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1109              Runnable task = new CheckedRunnable() {
1110                  public void realRun() throws InterruptedException {
1111 <                    done.await();
1111 >                    await(done);
1112                  }};
1113              for (int i = 0; i < 2; ++i)
1114 <                p.submit(Executors.callable(task));
1114 >                p.execute(task);
1115              for (int i = 0; i < 2; ++i) {
1116                  try {
1117                      p.execute(task);
# Line 1126 | Line 1119 | public class ThreadPoolExecutorTest exte
1119                  } catch (RejectedExecutionException success) {}
1120                  assertTrue(p.getTaskCount() <= 2);
1121              }
1129        } finally {
1130            done.countDown();
1131            joinPool(p);
1122          }
1123      }
1124  
# Line 1136 | Line 1126 | public class ThreadPoolExecutorTest exte
1126       * executor using CallerRunsPolicy runs task if saturated.
1127       */
1128      public void testSaturatedExecute2() {
1139        RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
1129          final ThreadPoolExecutor p =
1130              new ThreadPoolExecutor(1, 1,
1131                                     LONG_DELAY_MS,
1132                                     MILLISECONDS,
1133                                     new ArrayBlockingQueue<Runnable>(1),
1134 <                                   h);
1135 <        try {
1134 >                                   new ThreadPoolExecutor.CallerRunsPolicy());
1135 >        try (PoolCleaner cleaner = cleaner(p)) {
1136 >            final CountDownLatch done = new CountDownLatch(1);
1137 >            Runnable blocker = new CheckedRunnable() {
1138 >                public void realRun() throws InterruptedException {
1139 >                    await(done);
1140 >                }};
1141 >            p.execute(blocker);
1142              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1143 <            for (int i = 0; i < tasks.length; ++i)
1143 >            for (int i = 0; i < tasks.length; i++)
1144                  tasks[i] = new TrackedNoOpRunnable();
1145 <            TrackedLongRunnable mr = new TrackedLongRunnable();
1151 <            p.execute(mr);
1152 <            for (int i = 0; i < tasks.length; ++i)
1145 >            for (int i = 0; i < tasks.length; i++)
1146                  p.execute(tasks[i]);
1147 <            for (int i = 1; i < tasks.length; ++i)
1147 >            for (int i = 1; i < tasks.length; i++)
1148                  assertTrue(tasks[i].done);
1149 <            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1150 <        } finally {
1158 <            joinPool(p);
1149 >            assertFalse(tasks[0].done); // waiting in queue
1150 >            done.countDown();
1151          }
1152      }
1153  
# Line 1163 | Line 1155 | public class ThreadPoolExecutorTest exte
1155       * executor using DiscardPolicy drops task if saturated.
1156       */
1157      public void testSaturatedExecute3() {
1158 <        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
1158 >        final CountDownLatch done = new CountDownLatch(1);
1159 >        final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1160 >        for (int i = 0; i < tasks.length; ++i)
1161 >            tasks[i] = new TrackedNoOpRunnable();
1162          final ThreadPoolExecutor p =
1163              new ThreadPoolExecutor(1, 1,
1164 <                                   LONG_DELAY_MS, MILLISECONDS,
1165 <                                   new ArrayBlockingQueue<Runnable>(1),
1166 <                                   h);
1167 <        try {
1168 <            TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1169 <            for (int i = 0; i < tasks.length; ++i)
1175 <                tasks[i] = new TrackedNoOpRunnable();
1176 <            p.execute(new TrackedLongRunnable());
1164 >                          LONG_DELAY_MS, MILLISECONDS,
1165 >                          new ArrayBlockingQueue<Runnable>(1),
1166 >                          new ThreadPoolExecutor.DiscardPolicy());
1167 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1168 >            p.execute(awaiter(done));
1169 >
1170              for (TrackedNoOpRunnable task : tasks)
1171                  p.execute(task);
1172 <            for (TrackedNoOpRunnable task : tasks)
1173 <                assertFalse(task.done);
1181 <            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1182 <        } finally {
1183 <            joinPool(p);
1172 >            for (int i = 1; i < tasks.length; i++)
1173 >                assertFalse(tasks[i].done);
1174          }
1175 +        for (int i = 1; i < tasks.length; i++)
1176 +            assertFalse(tasks[i].done);
1177 +        assertTrue(tasks[0].done); // was waiting in queue
1178      }
1179  
1180      /**
1181       * executor using DiscardOldestPolicy drops oldest task if saturated.
1182       */
1183      public void testSaturatedExecute4() {
1184 <        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
1184 >        final CountDownLatch done = new CountDownLatch(1);
1185 >        LatchAwaiter r1 = awaiter(done);
1186 >        LatchAwaiter r2 = awaiter(done);
1187 >        LatchAwaiter r3 = awaiter(done);
1188          final ThreadPoolExecutor p =
1189              new ThreadPoolExecutor(1, 1,
1190                                     LONG_DELAY_MS, MILLISECONDS,
1191                                     new ArrayBlockingQueue<Runnable>(1),
1192 <                                   h);
1193 <        try {
1194 <            p.execute(new TrackedLongRunnable());
1195 <            TrackedLongRunnable r2 = new TrackedLongRunnable();
1192 >                                   new ThreadPoolExecutor.DiscardOldestPolicy());
1193 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1194 >            assertEquals(LatchAwaiter.NEW, r1.state);
1195 >            assertEquals(LatchAwaiter.NEW, r2.state);
1196 >            assertEquals(LatchAwaiter.NEW, r3.state);
1197 >            p.execute(r1);
1198              p.execute(r2);
1199              assertTrue(p.getQueue().contains(r2));
1202            TrackedNoOpRunnable r3 = new TrackedNoOpRunnable();
1200              p.execute(r3);
1201              assertFalse(p.getQueue().contains(r2));
1202              assertTrue(p.getQueue().contains(r3));
1206            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1207        } finally {
1208            joinPool(p);
1203          }
1204 +        assertEquals(LatchAwaiter.DONE, r1.state);
1205 +        assertEquals(LatchAwaiter.NEW, r2.state);
1206 +        assertEquals(LatchAwaiter.DONE, r3.state);
1207      }
1208  
1209      /**
1210       * execute throws RejectedExecutionException if shutdown
1211       */
1212      public void testRejectedExecutionExceptionOnShutdown() {
1213 <        ThreadPoolExecutor p =
1213 >        final ThreadPoolExecutor p =
1214              new ThreadPoolExecutor(1, 1,
1215                                     LONG_DELAY_MS, MILLISECONDS,
1216                                     new ArrayBlockingQueue<Runnable>(1));
1217          try { p.shutdown(); } catch (SecurityException ok) { return; }
1218 <        try {
1219 <            p.execute(new NoOpRunnable());
1220 <            shouldThrow();
1221 <        } catch (RejectedExecutionException success) {}
1222 <
1223 <        joinPool(p);
1218 >        try (PoolCleaner cleaner = cleaner(p)) {
1219 >            try {
1220 >                p.execute(new NoOpRunnable());
1221 >                shouldThrow();
1222 >            } catch (RejectedExecutionException success) {}
1223 >        }
1224      }
1225  
1226      /**
# Line 1237 | Line 1234 | public class ThreadPoolExecutorTest exte
1234                                     new ArrayBlockingQueue<Runnable>(1), h);
1235  
1236          try { p.shutdown(); } catch (SecurityException ok) { return; }
1237 <        try {
1237 >        try (PoolCleaner cleaner = cleaner(p)) {
1238              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1239              p.execute(r);
1240              assertFalse(r.done);
1244        } finally {
1245            joinPool(p);
1241          }
1242      }
1243  
# Line 1250 | Line 1245 | public class ThreadPoolExecutorTest exte
1245       * execute using DiscardPolicy drops task on shutdown
1246       */
1247      public void testDiscardOnShutdown() {
1248 <        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
1254 <        ThreadPoolExecutor p =
1248 >        final ThreadPoolExecutor p =
1249              new ThreadPoolExecutor(1, 1,
1250                                     LONG_DELAY_MS, MILLISECONDS,
1251                                     new ArrayBlockingQueue<Runnable>(1),
1252 <                                   h);
1252 >                                   new ThreadPoolExecutor.DiscardPolicy());
1253  
1254          try { p.shutdown(); } catch (SecurityException ok) { return; }
1255 <        try {
1255 >        try (PoolCleaner cleaner = cleaner(p)) {
1256              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1257              p.execute(r);
1258              assertFalse(r.done);
1265        } finally {
1266            joinPool(p);
1259          }
1260      }
1261  
# Line 1271 | Line 1263 | public class ThreadPoolExecutorTest exte
1263       * execute using DiscardOldestPolicy drops task on shutdown
1264       */
1265      public void testDiscardOldestOnShutdown() {
1266 <        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
1275 <        ThreadPoolExecutor p =
1266 >        final ThreadPoolExecutor p =
1267              new ThreadPoolExecutor(1, 1,
1268                                     LONG_DELAY_MS, MILLISECONDS,
1269                                     new ArrayBlockingQueue<Runnable>(1),
1270 <                                   h);
1270 >                                   new ThreadPoolExecutor.DiscardOldestPolicy());
1271  
1272          try { p.shutdown(); } catch (SecurityException ok) { return; }
1273 <        try {
1273 >        try (PoolCleaner cleaner = cleaner(p)) {
1274              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1275              p.execute(r);
1276              assertFalse(r.done);
1286        } finally {
1287            joinPool(p);
1277          }
1278      }
1279  
# Line 1292 | Line 1281 | public class ThreadPoolExecutorTest exte
1281       * execute(null) throws NPE
1282       */
1283      public void testExecuteNull() {
1284 <        ThreadPoolExecutor p =
1285 <            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1284 >        final ThreadPoolExecutor p =
1285 >            new ThreadPoolExecutor(1, 2,
1286 >                                   1L, SECONDS,
1287                                     new ArrayBlockingQueue<Runnable>(10));
1288 <        try {
1289 <            p.execute(null);
1290 <            shouldThrow();
1291 <        } catch (NullPointerException success) {}
1292 <
1293 <        joinPool(p);
1288 >        try (PoolCleaner cleaner = cleaner(p)) {
1289 >            try {
1290 >                p.execute(null);
1291 >                shouldThrow();
1292 >            } catch (NullPointerException success) {}
1293 >        }
1294      }
1295  
1296      /**
1297       * setCorePoolSize of negative value throws IllegalArgumentException
1298       */
1299      public void testCorePoolSizeIllegalArgumentException() {
1300 <        ThreadPoolExecutor p =
1300 >        final ThreadPoolExecutor p =
1301              new ThreadPoolExecutor(1, 2,
1302                                     LONG_DELAY_MS, MILLISECONDS,
1303                                     new ArrayBlockingQueue<Runnable>(10));
1304 <        try {
1305 <            p.setCorePoolSize(-1);
1306 <            shouldThrow();
1307 <        } catch (IllegalArgumentException success) {
1308 <        } finally {
1319 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1304 >        try (PoolCleaner cleaner = cleaner(p)) {
1305 >            try {
1306 >                p.setCorePoolSize(-1);
1307 >                shouldThrow();
1308 >            } catch (IllegalArgumentException success) {}
1309          }
1321        joinPool(p);
1310      }
1311  
1312      /**
# Line 1326 | Line 1314 | public class ThreadPoolExecutorTest exte
1314       * given a value less the core pool size
1315       */
1316      public void testMaximumPoolSizeIllegalArgumentException() {
1317 <        ThreadPoolExecutor p =
1317 >        final ThreadPoolExecutor p =
1318              new ThreadPoolExecutor(2, 3,
1319                                     LONG_DELAY_MS, MILLISECONDS,
1320                                     new ArrayBlockingQueue<Runnable>(10));
1321 <        try {
1322 <            p.setMaximumPoolSize(1);
1323 <            shouldThrow();
1324 <        } catch (IllegalArgumentException success) {
1325 <        } finally {
1338 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1321 >        try (PoolCleaner cleaner = cleaner(p)) {
1322 >            try {
1323 >                p.setMaximumPoolSize(1);
1324 >                shouldThrow();
1325 >            } catch (IllegalArgumentException success) {}
1326          }
1340        joinPool(p);
1327      }
1328  
1329      /**
# Line 1345 | Line 1331 | public class ThreadPoolExecutorTest exte
1331       * if given a negative value
1332       */
1333      public void testMaximumPoolSizeIllegalArgumentException2() {
1334 <        ThreadPoolExecutor p =
1334 >        final ThreadPoolExecutor p =
1335              new ThreadPoolExecutor(2, 3,
1336                                     LONG_DELAY_MS, MILLISECONDS,
1337                                     new ArrayBlockingQueue<Runnable>(10));
1338 <        try {
1339 <            p.setMaximumPoolSize(-1);
1340 <            shouldThrow();
1341 <        } catch (IllegalArgumentException success) {
1342 <        } finally {
1357 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1338 >        try (PoolCleaner cleaner = cleaner(p)) {
1339 >            try {
1340 >                p.setMaximumPoolSize(-1);
1341 >                shouldThrow();
1342 >            } catch (IllegalArgumentException success) {}
1343          }
1359        joinPool(p);
1344      }
1345  
1346      /**
# Line 1364 | Line 1348 | public class ThreadPoolExecutorTest exte
1348       * max pool size result in IllegalArgumentException.
1349       */
1350      public void testPoolSizeInvariants() {
1351 <        ThreadPoolExecutor p =
1351 >        final ThreadPoolExecutor p =
1352              new ThreadPoolExecutor(1, 1,
1353                                     LONG_DELAY_MS, MILLISECONDS,
1354                                     new ArrayBlockingQueue<Runnable>(10));
1355 <        for (int s = 1; s < 5; s++) {
1356 <            p.setMaximumPoolSize(s);
1357 <            p.setCorePoolSize(s);
1358 <            try {
1359 <                p.setMaximumPoolSize(s - 1);
1360 <                shouldThrow();
1361 <            } catch (IllegalArgumentException success) {}
1362 <            assertEquals(s, p.getCorePoolSize());
1363 <            assertEquals(s, p.getMaximumPoolSize());
1364 <            try {
1365 <                p.setCorePoolSize(s + 1);
1366 <                shouldThrow();
1367 <            } catch (IllegalArgumentException success) {}
1368 <            assertEquals(s, p.getCorePoolSize());
1369 <            assertEquals(s, p.getMaximumPoolSize());
1355 >        try (PoolCleaner cleaner = cleaner(p)) {
1356 >            for (int s = 1; s < 5; s++) {
1357 >                p.setMaximumPoolSize(s);
1358 >                p.setCorePoolSize(s);
1359 >                try {
1360 >                    p.setMaximumPoolSize(s - 1);
1361 >                    shouldThrow();
1362 >                } catch (IllegalArgumentException success) {}
1363 >                assertEquals(s, p.getCorePoolSize());
1364 >                assertEquals(s, p.getMaximumPoolSize());
1365 >                try {
1366 >                    p.setCorePoolSize(s + 1);
1367 >                    shouldThrow();
1368 >                } catch (IllegalArgumentException success) {}
1369 >                assertEquals(s, p.getCorePoolSize());
1370 >                assertEquals(s, p.getMaximumPoolSize());
1371 >            }
1372          }
1387        joinPool(p);
1373      }
1374  
1375      /**
# Line 1392 | Line 1377 | public class ThreadPoolExecutorTest exte
1377       * when given a negative value
1378       */
1379      public void testKeepAliveTimeIllegalArgumentException() {
1380 <        ThreadPoolExecutor p =
1380 >        final ThreadPoolExecutor p =
1381              new ThreadPoolExecutor(2, 3,
1382                                     LONG_DELAY_MS, MILLISECONDS,
1383                                     new ArrayBlockingQueue<Runnable>(10));
1384 <        try {
1385 <            p.setKeepAliveTime(-1,MILLISECONDS);
1386 <            shouldThrow();
1387 <        } catch (IllegalArgumentException success) {
1388 <        } finally {
1404 <            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          }
1406        joinPool(p);
1390      }
1391  
1392      /**
# Line 1411 | Line 1394 | public class ThreadPoolExecutorTest exte
1394       */
1395      public void testTerminated() {
1396          ExtendedTPE p = new ExtendedTPE();
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      /**
# Line 1421 | Line 1406 | public class ThreadPoolExecutorTest exte
1406       */
1407      public void testBeforeAfter() throws InterruptedException {
1408          ExtendedTPE p = new ExtendedTPE();
1409 <        try {
1409 >        try (PoolCleaner cleaner = cleaner(p)) {
1410              final CountDownLatch done = new CountDownLatch(1);
1411              p.execute(new CheckedRunnable() {
1412                  public void realRun() {
# Line 1431 | Line 1416 | public class ThreadPoolExecutorTest exte
1416              assertEquals(0, done.getCount());
1417              assertTrue(p.afterCalled());
1418              assertTrue(p.beforeCalled());
1434            try { p.shutdown(); } catch (SecurityException ok) { return; }
1435        } finally {
1436            joinPool(p);
1419          }
1420      }
1421  
# Line 1441 | Line 1423 | public class ThreadPoolExecutorTest exte
1423       * completed submit of callable returns result
1424       */
1425      public void testSubmitCallable() throws Exception {
1426 <        ExecutorService e =
1426 >        final ExecutorService e =
1427              new ThreadPoolExecutor(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);
1452        } finally {
1453            joinPool(e);
1434          }
1435      }
1436  
# Line 1458 | Line 1438 | public class ThreadPoolExecutorTest exte
1438       * completed submit of runnable returns successfully
1439       */
1440      public void testSubmitRunnable() throws Exception {
1441 <        ExecutorService e =
1441 >        final ExecutorService e =
1442              new ThreadPoolExecutor(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());
1469        } finally {
1470            joinPool(e);
1449          }
1450      }
1451  
# Line 1475 | Line 1453 | public class ThreadPoolExecutorTest exte
1453       * completed submit of (runnable, result) returns result
1454       */
1455      public void testSubmitRunnable2() throws Exception {
1456 <        ExecutorService e =
1456 >        final ExecutorService e =
1457              new ThreadPoolExecutor(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);
1486        } finally {
1487            joinPool(e);
1464          }
1465      }
1466  
# Line 1492 | Line 1468 | public class ThreadPoolExecutorTest exte
1468       * invokeAny(null) throws NPE
1469       */
1470      public void testInvokeAny1() throws Exception {
1471 <        ExecutorService e =
1471 >        final ExecutorService e =
1472              new ThreadPoolExecutor(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 {
1504 <            joinPool(e);
1475 >        try (PoolCleaner cleaner = cleaner(e)) {
1476 >            try {
1477 >                e.invokeAny(null);
1478 >                shouldThrow();
1479 >            } catch (NullPointerException success) {}
1480          }
1481      }
1482  
# Line 1509 | Line 1484 | public class ThreadPoolExecutorTest exte
1484       * invokeAny(empty collection) throws IAE
1485       */
1486      public void testInvokeAny2() throws Exception {
1487 <        ExecutorService e =
1487 >        final ExecutorService e =
1488              new ThreadPoolExecutor(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 {
1521 <            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  
# Line 1531 | Line 1505 | public class ThreadPoolExecutorTest exte
1505              new ThreadPoolExecutor(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<>();
1510 >            l.add(latchAwaitingStringTask(latch));
1511 >            l.add(null);
1512 >            try {
1513 >                e.invokeAny(l);
1514 >                shouldThrow();
1515 >            } catch (NullPointerException success) {}
1516              latch.countDown();
1543            joinPool(e);
1517          }
1518      }
1519  
# Line 1548 | Line 1521 | public class ThreadPoolExecutorTest exte
1521       * invokeAny(c) throws ExecutionException if no task completes
1522       */
1523      public void testInvokeAny4() throws Exception {
1524 <        ExecutorService e =
1524 >        final ExecutorService e =
1525              new ThreadPoolExecutor(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<>();
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  
# Line 1568 | Line 1541 | public class ThreadPoolExecutorTest exte
1541       * invokeAny(c) returns result of some task
1542       */
1543      public void testInvokeAny5() throws Exception {
1544 <        ExecutorService e =
1544 >        final ExecutorService e =
1545              new ThreadPoolExecutor(2, 2,
1546                                     LONG_DELAY_MS, MILLISECONDS,
1547                                     new ArrayBlockingQueue<Runnable>(10));
1548 <        try {
1549 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1548 >        try (PoolCleaner cleaner = cleaner(e)) {
1549 >            List<Callable<String>> l = new ArrayList<>();
1550              l.add(new StringTask());
1551              l.add(new StringTask());
1552              String result = e.invokeAny(l);
1553              assertSame(TEST_STRING, result);
1581        } finally {
1582            joinPool(e);
1554          }
1555      }
1556  
# Line 1587 | Line 1558 | public class ThreadPoolExecutorTest exte
1558       * invokeAll(null) throws NPE
1559       */
1560      public void testInvokeAll1() throws Exception {
1561 <        ExecutorService e =
1561 >        final ExecutorService e =
1562              new ThreadPoolExecutor(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 {
1599 <            joinPool(e);
1565 >        try (PoolCleaner cleaner = cleaner(e)) {
1566 >            try {
1567 >                e.invokeAll(null);
1568 >                shouldThrow();
1569 >            } catch (NullPointerException success) {}
1570          }
1571      }
1572  
# Line 1604 | Line 1574 | public class ThreadPoolExecutorTest exte
1574       * invokeAll(empty collection) returns empty collection
1575       */
1576      public void testInvokeAll2() throws InterruptedException {
1577 <        ExecutorService e =
1577 >        final ExecutorService e =
1578              new ThreadPoolExecutor(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());
1614        } finally {
1615            joinPool(e);
1584          }
1585      }
1586  
# Line 1620 | Line 1588 | public class ThreadPoolExecutorTest exte
1588       * invokeAll(c) throws NPE if c has null elements
1589       */
1590      public void testInvokeAll3() throws Exception {
1591 <        ExecutorService e =
1591 >        final ExecutorService e =
1592              new ThreadPoolExecutor(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 {
1635 <            joinPool(e);
1595 >        try (PoolCleaner cleaner = cleaner(e)) {
1596 >            List<Callable<String>> l = new ArrayList<>();
1597 >            l.add(new StringTask());
1598 >            l.add(null);
1599 >            try {
1600 >                e.invokeAll(l);
1601 >                shouldThrow();
1602 >            } catch (NullPointerException success) {}
1603          }
1604      }
1605  
# Line 1640 | Line 1607 | public class ThreadPoolExecutorTest exte
1607       * get of element of invokeAll(c) throws exception on failed task
1608       */
1609      public void testInvokeAll4() throws Exception {
1610 <        ExecutorService e =
1610 >        final ExecutorService e =
1611              new ThreadPoolExecutor(2, 2,
1612                                     LONG_DELAY_MS, MILLISECONDS,
1613                                     new ArrayBlockingQueue<Runnable>(10));
1614 <        try {
1615 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1614 >        try (PoolCleaner cleaner = cleaner(e)) {
1615 >            List<Callable<String>> l = new ArrayList<>();
1616              l.add(new NPETask());
1617              List<Future<String>> futures = e.invokeAll(l);
1618              assertEquals(1, futures.size());
# Line 1655 | Line 1622 | public class ThreadPoolExecutorTest exte
1622              } catch (ExecutionException success) {
1623                  assertTrue(success.getCause() instanceof NullPointerException);
1624              }
1658        } finally {
1659            joinPool(e);
1625          }
1626      }
1627  
# Line 1664 | Line 1629 | public class ThreadPoolExecutorTest exte
1629       * invokeAll(c) returns results of all completed tasks
1630       */
1631      public void testInvokeAll5() throws Exception {
1632 <        ExecutorService e =
1632 >        final ExecutorService e =
1633              new ThreadPoolExecutor(2, 2,
1634                                     LONG_DELAY_MS, MILLISECONDS,
1635                                     new ArrayBlockingQueue<Runnable>(10));
1636 <        try {
1637 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1636 >        try (PoolCleaner cleaner = cleaner(e)) {
1637 >            List<Callable<String>> l = new ArrayList<>();
1638              l.add(new StringTask());
1639              l.add(new StringTask());
1640              List<Future<String>> futures = e.invokeAll(l);
1641              assertEquals(2, futures.size());
1642              for (Future<String> future : futures)
1643                  assertSame(TEST_STRING, future.get());
1679        } finally {
1680            joinPool(e);
1644          }
1645      }
1646  
# Line 1685 | Line 1648 | public class ThreadPoolExecutorTest exte
1648       * timed invokeAny(null) throws NPE
1649       */
1650      public void testTimedInvokeAny1() throws Exception {
1651 <        ExecutorService e =
1651 >        final ExecutorService e =
1652              new ThreadPoolExecutor(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 {
1697 <            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  
# Line 1702 | Line 1664 | public class ThreadPoolExecutorTest exte
1664       * timed invokeAny(,,null) throws NPE
1665       */
1666      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1667 <        ExecutorService e =
1667 >        final ExecutorService e =
1668              new ThreadPoolExecutor(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 {
1716 <            joinPool(e);
1671 >        try (PoolCleaner cleaner = cleaner(e)) {
1672 >            List<Callable<String>> l = new ArrayList<>();
1673 >            l.add(new StringTask());
1674 >            try {
1675 >                e.invokeAny(l, MEDIUM_DELAY_MS, null);
1676 >                shouldThrow();
1677 >            } catch (NullPointerException success) {}
1678          }
1679      }
1680  
# Line 1721 | Line 1682 | public class ThreadPoolExecutorTest exte
1682       * timed invokeAny(empty collection) throws IAE
1683       */
1684      public void testTimedInvokeAny2() throws Exception {
1685 <        ExecutorService e =
1685 >        final ExecutorService e =
1686              new ThreadPoolExecutor(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  
# Line 1743 | Line 1704 | public class ThreadPoolExecutorTest exte
1704              new ThreadPoolExecutor(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<>();
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();
1755            joinPool(e);
1716          }
1717      }
1718  
# Line 1760 | Line 1720 | public class ThreadPoolExecutorTest exte
1720       * timed invokeAny(c) throws ExecutionException if no task completes
1721       */
1722      public void testTimedInvokeAny4() throws Exception {
1723 <        ExecutorService e =
1723 >        final ExecutorService e =
1724              new ThreadPoolExecutor(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 >            long startTime = System.nanoTime();
1729 >            List<Callable<String>> l = new ArrayList<>();
1730 >            l.add(new NPETask());
1731 >            try {
1732 >                e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1733 >                shouldThrow();
1734 >            } catch (ExecutionException success) {
1735 >                assertTrue(success.getCause() instanceof NullPointerException);
1736 >            }
1737 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1738          }
1739      }
1740  
# Line 1780 | Line 1742 | public class ThreadPoolExecutorTest exte
1742       * timed invokeAny(c) returns result of some task
1743       */
1744      public void testTimedInvokeAny5() throws Exception {
1745 <        ExecutorService e =
1745 >        final ExecutorService e =
1746              new ThreadPoolExecutor(2, 2,
1747                                     LONG_DELAY_MS, MILLISECONDS,
1748                                     new ArrayBlockingQueue<Runnable>(10));
1749 <        try {
1750 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1749 >        try (PoolCleaner cleaner = cleaner(e)) {
1750 >            long startTime = System.nanoTime();
1751 >            List<Callable<String>> l = new ArrayList<>();
1752              l.add(new StringTask());
1753              l.add(new StringTask());
1754 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1754 >            String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1755              assertSame(TEST_STRING, result);
1756 <        } finally {
1794 <            joinPool(e);
1756 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1757          }
1758      }
1759  
# Line 1799 | Line 1761 | public class ThreadPoolExecutorTest exte
1761       * timed invokeAll(null) throws NPE
1762       */
1763      public void testTimedInvokeAll1() throws Exception {
1764 <        ExecutorService e =
1764 >        final ExecutorService e =
1765              new ThreadPoolExecutor(2, 2,
1766                                     LONG_DELAY_MS, MILLISECONDS,
1767                                     new ArrayBlockingQueue<Runnable>(10));
1768 <        try {
1769 <            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1770 <            shouldThrow();
1771 <        } catch (NullPointerException success) {
1772 <        } finally {
1811 <            joinPool(e);
1768 >        try (PoolCleaner cleaner = cleaner(e)) {
1769 >            try {
1770 >                e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1771 >                shouldThrow();
1772 >            } catch (NullPointerException success) {}
1773          }
1774      }
1775  
# Line 1816 | Line 1777 | public class ThreadPoolExecutorTest exte
1777       * timed invokeAll(,,null) throws NPE
1778       */
1779      public void testTimedInvokeAllNullTimeUnit() throws Exception {
1780 <        ExecutorService e =
1780 >        final ExecutorService e =
1781              new ThreadPoolExecutor(2, 2,
1782                                     LONG_DELAY_MS, MILLISECONDS,
1783                                     new ArrayBlockingQueue<Runnable>(10));
1784 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1785 <        l.add(new StringTask());
1786 <        try {
1787 <            e.invokeAll(l, MEDIUM_DELAY_MS, null);
1788 <            shouldThrow();
1789 <        } catch (NullPointerException success) {
1790 <        } finally {
1830 <            joinPool(e);
1784 >        try (PoolCleaner cleaner = cleaner(e)) {
1785 >            List<Callable<String>> l = new ArrayList<>();
1786 >            l.add(new StringTask());
1787 >            try {
1788 >                e.invokeAll(l, MEDIUM_DELAY_MS, null);
1789 >                shouldThrow();
1790 >            } catch (NullPointerException success) {}
1791          }
1792      }
1793  
# Line 1835 | Line 1795 | public class ThreadPoolExecutorTest exte
1795       * timed invokeAll(empty collection) returns empty collection
1796       */
1797      public void testTimedInvokeAll2() throws InterruptedException {
1798 <        ExecutorService e =
1798 >        final ExecutorService e =
1799              new ThreadPoolExecutor(2, 2,
1800                                     LONG_DELAY_MS, MILLISECONDS,
1801                                     new ArrayBlockingQueue<Runnable>(10));
1802 <        try {
1803 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1802 >        try (PoolCleaner cleaner = cleaner(e)) {
1803 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(),
1804 >                                                 MEDIUM_DELAY_MS, MILLISECONDS);
1805              assertTrue(r.isEmpty());
1845        } finally {
1846            joinPool(e);
1806          }
1807      }
1808  
# Line 1851 | Line 1810 | public class ThreadPoolExecutorTest exte
1810       * timed invokeAll(c) throws NPE if c has null elements
1811       */
1812      public void testTimedInvokeAll3() throws Exception {
1813 <        ExecutorService e =
1813 >        final ExecutorService e =
1814              new ThreadPoolExecutor(2, 2,
1815                                     LONG_DELAY_MS, MILLISECONDS,
1816                                     new ArrayBlockingQueue<Runnable>(10));
1817 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1818 <        l.add(new StringTask());
1819 <        l.add(null);
1820 <        try {
1821 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1822 <            shouldThrow();
1823 <        } catch (NullPointerException success) {
1824 <        } finally {
1866 <            joinPool(e);
1817 >        try (PoolCleaner cleaner = cleaner(e)) {
1818 >            List<Callable<String>> l = new ArrayList<>();
1819 >            l.add(new StringTask());
1820 >            l.add(null);
1821 >            try {
1822 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1823 >                shouldThrow();
1824 >            } catch (NullPointerException success) {}
1825          }
1826      }
1827  
# Line 1871 | Line 1829 | public class ThreadPoolExecutorTest exte
1829       * get of element of invokeAll(c) throws exception on failed task
1830       */
1831      public void testTimedInvokeAll4() throws Exception {
1832 <        ExecutorService e =
1832 >        final ExecutorService e =
1833              new ThreadPoolExecutor(2, 2,
1834                                     LONG_DELAY_MS, MILLISECONDS,
1835                                     new ArrayBlockingQueue<Runnable>(10));
1836 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1837 <        l.add(new NPETask());
1838 <        List<Future<String>> futures =
1839 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1840 <        assertEquals(1, futures.size());
1841 <        try {
1842 <            futures.get(0).get();
1843 <            shouldThrow();
1844 <        } catch (ExecutionException success) {
1845 <            assertTrue(success.getCause() instanceof NullPointerException);
1846 <        } finally {
1847 <            joinPool(e);
1836 >        try (PoolCleaner cleaner = cleaner(e)) {
1837 >            List<Callable<String>> l = new ArrayList<>();
1838 >            l.add(new NPETask());
1839 >            List<Future<String>> futures =
1840 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1841 >            assertEquals(1, futures.size());
1842 >            try {
1843 >                futures.get(0).get();
1844 >                shouldThrow();
1845 >            } catch (ExecutionException success) {
1846 >                assertTrue(success.getCause() instanceof NullPointerException);
1847 >            }
1848          }
1849      }
1850  
# Line 1894 | Line 1852 | public class ThreadPoolExecutorTest exte
1852       * timed invokeAll(c) returns results of all completed tasks
1853       */
1854      public void testTimedInvokeAll5() throws Exception {
1855 <        ExecutorService e =
1855 >        final ExecutorService e =
1856              new ThreadPoolExecutor(2, 2,
1857                                     LONG_DELAY_MS, MILLISECONDS,
1858                                     new ArrayBlockingQueue<Runnable>(10));
1859 <        try {
1860 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1859 >        try (PoolCleaner cleaner = cleaner(e)) {
1860 >            List<Callable<String>> l = new ArrayList<>();
1861              l.add(new StringTask());
1862              l.add(new StringTask());
1863              List<Future<String>> futures =
# Line 1907 | Line 1865 | public class ThreadPoolExecutorTest exte
1865              assertEquals(2, futures.size());
1866              for (Future<String> future : futures)
1867                  assertSame(TEST_STRING, future.get());
1910        } finally {
1911            joinPool(e);
1868          }
1869      }
1870  
# Line 1916 | Line 1872 | public class ThreadPoolExecutorTest exte
1872       * timed invokeAll(c) cancels tasks not completed by timeout
1873       */
1874      public void testTimedInvokeAll6() throws Exception {
1875 <        ExecutorService e =
1876 <            new ThreadPoolExecutor(2, 2,
1877 <                                   LONG_DELAY_MS, MILLISECONDS,
1878 <                                   new ArrayBlockingQueue<Runnable>(10));
1879 <        try {
1880 <            for (long timeout = timeoutMillis();;) {
1875 >        for (long timeout = timeoutMillis();;) {
1876 >            final CountDownLatch done = new CountDownLatch(1);
1877 >            final Callable<String> waiter = new CheckedCallable<String>() {
1878 >                public String realCall() {
1879 >                    try { done.await(LONG_DELAY_MS, MILLISECONDS); }
1880 >                    catch (InterruptedException ok) {}
1881 >                    return "1"; }};
1882 >            final ExecutorService p =
1883 >                new ThreadPoolExecutor(2, 2,
1884 >                                       LONG_DELAY_MS, MILLISECONDS,
1885 >                                       new ArrayBlockingQueue<Runnable>(10));
1886 >            try (PoolCleaner cleaner = cleaner(p, done)) {
1887                  List<Callable<String>> tasks = new ArrayList<>();
1888                  tasks.add(new StringTask("0"));
1889 <                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1889 >                tasks.add(waiter);
1890                  tasks.add(new StringTask("2"));
1891                  long startTime = System.nanoTime();
1892                  List<Future<String>> futures =
1893 <                    e.invokeAll(tasks, timeout, MILLISECONDS);
1893 >                    p.invokeAll(tasks, timeout, MILLISECONDS);
1894                  assertEquals(tasks.size(), futures.size());
1895                  assertTrue(millisElapsedSince(startTime) >= timeout);
1896                  for (Future future : futures)
# Line 1944 | Line 1906 | public class ThreadPoolExecutorTest exte
1906                          fail("expected exactly one task to be cancelled");
1907                  }
1908              }
1947        } finally {
1948            joinPool(e);
1909          }
1910      }
1911  
# Line 1959 | Line 1919 | public class ThreadPoolExecutorTest exte
1919                                     LONG_DELAY_MS, MILLISECONDS,
1920                                     new LinkedBlockingQueue<Runnable>(),
1921                                     new FailingThreadFactory());
1922 <        try {
1922 >        try (PoolCleaner cleaner = cleaner(e)) {
1923              final int TASKS = 100;
1924              final CountDownLatch done = new CountDownLatch(TASKS);
1925              for (int k = 0; k < TASKS; ++k)
# Line 1968 | Line 1928 | public class ThreadPoolExecutorTest exte
1928                          done.countDown();
1929                      }});
1930              assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1971        } finally {
1972            joinPool(e);
1931          }
1932      }
1933  
# Line 1981 | Line 1939 | public class ThreadPoolExecutorTest exte
1939              new ThreadPoolExecutor(2, 2,
1940                                     1000, MILLISECONDS,
1941                                     new ArrayBlockingQueue<Runnable>(10));
1942 <        assertFalse(p.allowsCoreThreadTimeOut());
1943 <        joinPool(p);
1942 >        try (PoolCleaner cleaner = cleaner(p)) {
1943 >            assertFalse(p.allowsCoreThreadTimeOut());
1944 >        }
1945      }
1946  
1947      /**
# Line 1994 | Line 1953 | public class ThreadPoolExecutorTest exte
1953              new ThreadPoolExecutor(2, 10,
1954                                     keepAliveTime, MILLISECONDS,
1955                                     new ArrayBlockingQueue<Runnable>(10));
1956 <        final CountDownLatch threadStarted = new CountDownLatch(1);
1957 <        try {
1956 >        try (PoolCleaner cleaner = cleaner(p)) {
1957 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1958              p.allowCoreThreadTimeOut(true);
1959              p.execute(new CheckedRunnable() {
1960                  public void realRun() {
# Line 2010 | Line 1969 | public class ThreadPoolExecutorTest exte
1969                  Thread.yield();
1970              assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1971              assertEquals(0, p.getPoolSize());
2013        } finally {
2014            joinPool(p);
1972          }
1973      }
1974  
# Line 2024 | Line 1981 | public class ThreadPoolExecutorTest exte
1981              new ThreadPoolExecutor(2, 10,
1982                                     keepAliveTime, MILLISECONDS,
1983                                     new ArrayBlockingQueue<Runnable>(10));
1984 <        final CountDownLatch threadStarted = new CountDownLatch(1);
1985 <        try {
1984 >        try (PoolCleaner cleaner = cleaner(p)) {
1985 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1986              p.allowCoreThreadTimeOut(false);
1987              p.execute(new CheckedRunnable() {
1988                  public void realRun() throws InterruptedException {
# Line 2034 | Line 1991 | public class ThreadPoolExecutorTest exte
1991                  }});
1992              delay(2 * keepAliveTime);
1993              assertTrue(p.getPoolSize() >= 1);
2037        } finally {
2038            joinPool(p);
1994          }
1995      }
1996  
# Line 2054 | Line 2009 | public class ThreadPoolExecutorTest exte
2009              new ThreadPoolExecutor(1, 30,
2010                                     60, SECONDS,
2011                                     new ArrayBlockingQueue(30));
2012 <        try {
2012 >        try (PoolCleaner cleaner = cleaner(p)) {
2013              for (int i = 0; i < nTasks; ++i) {
2014                  for (;;) {
2015                      try {
# Line 2066 | Line 2021 | public class ThreadPoolExecutorTest exte
2021              }
2022              // enough time to run all tasks
2023              assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS));
2069        } finally {
2070            joinPool(p);
2024          }
2025      }
2026  
# Line 2075 | Line 2028 | public class ThreadPoolExecutorTest exte
2028       * get(cancelled task) throws CancellationException
2029       */
2030      public void testGet_cancelled() throws Exception {
2031 +        final CountDownLatch done = new CountDownLatch(1);
2032          final ExecutorService e =
2033              new ThreadPoolExecutor(1, 1,
2034                                     LONG_DELAY_MS, MILLISECONDS,
2035                                     new LinkedBlockingQueue<Runnable>());
2036 <        try {
2036 >        try (PoolCleaner cleaner = cleaner(e, done)) {
2037              final CountDownLatch blockerStarted = new CountDownLatch(1);
2084            final CountDownLatch done = new CountDownLatch(1);
2038              final List<Future<?>> futures = new ArrayList<>();
2039              for (int i = 0; i < 2; i++) {
2040                  Runnable r = new CheckedRunnable() { public void realRun()
# Line 2091 | Line 2044 | public class ThreadPoolExecutorTest exte
2044                  }};
2045                  futures.add(e.submit(r));
2046              }
2047 <            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
2047 >            await(blockerStarted);
2048              for (Future<?> future : futures) future.cancel(false);
2049              for (Future<?> future : futures) {
2050                  try {
# Line 2105 | Line 2058 | public class ThreadPoolExecutorTest exte
2058                  assertTrue(future.isCancelled());
2059                  assertTrue(future.isDone());
2060              }
2108            done.countDown();
2109        } finally {
2110            joinPool(e);
2061          }
2062      }
2063  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines