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.76 by jsr166, Sun Oct 4 02:04:56 2015 UTC vs.
Revision 1.100 by jsr166, Mon Oct 5 21:42:49 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(MEDIUM_DELAY_MS, MILLISECONDS));
119              assertEquals(1, p.getActiveCount());
124        } finally {
120              done.countDown();
126            joinPool(p);
121          }
122      }
123  
# Line 366 | 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 379 | 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 389 | 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(MEDIUM_DELAY_MS, MILLISECONDS));
392              assertEquals(1, p.getPoolSize());
393 <        } finally {
395 <            done.countDown();
396 <            joinPool(p);
393 >            done.countDown();   // release pool
394          }
395      }
396  
# Line 401 | 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);
410 <        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();
415                    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 431 | 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 445 | 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 475 | 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(MEDIUM_DELAY_MS, MILLISECONDS));
501              assertFalse(p.isTerminating());
502              done.countDown();
491        } 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          }
494        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
495        assertTrue(p.isTerminated());
508      }
509  
510      /**
# Line 503 | 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 513 | 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(MEDIUM_DELAY_MS, MILLISECONDS));
529              assertFalse(p.isTerminating());
530              done.countDown();
519        } 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          }
522        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
523        assertTrue(p.isTerminated());
524        assertFalse(p.isTerminating());
536      }
537  
538      /**
# Line 533 | 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 548 | 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(MEDIUM_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());
556        } finally {
567              done.countDown();
558            joinPool(p);
568          }
569      }
570  
# Line 568 | 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 580 | 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(MEDIUM_DELAY_MS, MILLISECONDS));
593              assertFalse(p.remove(tasks[0]));
594              assertTrue(q.contains(tasks[4]));
595              assertTrue(q.contains(tasks[3]));
# Line 590 | Line 599 | public class ThreadPoolExecutorTest exte
599              assertTrue(q.contains(tasks[3]));
600              assertTrue(p.remove(tasks[3]));
601              assertFalse(q.contains(tasks[3]));
593        } finally {
602              done.countDown();
595            joinPool(p);
603          }
604      }
605  
# Line 607 | 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)) {
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 619 | 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(MEDIUM_DELAY_MS, MILLISECONDS));
630              assertEquals(tasks.length, p.getTaskCount());
631              assertEquals(tasks.length - 1, q.size());
632              assertEquals(1L, p.getActiveCount());
# Line 632 | 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());
635        } finally {
642              done.countDown();
637            joinPool(p);
643          }
644      }
645  
# Line 650 | Line 655 | public class ThreadPoolExecutorTest exte
655              new ThreadPoolExecutor(poolSize, poolSize,
656                                     LONG_DELAY_MS, MILLISECONDS,
657                                     new ArrayBlockingQueue<Runnable>(10));
658 <        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
658 >        final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
659          Runnable waiter = new CheckedRunnable() { public void realRun() {
660              threadsStarted.countDown();
661              try {
# Line 1027 | Line 1032 | public class ThreadPoolExecutorTest exte
1032                                     60, SECONDS,
1033                                     new ArrayBlockingQueue<Runnable>(10));
1034  
1035 <        final CountDownLatch threadStarted = new CountDownLatch(1);
1036 <        final CountDownLatch done = new CountDownLatch(1);
1037 <        try {
1035 >        try (PoolCleaner cleaner = cleaner(p)) {
1036 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1037 >            final CountDownLatch done = new CountDownLatch(1);
1038              Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1039                  public void realRun() throws Exception {
1040                      Callable task = new CheckedCallable<Boolean>() {
# Line 1041 | Line 1046 | public class ThreadPoolExecutorTest exte
1046                      p.submit(task).get();
1047                  }});
1048  
1049 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
1049 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
1050              t.interrupt();
1051              awaitTermination(t, MEDIUM_DELAY_MS);
1047        } finally {
1052              done.countDown();
1049            joinPool(p);
1053          }
1054      }
1055  
# Line 1054 | Line 1057 | public class ThreadPoolExecutorTest exte
1057       * execute throws RejectedExecutionException if saturated.
1058       */
1059      public void testSaturatedExecute() {
1060 <        ThreadPoolExecutor p =
1060 >        final ThreadPoolExecutor p =
1061              new ThreadPoolExecutor(1, 1,
1062                                     LONG_DELAY_MS, MILLISECONDS,
1063                                     new ArrayBlockingQueue<Runnable>(1));
1064 <        final CountDownLatch done = new CountDownLatch(1);
1065 <        try {
1064 >        try (PoolCleaner cleaner = cleaner(p)) {
1065 >            final CountDownLatch done = new CountDownLatch(1);
1066              Runnable task = new CheckedRunnable() {
1067                  public void realRun() throws InterruptedException {
1068                      done.await();
# Line 1073 | Line 1076 | public class ThreadPoolExecutorTest exte
1076                  } catch (RejectedExecutionException success) {}
1077                  assertTrue(p.getTaskCount() <= 2);
1078              }
1076        } finally {
1079              done.countDown();
1078            joinPool(p);
1080          }
1081      }
1082  
# Line 1083 | Line 1084 | public class ThreadPoolExecutorTest exte
1084       * submit(runnable) throws RejectedExecutionException if saturated.
1085       */
1086      public void testSaturatedSubmitRunnable() {
1087 <        ThreadPoolExecutor p =
1087 >        final ThreadPoolExecutor p =
1088              new ThreadPoolExecutor(1, 1,
1089                                     LONG_DELAY_MS, MILLISECONDS,
1090                                     new ArrayBlockingQueue<Runnable>(1));
1091 <        final CountDownLatch done = new CountDownLatch(1);
1092 <        try {
1091 >        try (PoolCleaner cleaner = cleaner(p)) {
1092 >            final CountDownLatch done = new CountDownLatch(1);
1093              Runnable task = new CheckedRunnable() {
1094                  public void realRun() throws InterruptedException {
1095                      done.await();
# Line 1102 | Line 1103 | public class ThreadPoolExecutorTest exte
1103                  } catch (RejectedExecutionException success) {}
1104                  assertTrue(p.getTaskCount() <= 2);
1105              }
1105        } finally {
1106              done.countDown();
1107            joinPool(p);
1107          }
1108      }
1109  
# Line 1112 | Line 1111 | public class ThreadPoolExecutorTest exte
1111       * submit(callable) throws RejectedExecutionException if saturated.
1112       */
1113      public void testSaturatedSubmitCallable() {
1114 <        ThreadPoolExecutor p =
1114 >        final ThreadPoolExecutor p =
1115              new ThreadPoolExecutor(1, 1,
1116                                     LONG_DELAY_MS, MILLISECONDS,
1117                                     new ArrayBlockingQueue<Runnable>(1));
1118 <        final CountDownLatch done = new CountDownLatch(1);
1119 <        try {
1118 >        try (PoolCleaner cleaner = cleaner(p)) {
1119 >            final CountDownLatch done = new CountDownLatch(1);
1120              Runnable task = new CheckedRunnable() {
1121                  public void realRun() throws InterruptedException {
1122                      done.await();
# Line 1131 | Line 1130 | public class ThreadPoolExecutorTest exte
1130                  } catch (RejectedExecutionException success) {}
1131                  assertTrue(p.getTaskCount() <= 2);
1132              }
1134        } finally {
1133              done.countDown();
1136            joinPool(p);
1134          }
1135      }
1136  
# Line 1141 | Line 1138 | public class ThreadPoolExecutorTest exte
1138       * executor using CallerRunsPolicy runs task if saturated.
1139       */
1140      public void testSaturatedExecute2() {
1144        RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
1141          final ThreadPoolExecutor p =
1142              new ThreadPoolExecutor(1, 1,
1143                                     LONG_DELAY_MS,
1144                                     MILLISECONDS,
1145                                     new ArrayBlockingQueue<Runnable>(1),
1146 <                                   h);
1147 <        try {
1146 >                                   new ThreadPoolExecutor.CallerRunsPolicy());
1147 >        try (PoolCleaner cleaner = cleaner(p)) {
1148 >            final CountDownLatch done = new CountDownLatch(1);
1149 >            Runnable blocker = new CheckedRunnable() {
1150 >                public void realRun() throws InterruptedException {
1151 >                    done.await();
1152 >                }};
1153 >            p.execute(blocker);
1154              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1155 <            for (int i = 0; i < tasks.length; ++i)
1155 >            for (int i = 0; i < tasks.length; i++)
1156                  tasks[i] = new TrackedNoOpRunnable();
1157 <            TrackedLongRunnable mr = new TrackedLongRunnable();
1156 <            p.execute(mr);
1157 <            for (int i = 0; i < tasks.length; ++i)
1157 >            for (int i = 0; i < tasks.length; i++)
1158                  p.execute(tasks[i]);
1159 <            for (int i = 1; i < tasks.length; ++i)
1159 >            for (int i = 1; i < tasks.length; i++)
1160                  assertTrue(tasks[i].done);
1161 <            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1162 <        } finally {
1163 <            joinPool(p);
1161 >            assertFalse(tasks[0].done); // waiting in queue
1162 >            done.countDown();
1163          }
1164      }
1165  
# Line 1168 | Line 1167 | public class ThreadPoolExecutorTest exte
1167       * executor using DiscardPolicy drops task if saturated.
1168       */
1169      public void testSaturatedExecute3() {
1170 <        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
1170 >        final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1171 >        for (int i = 0; i < tasks.length; ++i)
1172 >            tasks[i] = new TrackedNoOpRunnable();
1173          final ThreadPoolExecutor p =
1174              new ThreadPoolExecutor(1, 1,
1175 <                                   LONG_DELAY_MS, MILLISECONDS,
1176 <                                   new ArrayBlockingQueue<Runnable>(1),
1177 <                                   h);
1178 <        try {
1179 <            TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1180 <            for (int i = 0; i < tasks.length; ++i)
1181 <                tasks[i] = new TrackedNoOpRunnable();
1181 <            p.execute(new TrackedLongRunnable());
1175 >                          LONG_DELAY_MS, MILLISECONDS,
1176 >                          new ArrayBlockingQueue<Runnable>(1),
1177 >                          new ThreadPoolExecutor.DiscardPolicy());
1178 >        try (PoolCleaner cleaner = cleaner(p)) {
1179 >            final CountDownLatch done = new CountDownLatch(1);
1180 >            p.execute(awaiter(done));
1181 >
1182              for (TrackedNoOpRunnable task : tasks)
1183                  p.execute(task);
1184 <            for (TrackedNoOpRunnable task : tasks)
1185 <                assertFalse(task.done);
1186 <            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1187 <        } finally {
1188 <            joinPool(p);
1184 >            for (int i = 1; i < tasks.length; i++)
1185 >                assertFalse(tasks[i].done);
1186 >            done.countDown();
1187          }
1188 +        for (int i = 1; i < tasks.length; i++)
1189 +            assertFalse(tasks[i].done);
1190 +        assertTrue(tasks[0].done); // was waiting in queue
1191      }
1192  
1193      /**
1194       * executor using DiscardOldestPolicy drops oldest task if saturated.
1195       */
1196      public void testSaturatedExecute4() {
1197 <        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
1197 >        final CountDownLatch done = new CountDownLatch(1);
1198 >        LatchAwaiter r1 = awaiter(done);
1199 >        LatchAwaiter r2 = awaiter(done);
1200 >        LatchAwaiter r3 = awaiter(done);
1201          final ThreadPoolExecutor p =
1202              new ThreadPoolExecutor(1, 1,
1203                                     LONG_DELAY_MS, MILLISECONDS,
1204                                     new ArrayBlockingQueue<Runnable>(1),
1205 <                                   h);
1206 <        try {
1207 <            p.execute(new TrackedLongRunnable());
1208 <            TrackedLongRunnable r2 = new TrackedLongRunnable();
1205 >                                   new ThreadPoolExecutor.DiscardOldestPolicy());
1206 >        try (PoolCleaner cleaner = cleaner(p)) {
1207 >            assertEquals(LatchAwaiter.NEW, r1.state);
1208 >            assertEquals(LatchAwaiter.NEW, r2.state);
1209 >            assertEquals(LatchAwaiter.NEW, r3.state);
1210 >            p.execute(r1);
1211              p.execute(r2);
1212              assertTrue(p.getQueue().contains(r2));
1207            TrackedNoOpRunnable r3 = new TrackedNoOpRunnable();
1213              p.execute(r3);
1214              assertFalse(p.getQueue().contains(r2));
1215              assertTrue(p.getQueue().contains(r3));
1216 <            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1212 <        } finally {
1213 <            joinPool(p);
1216 >            done.countDown();
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 1242 | 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);
1249        } finally {
1250            joinPool(p);
1255          }
1256      }
1257  
# Line 1255 | 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();
1259 <        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);
1270        } finally {
1271            joinPool(p);
1273          }
1274      }
1275  
# Line 1276 | 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();
1280 <        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);
1291        } finally {
1292            joinPool(p);
1291          }
1292      }
1293  
# Line 1297 | 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 {
1324 <            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          }
1326        joinPool(p);
1324      }
1325  
1326      /**
# Line 1331 | 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 {
1343 <            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          }
1345        joinPool(p);
1341      }
1342  
1343      /**
# Line 1350 | 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 {
1362 <            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          }
1364        joinPool(p);
1358      }
1359  
1360      /**
# Line 1369 | 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          }
1392        joinPool(p);
1387      }
1388  
1389      /**
# Line 1397 | 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 {
1409 <            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          }
1411        joinPool(p);
1404      }
1405  
1406      /**
# Line 1416 | 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 1426 | 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 1436 | Line 1430 | public class ThreadPoolExecutorTest exte
1430              assertEquals(0, done.getCount());
1431              assertTrue(p.afterCalled());
1432              assertTrue(p.beforeCalled());
1439            try { p.shutdown(); } catch (SecurityException ok) { return; }
1440        } finally {
1441            joinPool(p);
1433          }
1434      }
1435  
# Line 1446 | 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);
1457        } finally {
1458            joinPool(e);
1448          }
1449      }
1450  
# Line 1463 | 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());
1474        } finally {
1475            joinPool(e);
1463          }
1464      }
1465  
# Line 1480 | 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);
1491        } finally {
1492            joinPool(e);
1478          }
1479      }
1480  
# Line 1497 | 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 {
1509 <            joinPool(e);
1489 >        try (PoolCleaner cleaner = cleaner(e)) {
1490 >            try {
1491 >                e.invokeAny(null);
1492 >                shouldThrow();
1493 >            } catch (NullPointerException success) {}
1494          }
1495      }
1496  
# Line 1514 | 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 {
1526 <            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 1536 | 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<Callable<String>>();
1524 >            l.add(latchAwaitingStringTask(latch));
1525 >            l.add(null);
1526 >            try {
1527 >                e.invokeAny(l);
1528 >                shouldThrow();
1529 >            } catch (NullPointerException success) {}
1530              latch.countDown();
1548            joinPool(e);
1531          }
1532      }
1533  
# Line 1553 | 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<Callable<String>>();
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 1573 | 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 {
1562 >        try (PoolCleaner cleaner = cleaner(e)) {
1563              List<Callable<String>> l = new ArrayList<Callable<String>>();
1564              l.add(new StringTask());
1565              l.add(new StringTask());
1566              String result = e.invokeAny(l);
1567              assertSame(TEST_STRING, result);
1586        } finally {
1587            joinPool(e);
1568          }
1569      }
1570  
# Line 1592 | 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 {
1604 <            joinPool(e);
1579 >        try (PoolCleaner cleaner = cleaner(e)) {
1580 >            try {
1581 >                e.invokeAll(null);
1582 >                shouldThrow();
1583 >            } catch (NullPointerException success) {}
1584          }
1585      }
1586  
# Line 1609 | 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());
1619        } finally {
1620            joinPool(e);
1598          }
1599      }
1600  
# Line 1625 | 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 {
1640 <            joinPool(e);
1609 >        try (PoolCleaner cleaner = cleaner(e)) {
1610 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
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 1645 | 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 {
1628 >        try (PoolCleaner cleaner = cleaner(e)) {
1629              List<Callable<String>> l = new ArrayList<Callable<String>>();
1630              l.add(new NPETask());
1631              List<Future<String>> futures = e.invokeAll(l);
# Line 1660 | Line 1636 | public class ThreadPoolExecutorTest exte
1636              } catch (ExecutionException success) {
1637                  assertTrue(success.getCause() instanceof NullPointerException);
1638              }
1663        } finally {
1664            joinPool(e);
1639          }
1640      }
1641  
# Line 1669 | 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 {
1650 >        try (PoolCleaner cleaner = cleaner(e)) {
1651              List<Callable<String>> l = new ArrayList<Callable<String>>();
1652              l.add(new StringTask());
1653              l.add(new StringTask());
# Line 1681 | Line 1655 | public class ThreadPoolExecutorTest exte
1655              assertEquals(2, futures.size());
1656              for (Future<String> future : futures)
1657                  assertSame(TEST_STRING, future.get());
1684        } finally {
1685            joinPool(e);
1658          }
1659      }
1660  
# Line 1690 | 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 {
1702 <            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 1707 | 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 {
1721 <            joinPool(e);
1685 >        try (PoolCleaner cleaner = cleaner(e)) {
1686 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
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 1726 | 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 1748 | 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<Callable<String>>();
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();
1760            joinPool(e);
1730          }
1731      }
1732  
# Line 1765 | 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 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1743 >            l.add(new NPETask());
1744 >            try {
1745 >                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1746 >                shouldThrow();
1747 >            } catch (ExecutionException success) {
1748 >                assertTrue(success.getCause() instanceof NullPointerException);
1749 >            }
1750          }
1751      }
1752  
# Line 1785 | Line 1754 | public class ThreadPoolExecutorTest exte
1754       * timed invokeAny(c) returns result of some task
1755       */
1756      public void testTimedInvokeAny5() throws Exception {
1757 <        ExecutorService e =
1757 >        final ExecutorService e =
1758              new ThreadPoolExecutor(2, 2,
1759                                     LONG_DELAY_MS, MILLISECONDS,
1760                                     new ArrayBlockingQueue<Runnable>(10));
1761 <        try {
1761 >        try (PoolCleaner cleaner = cleaner(e)) {
1762              List<Callable<String>> l = new ArrayList<Callable<String>>();
1763              l.add(new StringTask());
1764              l.add(new StringTask());
1765              String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1766              assertSame(TEST_STRING, result);
1798        } finally {
1799            joinPool(e);
1767          }
1768      }
1769  
# Line 1804 | Line 1771 | public class ThreadPoolExecutorTest exte
1771       * timed invokeAll(null) throws NPE
1772       */
1773      public void testTimedInvokeAll1() throws Exception {
1774 <        ExecutorService e =
1774 >        final ExecutorService e =
1775              new ThreadPoolExecutor(2, 2,
1776                                     LONG_DELAY_MS, MILLISECONDS,
1777                                     new ArrayBlockingQueue<Runnable>(10));
1778 <        try {
1779 <            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1780 <            shouldThrow();
1781 <        } catch (NullPointerException success) {
1782 <        } finally {
1816 <            joinPool(e);
1778 >        try (PoolCleaner cleaner = cleaner(e)) {
1779 >            try {
1780 >                e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1781 >                shouldThrow();
1782 >            } catch (NullPointerException success) {}
1783          }
1784      }
1785  
# Line 1821 | Line 1787 | public class ThreadPoolExecutorTest exte
1787       * timed invokeAll(,,null) throws NPE
1788       */
1789      public void testTimedInvokeAllNullTimeUnit() throws Exception {
1790 <        ExecutorService e =
1790 >        final ExecutorService e =
1791              new ThreadPoolExecutor(2, 2,
1792                                     LONG_DELAY_MS, MILLISECONDS,
1793                                     new ArrayBlockingQueue<Runnable>(10));
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 <        } finally {
1835 <            joinPool(e);
1794 >        try (PoolCleaner cleaner = cleaner(e)) {
1795 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1796 >            l.add(new StringTask());
1797 >            try {
1798 >                e.invokeAll(l, MEDIUM_DELAY_MS, null);
1799 >                shouldThrow();
1800 >            } catch (NullPointerException success) {}
1801          }
1802      }
1803  
# Line 1840 | Line 1805 | public class ThreadPoolExecutorTest exte
1805       * timed invokeAll(empty collection) returns empty collection
1806       */
1807      public void testTimedInvokeAll2() throws InterruptedException {
1808 <        ExecutorService e =
1808 >        final ExecutorService e =
1809              new ThreadPoolExecutor(2, 2,
1810                                     LONG_DELAY_MS, MILLISECONDS,
1811                                     new ArrayBlockingQueue<Runnable>(10));
1812 <        try {
1813 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1812 >        try (PoolCleaner cleaner = cleaner(e)) {
1813 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(),
1814 >                                                 MEDIUM_DELAY_MS, MILLISECONDS);
1815              assertTrue(r.isEmpty());
1850        } finally {
1851            joinPool(e);
1816          }
1817      }
1818  
# Line 1856 | Line 1820 | public class ThreadPoolExecutorTest exte
1820       * timed invokeAll(c) throws NPE if c has null elements
1821       */
1822      public void testTimedInvokeAll3() throws Exception {
1823 <        ExecutorService e =
1823 >        final ExecutorService e =
1824              new ThreadPoolExecutor(2, 2,
1825                                     LONG_DELAY_MS, MILLISECONDS,
1826                                     new ArrayBlockingQueue<Runnable>(10));
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 <        } finally {
1871 <            joinPool(e);
1827 >        try (PoolCleaner cleaner = cleaner(e)) {
1828 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1829 >            l.add(new StringTask());
1830 >            l.add(null);
1831 >            try {
1832 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1833 >                shouldThrow();
1834 >            } catch (NullPointerException success) {}
1835          }
1836      }
1837  
# Line 1876 | Line 1839 | public class ThreadPoolExecutorTest exte
1839       * get of element of invokeAll(c) throws exception on failed task
1840       */
1841      public void testTimedInvokeAll4() throws Exception {
1842 <        ExecutorService e =
1842 >        final ExecutorService e =
1843              new ThreadPoolExecutor(2, 2,
1844                                     LONG_DELAY_MS, MILLISECONDS,
1845                                     new ArrayBlockingQueue<Runnable>(10));
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 <        } finally {
1857 <            joinPool(e);
1846 >        try (PoolCleaner cleaner = cleaner(e)) {
1847 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1848 >            l.add(new NPETask());
1849 >            List<Future<String>> futures =
1850 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1851 >            assertEquals(1, futures.size());
1852 >            try {
1853 >                futures.get(0).get();
1854 >                shouldThrow();
1855 >            } catch (ExecutionException success) {
1856 >                assertTrue(success.getCause() instanceof NullPointerException);
1857 >            }
1858          }
1859      }
1860  
# Line 1899 | Line 1862 | public class ThreadPoolExecutorTest exte
1862       * timed invokeAll(c) returns results of all completed tasks
1863       */
1864      public void testTimedInvokeAll5() throws Exception {
1865 <        ExecutorService e =
1865 >        final ExecutorService e =
1866              new ThreadPoolExecutor(2, 2,
1867                                     LONG_DELAY_MS, MILLISECONDS,
1868                                     new ArrayBlockingQueue<Runnable>(10));
1869 <        try {
1869 >        try (PoolCleaner cleaner = cleaner(e)) {
1870              List<Callable<String>> l = new ArrayList<Callable<String>>();
1871              l.add(new StringTask());
1872              l.add(new StringTask());
# Line 1912 | Line 1875 | public class ThreadPoolExecutorTest exte
1875              assertEquals(2, futures.size());
1876              for (Future<String> future : futures)
1877                  assertSame(TEST_STRING, future.get());
1915        } finally {
1916            joinPool(e);
1878          }
1879      }
1880  
# Line 1921 | Line 1882 | public class ThreadPoolExecutorTest exte
1882       * timed invokeAll(c) cancels tasks not completed by timeout
1883       */
1884      public void testTimedInvokeAll6() throws Exception {
1885 <        ExecutorService e =
1885 >        final ExecutorService e =
1886              new ThreadPoolExecutor(2, 2,
1887                                     LONG_DELAY_MS, MILLISECONDS,
1888                                     new ArrayBlockingQueue<Runnable>(10));
1889 <        try {
1889 >        try (PoolCleaner cleaner = cleaner(e)) {
1890              for (long timeout = timeoutMillis();;) {
1891                  List<Callable<String>> tasks = new ArrayList<>();
1892                  tasks.add(new StringTask("0"));
# Line 1949 | Line 1910 | public class ThreadPoolExecutorTest exte
1910                          fail("expected exactly one task to be cancelled");
1911                  }
1912              }
1952        } finally {
1953            joinPool(e);
1913          }
1914      }
1915  
# Line 1964 | Line 1923 | public class ThreadPoolExecutorTest exte
1923                                     LONG_DELAY_MS, MILLISECONDS,
1924                                     new LinkedBlockingQueue<Runnable>(),
1925                                     new FailingThreadFactory());
1926 <        try {
1926 >        try (PoolCleaner cleaner = cleaner(e)) {
1927              final int TASKS = 100;
1928              final CountDownLatch done = new CountDownLatch(TASKS);
1929              for (int k = 0; k < TASKS; ++k)
# Line 1973 | Line 1932 | public class ThreadPoolExecutorTest exte
1932                          done.countDown();
1933                      }});
1934              assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1976        } finally {
1977            joinPool(e);
1935          }
1936      }
1937  
# Line 1986 | Line 1943 | public class ThreadPoolExecutorTest exte
1943              new ThreadPoolExecutor(2, 2,
1944                                     1000, MILLISECONDS,
1945                                     new ArrayBlockingQueue<Runnable>(10));
1946 <        assertFalse(p.allowsCoreThreadTimeOut());
1947 <        joinPool(p);
1946 >        try (PoolCleaner cleaner = cleaner(p)) {
1947 >            assertFalse(p.allowsCoreThreadTimeOut());
1948 >        }
1949      }
1950  
1951      /**
# Line 1999 | Line 1957 | public class ThreadPoolExecutorTest exte
1957              new ThreadPoolExecutor(2, 10,
1958                                     keepAliveTime, MILLISECONDS,
1959                                     new ArrayBlockingQueue<Runnable>(10));
1960 <        final CountDownLatch threadStarted = new CountDownLatch(1);
1961 <        try {
1960 >        try (PoolCleaner cleaner = cleaner(p)) {
1961 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1962              p.allowCoreThreadTimeOut(true);
1963              p.execute(new CheckedRunnable() {
1964                  public void realRun() {
# Line 2015 | Line 1973 | public class ThreadPoolExecutorTest exte
1973                  Thread.yield();
1974              assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1975              assertEquals(0, p.getPoolSize());
2018        } finally {
2019            joinPool(p);
1976          }
1977      }
1978  
# Line 2029 | Line 1985 | public class ThreadPoolExecutorTest exte
1985              new ThreadPoolExecutor(2, 10,
1986                                     keepAliveTime, MILLISECONDS,
1987                                     new ArrayBlockingQueue<Runnable>(10));
1988 <        final CountDownLatch threadStarted = new CountDownLatch(1);
1989 <        try {
1988 >        try (PoolCleaner cleaner = cleaner(p)) {
1989 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1990              p.allowCoreThreadTimeOut(false);
1991              p.execute(new CheckedRunnable() {
1992                  public void realRun() throws InterruptedException {
# Line 2039 | Line 1995 | public class ThreadPoolExecutorTest exte
1995                  }});
1996              delay(2 * keepAliveTime);
1997              assertTrue(p.getPoolSize() >= 1);
2042        } finally {
2043            joinPool(p);
1998          }
1999      }
2000  
# Line 2059 | Line 2013 | public class ThreadPoolExecutorTest exte
2013              new ThreadPoolExecutor(1, 30,
2014                                     60, SECONDS,
2015                                     new ArrayBlockingQueue(30));
2016 <        try {
2016 >        try (PoolCleaner cleaner = cleaner(p)) {
2017              for (int i = 0; i < nTasks; ++i) {
2018                  for (;;) {
2019                      try {
# Line 2071 | Line 2025 | public class ThreadPoolExecutorTest exte
2025              }
2026              // enough time to run all tasks
2027              assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS));
2074        } finally {
2075            joinPool(p);
2028          }
2029      }
2030  
# Line 2084 | Line 2036 | public class ThreadPoolExecutorTest exte
2036              new ThreadPoolExecutor(1, 1,
2037                                     LONG_DELAY_MS, MILLISECONDS,
2038                                     new LinkedBlockingQueue<Runnable>());
2039 <        try {
2039 >        try (PoolCleaner cleaner = cleaner(e)) {
2040              final CountDownLatch blockerStarted = new CountDownLatch(1);
2041              final CountDownLatch done = new CountDownLatch(1);
2042              final List<Future<?>> futures = new ArrayList<>();
# Line 2111 | Line 2063 | public class ThreadPoolExecutorTest exte
2063                  assertTrue(future.isDone());
2064              }
2065              done.countDown();
2114        } finally {
2115            joinPool(e);
2066          }
2067      }
2068  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines