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.73 by jsr166, Sun Oct 4 01:56:51 2015 UTC vs.
Revision 1.102 by jsr166, Mon Oct 5 22:09:02 2015 UTC

# Line 87 | Line 87 | public class ThreadPoolExecutorTest exte
87              new ThreadPoolExecutor(1, 1,
88                                     LONG_DELAY_MS, MILLISECONDS,
89                                     new ArrayBlockingQueue<Runnable>(10));
90 <        final CountDownLatch done = new CountDownLatch(1);
91 <        final Runnable task = new CheckedRunnable() {
92 <            public void realRun() {
93 <                done.countDown();
94 <            }};
95 <        try {
90 >        try (PoolCleaner cleaner = cleaner(p)) {
91 >            final CountDownLatch done = new CountDownLatch(1);
92 >            final Runnable task = new CheckedRunnable() {
93 >                public void realRun() { done.countDown(); }};
94              p.execute(task);
95 <            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
98 <        } finally {
99 <            joinPool(p);
95 >            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
96          }
97      }
98  
# Line 109 | Line 105 | public class ThreadPoolExecutorTest exte
105              new ThreadPoolExecutor(2, 2,
106                                     LONG_DELAY_MS, MILLISECONDS,
107                                     new ArrayBlockingQueue<Runnable>(10));
108 <        final CountDownLatch threadStarted = new CountDownLatch(1);
109 <        final CountDownLatch done = new CountDownLatch(1);
110 <        try {
108 >        try (PoolCleaner cleaner = cleaner(p)) {
109 >            final CountDownLatch threadStarted = new CountDownLatch(1);
110 >            final CountDownLatch done = new CountDownLatch(1);
111              assertEquals(0, p.getActiveCount());
112              p.execute(new CheckedRunnable() {
113                  public void realRun() throws InterruptedException {
# Line 119 | Line 115 | public class ThreadPoolExecutorTest exte
115                      assertEquals(1, p.getActiveCount());
116                      done.await();
117                  }});
118 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
118 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
119              assertEquals(1, p.getActiveCount());
124        } finally {
120              done.countDown();
126            joinPool(p);
121          }
122      }
123  
# Line 306 | Line 300 | public class ThreadPoolExecutorTest exte
300              new ThreadPoolExecutor(1, 2,
301                                     LONG_DELAY_MS, MILLISECONDS,
302                                     new ArrayBlockingQueue<Runnable>(10));
303 <        RejectedExecutionHandler h = new NoOpREHandler();
304 <        p.setRejectedExecutionHandler(h);
305 <        assertSame(h, p.getRejectedExecutionHandler());
306 <        joinPool(p);
303 >        try (PoolCleaner cleaner = cleaner(p)) {
304 >            RejectedExecutionHandler handler = new NoOpREHandler();
305 >            p.setRejectedExecutionHandler(handler);
306 >            assertSame(handler, p.getRejectedExecutionHandler());
307 >        }
308      }
309  
310      /**
# Line 320 | Line 315 | public class ThreadPoolExecutorTest exte
315              new ThreadPoolExecutor(1, 2,
316                                     LONG_DELAY_MS, MILLISECONDS,
317                                     new ArrayBlockingQueue<Runnable>(10));
318 <        try {
319 <            p.setRejectedExecutionHandler(null);
320 <            shouldThrow();
321 <        } catch (NullPointerException success) {
322 <        } finally {
328 <            joinPool(p);
318 >        try (PoolCleaner cleaner = cleaner(p)) {
319 >            try {
320 >                p.setRejectedExecutionHandler(null);
321 >                shouldThrow();
322 >            } catch (NullPointerException success) {}
323          }
324      }
325  
# Line 339 | Line 333 | public class ThreadPoolExecutorTest exte
333              new ThreadPoolExecutor(THREADS, THREADS,
334                                     LONG_DELAY_MS, MILLISECONDS,
335                                     new ArrayBlockingQueue<Runnable>(10));
336 <        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
337 <        final CountDownLatch done = new CountDownLatch(1);
338 <        try {
336 >        try (PoolCleaner cleaner = cleaner(p)) {
337 >            final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
338 >            final CountDownLatch done = new CountDownLatch(1);
339              assertEquals(0, p.getLargestPoolSize());
340              for (int i = 0; i < THREADS; i++)
341                  p.execute(new CheckedRunnable() {
# Line 350 | Line 344 | public class ThreadPoolExecutorTest exte
344                          done.await();
345                          assertEquals(THREADS, p.getLargestPoolSize());
346                      }});
347 <            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
354 <            assertEquals(THREADS, p.getLargestPoolSize());
355 <        } finally {
356 <            done.countDown();
357 <            joinPool(p);
347 >            assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
348              assertEquals(THREADS, p.getLargestPoolSize());
349 +            done.countDown();   // release pool
350          }
351 +        assertEquals(THREADS, p.getLargestPoolSize());
352      }
353  
354      /**
# Line 368 | Line 360 | public class ThreadPoolExecutorTest exte
360              new ThreadPoolExecutor(2, 3,
361                                     LONG_DELAY_MS, MILLISECONDS,
362                                     new ArrayBlockingQueue<Runnable>(10));
363 <        assertEquals(3, p.getMaximumPoolSize());
364 <        joinPool(p);
363 >        try (PoolCleaner cleaner = cleaner(p)) {
364 >            assertEquals(3, p.getMaximumPoolSize());
365 >            p.setMaximumPoolSize(5);
366 >            assertEquals(5, p.getMaximumPoolSize());
367 >            p.setMaximumPoolSize(4);
368 >            assertEquals(4, p.getMaximumPoolSize());
369 >        }
370      }
371  
372      /**
# Line 381 | Line 378 | public class ThreadPoolExecutorTest exte
378              new ThreadPoolExecutor(1, 1,
379                                     LONG_DELAY_MS, MILLISECONDS,
380                                     new ArrayBlockingQueue<Runnable>(10));
381 <        final CountDownLatch threadStarted = new CountDownLatch(1);
382 <        final CountDownLatch done = new CountDownLatch(1);
383 <        try {
381 >        try (PoolCleaner cleaner = cleaner(p)) {
382 >            final CountDownLatch threadStarted = new CountDownLatch(1);
383 >            final CountDownLatch done = new CountDownLatch(1);
384              assertEquals(0, p.getPoolSize());
385              p.execute(new CheckedRunnable() {
386                  public void realRun() throws InterruptedException {
# Line 391 | Line 388 | public class ThreadPoolExecutorTest exte
388                      assertEquals(1, p.getPoolSize());
389                      done.await();
390                  }});
391 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
391 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
392              assertEquals(1, p.getPoolSize());
393 <        } finally {
397 <            done.countDown();
398 <            joinPool(p);
393 >            done.countDown();   // release pool
394          }
395      }
396  
# Line 403 | Line 398 | public class ThreadPoolExecutorTest exte
398       * getTaskCount increases, but doesn't overestimate, when tasks submitted
399       */
400      public void testGetTaskCount() throws InterruptedException {
401 +        final int TASKS = 3;
402 +        final CountDownLatch done = new CountDownLatch(1);
403          final ThreadPoolExecutor p =
404              new ThreadPoolExecutor(1, 1,
405                                     LONG_DELAY_MS, MILLISECONDS,
406                                     new ArrayBlockingQueue<Runnable>(10));
407 <        final CountDownLatch threadStarted = new CountDownLatch(1);
408 <        final CountDownLatch done = new CountDownLatch(1);
412 <        try {
407 >        try (PoolCleaner cleaner = cleaner(p, done)) {
408 >            final CountDownLatch threadStarted = new CountDownLatch(1);
409              assertEquals(0, p.getTaskCount());
410 +            assertEquals(0, p.getCompletedTaskCount());
411              p.execute(new CheckedRunnable() {
412                  public void realRun() throws InterruptedException {
413                      threadStarted.countDown();
417                    assertEquals(1, p.getTaskCount());
414                      done.await();
415                  }});
416 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
416 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
417              assertEquals(1, p.getTaskCount());
418 <        } finally {
419 <            done.countDown();
420 <            joinPool(p);
418 >            assertEquals(0, p.getCompletedTaskCount());
419 >            for (int i = 0; i < TASKS; i++) {
420 >                assertEquals(1 + i, p.getTaskCount());
421 >                p.execute(new CheckedRunnable() {
422 >                    public void realRun() throws InterruptedException {
423 >                        threadStarted.countDown();
424 >                        assertEquals(1 + TASKS, p.getTaskCount());
425 >                        done.await();
426 >                    }});
427 >            }
428 >            assertEquals(1 + TASKS, p.getTaskCount());
429 >            assertEquals(0, p.getCompletedTaskCount());
430          }
431 +        assertEquals(1 + TASKS, p.getTaskCount());
432 +        assertEquals(1 + TASKS, p.getCompletedTaskCount());
433      }
434  
435      /**
# Line 433 | Line 440 | public class ThreadPoolExecutorTest exte
440              new ThreadPoolExecutor(1, 1,
441                                     LONG_DELAY_MS, MILLISECONDS,
442                                     new ArrayBlockingQueue<Runnable>(10));
443 <        assertFalse(p.isShutdown());
444 <        try { p.shutdown(); } catch (SecurityException ok) { return; }
445 <        assertTrue(p.isShutdown());
446 <        joinPool(p);
443 >        try (PoolCleaner cleaner = cleaner(p)) {
444 >            assertFalse(p.isShutdown());
445 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
446 >            assertTrue(p.isShutdown());
447 >        }
448      }
449  
450      /**
# Line 447 | Line 455 | public class ThreadPoolExecutorTest exte
455              new ThreadPoolExecutor(1, 1,
456                                     LONG_DELAY_MS, MILLISECONDS,
457                                     new ArrayBlockingQueue<Runnable>(10));
458 <        assertFalse(p.isTerminated());
459 <        assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS));
460 <        assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
461 <        assertFalse(p.awaitTermination(-1L, NANOSECONDS));
462 <        assertFalse(p.awaitTermination(-1L, MILLISECONDS));
463 <        assertFalse(p.awaitTermination(0L, NANOSECONDS));
464 <        assertFalse(p.awaitTermination(0L, MILLISECONDS));
465 <        long timeoutNanos = 999999L;
466 <        long startTime = System.nanoTime();
467 <        assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
468 <        assertTrue(System.nanoTime() - startTime >= timeoutNanos);
469 <        assertFalse(p.isTerminated());
470 <        startTime = System.nanoTime();
471 <        long timeoutMillis = timeoutMillis();
472 <        assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS));
473 <        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
474 <        assertFalse(p.isTerminated());
475 <        p.shutdown();
476 <        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
477 <        assertTrue(p.isTerminated());
458 >        try (PoolCleaner cleaner = cleaner(p)) {
459 >            assertFalse(p.isTerminated());
460 >            assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS));
461 >            assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
462 >            assertFalse(p.awaitTermination(-1L, NANOSECONDS));
463 >            assertFalse(p.awaitTermination(-1L, MILLISECONDS));
464 >            assertFalse(p.awaitTermination(0L, NANOSECONDS));
465 >            assertFalse(p.awaitTermination(0L, MILLISECONDS));
466 >            long timeoutNanos = 999999L;
467 >            long startTime = System.nanoTime();
468 >            assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
469 >            assertTrue(System.nanoTime() - startTime >= timeoutNanos);
470 >            assertFalse(p.isTerminated());
471 >            startTime = System.nanoTime();
472 >            long timeoutMillis = timeoutMillis();
473 >            assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS));
474 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
475 >            assertFalse(p.isTerminated());
476 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
477 >            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
478 >            assertTrue(p.isTerminated());
479 >        }
480      }
481  
482      /**
# Line 477 | Line 487 | public class ThreadPoolExecutorTest exte
487              new ThreadPoolExecutor(1, 1,
488                                     LONG_DELAY_MS, MILLISECONDS,
489                                     new ArrayBlockingQueue<Runnable>(10));
490 <        final CountDownLatch threadStarted = new CountDownLatch(1);
491 <        final CountDownLatch done = new CountDownLatch(1);
492 <        assertFalse(p.isTerminated());
493 <        try {
490 >        try (PoolCleaner cleaner = cleaner(p)) {
491 >            final CountDownLatch threadStarted = new CountDownLatch(1);
492 >            final CountDownLatch done = new CountDownLatch(1);
493 >            assertFalse(p.isTerminating());
494              p.execute(new CheckedRunnable() {
495                  public void realRun() throws InterruptedException {
496 <                    assertFalse(p.isTerminated());
496 >                    assertFalse(p.isTerminating());
497                      threadStarted.countDown();
498                      done.await();
499                  }});
500 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
500 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
501              assertFalse(p.isTerminating());
502              done.countDown();
493        } finally {
503              try { p.shutdown(); } catch (SecurityException ok) { return; }
504 +            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
505 +            assertTrue(p.isTerminated());
506 +            assertFalse(p.isTerminating());
507          }
496        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
497        assertTrue(p.isTerminated());
508      }
509  
510      /**
# Line 505 | Line 515 | public class ThreadPoolExecutorTest exte
515              new ThreadPoolExecutor(1, 1,
516                                     LONG_DELAY_MS, MILLISECONDS,
517                                     new ArrayBlockingQueue<Runnable>(10));
518 <        final CountDownLatch threadStarted = new CountDownLatch(1);
519 <        final CountDownLatch done = new CountDownLatch(1);
520 <        try {
518 >        try (PoolCleaner cleaner = cleaner(p)) {
519 >            final CountDownLatch threadStarted = new CountDownLatch(1);
520 >            final CountDownLatch done = new CountDownLatch(1);
521              assertFalse(p.isTerminating());
522              p.execute(new CheckedRunnable() {
523                  public void realRun() throws InterruptedException {
# Line 515 | Line 525 | public class ThreadPoolExecutorTest exte
525                      threadStarted.countDown();
526                      done.await();
527                  }});
528 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
528 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
529              assertFalse(p.isTerminating());
530              done.countDown();
521        } finally {
531              try { p.shutdown(); } catch (SecurityException ok) { return; }
532 +            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
533 +            assertTrue(p.isTerminated());
534 +            assertFalse(p.isTerminating());
535          }
524        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
525        assertTrue(p.isTerminated());
526        assertFalse(p.isTerminating());
536      }
537  
538      /**
# Line 535 | Line 544 | public class ThreadPoolExecutorTest exte
544              new ThreadPoolExecutor(1, 1,
545                                     LONG_DELAY_MS, MILLISECONDS,
546                                     q);
547 <        final CountDownLatch threadStarted = new CountDownLatch(1);
548 <        final CountDownLatch done = new CountDownLatch(1);
549 <        try {
547 >        try (PoolCleaner cleaner = cleaner(p)) {
548 >            final CountDownLatch threadStarted = new CountDownLatch(1);
549 >            final CountDownLatch done = new CountDownLatch(1);
550              FutureTask[] tasks = new FutureTask[5];
551              for (int i = 0; i < tasks.length; i++) {
552                  Callable task = new CheckedCallable<Boolean>() {
# Line 550 | Line 559 | public class ThreadPoolExecutorTest exte
559                  tasks[i] = new FutureTask(task);
560                  p.execute(tasks[i]);
561              }
562 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
562 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
563              assertSame(q, p.getQueue());
564              assertFalse(q.contains(tasks[0]));
565              assertTrue(q.contains(tasks[tasks.length - 1]));
566              assertEquals(tasks.length - 1, q.size());
558        } finally {
567              done.countDown();
560            joinPool(p);
568          }
569      }
570  
# Line 570 | Line 577 | public class ThreadPoolExecutorTest exte
577              new ThreadPoolExecutor(1, 1,
578                                     LONG_DELAY_MS, MILLISECONDS,
579                                     q);
580 <        Runnable[] tasks = new Runnable[5];
581 <        final CountDownLatch threadStarted = new CountDownLatch(1);
582 <        final CountDownLatch done = new CountDownLatch(1);
583 <        try {
580 >        try (PoolCleaner cleaner = cleaner(p)) {
581 >            Runnable[] tasks = new Runnable[6];
582 >            final CountDownLatch threadStarted = new CountDownLatch(1);
583 >            final CountDownLatch done = new CountDownLatch(1);
584              for (int i = 0; i < tasks.length; i++) {
585                  tasks[i] = new CheckedRunnable() {
586                      public void realRun() throws InterruptedException {
# Line 582 | Line 589 | public class ThreadPoolExecutorTest exte
589                      }};
590                  p.execute(tasks[i]);
591              }
592 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
592 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
593              assertFalse(p.remove(tasks[0]));
594              assertTrue(q.contains(tasks[4]));
595              assertTrue(q.contains(tasks[3]));
# Line 592 | Line 599 | public class ThreadPoolExecutorTest exte
599              assertTrue(q.contains(tasks[3]));
600              assertTrue(p.remove(tasks[3]));
601              assertFalse(q.contains(tasks[3]));
595        } finally {
602              done.countDown();
597            joinPool(p);
603          }
604      }
605  
# Line 609 | Line 614 | public class ThreadPoolExecutorTest exte
614              new ThreadPoolExecutor(1, 1,
615                                     LONG_DELAY_MS, MILLISECONDS,
616                                     q);
617 <        FutureTask[] tasks = new FutureTask[5];
618 <        try {
617 >        try (PoolCleaner cleaner = cleaner(p, done)) {
618 >            FutureTask[] tasks = new FutureTask[5];
619              for (int i = 0; i < tasks.length; i++) {
620                  Callable task = new CheckedCallable<Boolean>() {
621                      public Boolean realCall() throws InterruptedException {
# Line 621 | Line 626 | public class ThreadPoolExecutorTest exte
626                  tasks[i] = new FutureTask(task);
627                  p.execute(tasks[i]);
628              }
629 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
629 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
630              assertEquals(tasks.length, p.getTaskCount());
631              assertEquals(tasks.length - 1, q.size());
632              assertEquals(1L, p.getActiveCount());
# Line 634 | Line 639 | public class ThreadPoolExecutorTest exte
639              p.purge();         // Nothing to do
640              assertEquals(tasks.length - 3, q.size());
641              assertEquals(tasks.length - 2, p.getTaskCount());
637        } finally {
638            done.countDown();
639            joinPool(p);
642          }
643      }
644  
# Line 652 | Line 654 | public class ThreadPoolExecutorTest exte
654              new ThreadPoolExecutor(poolSize, poolSize,
655                                     LONG_DELAY_MS, MILLISECONDS,
656                                     new ArrayBlockingQueue<Runnable>(10));
657 <        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
657 >        final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
658          Runnable waiter = new CheckedRunnable() { public void realRun() {
659              threadsStarted.countDown();
660              try {
# Line 1029 | Line 1031 | public class ThreadPoolExecutorTest exte
1031                                     60, SECONDS,
1032                                     new ArrayBlockingQueue<Runnable>(10));
1033  
1034 <        final CountDownLatch threadStarted = new CountDownLatch(1);
1035 <        final CountDownLatch done = new CountDownLatch(1);
1036 <        try {
1034 >        try (PoolCleaner cleaner = cleaner(p)) {
1035 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1036 >            final CountDownLatch done = new CountDownLatch(1);
1037              Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1038                  public void realRun() throws Exception {
1039                      Callable task = new CheckedCallable<Boolean>() {
# Line 1043 | Line 1045 | public class ThreadPoolExecutorTest exte
1045                      p.submit(task).get();
1046                  }});
1047  
1048 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
1048 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
1049              t.interrupt();
1050              awaitTermination(t, MEDIUM_DELAY_MS);
1049        } finally {
1051              done.countDown();
1051            joinPool(p);
1052          }
1053      }
1054  
# Line 1056 | Line 1056 | public class ThreadPoolExecutorTest exte
1056       * execute throws RejectedExecutionException if saturated.
1057       */
1058      public void testSaturatedExecute() {
1059 <        ThreadPoolExecutor p =
1059 >        final ThreadPoolExecutor p =
1060              new ThreadPoolExecutor(1, 1,
1061                                     LONG_DELAY_MS, MILLISECONDS,
1062                                     new ArrayBlockingQueue<Runnable>(1));
1063 <        final CountDownLatch done = new CountDownLatch(1);
1064 <        try {
1063 >        try (PoolCleaner cleaner = cleaner(p)) {
1064 >            final CountDownLatch done = new CountDownLatch(1);
1065              Runnable task = new CheckedRunnable() {
1066                  public void realRun() throws InterruptedException {
1067                      done.await();
# Line 1075 | Line 1075 | public class ThreadPoolExecutorTest exte
1075                  } catch (RejectedExecutionException success) {}
1076                  assertTrue(p.getTaskCount() <= 2);
1077              }
1078        } finally {
1078              done.countDown();
1080            joinPool(p);
1079          }
1080      }
1081  
# Line 1085 | Line 1083 | public class ThreadPoolExecutorTest exte
1083       * submit(runnable) throws RejectedExecutionException if saturated.
1084       */
1085      public void testSaturatedSubmitRunnable() {
1086 <        ThreadPoolExecutor p =
1086 >        final ThreadPoolExecutor p =
1087              new ThreadPoolExecutor(1, 1,
1088                                     LONG_DELAY_MS, MILLISECONDS,
1089                                     new ArrayBlockingQueue<Runnable>(1));
1090 <        final CountDownLatch done = new CountDownLatch(1);
1091 <        try {
1090 >        try (PoolCleaner cleaner = cleaner(p)) {
1091 >            final CountDownLatch done = new CountDownLatch(1);
1092              Runnable task = new CheckedRunnable() {
1093                  public void realRun() throws InterruptedException {
1094                      done.await();
# Line 1104 | Line 1102 | public class ThreadPoolExecutorTest exte
1102                  } catch (RejectedExecutionException success) {}
1103                  assertTrue(p.getTaskCount() <= 2);
1104              }
1107        } finally {
1105              done.countDown();
1109            joinPool(p);
1106          }
1107      }
1108  
# Line 1114 | Line 1110 | public class ThreadPoolExecutorTest exte
1110       * submit(callable) throws RejectedExecutionException if saturated.
1111       */
1112      public void testSaturatedSubmitCallable() {
1113 <        ThreadPoolExecutor p =
1113 >        final ThreadPoolExecutor p =
1114              new ThreadPoolExecutor(1, 1,
1115                                     LONG_DELAY_MS, MILLISECONDS,
1116                                     new ArrayBlockingQueue<Runnable>(1));
1117 <        final CountDownLatch done = new CountDownLatch(1);
1118 <        try {
1117 >        try (PoolCleaner cleaner = cleaner(p)) {
1118 >            final CountDownLatch done = new CountDownLatch(1);
1119              Runnable task = new CheckedRunnable() {
1120                  public void realRun() throws InterruptedException {
1121                      done.await();
# Line 1133 | Line 1129 | public class ThreadPoolExecutorTest exte
1129                  } catch (RejectedExecutionException success) {}
1130                  assertTrue(p.getTaskCount() <= 2);
1131              }
1136        } finally {
1132              done.countDown();
1138            joinPool(p);
1133          }
1134      }
1135  
# Line 1143 | Line 1137 | public class ThreadPoolExecutorTest exte
1137       * executor using CallerRunsPolicy runs task if saturated.
1138       */
1139      public void testSaturatedExecute2() {
1146        RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
1140          final ThreadPoolExecutor p =
1141              new ThreadPoolExecutor(1, 1,
1142                                     LONG_DELAY_MS,
1143                                     MILLISECONDS,
1144                                     new ArrayBlockingQueue<Runnable>(1),
1145 <                                   h);
1146 <        try {
1145 >                                   new ThreadPoolExecutor.CallerRunsPolicy());
1146 >        try (PoolCleaner cleaner = cleaner(p)) {
1147 >            final CountDownLatch done = new CountDownLatch(1);
1148 >            Runnable blocker = new CheckedRunnable() {
1149 >                public void realRun() throws InterruptedException {
1150 >                    done.await();
1151 >                }};
1152 >            p.execute(blocker);
1153              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1154 <            for (int i = 0; i < tasks.length; ++i)
1154 >            for (int i = 0; i < tasks.length; i++)
1155                  tasks[i] = new TrackedNoOpRunnable();
1156 <            TrackedLongRunnable mr = new TrackedLongRunnable();
1158 <            p.execute(mr);
1159 <            for (int i = 0; i < tasks.length; ++i)
1156 >            for (int i = 0; i < tasks.length; i++)
1157                  p.execute(tasks[i]);
1158 <            for (int i = 1; i < tasks.length; ++i)
1158 >            for (int i = 1; i < tasks.length; i++)
1159                  assertTrue(tasks[i].done);
1160 <            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1161 <        } finally {
1165 <            joinPool(p);
1160 >            assertFalse(tasks[0].done); // waiting in queue
1161 >            done.countDown();
1162          }
1163      }
1164  
# Line 1170 | Line 1166 | public class ThreadPoolExecutorTest exte
1166       * executor using DiscardPolicy drops task if saturated.
1167       */
1168      public void testSaturatedExecute3() {
1169 <        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
1169 >        final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1170 >        for (int i = 0; i < tasks.length; ++i)
1171 >            tasks[i] = new TrackedNoOpRunnable();
1172          final ThreadPoolExecutor p =
1173              new ThreadPoolExecutor(1, 1,
1174 <                                   LONG_DELAY_MS, MILLISECONDS,
1175 <                                   new ArrayBlockingQueue<Runnable>(1),
1176 <                                   h);
1177 <        try {
1178 <            TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1179 <            for (int i = 0; i < tasks.length; ++i)
1180 <                tasks[i] = new TrackedNoOpRunnable();
1183 <            p.execute(new TrackedLongRunnable());
1174 >                          LONG_DELAY_MS, MILLISECONDS,
1175 >                          new ArrayBlockingQueue<Runnable>(1),
1176 >                          new ThreadPoolExecutor.DiscardPolicy());
1177 >        try (PoolCleaner cleaner = cleaner(p)) {
1178 >            final CountDownLatch done = new CountDownLatch(1);
1179 >            p.execute(awaiter(done));
1180 >
1181              for (TrackedNoOpRunnable task : tasks)
1182                  p.execute(task);
1183 <            for (TrackedNoOpRunnable task : tasks)
1184 <                assertFalse(task.done);
1185 <            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1189 <        } finally {
1190 <            joinPool(p);
1183 >            for (int i = 1; i < tasks.length; i++)
1184 >                assertFalse(tasks[i].done);
1185 >            done.countDown();
1186          }
1187 +        for (int i = 1; i < tasks.length; i++)
1188 +            assertFalse(tasks[i].done);
1189 +        assertTrue(tasks[0].done); // was waiting in queue
1190      }
1191  
1192      /**
1193       * executor using DiscardOldestPolicy drops oldest task if saturated.
1194       */
1195      public void testSaturatedExecute4() {
1196 <        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
1196 >        final CountDownLatch done = new CountDownLatch(1);
1197 >        LatchAwaiter r1 = awaiter(done);
1198 >        LatchAwaiter r2 = awaiter(done);
1199 >        LatchAwaiter r3 = awaiter(done);
1200          final ThreadPoolExecutor p =
1201              new ThreadPoolExecutor(1, 1,
1202                                     LONG_DELAY_MS, MILLISECONDS,
1203                                     new ArrayBlockingQueue<Runnable>(1),
1204 <                                   h);
1205 <        try {
1206 <            p.execute(new TrackedLongRunnable());
1207 <            TrackedLongRunnable r2 = new TrackedLongRunnable();
1204 >                                   new ThreadPoolExecutor.DiscardOldestPolicy());
1205 >        try (PoolCleaner cleaner = cleaner(p)) {
1206 >            assertEquals(LatchAwaiter.NEW, r1.state);
1207 >            assertEquals(LatchAwaiter.NEW, r2.state);
1208 >            assertEquals(LatchAwaiter.NEW, r3.state);
1209 >            p.execute(r1);
1210              p.execute(r2);
1211              assertTrue(p.getQueue().contains(r2));
1209            TrackedNoOpRunnable r3 = new TrackedNoOpRunnable();
1212              p.execute(r3);
1213              assertFalse(p.getQueue().contains(r2));
1214              assertTrue(p.getQueue().contains(r3));
1215 <            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1214 <        } finally {
1215 <            joinPool(p);
1215 >            done.countDown();
1216          }
1217 +        assertEquals(LatchAwaiter.DONE, r1.state);
1218 +        assertEquals(LatchAwaiter.NEW, r2.state);
1219 +        assertEquals(LatchAwaiter.DONE, r3.state);
1220      }
1221  
1222      /**
1223       * execute throws RejectedExecutionException if shutdown
1224       */
1225      public void testRejectedExecutionExceptionOnShutdown() {
1226 <        ThreadPoolExecutor p =
1226 >        final ThreadPoolExecutor p =
1227              new ThreadPoolExecutor(1, 1,
1228                                     LONG_DELAY_MS, MILLISECONDS,
1229                                     new ArrayBlockingQueue<Runnable>(1));
1230          try { p.shutdown(); } catch (SecurityException ok) { return; }
1231 <        try {
1232 <            p.execute(new NoOpRunnable());
1233 <            shouldThrow();
1234 <        } catch (RejectedExecutionException success) {}
1235 <
1236 <        joinPool(p);
1231 >        try (PoolCleaner cleaner = cleaner(p)) {
1232 >            try {
1233 >                p.execute(new NoOpRunnable());
1234 >                shouldThrow();
1235 >            } catch (RejectedExecutionException success) {}
1236 >        }
1237      }
1238  
1239      /**
# Line 1244 | Line 1247 | public class ThreadPoolExecutorTest exte
1247                                     new ArrayBlockingQueue<Runnable>(1), h);
1248  
1249          try { p.shutdown(); } catch (SecurityException ok) { return; }
1250 <        try {
1250 >        try (PoolCleaner cleaner = cleaner(p)) {
1251              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1252              p.execute(r);
1253              assertFalse(r.done);
1251        } finally {
1252            joinPool(p);
1254          }
1255      }
1256  
# Line 1257 | Line 1258 | public class ThreadPoolExecutorTest exte
1258       * execute using DiscardPolicy drops task on shutdown
1259       */
1260      public void testDiscardOnShutdown() {
1261 <        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
1261 <        ThreadPoolExecutor p =
1261 >        final ThreadPoolExecutor p =
1262              new ThreadPoolExecutor(1, 1,
1263                                     LONG_DELAY_MS, MILLISECONDS,
1264                                     new ArrayBlockingQueue<Runnable>(1),
1265 <                                   h);
1265 >                                   new ThreadPoolExecutor.DiscardPolicy());
1266  
1267          try { p.shutdown(); } catch (SecurityException ok) { return; }
1268 <        try {
1268 >        try (PoolCleaner cleaner = cleaner(p)) {
1269              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1270              p.execute(r);
1271              assertFalse(r.done);
1272        } finally {
1273            joinPool(p);
1272          }
1273      }
1274  
# Line 1278 | Line 1276 | public class ThreadPoolExecutorTest exte
1276       * execute using DiscardOldestPolicy drops task on shutdown
1277       */
1278      public void testDiscardOldestOnShutdown() {
1279 <        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
1282 <        ThreadPoolExecutor p =
1279 >        final ThreadPoolExecutor p =
1280              new ThreadPoolExecutor(1, 1,
1281                                     LONG_DELAY_MS, MILLISECONDS,
1282                                     new ArrayBlockingQueue<Runnable>(1),
1283 <                                   h);
1283 >                                   new ThreadPoolExecutor.DiscardOldestPolicy());
1284  
1285          try { p.shutdown(); } catch (SecurityException ok) { return; }
1286 <        try {
1286 >        try (PoolCleaner cleaner = cleaner(p)) {
1287              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1288              p.execute(r);
1289              assertFalse(r.done);
1293        } finally {
1294            joinPool(p);
1290          }
1291      }
1292  
# Line 1299 | Line 1294 | public class ThreadPoolExecutorTest exte
1294       * execute(null) throws NPE
1295       */
1296      public void testExecuteNull() {
1297 <        ThreadPoolExecutor p =
1298 <            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1297 >        final ThreadPoolExecutor p =
1298 >            new ThreadPoolExecutor(1, 2,
1299 >                                   1L, SECONDS,
1300                                     new ArrayBlockingQueue<Runnable>(10));
1301 <        try {
1302 <            p.execute(null);
1303 <            shouldThrow();
1304 <        } catch (NullPointerException success) {}
1305 <
1306 <        joinPool(p);
1301 >        try (PoolCleaner cleaner = cleaner(p)) {
1302 >            try {
1303 >                p.execute(null);
1304 >                shouldThrow();
1305 >            } catch (NullPointerException success) {}
1306 >        }
1307      }
1308  
1309      /**
1310       * setCorePoolSize of negative value throws IllegalArgumentException
1311       */
1312      public void testCorePoolSizeIllegalArgumentException() {
1313 <        ThreadPoolExecutor p =
1313 >        final ThreadPoolExecutor p =
1314              new ThreadPoolExecutor(1, 2,
1315                                     LONG_DELAY_MS, MILLISECONDS,
1316                                     new ArrayBlockingQueue<Runnable>(10));
1317 <        try {
1318 <            p.setCorePoolSize(-1);
1319 <            shouldThrow();
1320 <        } catch (IllegalArgumentException success) {
1321 <        } finally {
1326 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1317 >        try (PoolCleaner cleaner = cleaner(p)) {
1318 >            try {
1319 >                p.setCorePoolSize(-1);
1320 >                shouldThrow();
1321 >            } catch (IllegalArgumentException success) {}
1322          }
1328        joinPool(p);
1323      }
1324  
1325      /**
# Line 1333 | Line 1327 | public class ThreadPoolExecutorTest exte
1327       * given a value less the core pool size
1328       */
1329      public void testMaximumPoolSizeIllegalArgumentException() {
1330 <        ThreadPoolExecutor p =
1330 >        final ThreadPoolExecutor p =
1331              new ThreadPoolExecutor(2, 3,
1332                                     LONG_DELAY_MS, MILLISECONDS,
1333                                     new ArrayBlockingQueue<Runnable>(10));
1334 <        try {
1335 <            p.setMaximumPoolSize(1);
1336 <            shouldThrow();
1337 <        } catch (IllegalArgumentException success) {
1338 <        } finally {
1345 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1334 >        try (PoolCleaner cleaner = cleaner(p)) {
1335 >            try {
1336 >                p.setMaximumPoolSize(1);
1337 >                shouldThrow();
1338 >            } catch (IllegalArgumentException success) {}
1339          }
1347        joinPool(p);
1340      }
1341  
1342      /**
# Line 1352 | Line 1344 | public class ThreadPoolExecutorTest exte
1344       * if given a negative value
1345       */
1346      public void testMaximumPoolSizeIllegalArgumentException2() {
1347 <        ThreadPoolExecutor p =
1347 >        final ThreadPoolExecutor p =
1348              new ThreadPoolExecutor(2, 3,
1349                                     LONG_DELAY_MS, MILLISECONDS,
1350                                     new ArrayBlockingQueue<Runnable>(10));
1351 <        try {
1352 <            p.setMaximumPoolSize(-1);
1353 <            shouldThrow();
1354 <        } catch (IllegalArgumentException success) {
1355 <        } finally {
1364 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1351 >        try (PoolCleaner cleaner = cleaner(p)) {
1352 >            try {
1353 >                p.setMaximumPoolSize(-1);
1354 >                shouldThrow();
1355 >            } catch (IllegalArgumentException success) {}
1356          }
1366        joinPool(p);
1357      }
1358  
1359      /**
# Line 1371 | Line 1361 | public class ThreadPoolExecutorTest exte
1361       * max pool size result in IllegalArgumentException.
1362       */
1363      public void testPoolSizeInvariants() {
1364 <        ThreadPoolExecutor p =
1364 >        final ThreadPoolExecutor p =
1365              new ThreadPoolExecutor(1, 1,
1366                                     LONG_DELAY_MS, MILLISECONDS,
1367                                     new ArrayBlockingQueue<Runnable>(10));
1368 <        for (int s = 1; s < 5; s++) {
1369 <            p.setMaximumPoolSize(s);
1370 <            p.setCorePoolSize(s);
1371 <            try {
1372 <                p.setMaximumPoolSize(s - 1);
1373 <                shouldThrow();
1374 <            } catch (IllegalArgumentException success) {}
1375 <            assertEquals(s, p.getCorePoolSize());
1376 <            assertEquals(s, p.getMaximumPoolSize());
1377 <            try {
1378 <                p.setCorePoolSize(s + 1);
1379 <                shouldThrow();
1380 <            } catch (IllegalArgumentException success) {}
1381 <            assertEquals(s, p.getCorePoolSize());
1382 <            assertEquals(s, p.getMaximumPoolSize());
1368 >        try (PoolCleaner cleaner = cleaner(p)) {
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());
1384 >            }
1385          }
1394        joinPool(p);
1386      }
1387  
1388      /**
# Line 1399 | Line 1390 | public class ThreadPoolExecutorTest exte
1390       * when given a negative value
1391       */
1392      public void testKeepAliveTimeIllegalArgumentException() {
1393 <        ThreadPoolExecutor p =
1393 >        final ThreadPoolExecutor p =
1394              new ThreadPoolExecutor(2, 3,
1395                                     LONG_DELAY_MS, MILLISECONDS,
1396                                     new ArrayBlockingQueue<Runnable>(10));
1397 <        try {
1398 <            p.setKeepAliveTime(-1,MILLISECONDS);
1399 <            shouldThrow();
1400 <        } catch (IllegalArgumentException success) {
1401 <        } finally {
1411 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1397 >        try (PoolCleaner cleaner = cleaner(p)) {
1398 >            try {
1399 >                p.setKeepAliveTime(-1, MILLISECONDS);
1400 >                shouldThrow();
1401 >            } catch (IllegalArgumentException success) {}
1402          }
1413        joinPool(p);
1403      }
1404  
1405      /**
# Line 1418 | Line 1407 | public class ThreadPoolExecutorTest exte
1407       */
1408      public void testTerminated() {
1409          ExtendedTPE p = new ExtendedTPE();
1410 <        try { p.shutdown(); } catch (SecurityException ok) { return; }
1411 <        assertTrue(p.terminatedCalled());
1412 <        joinPool(p);
1410 >        try (PoolCleaner cleaner = cleaner(p)) {
1411 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1412 >            assertTrue(p.terminatedCalled());
1413 >            assertTrue(p.isShutdown());
1414 >        }
1415      }
1416  
1417      /**
# Line 1428 | Line 1419 | public class ThreadPoolExecutorTest exte
1419       */
1420      public void testBeforeAfter() throws InterruptedException {
1421          ExtendedTPE p = new ExtendedTPE();
1422 <        try {
1422 >        try (PoolCleaner cleaner = cleaner(p)) {
1423              final CountDownLatch done = new CountDownLatch(1);
1424              p.execute(new CheckedRunnable() {
1425                  public void realRun() {
# Line 1438 | Line 1429 | public class ThreadPoolExecutorTest exte
1429              assertEquals(0, done.getCount());
1430              assertTrue(p.afterCalled());
1431              assertTrue(p.beforeCalled());
1441            try { p.shutdown(); } catch (SecurityException ok) { return; }
1442        } finally {
1443            joinPool(p);
1432          }
1433      }
1434  
# Line 1448 | Line 1436 | public class ThreadPoolExecutorTest exte
1436       * completed submit of callable returns result
1437       */
1438      public void testSubmitCallable() throws Exception {
1439 <        ExecutorService e =
1439 >        final ExecutorService e =
1440              new ThreadPoolExecutor(2, 2,
1441                                     LONG_DELAY_MS, MILLISECONDS,
1442                                     new ArrayBlockingQueue<Runnable>(10));
1443 <        try {
1443 >        try (PoolCleaner cleaner = cleaner(e)) {
1444              Future<String> future = e.submit(new StringTask());
1445              String result = future.get();
1446              assertSame(TEST_STRING, result);
1459        } finally {
1460            joinPool(e);
1447          }
1448      }
1449  
# Line 1465 | Line 1451 | public class ThreadPoolExecutorTest exte
1451       * completed submit of runnable returns successfully
1452       */
1453      public void testSubmitRunnable() throws Exception {
1454 <        ExecutorService e =
1454 >        final ExecutorService e =
1455              new ThreadPoolExecutor(2, 2,
1456                                     LONG_DELAY_MS, MILLISECONDS,
1457                                     new ArrayBlockingQueue<Runnable>(10));
1458 <        try {
1458 >        try (PoolCleaner cleaner = cleaner(e)) {
1459              Future<?> future = e.submit(new NoOpRunnable());
1460              future.get();
1461              assertTrue(future.isDone());
1476        } finally {
1477            joinPool(e);
1462          }
1463      }
1464  
# Line 1482 | Line 1466 | public class ThreadPoolExecutorTest exte
1466       * completed submit of (runnable, result) returns result
1467       */
1468      public void testSubmitRunnable2() throws Exception {
1469 <        ExecutorService e =
1469 >        final ExecutorService e =
1470              new ThreadPoolExecutor(2, 2,
1471                                     LONG_DELAY_MS, MILLISECONDS,
1472                                     new ArrayBlockingQueue<Runnable>(10));
1473 <        try {
1473 >        try (PoolCleaner cleaner = cleaner(e)) {
1474              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1475              String result = future.get();
1476              assertSame(TEST_STRING, result);
1493        } finally {
1494            joinPool(e);
1477          }
1478      }
1479  
# Line 1499 | Line 1481 | public class ThreadPoolExecutorTest exte
1481       * invokeAny(null) throws NPE
1482       */
1483      public void testInvokeAny1() throws Exception {
1484 <        ExecutorService e =
1484 >        final ExecutorService e =
1485              new ThreadPoolExecutor(2, 2,
1486                                     LONG_DELAY_MS, MILLISECONDS,
1487                                     new ArrayBlockingQueue<Runnable>(10));
1488 <        try {
1489 <            e.invokeAny(null);
1490 <            shouldThrow();
1491 <        } catch (NullPointerException success) {
1492 <        } finally {
1511 <            joinPool(e);
1488 >        try (PoolCleaner cleaner = cleaner(e)) {
1489 >            try {
1490 >                e.invokeAny(null);
1491 >                shouldThrow();
1492 >            } catch (NullPointerException success) {}
1493          }
1494      }
1495  
# Line 1516 | Line 1497 | public class ThreadPoolExecutorTest exte
1497       * invokeAny(empty collection) throws IAE
1498       */
1499      public void testInvokeAny2() throws Exception {
1500 <        ExecutorService e =
1500 >        final ExecutorService e =
1501              new ThreadPoolExecutor(2, 2,
1502                                     LONG_DELAY_MS, MILLISECONDS,
1503                                     new ArrayBlockingQueue<Runnable>(10));
1504 <        try {
1505 <            e.invokeAny(new ArrayList<Callable<String>>());
1506 <            shouldThrow();
1507 <        } catch (IllegalArgumentException success) {
1508 <        } finally {
1528 <            joinPool(e);
1504 >        try (PoolCleaner cleaner = cleaner(e)) {
1505 >            try {
1506 >                e.invokeAny(new ArrayList<Callable<String>>());
1507 >                shouldThrow();
1508 >            } catch (IllegalArgumentException success) {}
1509          }
1510      }
1511  
# Line 1538 | Line 1518 | public class ThreadPoolExecutorTest exte
1518              new ThreadPoolExecutor(2, 2,
1519                                     LONG_DELAY_MS, MILLISECONDS,
1520                                     new ArrayBlockingQueue<Runnable>(10));
1521 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1522 <        l.add(latchAwaitingStringTask(latch));
1523 <        l.add(null);
1524 <        try {
1525 <            e.invokeAny(l);
1526 <            shouldThrow();
1527 <        } catch (NullPointerException success) {
1528 <        } finally {
1521 >        try (PoolCleaner cleaner = cleaner(e)) {
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              latch.countDown();
1550            joinPool(e);
1530          }
1531      }
1532  
# Line 1555 | Line 1534 | public class ThreadPoolExecutorTest exte
1534       * invokeAny(c) throws ExecutionException if no task completes
1535       */
1536      public void testInvokeAny4() throws Exception {
1537 <        ExecutorService e =
1537 >        final ExecutorService e =
1538              new ThreadPoolExecutor(2, 2,
1539                                     LONG_DELAY_MS, MILLISECONDS,
1540                                     new ArrayBlockingQueue<Runnable>(10));
1541 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1542 <        l.add(new NPETask());
1543 <        try {
1544 <            e.invokeAny(l);
1545 <            shouldThrow();
1546 <        } catch (ExecutionException success) {
1547 <            assertTrue(success.getCause() instanceof NullPointerException);
1548 <        } finally {
1549 <            joinPool(e);
1541 >        try (PoolCleaner cleaner = cleaner(e)) {
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 >            }
1550          }
1551      }
1552  
# Line 1575 | Line 1554 | public class ThreadPoolExecutorTest exte
1554       * invokeAny(c) returns result of some task
1555       */
1556      public void testInvokeAny5() throws Exception {
1557 <        ExecutorService e =
1557 >        final ExecutorService e =
1558              new ThreadPoolExecutor(2, 2,
1559                                     LONG_DELAY_MS, MILLISECONDS,
1560                                     new ArrayBlockingQueue<Runnable>(10));
1561 <        try {
1561 >        try (PoolCleaner cleaner = cleaner(e)) {
1562              List<Callable<String>> l = new ArrayList<Callable<String>>();
1563              l.add(new StringTask());
1564              l.add(new StringTask());
1565              String result = e.invokeAny(l);
1566              assertSame(TEST_STRING, result);
1588        } finally {
1589            joinPool(e);
1567          }
1568      }
1569  
# Line 1594 | Line 1571 | public class ThreadPoolExecutorTest exte
1571       * invokeAll(null) throws NPE
1572       */
1573      public void testInvokeAll1() throws Exception {
1574 <        ExecutorService e =
1574 >        final ExecutorService e =
1575              new ThreadPoolExecutor(2, 2,
1576                                     LONG_DELAY_MS, MILLISECONDS,
1577                                     new ArrayBlockingQueue<Runnable>(10));
1578 <        try {
1579 <            e.invokeAll(null);
1580 <            shouldThrow();
1581 <        } catch (NullPointerException success) {
1582 <        } finally {
1606 <            joinPool(e);
1578 >        try (PoolCleaner cleaner = cleaner(e)) {
1579 >            try {
1580 >                e.invokeAll(null);
1581 >                shouldThrow();
1582 >            } catch (NullPointerException success) {}
1583          }
1584      }
1585  
# Line 1611 | Line 1587 | public class ThreadPoolExecutorTest exte
1587       * invokeAll(empty collection) returns empty collection
1588       */
1589      public void testInvokeAll2() throws InterruptedException {
1590 <        ExecutorService e =
1590 >        final ExecutorService e =
1591              new ThreadPoolExecutor(2, 2,
1592                                     LONG_DELAY_MS, MILLISECONDS,
1593                                     new ArrayBlockingQueue<Runnable>(10));
1594 <        try {
1594 >        try (PoolCleaner cleaner = cleaner(e)) {
1595              List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1596              assertTrue(r.isEmpty());
1621        } finally {
1622            joinPool(e);
1597          }
1598      }
1599  
# Line 1627 | Line 1601 | public class ThreadPoolExecutorTest exte
1601       * invokeAll(c) throws NPE if c has null elements
1602       */
1603      public void testInvokeAll3() throws Exception {
1604 <        ExecutorService e =
1604 >        final ExecutorService e =
1605              new ThreadPoolExecutor(2, 2,
1606                                     LONG_DELAY_MS, MILLISECONDS,
1607                                     new ArrayBlockingQueue<Runnable>(10));
1608 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1609 <        l.add(new StringTask());
1610 <        l.add(null);
1611 <        try {
1612 <            e.invokeAll(l);
1613 <            shouldThrow();
1614 <        } catch (NullPointerException success) {
1615 <        } finally {
1642 <            joinPool(e);
1608 >        try (PoolCleaner cleaner = cleaner(e)) {
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          }
1617      }
1618  
# Line 1647 | Line 1620 | public class ThreadPoolExecutorTest exte
1620       * get of element of invokeAll(c) throws exception on failed task
1621       */
1622      public void testInvokeAll4() throws Exception {
1623 <        ExecutorService e =
1623 >        final ExecutorService e =
1624              new ThreadPoolExecutor(2, 2,
1625                                     LONG_DELAY_MS, MILLISECONDS,
1626                                     new ArrayBlockingQueue<Runnable>(10));
1627 <        try {
1627 >        try (PoolCleaner cleaner = cleaner(e)) {
1628              List<Callable<String>> l = new ArrayList<Callable<String>>();
1629              l.add(new NPETask());
1630              List<Future<String>> futures = e.invokeAll(l);
# Line 1662 | Line 1635 | public class ThreadPoolExecutorTest exte
1635              } catch (ExecutionException success) {
1636                  assertTrue(success.getCause() instanceof NullPointerException);
1637              }
1665        } finally {
1666            joinPool(e);
1638          }
1639      }
1640  
# Line 1671 | Line 1642 | public class ThreadPoolExecutorTest exte
1642       * invokeAll(c) returns results of all completed tasks
1643       */
1644      public void testInvokeAll5() throws Exception {
1645 <        ExecutorService e =
1645 >        final ExecutorService e =
1646              new ThreadPoolExecutor(2, 2,
1647                                     LONG_DELAY_MS, MILLISECONDS,
1648                                     new ArrayBlockingQueue<Runnable>(10));
1649 <        try {
1649 >        try (PoolCleaner cleaner = cleaner(e)) {
1650              List<Callable<String>> l = new ArrayList<Callable<String>>();
1651              l.add(new StringTask());
1652              l.add(new StringTask());
# Line 1683 | Line 1654 | public class ThreadPoolExecutorTest exte
1654              assertEquals(2, futures.size());
1655              for (Future<String> future : futures)
1656                  assertSame(TEST_STRING, future.get());
1686        } finally {
1687            joinPool(e);
1657          }
1658      }
1659  
# Line 1692 | Line 1661 | public class ThreadPoolExecutorTest exte
1661       * timed invokeAny(null) throws NPE
1662       */
1663      public void testTimedInvokeAny1() throws Exception {
1664 <        ExecutorService e =
1664 >        final ExecutorService e =
1665              new ThreadPoolExecutor(2, 2,
1666                                     LONG_DELAY_MS, MILLISECONDS,
1667                                     new ArrayBlockingQueue<Runnable>(10));
1668 <        try {
1669 <            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1670 <            shouldThrow();
1671 <        } catch (NullPointerException success) {
1672 <        } finally {
1704 <            joinPool(e);
1668 >        try (PoolCleaner cleaner = cleaner(e)) {
1669 >            try {
1670 >                e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1671 >                shouldThrow();
1672 >            } catch (NullPointerException success) {}
1673          }
1674      }
1675  
# Line 1709 | Line 1677 | public class ThreadPoolExecutorTest exte
1677       * timed invokeAny(,,null) throws NPE
1678       */
1679      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1680 <        ExecutorService e =
1680 >        final ExecutorService e =
1681              new ThreadPoolExecutor(2, 2,
1682                                     LONG_DELAY_MS, MILLISECONDS,
1683                                     new ArrayBlockingQueue<Runnable>(10));
1684 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1685 <        l.add(new StringTask());
1686 <        try {
1687 <            e.invokeAny(l, MEDIUM_DELAY_MS, null);
1688 <            shouldThrow();
1689 <        } catch (NullPointerException success) {
1690 <        } finally {
1723 <            joinPool(e);
1684 >        try (PoolCleaner cleaner = cleaner(e)) {
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          }
1692      }
1693  
# Line 1728 | Line 1695 | public class ThreadPoolExecutorTest exte
1695       * timed invokeAny(empty collection) throws IAE
1696       */
1697      public void testTimedInvokeAny2() throws Exception {
1698 <        ExecutorService e =
1698 >        final ExecutorService e =
1699              new ThreadPoolExecutor(2, 2,
1700                                     LONG_DELAY_MS, MILLISECONDS,
1701                                     new ArrayBlockingQueue<Runnable>(10));
1702 <        try {
1703 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1704 <            shouldThrow();
1705 <        } catch (IllegalArgumentException success) {
1706 <        } finally {
1707 <            joinPool(e);
1702 >        try (PoolCleaner cleaner = cleaner(e)) {
1703 >            try {
1704 >                e.invokeAny(new ArrayList<Callable<String>>(),
1705 >                            MEDIUM_DELAY_MS, MILLISECONDS);
1706 >                shouldThrow();
1707 >            } catch (IllegalArgumentException success) {}
1708          }
1709      }
1710  
# Line 1750 | Line 1717 | public class ThreadPoolExecutorTest exte
1717              new ThreadPoolExecutor(2, 2,
1718                                     LONG_DELAY_MS, MILLISECONDS,
1719                                     new ArrayBlockingQueue<Runnable>(10));
1720 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1721 <        l.add(latchAwaitingStringTask(latch));
1722 <        l.add(null);
1723 <        try {
1724 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1725 <            shouldThrow();
1726 <        } catch (NullPointerException success) {
1727 <        } finally {
1720 >        try (PoolCleaner cleaner = cleaner(e)) {
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              latch.countDown();
1762            joinPool(e);
1729          }
1730      }
1731  
# Line 1767 | Line 1733 | public class ThreadPoolExecutorTest exte
1733       * timed invokeAny(c) throws ExecutionException if no task completes
1734       */
1735      public void testTimedInvokeAny4() throws Exception {
1736 <        ExecutorService e =
1736 >        final ExecutorService e =
1737              new ThreadPoolExecutor(2, 2,
1738                                     LONG_DELAY_MS, MILLISECONDS,
1739                                     new ArrayBlockingQueue<Runnable>(10));
1740 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1741 <        l.add(new NPETask());
1742 <        try {
1743 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1744 <            shouldThrow();
1745 <        } catch (ExecutionException success) {
1746 <            assertTrue(success.getCause() instanceof NullPointerException);
1747 <        } finally {
1748 <            joinPool(e);
1740 >        try (PoolCleaner cleaner = cleaner(e)) {
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 >            }
1749          }
1750      }
1751  
# Line 1787 | Line 1753 | public class ThreadPoolExecutorTest exte
1753       * timed invokeAny(c) returns result of some task
1754       */
1755      public void testTimedInvokeAny5() throws Exception {
1756 <        ExecutorService e =
1756 >        final ExecutorService e =
1757              new ThreadPoolExecutor(2, 2,
1758                                     LONG_DELAY_MS, MILLISECONDS,
1759                                     new ArrayBlockingQueue<Runnable>(10));
1760 <        try {
1760 >        try (PoolCleaner cleaner = cleaner(e)) {
1761              List<Callable<String>> l = new ArrayList<Callable<String>>();
1762              l.add(new StringTask());
1763              l.add(new StringTask());
1764              String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1765              assertSame(TEST_STRING, result);
1800        } finally {
1801            joinPool(e);
1766          }
1767      }
1768  
# Line 1806 | Line 1770 | public class ThreadPoolExecutorTest exte
1770       * timed invokeAll(null) throws NPE
1771       */
1772      public void testTimedInvokeAll1() throws Exception {
1773 <        ExecutorService e =
1773 >        final ExecutorService e =
1774              new ThreadPoolExecutor(2, 2,
1775                                     LONG_DELAY_MS, MILLISECONDS,
1776                                     new ArrayBlockingQueue<Runnable>(10));
1777 <        try {
1778 <            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1779 <            shouldThrow();
1780 <        } catch (NullPointerException success) {
1781 <        } finally {
1818 <            joinPool(e);
1777 >        try (PoolCleaner cleaner = cleaner(e)) {
1778 >            try {
1779 >                e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1780 >                shouldThrow();
1781 >            } catch (NullPointerException success) {}
1782          }
1783      }
1784  
# Line 1823 | Line 1786 | public class ThreadPoolExecutorTest exte
1786       * timed invokeAll(,,null) throws NPE
1787       */
1788      public void testTimedInvokeAllNullTimeUnit() throws Exception {
1789 <        ExecutorService e =
1789 >        final ExecutorService e =
1790              new ThreadPoolExecutor(2, 2,
1791                                     LONG_DELAY_MS, MILLISECONDS,
1792                                     new ArrayBlockingQueue<Runnable>(10));
1793 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1794 <        l.add(new StringTask());
1795 <        try {
1796 <            e.invokeAll(l, MEDIUM_DELAY_MS, null);
1797 <            shouldThrow();
1798 <        } catch (NullPointerException success) {
1799 <        } finally {
1837 <            joinPool(e);
1793 >        try (PoolCleaner cleaner = cleaner(e)) {
1794 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1795 >            l.add(new StringTask());
1796 >            try {
1797 >                e.invokeAll(l, MEDIUM_DELAY_MS, null);
1798 >                shouldThrow();
1799 >            } catch (NullPointerException success) {}
1800          }
1801      }
1802  
# Line 1842 | Line 1804 | public class ThreadPoolExecutorTest exte
1804       * timed invokeAll(empty collection) returns empty collection
1805       */
1806      public void testTimedInvokeAll2() throws InterruptedException {
1807 <        ExecutorService e =
1807 >        final ExecutorService e =
1808              new ThreadPoolExecutor(2, 2,
1809                                     LONG_DELAY_MS, MILLISECONDS,
1810                                     new ArrayBlockingQueue<Runnable>(10));
1811 <        try {
1812 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1811 >        try (PoolCleaner cleaner = cleaner(e)) {
1812 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(),
1813 >                                                 MEDIUM_DELAY_MS, MILLISECONDS);
1814              assertTrue(r.isEmpty());
1852        } finally {
1853            joinPool(e);
1815          }
1816      }
1817  
# Line 1858 | Line 1819 | public class ThreadPoolExecutorTest exte
1819       * timed invokeAll(c) throws NPE if c has null elements
1820       */
1821      public void testTimedInvokeAll3() throws Exception {
1822 <        ExecutorService e =
1822 >        final ExecutorService e =
1823              new ThreadPoolExecutor(2, 2,
1824                                     LONG_DELAY_MS, MILLISECONDS,
1825                                     new ArrayBlockingQueue<Runnable>(10));
1826 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1827 <        l.add(new StringTask());
1828 <        l.add(null);
1829 <        try {
1830 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1831 <            shouldThrow();
1832 <        } catch (NullPointerException success) {
1833 <        } finally {
1873 <            joinPool(e);
1826 >        try (PoolCleaner cleaner = cleaner(e)) {
1827 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1828 >            l.add(new StringTask());
1829 >            l.add(null);
1830 >            try {
1831 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1832 >                shouldThrow();
1833 >            } catch (NullPointerException success) {}
1834          }
1835      }
1836  
# Line 1878 | Line 1838 | public class ThreadPoolExecutorTest exte
1838       * get of element of invokeAll(c) throws exception on failed task
1839       */
1840      public void testTimedInvokeAll4() throws Exception {
1841 <        ExecutorService e =
1841 >        final ExecutorService e =
1842              new ThreadPoolExecutor(2, 2,
1843                                     LONG_DELAY_MS, MILLISECONDS,
1844                                     new ArrayBlockingQueue<Runnable>(10));
1845 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1846 <        l.add(new NPETask());
1847 <        List<Future<String>> futures =
1848 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1849 <        assertEquals(1, futures.size());
1850 <        try {
1851 <            futures.get(0).get();
1852 <            shouldThrow();
1853 <        } catch (ExecutionException success) {
1854 <            assertTrue(success.getCause() instanceof NullPointerException);
1855 <        } finally {
1856 <            joinPool(e);
1845 >        try (PoolCleaner cleaner = cleaner(e)) {
1846 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1847 >            l.add(new NPETask());
1848 >            List<Future<String>> futures =
1849 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1850 >            assertEquals(1, futures.size());
1851 >            try {
1852 >                futures.get(0).get();
1853 >                shouldThrow();
1854 >            } catch (ExecutionException success) {
1855 >                assertTrue(success.getCause() instanceof NullPointerException);
1856 >            }
1857          }
1858      }
1859  
# Line 1901 | Line 1861 | public class ThreadPoolExecutorTest exte
1861       * timed invokeAll(c) returns results of all completed tasks
1862       */
1863      public void testTimedInvokeAll5() throws Exception {
1864 <        ExecutorService e =
1864 >        final ExecutorService e =
1865              new ThreadPoolExecutor(2, 2,
1866                                     LONG_DELAY_MS, MILLISECONDS,
1867                                     new ArrayBlockingQueue<Runnable>(10));
1868 <        try {
1868 >        try (PoolCleaner cleaner = cleaner(e)) {
1869              List<Callable<String>> l = new ArrayList<Callable<String>>();
1870              l.add(new StringTask());
1871              l.add(new StringTask());
# Line 1914 | Line 1874 | public class ThreadPoolExecutorTest exte
1874              assertEquals(2, futures.size());
1875              for (Future<String> future : futures)
1876                  assertSame(TEST_STRING, future.get());
1917        } finally {
1918            joinPool(e);
1877          }
1878      }
1879  
# Line 1923 | Line 1881 | public class ThreadPoolExecutorTest exte
1881       * timed invokeAll(c) cancels tasks not completed by timeout
1882       */
1883      public void testTimedInvokeAll6() throws Exception {
1884 <        ExecutorService e =
1884 >        final ExecutorService e =
1885              new ThreadPoolExecutor(2, 2,
1886                                     LONG_DELAY_MS, MILLISECONDS,
1887                                     new ArrayBlockingQueue<Runnable>(10));
1888 <        try {
1888 >        try (PoolCleaner cleaner = cleaner(e)) {
1889              for (long timeout = timeoutMillis();;) {
1890                  List<Callable<String>> tasks = new ArrayList<>();
1891                  tasks.add(new StringTask("0"));
# Line 1951 | Line 1909 | public class ThreadPoolExecutorTest exte
1909                          fail("expected exactly one task to be cancelled");
1910                  }
1911              }
1954        } finally {
1955            joinPool(e);
1912          }
1913      }
1914  
# Line 1966 | Line 1922 | public class ThreadPoolExecutorTest exte
1922                                     LONG_DELAY_MS, MILLISECONDS,
1923                                     new LinkedBlockingQueue<Runnable>(),
1924                                     new FailingThreadFactory());
1925 <        try {
1925 >        try (PoolCleaner cleaner = cleaner(e)) {
1926              final int TASKS = 100;
1927              final CountDownLatch done = new CountDownLatch(TASKS);
1928              for (int k = 0; k < TASKS; ++k)
# Line 1975 | Line 1931 | public class ThreadPoolExecutorTest exte
1931                          done.countDown();
1932                      }});
1933              assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1978        } finally {
1979            joinPool(e);
1934          }
1935      }
1936  
# Line 1988 | Line 1942 | public class ThreadPoolExecutorTest exte
1942              new ThreadPoolExecutor(2, 2,
1943                                     1000, MILLISECONDS,
1944                                     new ArrayBlockingQueue<Runnable>(10));
1945 <        assertFalse(p.allowsCoreThreadTimeOut());
1946 <        joinPool(p);
1945 >        try (PoolCleaner cleaner = cleaner(p)) {
1946 >            assertFalse(p.allowsCoreThreadTimeOut());
1947 >        }
1948      }
1949  
1950      /**
# Line 2001 | Line 1956 | public class ThreadPoolExecutorTest exte
1956              new ThreadPoolExecutor(2, 10,
1957                                     keepAliveTime, MILLISECONDS,
1958                                     new ArrayBlockingQueue<Runnable>(10));
1959 <        final CountDownLatch threadStarted = new CountDownLatch(1);
1960 <        try {
1959 >        try (PoolCleaner cleaner = cleaner(p)) {
1960 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1961              p.allowCoreThreadTimeOut(true);
1962              p.execute(new CheckedRunnable() {
1963                  public void realRun() {
# Line 2017 | Line 1972 | public class ThreadPoolExecutorTest exte
1972                  Thread.yield();
1973              assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1974              assertEquals(0, p.getPoolSize());
2020        } finally {
2021            joinPool(p);
1975          }
1976      }
1977  
# Line 2031 | Line 1984 | public class ThreadPoolExecutorTest exte
1984              new ThreadPoolExecutor(2, 10,
1985                                     keepAliveTime, MILLISECONDS,
1986                                     new ArrayBlockingQueue<Runnable>(10));
1987 <        final CountDownLatch threadStarted = new CountDownLatch(1);
1988 <        try {
1987 >        try (PoolCleaner cleaner = cleaner(p)) {
1988 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1989              p.allowCoreThreadTimeOut(false);
1990              p.execute(new CheckedRunnable() {
1991                  public void realRun() throws InterruptedException {
# Line 2041 | Line 1994 | public class ThreadPoolExecutorTest exte
1994                  }});
1995              delay(2 * keepAliveTime);
1996              assertTrue(p.getPoolSize() >= 1);
2044        } finally {
2045            joinPool(p);
1997          }
1998      }
1999  
# Line 2061 | Line 2012 | public class ThreadPoolExecutorTest exte
2012              new ThreadPoolExecutor(1, 30,
2013                                     60, SECONDS,
2014                                     new ArrayBlockingQueue(30));
2015 <        try {
2015 >        try (PoolCleaner cleaner = cleaner(p)) {
2016              for (int i = 0; i < nTasks; ++i) {
2017                  for (;;) {
2018                      try {
# Line 2073 | Line 2024 | public class ThreadPoolExecutorTest exte
2024              }
2025              // enough time to run all tasks
2026              assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS));
2076        } finally {
2077            joinPool(p);
2027          }
2028      }
2029  
# Line 2086 | Line 2035 | public class ThreadPoolExecutorTest exte
2035              new ThreadPoolExecutor(1, 1,
2036                                     LONG_DELAY_MS, MILLISECONDS,
2037                                     new LinkedBlockingQueue<Runnable>());
2038 <        try {
2038 >        try (PoolCleaner cleaner = cleaner(e)) {
2039              final CountDownLatch blockerStarted = new CountDownLatch(1);
2040              final CountDownLatch done = new CountDownLatch(1);
2041              final List<Future<?>> futures = new ArrayList<>();
# Line 2113 | Line 2062 | public class ThreadPoolExecutorTest exte
2062                  assertTrue(future.isDone());
2063              }
2064              done.countDown();
2116        } finally {
2117            joinPool(e);
2065          }
2066      }
2067  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines