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.84 by jsr166, Sun Oct 4 02:31:15 2015 UTC vs.
Revision 1.118 by jsr166, Mon May 29 19:15:03 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 92 | Line 90 | public class ThreadPoolExecutorTest exte
90              final Runnable task = new CheckedRunnable() {
91                  public void realRun() { done.countDown(); }};
92              p.execute(task);
93 <            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
93 >            await(done);
94          }
95      }
96  
# 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 187 | Line 184 | public class ThreadPoolExecutorTest exte
184                  public void realRun() throws InterruptedException {
185                      threadStarted.countDown();
186                      assertEquals(0, p.getCompletedTaskCount());
187 <                    threadProceed.await();
187 >                    await(threadProceed);
188                      threadDone.countDown();
189                  }});
190              await(threadStarted);
191              assertEquals(0, p.getCompletedTaskCount());
192              threadProceed.countDown();
193 <            threadDone.await();
193 >            await(threadDone);
194              long startTime = System.nanoTime();
195              while (p.getCompletedTaskCount() != 1) {
196                  if (millisElapsedSince(startTime) > LONG_DELAY_MS)
# Line 277 | Line 274 | public class ThreadPoolExecutorTest exte
274      }
275  
276      /**
277 +     * The default rejected execution handler is AbortPolicy.
278 +     */
279 +    public void testDefaultRejectedExecutionHandler() {
280 +        final ThreadPoolExecutor p =
281 +            new ThreadPoolExecutor(1, 2,
282 +                                   LONG_DELAY_MS, MILLISECONDS,
283 +                                   new ArrayBlockingQueue<Runnable>(10));
284 +        try (PoolCleaner cleaner = cleaner(p)) {
285 +            assertTrue(p.getRejectedExecutionHandler()
286 +                       instanceof ThreadPoolExecutor.AbortPolicy);
287 +        }
288 +    }
289 +
290 +    /**
291       * getRejectedExecutionHandler returns handler in constructor if not set
292       */
293      public void testGetRejectedExecutionHandler() {
# Line 329 | Line 340 | public class ThreadPoolExecutorTest exte
340       */
341      public void testGetLargestPoolSize() throws InterruptedException {
342          final int THREADS = 3;
343 +        final CountDownLatch done = new CountDownLatch(1);
344          final ThreadPoolExecutor p =
345              new ThreadPoolExecutor(THREADS, THREADS,
346                                     LONG_DELAY_MS, MILLISECONDS,
347                                     new ArrayBlockingQueue<Runnable>(10));
348 <        try (PoolCleaner cleaner = cleaner(p)) {
337 <            final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
338 <            final CountDownLatch done = new CountDownLatch(1);
348 >        try (PoolCleaner cleaner = cleaner(p, done)) {
349              assertEquals(0, p.getLargestPoolSize());
350 +            final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
351              for (int i = 0; i < THREADS; i++)
352                  p.execute(new CheckedRunnable() {
353                      public void realRun() throws InterruptedException {
354                          threadsStarted.countDown();
355 <                        done.await();
355 >                        await(done);
356                          assertEquals(THREADS, p.getLargestPoolSize());
357                      }});
358 <            assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
358 >            await(threadsStarted);
359              assertEquals(THREADS, p.getLargestPoolSize());
349            done.countDown();   // release pool
360          }
361          assertEquals(THREADS, p.getLargestPoolSize());
362      }
# Line 374 | Line 384 | public class ThreadPoolExecutorTest exte
384       * become active
385       */
386      public void testGetPoolSize() throws InterruptedException {
387 +        final CountDownLatch done = new CountDownLatch(1);
388          final ThreadPoolExecutor p =
389              new ThreadPoolExecutor(1, 1,
390                                     LONG_DELAY_MS, MILLISECONDS,
391                                     new ArrayBlockingQueue<Runnable>(10));
392 <        try (PoolCleaner cleaner = cleaner(p)) {
382 <            final CountDownLatch threadStarted = new CountDownLatch(1);
383 <            final CountDownLatch done = new CountDownLatch(1);
392 >        try (PoolCleaner cleaner = cleaner(p, done)) {
393              assertEquals(0, p.getPoolSize());
394 +            final CountDownLatch threadStarted = new CountDownLatch(1);
395              p.execute(new CheckedRunnable() {
396                  public void realRun() throws InterruptedException {
397                      threadStarted.countDown();
398                      assertEquals(1, p.getPoolSize());
399 <                    done.await();
399 >                    await(done);
400                  }});
401 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
401 >            await(threadStarted);
402              assertEquals(1, p.getPoolSize());
393            done.countDown();   // release pool
403          }
404      }
405  
# Line 398 | Line 407 | public class ThreadPoolExecutorTest exte
407       * getTaskCount increases, but doesn't overestimate, when tasks submitted
408       */
409      public void testGetTaskCount() throws InterruptedException {
410 +        final int TASKS = 3;
411 +        final CountDownLatch done = new CountDownLatch(1);
412          final ThreadPoolExecutor p =
413              new ThreadPoolExecutor(1, 1,
414                                     LONG_DELAY_MS, MILLISECONDS,
415                                     new ArrayBlockingQueue<Runnable>(10));
416 <        try (PoolCleaner cleaner = cleaner(p)) {
416 >        try (PoolCleaner cleaner = cleaner(p, done)) {
417              final CountDownLatch threadStarted = new CountDownLatch(1);
407            final CountDownLatch done = new CountDownLatch(1);
418              assertEquals(0, p.getTaskCount());
419 +            assertEquals(0, p.getCompletedTaskCount());
420              p.execute(new CheckedRunnable() {
421                  public void realRun() throws InterruptedException {
422                      threadStarted.countDown();
423 <                    assertEquals(1, p.getTaskCount());
413 <                    done.await();
423 >                    await(done);
424                  }});
425 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
425 >            await(threadStarted);
426              assertEquals(1, p.getTaskCount());
427 <            done.countDown();
427 >            assertEquals(0, p.getCompletedTaskCount());
428 >            for (int i = 0; i < TASKS; i++) {
429 >                assertEquals(1 + i, p.getTaskCount());
430 >                p.execute(new CheckedRunnable() {
431 >                    public void realRun() throws InterruptedException {
432 >                        threadStarted.countDown();
433 >                        assertEquals(1 + TASKS, p.getTaskCount());
434 >                        await(done);
435 >                    }});
436 >            }
437 >            assertEquals(1 + TASKS, p.getTaskCount());
438 >            assertEquals(0, p.getCompletedTaskCount());
439          }
440 +        assertEquals(1 + TASKS, p.getTaskCount());
441 +        assertEquals(1 + TASKS, p.getCompletedTaskCount());
442      }
443  
444      /**
# Line 447 | Line 470 | public class ThreadPoolExecutorTest exte
470              assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
471              assertFalse(p.awaitTermination(-1L, NANOSECONDS));
472              assertFalse(p.awaitTermination(-1L, MILLISECONDS));
473 <            assertFalse(p.awaitTermination(0L, NANOSECONDS));
474 <            assertFalse(p.awaitTermination(0L, MILLISECONDS));
473 >            assertFalse(p.awaitTermination(randomExpiredTimeout(),
474 >                                           randomTimeUnit()));
475              long timeoutNanos = 999999L;
476              long startTime = System.nanoTime();
477              assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
# Line 481 | Line 504 | public class ThreadPoolExecutorTest exte
504                  public void realRun() throws InterruptedException {
505                      assertFalse(p.isTerminating());
506                      threadStarted.countDown();
507 <                    done.await();
507 >                    await(done);
508                  }});
509 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
509 >            await(threadStarted);
510              assertFalse(p.isTerminating());
511              done.countDown();
512              try { p.shutdown(); } catch (SecurityException ok) { return; }
# Line 501 | Line 524 | public class ThreadPoolExecutorTest exte
524              new ThreadPoolExecutor(1, 1,
525                                     LONG_DELAY_MS, MILLISECONDS,
526                                     new ArrayBlockingQueue<Runnable>(10));
527 <        final CountDownLatch threadStarted = new CountDownLatch(1);
528 <        final CountDownLatch done = new CountDownLatch(1);
529 <        try {
527 >        try (PoolCleaner cleaner = cleaner(p)) {
528 >            final CountDownLatch threadStarted = new CountDownLatch(1);
529 >            final CountDownLatch done = new CountDownLatch(1);
530              assertFalse(p.isTerminating());
531              p.execute(new CheckedRunnable() {
532                  public void realRun() throws InterruptedException {
533                      assertFalse(p.isTerminating());
534                      threadStarted.countDown();
535 <                    done.await();
535 >                    await(done);
536                  }});
537 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
537 >            await(threadStarted);
538              assertFalse(p.isTerminating());
539              done.countDown();
517        } finally {
540              try { p.shutdown(); } catch (SecurityException ok) { return; }
541 +            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
542 +            assertTrue(p.isTerminated());
543 +            assertFalse(p.isTerminating());
544          }
520        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
521        assertTrue(p.isTerminated());
522        assertFalse(p.isTerminating());
545      }
546  
547      /**
548       * getQueue returns the work queue, which contains queued tasks
549       */
550      public void testGetQueue() throws InterruptedException {
551 <        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
551 >        final CountDownLatch done = new CountDownLatch(1);
552 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
553          final ThreadPoolExecutor p =
554              new ThreadPoolExecutor(1, 1,
555                                     LONG_DELAY_MS, MILLISECONDS,
556                                     q);
557 <        final CountDownLatch threadStarted = new CountDownLatch(1);
558 <        final CountDownLatch done = new CountDownLatch(1);
536 <        try {
557 >        try (PoolCleaner cleaner = cleaner(p, done)) {
558 >            final CountDownLatch threadStarted = new CountDownLatch(1);
559              FutureTask[] tasks = new FutureTask[5];
560              for (int i = 0; i < tasks.length; i++) {
561                  Callable task = new CheckedCallable<Boolean>() {
562                      public Boolean realCall() throws InterruptedException {
563                          threadStarted.countDown();
564                          assertSame(q, p.getQueue());
565 <                        done.await();
565 >                        await(done);
566                          return Boolean.TRUE;
567                      }};
568                  tasks[i] = new FutureTask(task);
569                  p.execute(tasks[i]);
570              }
571 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
571 >            await(threadStarted);
572              assertSame(q, p.getQueue());
573              assertFalse(q.contains(tasks[0]));
574              assertTrue(q.contains(tasks[tasks.length - 1]));
575              assertEquals(tasks.length - 1, q.size());
554        } finally {
555            done.countDown();
556            joinPool(p);
576          }
577      }
578  
# Line 561 | Line 580 | public class ThreadPoolExecutorTest exte
580       * remove(task) removes queued task, and fails to remove active task
581       */
582      public void testRemove() throws InterruptedException {
583 <        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
583 >        final CountDownLatch done = new CountDownLatch(1);
584 >        BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
585          final ThreadPoolExecutor p =
586              new ThreadPoolExecutor(1, 1,
587                                     LONG_DELAY_MS, MILLISECONDS,
588                                     q);
589 <        Runnable[] tasks = new Runnable[5];
590 <        final CountDownLatch threadStarted = new CountDownLatch(1);
591 <        final CountDownLatch done = new CountDownLatch(1);
572 <        try {
589 >        try (PoolCleaner cleaner = cleaner(p, done)) {
590 >            Runnable[] tasks = new Runnable[6];
591 >            final CountDownLatch threadStarted = new CountDownLatch(1);
592              for (int i = 0; i < tasks.length; i++) {
593                  tasks[i] = new CheckedRunnable() {
594                      public void realRun() throws InterruptedException {
595                          threadStarted.countDown();
596 <                        done.await();
596 >                        await(done);
597                      }};
598                  p.execute(tasks[i]);
599              }
600 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
600 >            await(threadStarted);
601              assertFalse(p.remove(tasks[0]));
602              assertTrue(q.contains(tasks[4]));
603              assertTrue(q.contains(tasks[3]));
# Line 588 | Line 607 | public class ThreadPoolExecutorTest exte
607              assertTrue(q.contains(tasks[3]));
608              assertTrue(p.remove(tasks[3]));
609              assertFalse(q.contains(tasks[3]));
591        } finally {
592            done.countDown();
593            joinPool(p);
610          }
611      }
612  
# Line 600 | Line 616 | public class ThreadPoolExecutorTest exte
616      public void testPurge() throws InterruptedException {
617          final CountDownLatch threadStarted = new CountDownLatch(1);
618          final CountDownLatch done = new CountDownLatch(1);
619 <        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
619 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
620          final ThreadPoolExecutor p =
621              new ThreadPoolExecutor(1, 1,
622                                     LONG_DELAY_MS, MILLISECONDS,
623                                     q);
624 <        FutureTask[] tasks = new FutureTask[5];
625 <        try {
624 >        try (PoolCleaner cleaner = cleaner(p, done)) {
625 >            FutureTask[] tasks = new FutureTask[5];
626              for (int i = 0; i < tasks.length; i++) {
627                  Callable task = new CheckedCallable<Boolean>() {
628                      public Boolean realCall() throws InterruptedException {
629                          threadStarted.countDown();
630 <                        done.await();
630 >                        await(done);
631                          return Boolean.TRUE;
632                      }};
633                  tasks[i] = new FutureTask(task);
634                  p.execute(tasks[i]);
635              }
636 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
636 >            await(threadStarted);
637              assertEquals(tasks.length, p.getTaskCount());
638              assertEquals(tasks.length - 1, q.size());
639              assertEquals(1L, p.getActiveCount());
# Line 630 | Line 646 | public class ThreadPoolExecutorTest exte
646              p.purge();         // Nothing to do
647              assertEquals(tasks.length - 3, q.size());
648              assertEquals(tasks.length - 2, p.getTaskCount());
633        } finally {
634            done.countDown();
635            joinPool(p);
649          }
650      }
651  
# Line 648 | Line 661 | public class ThreadPoolExecutorTest exte
661              new ThreadPoolExecutor(poolSize, poolSize,
662                                     LONG_DELAY_MS, MILLISECONDS,
663                                     new ArrayBlockingQueue<Runnable>(10));
664 <        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
664 >        final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
665          Runnable waiter = new CheckedRunnable() { public void realRun() {
666              threadsStarted.countDown();
667              try {
# Line 658 | Line 671 | public class ThreadPoolExecutorTest exte
671          }};
672          for (int i = 0; i < count; i++)
673              p.execute(waiter);
674 <        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
674 >        await(threadsStarted);
675          assertEquals(poolSize, p.getActiveCount());
676          assertEquals(0, p.getCompletedTaskCount());
677          final List<Runnable> queuedTasks;
# Line 1020 | Line 1033 | public class ThreadPoolExecutorTest exte
1033       * get of submitted callable throws InterruptedException if interrupted
1034       */
1035      public void testInterruptedSubmit() throws InterruptedException {
1036 +        final CountDownLatch done = new CountDownLatch(1);
1037          final ThreadPoolExecutor p =
1038              new ThreadPoolExecutor(1, 1,
1039                                     60, SECONDS,
1040                                     new ArrayBlockingQueue<Runnable>(10));
1041  
1042 <        final CountDownLatch threadStarted = new CountDownLatch(1);
1043 <        final CountDownLatch done = new CountDownLatch(1);
1030 <        try {
1042 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1043 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1044              Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1045                  public void realRun() throws Exception {
1046                      Callable task = new CheckedCallable<Boolean>() {
1047                          public Boolean realCall() throws InterruptedException {
1048                              threadStarted.countDown();
1049 <                            done.await();
1049 >                            await(done);
1050                              return Boolean.TRUE;
1051                          }};
1052                      p.submit(task).get();
1053                  }});
1054  
1055 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
1055 >            await(threadStarted);
1056              t.interrupt();
1057 <            awaitTermination(t, MEDIUM_DELAY_MS);
1045 <        } finally {
1046 <            done.countDown();
1047 <            joinPool(p);
1057 >            awaitTermination(t);
1058          }
1059      }
1060  
# Line 1052 | Line 1062 | public class ThreadPoolExecutorTest exte
1062       * execute throws RejectedExecutionException if saturated.
1063       */
1064      public void testSaturatedExecute() {
1065 <        ThreadPoolExecutor p =
1065 >        final CountDownLatch done = new CountDownLatch(1);
1066 >        final ThreadPoolExecutor p =
1067              new ThreadPoolExecutor(1, 1,
1068                                     LONG_DELAY_MS, MILLISECONDS,
1069                                     new ArrayBlockingQueue<Runnable>(1));
1070 <        final CountDownLatch done = new CountDownLatch(1);
1060 <        try {
1070 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1071              Runnable task = new CheckedRunnable() {
1072                  public void realRun() throws InterruptedException {
1073 <                    done.await();
1073 >                    await(done);
1074                  }};
1075              for (int i = 0; i < 2; ++i)
1076                  p.execute(task);
# Line 1071 | Line 1081 | public class ThreadPoolExecutorTest exte
1081                  } catch (RejectedExecutionException success) {}
1082                  assertTrue(p.getTaskCount() <= 2);
1083              }
1074        } finally {
1075            done.countDown();
1076            joinPool(p);
1084          }
1085      }
1086  
# Line 1081 | Line 1088 | public class ThreadPoolExecutorTest exte
1088       * submit(runnable) throws RejectedExecutionException if saturated.
1089       */
1090      public void testSaturatedSubmitRunnable() {
1091 <        ThreadPoolExecutor p =
1091 >        final CountDownLatch done = new CountDownLatch(1);
1092 >        final ThreadPoolExecutor p =
1093              new ThreadPoolExecutor(1, 1,
1094                                     LONG_DELAY_MS, MILLISECONDS,
1095                                     new ArrayBlockingQueue<Runnable>(1));
1096 <        final CountDownLatch done = new CountDownLatch(1);
1089 <        try {
1096 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1097              Runnable task = new CheckedRunnable() {
1098                  public void realRun() throws InterruptedException {
1099 <                    done.await();
1099 >                    await(done);
1100                  }};
1101              for (int i = 0; i < 2; ++i)
1102                  p.submit(task);
# Line 1100 | Line 1107 | public class ThreadPoolExecutorTest exte
1107                  } catch (RejectedExecutionException success) {}
1108                  assertTrue(p.getTaskCount() <= 2);
1109              }
1103        } finally {
1104            done.countDown();
1105            joinPool(p);
1110          }
1111      }
1112  
# Line 1110 | Line 1114 | public class ThreadPoolExecutorTest exte
1114       * submit(callable) throws RejectedExecutionException if saturated.
1115       */
1116      public void testSaturatedSubmitCallable() {
1117 <        ThreadPoolExecutor p =
1117 >        final CountDownLatch done = new CountDownLatch(1);
1118 >        final ThreadPoolExecutor p =
1119              new ThreadPoolExecutor(1, 1,
1120                                     LONG_DELAY_MS, MILLISECONDS,
1121                                     new ArrayBlockingQueue<Runnable>(1));
1122 <        final CountDownLatch done = new CountDownLatch(1);
1118 <        try {
1122 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1123              Runnable task = new CheckedRunnable() {
1124                  public void realRun() throws InterruptedException {
1125 <                    done.await();
1125 >                    await(done);
1126                  }};
1127              for (int i = 0; i < 2; ++i)
1128 <                p.submit(Executors.callable(task));
1128 >                p.execute(task);
1129              for (int i = 0; i < 2; ++i) {
1130                  try {
1131                      p.execute(task);
# Line 1129 | Line 1133 | public class ThreadPoolExecutorTest exte
1133                  } catch (RejectedExecutionException success) {}
1134                  assertTrue(p.getTaskCount() <= 2);
1135              }
1132        } finally {
1133            done.countDown();
1134            joinPool(p);
1136          }
1137      }
1138  
# Line 1139 | Line 1140 | public class ThreadPoolExecutorTest exte
1140       * executor using CallerRunsPolicy runs task if saturated.
1141       */
1142      public void testSaturatedExecute2() {
1142        RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
1143          final ThreadPoolExecutor p =
1144              new ThreadPoolExecutor(1, 1,
1145                                     LONG_DELAY_MS,
1146                                     MILLISECONDS,
1147                                     new ArrayBlockingQueue<Runnable>(1),
1148 <                                   h);
1149 <        try {
1148 >                                   new ThreadPoolExecutor.CallerRunsPolicy());
1149 >        try (PoolCleaner cleaner = cleaner(p)) {
1150 >            final CountDownLatch done = new CountDownLatch(1);
1151 >            Runnable blocker = new CheckedRunnable() {
1152 >                public void realRun() throws InterruptedException {
1153 >                    await(done);
1154 >                }};
1155 >            p.execute(blocker);
1156              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1157 <            for (int i = 0; i < tasks.length; ++i)
1157 >            for (int i = 0; i < tasks.length; i++)
1158                  tasks[i] = new TrackedNoOpRunnable();
1159 <            TrackedLongRunnable mr = new TrackedLongRunnable();
1154 <            p.execute(mr);
1155 <            for (int i = 0; i < tasks.length; ++i)
1159 >            for (int i = 0; i < tasks.length; i++)
1160                  p.execute(tasks[i]);
1161 <            for (int i = 1; i < tasks.length; ++i)
1161 >            for (int i = 1; i < tasks.length; i++)
1162                  assertTrue(tasks[i].done);
1163 <            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1164 <        } finally {
1161 <            joinPool(p);
1163 >            assertFalse(tasks[0].done); // waiting in queue
1164 >            done.countDown();
1165          }
1166      }
1167  
# Line 1166 | Line 1169 | public class ThreadPoolExecutorTest exte
1169       * executor using DiscardPolicy drops task if saturated.
1170       */
1171      public void testSaturatedExecute3() {
1172 <        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
1172 >        final CountDownLatch done = new CountDownLatch(1);
1173 >        final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1174 >        for (int i = 0; i < tasks.length; ++i)
1175 >            tasks[i] = new TrackedNoOpRunnable();
1176          final ThreadPoolExecutor p =
1177              new ThreadPoolExecutor(1, 1,
1178 <                                   LONG_DELAY_MS, MILLISECONDS,
1179 <                                   new ArrayBlockingQueue<Runnable>(1),
1180 <                                   h);
1181 <        try {
1182 <            TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1183 <            for (int i = 0; i < tasks.length; ++i)
1178 <                tasks[i] = new TrackedNoOpRunnable();
1179 <            p.execute(new TrackedLongRunnable());
1178 >                          LONG_DELAY_MS, MILLISECONDS,
1179 >                          new ArrayBlockingQueue<Runnable>(1),
1180 >                          new ThreadPoolExecutor.DiscardPolicy());
1181 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1182 >            p.execute(awaiter(done));
1183 >
1184              for (TrackedNoOpRunnable task : tasks)
1185                  p.execute(task);
1186 <            for (TrackedNoOpRunnable task : tasks)
1187 <                assertFalse(task.done);
1184 <            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1185 <        } finally {
1186 <            joinPool(p);
1186 >            for (int i = 1; i < tasks.length; i++)
1187 >                assertFalse(tasks[i].done);
1188          }
1189 +        for (int i = 1; i < tasks.length; i++)
1190 +            assertFalse(tasks[i].done);
1191 +        assertTrue(tasks[0].done); // was waiting in queue
1192      }
1193  
1194      /**
1195       * executor using DiscardOldestPolicy drops oldest task if saturated.
1196       */
1197      public void testSaturatedExecute4() {
1198 <        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
1198 >        final CountDownLatch done = new CountDownLatch(1);
1199 >        LatchAwaiter r1 = awaiter(done);
1200 >        LatchAwaiter r2 = awaiter(done);
1201 >        LatchAwaiter r3 = awaiter(done);
1202          final ThreadPoolExecutor p =
1203              new ThreadPoolExecutor(1, 1,
1204                                     LONG_DELAY_MS, MILLISECONDS,
1205                                     new ArrayBlockingQueue<Runnable>(1),
1206 <                                   h);
1207 <        try {
1208 <            p.execute(new TrackedLongRunnable());
1209 <            TrackedLongRunnable r2 = new TrackedLongRunnable();
1206 >                                   new ThreadPoolExecutor.DiscardOldestPolicy());
1207 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1208 >            assertEquals(LatchAwaiter.NEW, r1.state);
1209 >            assertEquals(LatchAwaiter.NEW, r2.state);
1210 >            assertEquals(LatchAwaiter.NEW, r3.state);
1211 >            p.execute(r1);
1212              p.execute(r2);
1213              assertTrue(p.getQueue().contains(r2));
1205            TrackedNoOpRunnable r3 = new TrackedNoOpRunnable();
1214              p.execute(r3);
1215              assertFalse(p.getQueue().contains(r2));
1216              assertTrue(p.getQueue().contains(r3));
1209            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1210        } finally {
1211            joinPool(p);
1217          }
1218 +        assertEquals(LatchAwaiter.DONE, r1.state);
1219 +        assertEquals(LatchAwaiter.NEW, r2.state);
1220 +        assertEquals(LatchAwaiter.DONE, r3.state);
1221      }
1222  
1223      /**
1224       * execute throws RejectedExecutionException if shutdown
1225       */
1226      public void testRejectedExecutionExceptionOnShutdown() {
1227 <        ThreadPoolExecutor p =
1227 >        final ThreadPoolExecutor p =
1228              new ThreadPoolExecutor(1, 1,
1229                                     LONG_DELAY_MS, MILLISECONDS,
1230                                     new ArrayBlockingQueue<Runnable>(1));
1231          try { p.shutdown(); } catch (SecurityException ok) { return; }
1232 <        try {
1233 <            p.execute(new NoOpRunnable());
1234 <            shouldThrow();
1235 <        } catch (RejectedExecutionException success) {}
1236 <
1237 <        joinPool(p);
1232 >        try (PoolCleaner cleaner = cleaner(p)) {
1233 >            try {
1234 >                p.execute(new NoOpRunnable());
1235 >                shouldThrow();
1236 >            } catch (RejectedExecutionException success) {}
1237 >        }
1238      }
1239  
1240      /**
# Line 1240 | Line 1248 | public class ThreadPoolExecutorTest exte
1248                                     new ArrayBlockingQueue<Runnable>(1), h);
1249  
1250          try { p.shutdown(); } catch (SecurityException ok) { return; }
1251 <        try {
1251 >        try (PoolCleaner cleaner = cleaner(p)) {
1252              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1253              p.execute(r);
1254              assertFalse(r.done);
1247        } finally {
1248            joinPool(p);
1255          }
1256      }
1257  
# Line 1253 | Line 1259 | public class ThreadPoolExecutorTest exte
1259       * execute using DiscardPolicy drops task on shutdown
1260       */
1261      public void testDiscardOnShutdown() {
1262 <        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
1257 <        ThreadPoolExecutor p =
1262 >        final ThreadPoolExecutor p =
1263              new ThreadPoolExecutor(1, 1,
1264                                     LONG_DELAY_MS, MILLISECONDS,
1265                                     new ArrayBlockingQueue<Runnable>(1),
1266 <                                   h);
1266 >                                   new ThreadPoolExecutor.DiscardPolicy());
1267  
1268          try { p.shutdown(); } catch (SecurityException ok) { return; }
1269 <        try {
1269 >        try (PoolCleaner cleaner = cleaner(p)) {
1270              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1271              p.execute(r);
1272              assertFalse(r.done);
1268        } finally {
1269            joinPool(p);
1273          }
1274      }
1275  
# Line 1274 | Line 1277 | public class ThreadPoolExecutorTest exte
1277       * execute using DiscardOldestPolicy drops task on shutdown
1278       */
1279      public void testDiscardOldestOnShutdown() {
1280 <        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
1278 <        ThreadPoolExecutor p =
1280 >        final ThreadPoolExecutor p =
1281              new ThreadPoolExecutor(1, 1,
1282                                     LONG_DELAY_MS, MILLISECONDS,
1283                                     new ArrayBlockingQueue<Runnable>(1),
1284 <                                   h);
1284 >                                   new ThreadPoolExecutor.DiscardOldestPolicy());
1285  
1286          try { p.shutdown(); } catch (SecurityException ok) { return; }
1287 <        try {
1287 >        try (PoolCleaner cleaner = cleaner(p)) {
1288              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1289              p.execute(r);
1290              assertFalse(r.done);
1289        } finally {
1290            joinPool(p);
1291          }
1292      }
1293  
# Line 1295 | Line 1295 | public class ThreadPoolExecutorTest exte
1295       * execute(null) throws NPE
1296       */
1297      public void testExecuteNull() {
1298 <        ThreadPoolExecutor p =
1299 <            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1298 >        final ThreadPoolExecutor p =
1299 >            new ThreadPoolExecutor(1, 2,
1300 >                                   1L, SECONDS,
1301                                     new ArrayBlockingQueue<Runnable>(10));
1302 <        try {
1303 <            p.execute(null);
1304 <            shouldThrow();
1305 <        } catch (NullPointerException success) {}
1306 <
1307 <        joinPool(p);
1302 >        try (PoolCleaner cleaner = cleaner(p)) {
1303 >            try {
1304 >                p.execute(null);
1305 >                shouldThrow();
1306 >            } catch (NullPointerException success) {}
1307 >        }
1308      }
1309  
1310      /**
1311       * setCorePoolSize of negative value throws IllegalArgumentException
1312       */
1313      public void testCorePoolSizeIllegalArgumentException() {
1314 <        ThreadPoolExecutor p =
1314 >        final ThreadPoolExecutor p =
1315              new ThreadPoolExecutor(1, 2,
1316                                     LONG_DELAY_MS, MILLISECONDS,
1317                                     new ArrayBlockingQueue<Runnable>(10));
1318 <        try {
1319 <            p.setCorePoolSize(-1);
1320 <            shouldThrow();
1321 <        } catch (IllegalArgumentException success) {
1322 <        } finally {
1322 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1318 >        try (PoolCleaner cleaner = cleaner(p)) {
1319 >            try {
1320 >                p.setCorePoolSize(-1);
1321 >                shouldThrow();
1322 >            } catch (IllegalArgumentException success) {}
1323          }
1324        joinPool(p);
1324      }
1325  
1326      /**
# Line 1329 | Line 1328 | public class ThreadPoolExecutorTest exte
1328       * given a value less the core pool size
1329       */
1330      public void testMaximumPoolSizeIllegalArgumentException() {
1331 <        ThreadPoolExecutor p =
1331 >        final ThreadPoolExecutor p =
1332              new ThreadPoolExecutor(2, 3,
1333                                     LONG_DELAY_MS, MILLISECONDS,
1334                                     new ArrayBlockingQueue<Runnable>(10));
1335 <        try {
1336 <            p.setMaximumPoolSize(1);
1337 <            shouldThrow();
1338 <        } catch (IllegalArgumentException success) {
1339 <        } finally {
1341 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1335 >        try (PoolCleaner cleaner = cleaner(p)) {
1336 >            try {
1337 >                p.setMaximumPoolSize(1);
1338 >                shouldThrow();
1339 >            } catch (IllegalArgumentException success) {}
1340          }
1343        joinPool(p);
1341      }
1342  
1343      /**
# Line 1348 | Line 1345 | public class ThreadPoolExecutorTest exte
1345       * if given a negative value
1346       */
1347      public void testMaximumPoolSizeIllegalArgumentException2() {
1348 <        ThreadPoolExecutor p =
1348 >        final ThreadPoolExecutor p =
1349              new ThreadPoolExecutor(2, 3,
1350                                     LONG_DELAY_MS, MILLISECONDS,
1351                                     new ArrayBlockingQueue<Runnable>(10));
1352 <        try {
1353 <            p.setMaximumPoolSize(-1);
1354 <            shouldThrow();
1355 <        } catch (IllegalArgumentException success) {
1356 <        } finally {
1360 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1352 >        try (PoolCleaner cleaner = cleaner(p)) {
1353 >            try {
1354 >                p.setMaximumPoolSize(-1);
1355 >                shouldThrow();
1356 >            } catch (IllegalArgumentException success) {}
1357          }
1362        joinPool(p);
1358      }
1359  
1360      /**
# Line 1367 | Line 1362 | public class ThreadPoolExecutorTest exte
1362       * max pool size result in IllegalArgumentException.
1363       */
1364      public void testPoolSizeInvariants() {
1365 <        ThreadPoolExecutor p =
1365 >        final ThreadPoolExecutor p =
1366              new ThreadPoolExecutor(1, 1,
1367                                     LONG_DELAY_MS, MILLISECONDS,
1368                                     new ArrayBlockingQueue<Runnable>(10));
1369 <        for (int s = 1; s < 5; s++) {
1370 <            p.setMaximumPoolSize(s);
1371 <            p.setCorePoolSize(s);
1372 <            try {
1373 <                p.setMaximumPoolSize(s - 1);
1374 <                shouldThrow();
1375 <            } catch (IllegalArgumentException success) {}
1376 <            assertEquals(s, p.getCorePoolSize());
1377 <            assertEquals(s, p.getMaximumPoolSize());
1378 <            try {
1379 <                p.setCorePoolSize(s + 1);
1380 <                shouldThrow();
1381 <            } catch (IllegalArgumentException success) {}
1382 <            assertEquals(s, p.getCorePoolSize());
1383 <            assertEquals(s, p.getMaximumPoolSize());
1369 >        try (PoolCleaner cleaner = cleaner(p)) {
1370 >            for (int s = 1; s < 5; s++) {
1371 >                p.setMaximumPoolSize(s);
1372 >                p.setCorePoolSize(s);
1373 >                try {
1374 >                    p.setMaximumPoolSize(s - 1);
1375 >                    shouldThrow();
1376 >                } catch (IllegalArgumentException success) {}
1377 >                assertEquals(s, p.getCorePoolSize());
1378 >                assertEquals(s, p.getMaximumPoolSize());
1379 >                try {
1380 >                    p.setCorePoolSize(s + 1);
1381 >                    shouldThrow();
1382 >                } catch (IllegalArgumentException success) {}
1383 >                assertEquals(s, p.getCorePoolSize());
1384 >                assertEquals(s, p.getMaximumPoolSize());
1385 >            }
1386          }
1390        joinPool(p);
1387      }
1388  
1389      /**
# Line 1395 | Line 1391 | public class ThreadPoolExecutorTest exte
1391       * when given a negative value
1392       */
1393      public void testKeepAliveTimeIllegalArgumentException() {
1394 <        ThreadPoolExecutor p =
1394 >        final ThreadPoolExecutor p =
1395              new ThreadPoolExecutor(2, 3,
1396                                     LONG_DELAY_MS, MILLISECONDS,
1397                                     new ArrayBlockingQueue<Runnable>(10));
1398 <        try {
1399 <            p.setKeepAliveTime(-1,MILLISECONDS);
1400 <            shouldThrow();
1401 <        } catch (IllegalArgumentException success) {
1402 <        } finally {
1407 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1398 >        try (PoolCleaner cleaner = cleaner(p)) {
1399 >            try {
1400 >                p.setKeepAliveTime(-1, MILLISECONDS);
1401 >                shouldThrow();
1402 >            } catch (IllegalArgumentException success) {}
1403          }
1409        joinPool(p);
1404      }
1405  
1406      /**
# Line 1414 | Line 1408 | public class ThreadPoolExecutorTest exte
1408       */
1409      public void testTerminated() {
1410          ExtendedTPE p = new ExtendedTPE();
1411 <        try { p.shutdown(); } catch (SecurityException ok) { return; }
1412 <        assertTrue(p.terminatedCalled());
1413 <        joinPool(p);
1411 >        try (PoolCleaner cleaner = cleaner(p)) {
1412 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1413 >            assertTrue(p.terminatedCalled());
1414 >            assertTrue(p.isShutdown());
1415 >        }
1416      }
1417  
1418      /**
# Line 1424 | Line 1420 | public class ThreadPoolExecutorTest exte
1420       */
1421      public void testBeforeAfter() throws InterruptedException {
1422          ExtendedTPE p = new ExtendedTPE();
1423 <        try {
1423 >        try (PoolCleaner cleaner = cleaner(p)) {
1424              final CountDownLatch done = new CountDownLatch(1);
1425              p.execute(new CheckedRunnable() {
1426                  public void realRun() {
# Line 1434 | Line 1430 | public class ThreadPoolExecutorTest exte
1430              assertEquals(0, done.getCount());
1431              assertTrue(p.afterCalled());
1432              assertTrue(p.beforeCalled());
1437            try { p.shutdown(); } catch (SecurityException ok) { return; }
1438        } finally {
1439            joinPool(p);
1433          }
1434      }
1435  
# Line 1444 | Line 1437 | public class ThreadPoolExecutorTest exte
1437       * completed submit of callable returns result
1438       */
1439      public void testSubmitCallable() throws Exception {
1440 <        ExecutorService e =
1440 >        final ExecutorService e =
1441              new ThreadPoolExecutor(2, 2,
1442                                     LONG_DELAY_MS, MILLISECONDS,
1443                                     new ArrayBlockingQueue<Runnable>(10));
1444 <        try {
1444 >        try (PoolCleaner cleaner = cleaner(e)) {
1445              Future<String> future = e.submit(new StringTask());
1446              String result = future.get();
1447              assertSame(TEST_STRING, result);
1455        } finally {
1456            joinPool(e);
1448          }
1449      }
1450  
# Line 1461 | Line 1452 | public class ThreadPoolExecutorTest exte
1452       * completed submit of runnable returns successfully
1453       */
1454      public void testSubmitRunnable() throws Exception {
1455 <        ExecutorService e =
1455 >        final ExecutorService e =
1456              new ThreadPoolExecutor(2, 2,
1457                                     LONG_DELAY_MS, MILLISECONDS,
1458                                     new ArrayBlockingQueue<Runnable>(10));
1459 <        try {
1459 >        try (PoolCleaner cleaner = cleaner(e)) {
1460              Future<?> future = e.submit(new NoOpRunnable());
1461              future.get();
1462              assertTrue(future.isDone());
1472        } finally {
1473            joinPool(e);
1463          }
1464      }
1465  
# Line 1478 | Line 1467 | public class ThreadPoolExecutorTest exte
1467       * completed submit of (runnable, result) returns result
1468       */
1469      public void testSubmitRunnable2() throws Exception {
1470 <        ExecutorService e =
1470 >        final ExecutorService e =
1471              new ThreadPoolExecutor(2, 2,
1472                                     LONG_DELAY_MS, MILLISECONDS,
1473                                     new ArrayBlockingQueue<Runnable>(10));
1474 <        try {
1474 >        try (PoolCleaner cleaner = cleaner(e)) {
1475              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1476              String result = future.get();
1477              assertSame(TEST_STRING, result);
1489        } finally {
1490            joinPool(e);
1478          }
1479      }
1480  
# Line 1495 | Line 1482 | public class ThreadPoolExecutorTest exte
1482       * invokeAny(null) throws NPE
1483       */
1484      public void testInvokeAny1() throws Exception {
1485 <        ExecutorService e =
1485 >        final ExecutorService e =
1486              new ThreadPoolExecutor(2, 2,
1487                                     LONG_DELAY_MS, MILLISECONDS,
1488                                     new ArrayBlockingQueue<Runnable>(10));
1489 <        try {
1490 <            e.invokeAny(null);
1491 <            shouldThrow();
1492 <        } catch (NullPointerException success) {
1493 <        } finally {
1507 <            joinPool(e);
1489 >        try (PoolCleaner cleaner = cleaner(e)) {
1490 >            try {
1491 >                e.invokeAny(null);
1492 >                shouldThrow();
1493 >            } catch (NullPointerException success) {}
1494          }
1495      }
1496  
# Line 1512 | Line 1498 | public class ThreadPoolExecutorTest exte
1498       * invokeAny(empty collection) throws IAE
1499       */
1500      public void testInvokeAny2() throws Exception {
1501 <        ExecutorService e =
1501 >        final ExecutorService e =
1502              new ThreadPoolExecutor(2, 2,
1503                                     LONG_DELAY_MS, MILLISECONDS,
1504                                     new ArrayBlockingQueue<Runnable>(10));
1505 <        try {
1506 <            e.invokeAny(new ArrayList<Callable<String>>());
1507 <            shouldThrow();
1508 <        } catch (IllegalArgumentException success) {
1509 <        } finally {
1524 <            joinPool(e);
1505 >        try (PoolCleaner cleaner = cleaner(e)) {
1506 >            try {
1507 >                e.invokeAny(new ArrayList<Callable<String>>());
1508 >                shouldThrow();
1509 >            } catch (IllegalArgumentException success) {}
1510          }
1511      }
1512  
# Line 1534 | Line 1519 | public class ThreadPoolExecutorTest exte
1519              new ThreadPoolExecutor(2, 2,
1520                                     LONG_DELAY_MS, MILLISECONDS,
1521                                     new ArrayBlockingQueue<Runnable>(10));
1522 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1523 <        l.add(latchAwaitingStringTask(latch));
1524 <        l.add(null);
1525 <        try {
1526 <            e.invokeAny(l);
1527 <            shouldThrow();
1528 <        } catch (NullPointerException success) {
1529 <        } finally {
1522 >        try (PoolCleaner cleaner = cleaner(e)) {
1523 >            List<Callable<String>> l = new ArrayList<>();
1524 >            l.add(latchAwaitingStringTask(latch));
1525 >            l.add(null);
1526 >            try {
1527 >                e.invokeAny(l);
1528 >                shouldThrow();
1529 >            } catch (NullPointerException success) {}
1530              latch.countDown();
1546            joinPool(e);
1531          }
1532      }
1533  
# Line 1551 | Line 1535 | public class ThreadPoolExecutorTest exte
1535       * invokeAny(c) throws ExecutionException if no task completes
1536       */
1537      public void testInvokeAny4() throws Exception {
1538 <        ExecutorService e =
1538 >        final ExecutorService e =
1539              new ThreadPoolExecutor(2, 2,
1540                                     LONG_DELAY_MS, MILLISECONDS,
1541                                     new ArrayBlockingQueue<Runnable>(10));
1542 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1543 <        l.add(new NPETask());
1544 <        try {
1545 <            e.invokeAny(l);
1546 <            shouldThrow();
1547 <        } catch (ExecutionException success) {
1548 <            assertTrue(success.getCause() instanceof NullPointerException);
1549 <        } finally {
1550 <            joinPool(e);
1542 >        try (PoolCleaner cleaner = cleaner(e)) {
1543 >            List<Callable<String>> l = new ArrayList<>();
1544 >            l.add(new NPETask());
1545 >            try {
1546 >                e.invokeAny(l);
1547 >                shouldThrow();
1548 >            } catch (ExecutionException success) {
1549 >                assertTrue(success.getCause() instanceof NullPointerException);
1550 >            }
1551          }
1552      }
1553  
# Line 1571 | Line 1555 | public class ThreadPoolExecutorTest exte
1555       * invokeAny(c) returns result of some task
1556       */
1557      public void testInvokeAny5() throws Exception {
1558 <        ExecutorService e =
1558 >        final ExecutorService e =
1559              new ThreadPoolExecutor(2, 2,
1560                                     LONG_DELAY_MS, MILLISECONDS,
1561                                     new ArrayBlockingQueue<Runnable>(10));
1562 <        try {
1563 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1562 >        try (PoolCleaner cleaner = cleaner(e)) {
1563 >            List<Callable<String>> l = new ArrayList<>();
1564              l.add(new StringTask());
1565              l.add(new StringTask());
1566              String result = e.invokeAny(l);
1567              assertSame(TEST_STRING, result);
1584        } finally {
1585            joinPool(e);
1568          }
1569      }
1570  
# Line 1590 | Line 1572 | public class ThreadPoolExecutorTest exte
1572       * invokeAll(null) throws NPE
1573       */
1574      public void testInvokeAll1() throws Exception {
1575 <        ExecutorService e =
1575 >        final ExecutorService e =
1576              new ThreadPoolExecutor(2, 2,
1577                                     LONG_DELAY_MS, MILLISECONDS,
1578                                     new ArrayBlockingQueue<Runnable>(10));
1579 <        try {
1580 <            e.invokeAll(null);
1581 <            shouldThrow();
1582 <        } catch (NullPointerException success) {
1583 <        } finally {
1602 <            joinPool(e);
1579 >        try (PoolCleaner cleaner = cleaner(e)) {
1580 >            try {
1581 >                e.invokeAll(null);
1582 >                shouldThrow();
1583 >            } catch (NullPointerException success) {}
1584          }
1585      }
1586  
# Line 1607 | Line 1588 | public class ThreadPoolExecutorTest exte
1588       * invokeAll(empty collection) returns empty collection
1589       */
1590      public void testInvokeAll2() throws InterruptedException {
1591 <        ExecutorService e =
1591 >        final ExecutorService e =
1592              new ThreadPoolExecutor(2, 2,
1593                                     LONG_DELAY_MS, MILLISECONDS,
1594                                     new ArrayBlockingQueue<Runnable>(10));
1595 <        try {
1595 >        try (PoolCleaner cleaner = cleaner(e)) {
1596              List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1597              assertTrue(r.isEmpty());
1617        } finally {
1618            joinPool(e);
1598          }
1599      }
1600  
# Line 1623 | Line 1602 | public class ThreadPoolExecutorTest exte
1602       * invokeAll(c) throws NPE if c has null elements
1603       */
1604      public void testInvokeAll3() throws Exception {
1605 <        ExecutorService e =
1605 >        final ExecutorService e =
1606              new ThreadPoolExecutor(2, 2,
1607                                     LONG_DELAY_MS, MILLISECONDS,
1608                                     new ArrayBlockingQueue<Runnable>(10));
1609 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1610 <        l.add(new StringTask());
1611 <        l.add(null);
1612 <        try {
1613 <            e.invokeAll(l);
1614 <            shouldThrow();
1615 <        } catch (NullPointerException success) {
1616 <        } finally {
1638 <            joinPool(e);
1609 >        try (PoolCleaner cleaner = cleaner(e)) {
1610 >            List<Callable<String>> l = new ArrayList<>();
1611 >            l.add(new StringTask());
1612 >            l.add(null);
1613 >            try {
1614 >                e.invokeAll(l);
1615 >                shouldThrow();
1616 >            } catch (NullPointerException success) {}
1617          }
1618      }
1619  
# Line 1643 | Line 1621 | public class ThreadPoolExecutorTest exte
1621       * get of element of invokeAll(c) throws exception on failed task
1622       */
1623      public void testInvokeAll4() throws Exception {
1624 <        ExecutorService e =
1624 >        final ExecutorService e =
1625              new ThreadPoolExecutor(2, 2,
1626                                     LONG_DELAY_MS, MILLISECONDS,
1627                                     new ArrayBlockingQueue<Runnable>(10));
1628 <        try {
1629 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1628 >        try (PoolCleaner cleaner = cleaner(e)) {
1629 >            List<Callable<String>> l = new ArrayList<>();
1630              l.add(new NPETask());
1631              List<Future<String>> futures = e.invokeAll(l);
1632              assertEquals(1, futures.size());
# Line 1658 | Line 1636 | public class ThreadPoolExecutorTest exte
1636              } catch (ExecutionException success) {
1637                  assertTrue(success.getCause() instanceof NullPointerException);
1638              }
1661        } finally {
1662            joinPool(e);
1639          }
1640      }
1641  
# Line 1667 | Line 1643 | public class ThreadPoolExecutorTest exte
1643       * invokeAll(c) returns results of all completed tasks
1644       */
1645      public void testInvokeAll5() throws Exception {
1646 <        ExecutorService e =
1646 >        final ExecutorService e =
1647              new ThreadPoolExecutor(2, 2,
1648                                     LONG_DELAY_MS, MILLISECONDS,
1649                                     new ArrayBlockingQueue<Runnable>(10));
1650 <        try {
1651 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1650 >        try (PoolCleaner cleaner = cleaner(e)) {
1651 >            List<Callable<String>> l = new ArrayList<>();
1652              l.add(new StringTask());
1653              l.add(new StringTask());
1654              List<Future<String>> futures = e.invokeAll(l);
1655              assertEquals(2, futures.size());
1656              for (Future<String> future : futures)
1657                  assertSame(TEST_STRING, future.get());
1682        } finally {
1683            joinPool(e);
1658          }
1659      }
1660  
# Line 1688 | Line 1662 | public class ThreadPoolExecutorTest exte
1662       * timed invokeAny(null) throws NPE
1663       */
1664      public void testTimedInvokeAny1() throws Exception {
1665 <        ExecutorService e =
1665 >        final ExecutorService e =
1666              new ThreadPoolExecutor(2, 2,
1667                                     LONG_DELAY_MS, MILLISECONDS,
1668                                     new ArrayBlockingQueue<Runnable>(10));
1669 <        try {
1670 <            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1671 <            shouldThrow();
1672 <        } catch (NullPointerException success) {
1673 <        } finally {
1700 <            joinPool(e);
1669 >        try (PoolCleaner cleaner = cleaner(e)) {
1670 >            try {
1671 >                e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1672 >                shouldThrow();
1673 >            } catch (NullPointerException success) {}
1674          }
1675      }
1676  
# Line 1705 | Line 1678 | public class ThreadPoolExecutorTest exte
1678       * timed invokeAny(,,null) throws NPE
1679       */
1680      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1681 <        ExecutorService e =
1681 >        final ExecutorService e =
1682              new ThreadPoolExecutor(2, 2,
1683                                     LONG_DELAY_MS, MILLISECONDS,
1684                                     new ArrayBlockingQueue<Runnable>(10));
1685 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1686 <        l.add(new StringTask());
1687 <        try {
1688 <            e.invokeAny(l, MEDIUM_DELAY_MS, null);
1689 <            shouldThrow();
1690 <        } catch (NullPointerException success) {
1691 <        } finally {
1719 <            joinPool(e);
1685 >        try (PoolCleaner cleaner = cleaner(e)) {
1686 >            List<Callable<String>> l = new ArrayList<>();
1687 >            l.add(new StringTask());
1688 >            try {
1689 >                e.invokeAny(l, MEDIUM_DELAY_MS, null);
1690 >                shouldThrow();
1691 >            } catch (NullPointerException success) {}
1692          }
1693      }
1694  
# Line 1724 | Line 1696 | public class ThreadPoolExecutorTest exte
1696       * timed invokeAny(empty collection) throws IAE
1697       */
1698      public void testTimedInvokeAny2() throws Exception {
1699 <        ExecutorService e =
1699 >        final ExecutorService e =
1700              new ThreadPoolExecutor(2, 2,
1701                                     LONG_DELAY_MS, MILLISECONDS,
1702                                     new ArrayBlockingQueue<Runnable>(10));
1703 <        try {
1704 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1705 <            shouldThrow();
1706 <        } catch (IllegalArgumentException success) {
1707 <        } finally {
1708 <            joinPool(e);
1703 >        try (PoolCleaner cleaner = cleaner(e)) {
1704 >            try {
1705 >                e.invokeAny(new ArrayList<Callable<String>>(),
1706 >                            MEDIUM_DELAY_MS, MILLISECONDS);
1707 >                shouldThrow();
1708 >            } catch (IllegalArgumentException success) {}
1709          }
1710      }
1711  
# Line 1746 | Line 1718 | public class ThreadPoolExecutorTest exte
1718              new ThreadPoolExecutor(2, 2,
1719                                     LONG_DELAY_MS, MILLISECONDS,
1720                                     new ArrayBlockingQueue<Runnable>(10));
1721 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1722 <        l.add(latchAwaitingStringTask(latch));
1723 <        l.add(null);
1724 <        try {
1725 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1726 <            shouldThrow();
1727 <        } catch (NullPointerException success) {
1728 <        } finally {
1721 >        try (PoolCleaner cleaner = cleaner(e)) {
1722 >            List<Callable<String>> l = new ArrayList<>();
1723 >            l.add(latchAwaitingStringTask(latch));
1724 >            l.add(null);
1725 >            try {
1726 >                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1727 >                shouldThrow();
1728 >            } catch (NullPointerException success) {}
1729              latch.countDown();
1758            joinPool(e);
1730          }
1731      }
1732  
# Line 1763 | Line 1734 | public class ThreadPoolExecutorTest exte
1734       * timed invokeAny(c) throws ExecutionException if no task completes
1735       */
1736      public void testTimedInvokeAny4() throws Exception {
1737 <        ExecutorService e =
1737 >        final ExecutorService e =
1738              new ThreadPoolExecutor(2, 2,
1739                                     LONG_DELAY_MS, MILLISECONDS,
1740                                     new ArrayBlockingQueue<Runnable>(10));
1741 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1742 <        l.add(new NPETask());
1743 <        try {
1744 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1745 <            shouldThrow();
1746 <        } catch (ExecutionException success) {
1747 <            assertTrue(success.getCause() instanceof NullPointerException);
1748 <        } finally {
1749 <            joinPool(e);
1741 >        try (PoolCleaner cleaner = cleaner(e)) {
1742 >            long startTime = System.nanoTime();
1743 >            List<Callable<String>> l = new ArrayList<>();
1744 >            l.add(new NPETask());
1745 >            try {
1746 >                e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1747 >                shouldThrow();
1748 >            } catch (ExecutionException success) {
1749 >                assertTrue(success.getCause() instanceof NullPointerException);
1750 >            }
1751 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1752          }
1753      }
1754  
# Line 1783 | Line 1756 | public class ThreadPoolExecutorTest exte
1756       * timed invokeAny(c) returns result of some task
1757       */
1758      public void testTimedInvokeAny5() throws Exception {
1759 <        ExecutorService e =
1759 >        final ExecutorService e =
1760              new ThreadPoolExecutor(2, 2,
1761                                     LONG_DELAY_MS, MILLISECONDS,
1762                                     new ArrayBlockingQueue<Runnable>(10));
1763 <        try {
1764 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1763 >        try (PoolCleaner cleaner = cleaner(e)) {
1764 >            long startTime = System.nanoTime();
1765 >            List<Callable<String>> l = new ArrayList<>();
1766              l.add(new StringTask());
1767              l.add(new StringTask());
1768 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1768 >            String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1769              assertSame(TEST_STRING, result);
1770 <        } finally {
1797 <            joinPool(e);
1770 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1771          }
1772      }
1773  
# Line 1802 | Line 1775 | public class ThreadPoolExecutorTest exte
1775       * timed invokeAll(null) throws NPE
1776       */
1777      public void testTimedInvokeAll1() throws Exception {
1778 <        ExecutorService e =
1778 >        final ExecutorService e =
1779              new ThreadPoolExecutor(2, 2,
1780                                     LONG_DELAY_MS, MILLISECONDS,
1781                                     new ArrayBlockingQueue<Runnable>(10));
1782 <        try {
1783 <            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1784 <            shouldThrow();
1785 <        } catch (NullPointerException success) {
1786 <        } finally {
1814 <            joinPool(e);
1782 >        try (PoolCleaner cleaner = cleaner(e)) {
1783 >            try {
1784 >                e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1785 >                shouldThrow();
1786 >            } catch (NullPointerException success) {}
1787          }
1788      }
1789  
# Line 1819 | Line 1791 | public class ThreadPoolExecutorTest exte
1791       * timed invokeAll(,,null) throws NPE
1792       */
1793      public void testTimedInvokeAllNullTimeUnit() throws Exception {
1794 <        ExecutorService e =
1794 >        final ExecutorService e =
1795              new ThreadPoolExecutor(2, 2,
1796                                     LONG_DELAY_MS, MILLISECONDS,
1797                                     new ArrayBlockingQueue<Runnable>(10));
1798 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1799 <        l.add(new StringTask());
1800 <        try {
1801 <            e.invokeAll(l, MEDIUM_DELAY_MS, null);
1802 <            shouldThrow();
1803 <        } catch (NullPointerException success) {
1804 <        } finally {
1833 <            joinPool(e);
1798 >        try (PoolCleaner cleaner = cleaner(e)) {
1799 >            List<Callable<String>> l = new ArrayList<>();
1800 >            l.add(new StringTask());
1801 >            try {
1802 >                e.invokeAll(l, MEDIUM_DELAY_MS, null);
1803 >                shouldThrow();
1804 >            } catch (NullPointerException success) {}
1805          }
1806      }
1807  
# Line 1838 | Line 1809 | public class ThreadPoolExecutorTest exte
1809       * timed invokeAll(empty collection) returns empty collection
1810       */
1811      public void testTimedInvokeAll2() throws InterruptedException {
1812 <        ExecutorService e =
1812 >        final ExecutorService e =
1813              new ThreadPoolExecutor(2, 2,
1814                                     LONG_DELAY_MS, MILLISECONDS,
1815                                     new ArrayBlockingQueue<Runnable>(10));
1816 <        try {
1817 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1816 >        try (PoolCleaner cleaner = cleaner(e)) {
1817 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(),
1818 >                                                 MEDIUM_DELAY_MS, MILLISECONDS);
1819              assertTrue(r.isEmpty());
1848        } finally {
1849            joinPool(e);
1820          }
1821      }
1822  
# Line 1854 | Line 1824 | public class ThreadPoolExecutorTest exte
1824       * timed invokeAll(c) throws NPE if c has null elements
1825       */
1826      public void testTimedInvokeAll3() throws Exception {
1827 <        ExecutorService e =
1827 >        final ExecutorService e =
1828              new ThreadPoolExecutor(2, 2,
1829                                     LONG_DELAY_MS, MILLISECONDS,
1830                                     new ArrayBlockingQueue<Runnable>(10));
1831 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1832 <        l.add(new StringTask());
1833 <        l.add(null);
1834 <        try {
1835 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1836 <            shouldThrow();
1837 <        } catch (NullPointerException success) {
1838 <        } finally {
1869 <            joinPool(e);
1831 >        try (PoolCleaner cleaner = cleaner(e)) {
1832 >            List<Callable<String>> l = new ArrayList<>();
1833 >            l.add(new StringTask());
1834 >            l.add(null);
1835 >            try {
1836 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1837 >                shouldThrow();
1838 >            } catch (NullPointerException success) {}
1839          }
1840      }
1841  
# Line 1874 | Line 1843 | public class ThreadPoolExecutorTest exte
1843       * get of element of invokeAll(c) throws exception on failed task
1844       */
1845      public void testTimedInvokeAll4() throws Exception {
1846 <        ExecutorService e =
1846 >        final ExecutorService e =
1847              new ThreadPoolExecutor(2, 2,
1848                                     LONG_DELAY_MS, MILLISECONDS,
1849                                     new ArrayBlockingQueue<Runnable>(10));
1850 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1851 <        l.add(new NPETask());
1852 <        List<Future<String>> futures =
1853 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1854 <        assertEquals(1, futures.size());
1855 <        try {
1856 <            futures.get(0).get();
1857 <            shouldThrow();
1858 <        } catch (ExecutionException success) {
1859 <            assertTrue(success.getCause() instanceof NullPointerException);
1860 <        } finally {
1861 <            joinPool(e);
1850 >        try (PoolCleaner cleaner = cleaner(e)) {
1851 >            List<Callable<String>> l = new ArrayList<>();
1852 >            l.add(new NPETask());
1853 >            List<Future<String>> futures =
1854 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1855 >            assertEquals(1, futures.size());
1856 >            try {
1857 >                futures.get(0).get();
1858 >                shouldThrow();
1859 >            } catch (ExecutionException success) {
1860 >                assertTrue(success.getCause() instanceof NullPointerException);
1861 >            }
1862          }
1863      }
1864  
# Line 1897 | Line 1866 | public class ThreadPoolExecutorTest exte
1866       * timed invokeAll(c) returns results of all completed tasks
1867       */
1868      public void testTimedInvokeAll5() throws Exception {
1869 <        ExecutorService e =
1869 >        final ExecutorService e =
1870              new ThreadPoolExecutor(2, 2,
1871                                     LONG_DELAY_MS, MILLISECONDS,
1872                                     new ArrayBlockingQueue<Runnable>(10));
1873 <        try {
1874 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1873 >        try (PoolCleaner cleaner = cleaner(e)) {
1874 >            List<Callable<String>> l = new ArrayList<>();
1875              l.add(new StringTask());
1876              l.add(new StringTask());
1877              List<Future<String>> futures =
# Line 1910 | Line 1879 | public class ThreadPoolExecutorTest exte
1879              assertEquals(2, futures.size());
1880              for (Future<String> future : futures)
1881                  assertSame(TEST_STRING, future.get());
1913        } finally {
1914            joinPool(e);
1882          }
1883      }
1884  
# Line 1919 | Line 1886 | public class ThreadPoolExecutorTest exte
1886       * timed invokeAll(c) cancels tasks not completed by timeout
1887       */
1888      public void testTimedInvokeAll6() throws Exception {
1889 <        ExecutorService e =
1890 <            new ThreadPoolExecutor(2, 2,
1891 <                                   LONG_DELAY_MS, MILLISECONDS,
1892 <                                   new ArrayBlockingQueue<Runnable>(10));
1893 <        try {
1894 <            for (long timeout = timeoutMillis();;) {
1889 >        for (long timeout = timeoutMillis();;) {
1890 >            final CountDownLatch done = new CountDownLatch(1);
1891 >            final Callable<String> waiter = new CheckedCallable<String>() {
1892 >                public String realCall() {
1893 >                    try { done.await(LONG_DELAY_MS, MILLISECONDS); }
1894 >                    catch (InterruptedException ok) {}
1895 >                    return "1"; }};
1896 >            final ExecutorService p =
1897 >                new ThreadPoolExecutor(2, 2,
1898 >                                       LONG_DELAY_MS, MILLISECONDS,
1899 >                                       new ArrayBlockingQueue<Runnable>(10));
1900 >            try (PoolCleaner cleaner = cleaner(p, done)) {
1901                  List<Callable<String>> tasks = new ArrayList<>();
1902                  tasks.add(new StringTask("0"));
1903 <                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1903 >                tasks.add(waiter);
1904                  tasks.add(new StringTask("2"));
1905                  long startTime = System.nanoTime();
1906                  List<Future<String>> futures =
1907 <                    e.invokeAll(tasks, timeout, MILLISECONDS);
1907 >                    p.invokeAll(tasks, timeout, MILLISECONDS);
1908                  assertEquals(tasks.size(), futures.size());
1909                  assertTrue(millisElapsedSince(startTime) >= timeout);
1910                  for (Future future : futures)
# Line 1947 | Line 1920 | public class ThreadPoolExecutorTest exte
1920                          fail("expected exactly one task to be cancelled");
1921                  }
1922              }
1950        } finally {
1951            joinPool(e);
1923          }
1924      }
1925  
# Line 1962 | Line 1933 | public class ThreadPoolExecutorTest exte
1933                                     LONG_DELAY_MS, MILLISECONDS,
1934                                     new LinkedBlockingQueue<Runnable>(),
1935                                     new FailingThreadFactory());
1936 <        try {
1936 >        try (PoolCleaner cleaner = cleaner(e)) {
1937              final int TASKS = 100;
1938              final CountDownLatch done = new CountDownLatch(TASKS);
1939              for (int k = 0; k < TASKS; ++k)
# Line 1970 | Line 1941 | public class ThreadPoolExecutorTest exte
1941                      public void realRun() {
1942                          done.countDown();
1943                      }});
1944 <            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1974 <        } finally {
1975 <            joinPool(e);
1944 >            await(done);
1945          }
1946      }
1947  
# Line 1984 | Line 1953 | public class ThreadPoolExecutorTest exte
1953              new ThreadPoolExecutor(2, 2,
1954                                     1000, MILLISECONDS,
1955                                     new ArrayBlockingQueue<Runnable>(10));
1956 <        assertFalse(p.allowsCoreThreadTimeOut());
1957 <        joinPool(p);
1956 >        try (PoolCleaner cleaner = cleaner(p)) {
1957 >            assertFalse(p.allowsCoreThreadTimeOut());
1958 >        }
1959      }
1960  
1961      /**
# Line 1997 | Line 1967 | public class ThreadPoolExecutorTest exte
1967              new ThreadPoolExecutor(2, 10,
1968                                     keepAliveTime, MILLISECONDS,
1969                                     new ArrayBlockingQueue<Runnable>(10));
1970 <        final CountDownLatch threadStarted = new CountDownLatch(1);
1971 <        try {
1970 >        try (PoolCleaner cleaner = cleaner(p)) {
1971 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1972              p.allowCoreThreadTimeOut(true);
1973              p.execute(new CheckedRunnable() {
1974                  public void realRun() {
# Line 2013 | Line 1983 | public class ThreadPoolExecutorTest exte
1983                  Thread.yield();
1984              assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1985              assertEquals(0, p.getPoolSize());
2016        } finally {
2017            joinPool(p);
1986          }
1987      }
1988  
# Line 2027 | Line 1995 | public class ThreadPoolExecutorTest exte
1995              new ThreadPoolExecutor(2, 10,
1996                                     keepAliveTime, MILLISECONDS,
1997                                     new ArrayBlockingQueue<Runnable>(10));
1998 <        final CountDownLatch threadStarted = new CountDownLatch(1);
1999 <        try {
1998 >        try (PoolCleaner cleaner = cleaner(p)) {
1999 >            final CountDownLatch threadStarted = new CountDownLatch(1);
2000              p.allowCoreThreadTimeOut(false);
2001              p.execute(new CheckedRunnable() {
2002                  public void realRun() throws InterruptedException {
# Line 2037 | Line 2005 | public class ThreadPoolExecutorTest exte
2005                  }});
2006              delay(2 * keepAliveTime);
2007              assertTrue(p.getPoolSize() >= 1);
2040        } finally {
2041            joinPool(p);
2008          }
2009      }
2010  
# Line 2057 | Line 2023 | public class ThreadPoolExecutorTest exte
2023              new ThreadPoolExecutor(1, 30,
2024                                     60, SECONDS,
2025                                     new ArrayBlockingQueue(30));
2026 <        try {
2026 >        try (PoolCleaner cleaner = cleaner(p)) {
2027              for (int i = 0; i < nTasks; ++i) {
2028                  for (;;) {
2029                      try {
# Line 2068 | Line 2034 | public class ThreadPoolExecutorTest exte
2034                  }
2035              }
2036              // enough time to run all tasks
2037 <            assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS));
2072 <        } finally {
2073 <            joinPool(p);
2037 >            await(done, nTasks * SHORT_DELAY_MS);
2038          }
2039      }
2040  
# Line 2078 | Line 2042 | public class ThreadPoolExecutorTest exte
2042       * get(cancelled task) throws CancellationException
2043       */
2044      public void testGet_cancelled() throws Exception {
2045 +        final CountDownLatch done = new CountDownLatch(1);
2046          final ExecutorService e =
2047              new ThreadPoolExecutor(1, 1,
2048                                     LONG_DELAY_MS, MILLISECONDS,
2049                                     new LinkedBlockingQueue<Runnable>());
2050 <        try {
2050 >        try (PoolCleaner cleaner = cleaner(e, done)) {
2051              final CountDownLatch blockerStarted = new CountDownLatch(1);
2087            final CountDownLatch done = new CountDownLatch(1);
2052              final List<Future<?>> futures = new ArrayList<>();
2053              for (int i = 0; i < 2; i++) {
2054                  Runnable r = new CheckedRunnable() { public void realRun()
# Line 2094 | Line 2058 | public class ThreadPoolExecutorTest exte
2058                  }};
2059                  futures.add(e.submit(r));
2060              }
2061 <            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
2061 >            await(blockerStarted);
2062              for (Future<?> future : futures) future.cancel(false);
2063              for (Future<?> future : futures) {
2064                  try {
# Line 2108 | Line 2072 | public class ThreadPoolExecutorTest exte
2072                  assertTrue(future.isCancelled());
2073                  assertTrue(future.isDone());
2074              }
2111            done.countDown();
2112        } finally {
2113            joinPool(e);
2075          }
2076      }
2077  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines