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

Comparing jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java (file contents):
Revision 1.69 by jsr166, Sun Oct 4 02:29:46 2015 UTC vs.
Revision 1.88 by jsr166, Tue Oct 6 00:41:47 2015 UTC

# Line 262 | Line 262 | public class ThreadPoolExecutorSubclassT
262                      assertEquals(1, p.getActiveCount());
263                      done.await();
264                  }});
265 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
265 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
266              assertEquals(1, p.getActiveCount());
267              done.countDown();
268          }
# Line 272 | Line 272 | public class ThreadPoolExecutorSubclassT
272       * prestartCoreThread starts a thread if under corePoolSize, else doesn't
273       */
274      public void testPrestartCoreThread() {
275 <        ThreadPoolExecutor p =
275 >        final ThreadPoolExecutor p =
276              new CustomTPE(2, 6,
277                            LONG_DELAY_MS, MILLISECONDS,
278                            new ArrayBlockingQueue<Runnable>(10));
# Line 298 | Line 298 | public class ThreadPoolExecutorSubclassT
298       * prestartAllCoreThreads starts all corePoolSize threads
299       */
300      public void testPrestartAllCoreThreads() {
301 <        ThreadPoolExecutor p =
301 >        final ThreadPoolExecutor p =
302              new CustomTPE(2, 6,
303                            LONG_DELAY_MS, MILLISECONDS,
304                            new ArrayBlockingQueue<Runnable>(10));
# Line 476 | Line 476 | public class ThreadPoolExecutorSubclassT
476       */
477      public void testGetLargestPoolSize() throws InterruptedException {
478          final int THREADS = 3;
479 +        final CountDownLatch done = new CountDownLatch(1);
480          final ThreadPoolExecutor p =
481              new CustomTPE(THREADS, THREADS,
482                            LONG_DELAY_MS, MILLISECONDS,
483                            new ArrayBlockingQueue<Runnable>(10));
484 <        try (PoolCleaner cleaner = cleaner(p)) {
484 <            final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
485 <            final CountDownLatch done = new CountDownLatch(1);
484 >        try (PoolCleaner cleaner = cleaner(p, done)) {
485              assertEquals(0, p.getLargestPoolSize());
486 +            final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
487              for (int i = 0; i < THREADS; i++)
488                  p.execute(new CheckedRunnable() {
489                      public void realRun() throws InterruptedException {
# Line 491 | Line 491 | public class ThreadPoolExecutorSubclassT
491                          done.await();
492                          assertEquals(THREADS, p.getLargestPoolSize());
493                      }});
494 <            assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
494 >            assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
495              assertEquals(THREADS, p.getLargestPoolSize());
496            done.countDown();   // release pool
496          }
497          assertEquals(THREADS, p.getLargestPoolSize());
498      }
# Line 521 | Line 520 | public class ThreadPoolExecutorSubclassT
520       * become active
521       */
522      public void testGetPoolSize() throws InterruptedException {
523 +        final CountDownLatch done = new CountDownLatch(1);
524          final ThreadPoolExecutor p =
525              new CustomTPE(1, 1,
526                            LONG_DELAY_MS, MILLISECONDS,
527                            new ArrayBlockingQueue<Runnable>(10));
528 <        try (PoolCleaner cleaner = cleaner(p)) {
529 <            final CountDownLatch threadStarted = new CountDownLatch(1);
530 <            final CountDownLatch done = new CountDownLatch(1);
528 >        try (PoolCleaner cleaner = cleaner(p, done)) {
529              assertEquals(0, p.getPoolSize());
530 +            final CountDownLatch threadStarted = new CountDownLatch(1);
531              p.execute(new CheckedRunnable() {
532                  public void realRun() throws InterruptedException {
533                      threadStarted.countDown();
534                      assertEquals(1, p.getPoolSize());
535                      done.await();
536                  }});
537 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
537 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
538              assertEquals(1, p.getPoolSize());
540            done.countDown();   // release pool
539          }
540      }
541  
# Line 545 | Line 543 | public class ThreadPoolExecutorSubclassT
543       * getTaskCount increases, but doesn't overestimate, when tasks submitted
544       */
545      public void testGetTaskCount() throws InterruptedException {
546 +        final int TASKS = 3;
547 +        final CountDownLatch done = new CountDownLatch(1);
548          final ThreadPoolExecutor p =
549              new CustomTPE(1, 1,
550                            LONG_DELAY_MS, MILLISECONDS,
551                            new ArrayBlockingQueue<Runnable>(10));
552 <        try (PoolCleaner cleaner = cleaner(p)) {
552 >        try (PoolCleaner cleaner = cleaner(p, done)) {
553              final CountDownLatch threadStarted = new CountDownLatch(1);
554            final CountDownLatch done = new CountDownLatch(1);
554              assertEquals(0, p.getTaskCount());
555 +            assertEquals(0, p.getCompletedTaskCount());
556              p.execute(new CheckedRunnable() {
557                  public void realRun() throws InterruptedException {
558                      threadStarted.countDown();
559                    assertEquals(1, p.getTaskCount());
559                      done.await();
560                  }});
561 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
561 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
562              assertEquals(1, p.getTaskCount());
563 <            done.countDown();
563 >            assertEquals(0, p.getCompletedTaskCount());
564 >            for (int i = 0; i < TASKS; i++) {
565 >                assertEquals(1 + i, p.getTaskCount());
566 >                p.execute(new CheckedRunnable() {
567 >                    public void realRun() throws InterruptedException {
568 >                        threadStarted.countDown();
569 >                        assertEquals(1 + TASKS, p.getTaskCount());
570 >                        done.await();
571 >                    }});
572 >            }
573 >            assertEquals(1 + TASKS, p.getTaskCount());
574 >            assertEquals(0, p.getCompletedTaskCount());
575          }
576 +        assertEquals(1 + TASKS, p.getTaskCount());
577 +        assertEquals(1 + TASKS, p.getCompletedTaskCount());
578      }
579  
580      /**
# Line 598 | Line 610 | public class ThreadPoolExecutorSubclassT
610                      threadStarted.countDown();
611                      done.await();
612                  }});
613 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
613 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
614              assertFalse(p.isTerminating());
615              done.countDown();
616              try { p.shutdown(); } catch (SecurityException ok) { return; }
# Line 616 | Line 628 | public class ThreadPoolExecutorSubclassT
628              new CustomTPE(1, 1,
629                            LONG_DELAY_MS, MILLISECONDS,
630                            new ArrayBlockingQueue<Runnable>(10));
631 <        final CountDownLatch threadStarted = new CountDownLatch(1);
632 <        final CountDownLatch done = new CountDownLatch(1);
633 <        try {
631 >        try (PoolCleaner cleaner = cleaner(p)) {
632 >            final CountDownLatch threadStarted = new CountDownLatch(1);
633 >            final CountDownLatch done = new CountDownLatch(1);
634              assertFalse(p.isTerminating());
635              p.execute(new CheckedRunnable() {
636                  public void realRun() throws InterruptedException {
# Line 626 | Line 638 | public class ThreadPoolExecutorSubclassT
638                      threadStarted.countDown();
639                      done.await();
640                  }});
641 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
641 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
642              assertFalse(p.isTerminating());
643              done.countDown();
632        } finally {
644              try { p.shutdown(); } catch (SecurityException ok) { return; }
645 +            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
646 +            assertTrue(p.isTerminated());
647 +            assertFalse(p.isTerminating());
648          }
635        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
636        assertTrue(p.isTerminated());
637        assertFalse(p.isTerminating());
649      }
650  
651      /**
# Line 646 | Line 657 | public class ThreadPoolExecutorSubclassT
657              new CustomTPE(1, 1,
658                            LONG_DELAY_MS, MILLISECONDS,
659                            q);
660 <        final CountDownLatch threadStarted = new CountDownLatch(1);
661 <        final CountDownLatch done = new CountDownLatch(1);
662 <        try {
660 >        try (PoolCleaner cleaner = cleaner(p)) {
661 >            final CountDownLatch threadStarted = new CountDownLatch(1);
662 >            final CountDownLatch done = new CountDownLatch(1);
663              FutureTask[] tasks = new FutureTask[5];
664              for (int i = 0; i < tasks.length; i++) {
665                  Callable task = new CheckedCallable<Boolean>() {
# Line 661 | Line 672 | public class ThreadPoolExecutorSubclassT
672                  tasks[i] = new FutureTask(task);
673                  p.execute(tasks[i]);
674              }
675 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
675 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
676              assertSame(q, p.getQueue());
677              assertFalse(q.contains(tasks[0]));
678              assertTrue(q.contains(tasks[tasks.length - 1]));
679              assertEquals(tasks.length - 1, q.size());
669        } finally {
680              done.countDown();
671            joinPool(p);
681          }
682      }
683  
# Line 681 | Line 690 | public class ThreadPoolExecutorSubclassT
690              new CustomTPE(1, 1,
691                            LONG_DELAY_MS, MILLISECONDS,
692                            q);
693 <        Runnable[] tasks = new Runnable[6];
694 <        final CountDownLatch threadStarted = new CountDownLatch(1);
695 <        final CountDownLatch done = new CountDownLatch(1);
696 <        try {
693 >        try (PoolCleaner cleaner = cleaner(p)) {
694 >            Runnable[] tasks = new Runnable[6];
695 >            final CountDownLatch threadStarted = new CountDownLatch(1);
696 >            final CountDownLatch done = new CountDownLatch(1);
697              for (int i = 0; i < tasks.length; i++) {
698                  tasks[i] = new CheckedRunnable() {
699 <                        public void realRun() throws InterruptedException {
700 <                            threadStarted.countDown();
701 <                            done.await();
702 <                        }};
699 >                    public void realRun() throws InterruptedException {
700 >                        threadStarted.countDown();
701 >                        done.await();
702 >                    }};
703                  p.execute(tasks[i]);
704              }
705 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
705 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
706              assertFalse(p.remove(tasks[0]));
707              assertTrue(q.contains(tasks[4]));
708              assertTrue(q.contains(tasks[3]));
# Line 703 | Line 712 | public class ThreadPoolExecutorSubclassT
712              assertTrue(q.contains(tasks[3]));
713              assertTrue(p.remove(tasks[3]));
714              assertFalse(q.contains(tasks[3]));
706        } finally {
715              done.countDown();
708            joinPool(p);
716          }
717      }
718  
# Line 720 | Line 727 | public class ThreadPoolExecutorSubclassT
727              new CustomTPE(1, 1,
728                            LONG_DELAY_MS, MILLISECONDS,
729                            q);
730 <        FutureTask[] tasks = new FutureTask[5];
731 <        try {
730 >        try (PoolCleaner cleaner = cleaner(p, done)) {
731 >            FutureTask[] tasks = new FutureTask[5];
732              for (int i = 0; i < tasks.length; i++) {
733                  Callable task = new CheckedCallable<Boolean>() {
734                      public Boolean realCall() throws InterruptedException {
# Line 732 | Line 739 | public class ThreadPoolExecutorSubclassT
739                  tasks[i] = new FutureTask(task);
740                  p.execute(tasks[i]);
741              }
742 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
742 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
743              assertEquals(tasks.length, p.getTaskCount());
744              assertEquals(tasks.length - 1, q.size());
745              assertEquals(1L, p.getActiveCount());
# Line 745 | Line 752 | public class ThreadPoolExecutorSubclassT
752              p.purge();         // Nothing to do
753              assertEquals(tasks.length - 3, q.size());
754              assertEquals(tasks.length - 2, p.getTaskCount());
748        } finally {
749            done.countDown();
750            joinPool(p);
755          }
756      }
757  
# Line 759 | Line 763 | public class ThreadPoolExecutorSubclassT
763          final int poolSize = 2;
764          final int count = 5;
765          final AtomicInteger ran = new AtomicInteger(0);
766 <        ThreadPoolExecutor p =
767 <            new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS,
766 >        final ThreadPoolExecutor p =
767 >            new CustomTPE(poolSize, poolSize,
768 >                          LONG_DELAY_MS, MILLISECONDS,
769                            new ArrayBlockingQueue<Runnable>(10));
770 <        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
770 >        final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
771          Runnable waiter = new CheckedRunnable() { public void realRun() {
772              threadsStarted.countDown();
773              try {
# Line 1131 | Line 1136 | public class ThreadPoolExecutorSubclassT
1136       * execute throws RejectedExecutionException if saturated.
1137       */
1138      public void testSaturatedExecute() {
1139 <        ThreadPoolExecutor p =
1139 >        final ThreadPoolExecutor p =
1140              new CustomTPE(1, 1,
1141                            LONG_DELAY_MS, MILLISECONDS,
1142                            new ArrayBlockingQueue<Runnable>(1));
1143 <        final CountDownLatch done = new CountDownLatch(1);
1144 <        try {
1143 >        try (PoolCleaner cleaner = cleaner(p)) {
1144 >            final CountDownLatch done = new CountDownLatch(1);
1145              Runnable task = new CheckedRunnable() {
1146                  public void realRun() throws InterruptedException {
1147                      done.await();
# Line 1150 | Line 1155 | public class ThreadPoolExecutorSubclassT
1155                  } catch (RejectedExecutionException success) {}
1156                  assertTrue(p.getTaskCount() <= 2);
1157              }
1153        } finally {
1158              done.countDown();
1155            joinPool(p);
1159          }
1160      }
1161  
# Line 1160 | Line 1163 | public class ThreadPoolExecutorSubclassT
1163       * executor using CallerRunsPolicy runs task if saturated.
1164       */
1165      public void testSaturatedExecute2() {
1166 <        RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1167 <        ThreadPoolExecutor p = new CustomTPE(1, 1,
1168 <                                             LONG_DELAY_MS, MILLISECONDS,
1169 <                                             new ArrayBlockingQueue<Runnable>(1),
1170 <                                             h);
1171 <        try {
1166 >        final ThreadPoolExecutor p =
1167 >            new CustomTPE(1, 1,
1168 >                          LONG_DELAY_MS, MILLISECONDS,
1169 >                          new ArrayBlockingQueue<Runnable>(1),
1170 >                          new CustomTPE.CallerRunsPolicy());
1171 >        try (PoolCleaner cleaner = cleaner(p)) {
1172 >            final CountDownLatch done = new CountDownLatch(1);
1173 >            Runnable blocker = new CheckedRunnable() {
1174 >                public void realRun() throws InterruptedException {
1175 >                    done.await();
1176 >                }};
1177 >            p.execute(blocker);
1178              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1179 <            for (int i = 0; i < tasks.length; ++i)
1179 >            for (int i = 0; i < tasks.length; i++)
1180                  tasks[i] = new TrackedNoOpRunnable();
1181 <            TrackedLongRunnable mr = new TrackedLongRunnable();
1173 <            p.execute(mr);
1174 <            for (int i = 0; i < tasks.length; ++i)
1181 >            for (int i = 0; i < tasks.length; i++)
1182                  p.execute(tasks[i]);
1183 <            for (int i = 1; i < tasks.length; ++i)
1183 >            for (int i = 1; i < tasks.length; i++)
1184                  assertTrue(tasks[i].done);
1185 <            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1186 <        } finally {
1180 <            joinPool(p);
1185 >            assertFalse(tasks[0].done); // waiting in queue
1186 >            done.countDown();
1187          }
1188      }
1189  
# Line 1185 | Line 1191 | public class ThreadPoolExecutorSubclassT
1191       * executor using DiscardPolicy drops task if saturated.
1192       */
1193      public void testSaturatedExecute3() {
1194 <        RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1195 <        ThreadPoolExecutor p =
1194 >        final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1195 >        for (int i = 0; i < tasks.length; ++i)
1196 >            tasks[i] = new TrackedNoOpRunnable();
1197 >        final ThreadPoolExecutor p =
1198              new CustomTPE(1, 1,
1199                            LONG_DELAY_MS, MILLISECONDS,
1200                            new ArrayBlockingQueue<Runnable>(1),
1201 <                          h);
1202 <        try {
1203 <            TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1204 <            for (int i = 0; i < tasks.length; ++i)
1205 <                tasks[i] = new TrackedNoOpRunnable();
1198 <            p.execute(new TrackedLongRunnable());
1201 >                          new CustomTPE.DiscardPolicy());
1202 >        try (PoolCleaner cleaner = cleaner(p)) {
1203 >            final CountDownLatch done = new CountDownLatch(1);
1204 >            p.execute(awaiter(done));
1205 >
1206              for (TrackedNoOpRunnable task : tasks)
1207                  p.execute(task);
1208 <            for (TrackedNoOpRunnable task : tasks)
1209 <                assertFalse(task.done);
1210 <            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1204 <        } finally {
1205 <            joinPool(p);
1208 >            for (int i = 1; i < tasks.length; i++)
1209 >                assertFalse(tasks[i].done);
1210 >            done.countDown();
1211          }
1212 +        for (int i = 1; i < tasks.length; i++)
1213 +            assertFalse(tasks[i].done);
1214 +        assertTrue(tasks[0].done); // was waiting in queue
1215      }
1216  
1217      /**
1218       * executor using DiscardOldestPolicy drops oldest task if saturated.
1219       */
1220      public void testSaturatedExecute4() {
1221 <        RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
1222 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1223 <        try {
1224 <            p.execute(new TrackedLongRunnable());
1225 <            TrackedLongRunnable r2 = new TrackedLongRunnable();
1221 >        final CountDownLatch done = new CountDownLatch(1);
1222 >        LatchAwaiter r1 = awaiter(done);
1223 >        LatchAwaiter r2 = awaiter(done);
1224 >        LatchAwaiter r3 = awaiter(done);
1225 >        final ThreadPoolExecutor p =
1226 >            new CustomTPE(1, 1,
1227 >                          LONG_DELAY_MS, MILLISECONDS,
1228 >                          new ArrayBlockingQueue<Runnable>(1),
1229 >                          new CustomTPE.DiscardOldestPolicy());
1230 >        try (PoolCleaner cleaner = cleaner(p)) {
1231 >            assertEquals(LatchAwaiter.NEW, r1.state);
1232 >            assertEquals(LatchAwaiter.NEW, r2.state);
1233 >            assertEquals(LatchAwaiter.NEW, r3.state);
1234 >            p.execute(r1);
1235              p.execute(r2);
1236              assertTrue(p.getQueue().contains(r2));
1220            TrackedNoOpRunnable r3 = new TrackedNoOpRunnable();
1237              p.execute(r3);
1238              assertFalse(p.getQueue().contains(r2));
1239              assertTrue(p.getQueue().contains(r3));
1240 <            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1225 <        } finally {
1226 <            joinPool(p);
1240 >            done.countDown();
1241          }
1242 +        assertEquals(LatchAwaiter.DONE, r1.state);
1243 +        assertEquals(LatchAwaiter.NEW, r2.state);
1244 +        assertEquals(LatchAwaiter.DONE, r3.state);
1245      }
1246  
1247      /**
1248       * execute throws RejectedExecutionException if shutdown
1249       */
1250      public void testRejectedExecutionExceptionOnShutdown() {
1251 <        ThreadPoolExecutor p =
1252 <            new CustomTPE(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
1251 >        final ThreadPoolExecutor p =
1252 >            new CustomTPE(1, 1,
1253 >                          LONG_DELAY_MS, MILLISECONDS,
1254 >                          new ArrayBlockingQueue<Runnable>(1));
1255          try { p.shutdown(); } catch (SecurityException ok) { return; }
1256 <        try {
1257 <            p.execute(new NoOpRunnable());
1258 <            shouldThrow();
1259 <        } catch (RejectedExecutionException success) {}
1260 <
1261 <        joinPool(p);
1256 >        try (PoolCleaner cleaner = cleaner(p)) {
1257 >            try {
1258 >                p.execute(new NoOpRunnable());
1259 >                shouldThrow();
1260 >            } catch (RejectedExecutionException success) {}
1261 >        }
1262      }
1263  
1264      /**
1265       * execute using CallerRunsPolicy drops task on shutdown
1266       */
1267      public void testCallerRunsOnShutdown() {
1268 <        RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1269 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1270 <
1268 >        final ThreadPoolExecutor p =
1269 >            new CustomTPE(1, 1,
1270 >                          LONG_DELAY_MS, MILLISECONDS,
1271 >                          new ArrayBlockingQueue<Runnable>(1),
1272 >                          new CustomTPE.CallerRunsPolicy());
1273          try { p.shutdown(); } catch (SecurityException ok) { return; }
1274 <        try {
1274 >        try (PoolCleaner cleaner = cleaner(p)) {
1275              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1276              p.execute(r);
1277              assertFalse(r.done);
1257        } finally {
1258            joinPool(p);
1278          }
1279      }
1280  
# Line 1263 | Line 1282 | public class ThreadPoolExecutorSubclassT
1282       * execute using DiscardPolicy drops task on shutdown
1283       */
1284      public void testDiscardOnShutdown() {
1285 <        RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1286 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1287 <
1285 >        final ThreadPoolExecutor p =
1286 >            new CustomTPE(1, 1,
1287 >                          LONG_DELAY_MS, MILLISECONDS,
1288 >                          new ArrayBlockingQueue<Runnable>(1),
1289 >                          new CustomTPE.DiscardPolicy());
1290          try { p.shutdown(); } catch (SecurityException ok) { return; }
1291 <        try {
1291 >        try (PoolCleaner cleaner = cleaner(p)) {
1292              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1293              p.execute(r);
1294              assertFalse(r.done);
1274        } finally {
1275            joinPool(p);
1295          }
1296      }
1297  
# Line 1280 | Line 1299 | public class ThreadPoolExecutorSubclassT
1299       * execute using DiscardOldestPolicy drops task on shutdown
1300       */
1301      public void testDiscardOldestOnShutdown() {
1302 <        RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
1303 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1302 >        final ThreadPoolExecutor p =
1303 >            new CustomTPE(1, 1,
1304 >                          LONG_DELAY_MS, MILLISECONDS,
1305 >                          new ArrayBlockingQueue<Runnable>(1),
1306 >                          new CustomTPE.DiscardOldestPolicy());
1307  
1308          try { p.shutdown(); } catch (SecurityException ok) { return; }
1309 <        try {
1309 >        try (PoolCleaner cleaner = cleaner(p)) {
1310              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1311              p.execute(r);
1312              assertFalse(r.done);
1291        } finally {
1292            joinPool(p);
1313          }
1314      }
1315  
# Line 1297 | Line 1317 | public class ThreadPoolExecutorSubclassT
1317       * execute(null) throws NPE
1318       */
1319      public void testExecuteNull() {
1320 <        ThreadPoolExecutor p =
1321 <            new CustomTPE(1, 2, 1L, SECONDS,
1320 >        final ThreadPoolExecutor p =
1321 >            new CustomTPE(1, 2,
1322 >                          1L, SECONDS,
1323                            new ArrayBlockingQueue<Runnable>(10));
1324 <        try {
1325 <            p.execute(null);
1326 <            shouldThrow();
1327 <        } catch (NullPointerException success) {}
1328 <
1329 <        joinPool(p);
1324 >        try (PoolCleaner cleaner = cleaner(p)) {
1325 >            try {
1326 >                p.execute(null);
1327 >                shouldThrow();
1328 >            } catch (NullPointerException success) {}
1329 >        }
1330      }
1331  
1332      /**
1333       * setCorePoolSize of negative value throws IllegalArgumentException
1334       */
1335      public void testCorePoolSizeIllegalArgumentException() {
1336 <        ThreadPoolExecutor p =
1337 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1338 <        try {
1339 <            p.setCorePoolSize(-1);
1340 <            shouldThrow();
1341 <        } catch (IllegalArgumentException success) {
1342 <        } finally {
1343 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1336 >        final ThreadPoolExecutor p =
1337 >            new CustomTPE(1, 2,
1338 >                          LONG_DELAY_MS, MILLISECONDS,
1339 >                          new ArrayBlockingQueue<Runnable>(10));
1340 >        try (PoolCleaner cleaner = cleaner(p)) {
1341 >            try {
1342 >                p.setCorePoolSize(-1);
1343 >                shouldThrow();
1344 >            } catch (IllegalArgumentException success) {}
1345          }
1324        joinPool(p);
1346      }
1347  
1348      /**
# Line 1329 | Line 1350 | public class ThreadPoolExecutorSubclassT
1350       * if given a value less the core pool size
1351       */
1352      public void testMaximumPoolSizeIllegalArgumentException() {
1353 <        ThreadPoolExecutor p =
1354 <            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1355 <        try {
1356 <            p.setMaximumPoolSize(1);
1357 <            shouldThrow();
1358 <        } catch (IllegalArgumentException success) {
1359 <        } finally {
1360 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1353 >        final ThreadPoolExecutor p =
1354 >            new CustomTPE(2, 3,
1355 >                          LONG_DELAY_MS, MILLISECONDS,
1356 >                          new ArrayBlockingQueue<Runnable>(10));
1357 >        try (PoolCleaner cleaner = cleaner(p)) {
1358 >            try {
1359 >                p.setMaximumPoolSize(1);
1360 >                shouldThrow();
1361 >            } catch (IllegalArgumentException success) {}
1362          }
1341        joinPool(p);
1363      }
1364  
1365      /**
# Line 1346 | Line 1367 | public class ThreadPoolExecutorSubclassT
1367       * if given a negative value
1368       */
1369      public void testMaximumPoolSizeIllegalArgumentException2() {
1370 <        ThreadPoolExecutor p =
1371 <            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1372 <        try {
1373 <            p.setMaximumPoolSize(-1);
1374 <            shouldThrow();
1375 <        } catch (IllegalArgumentException success) {
1376 <        } finally {
1377 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1370 >        final ThreadPoolExecutor p =
1371 >            new CustomTPE(2, 3,
1372 >                          LONG_DELAY_MS,
1373 >                          MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1374 >        try (PoolCleaner cleaner = cleaner(p)) {
1375 >            try {
1376 >                p.setMaximumPoolSize(-1);
1377 >                shouldThrow();
1378 >            } catch (IllegalArgumentException success) {}
1379          }
1358        joinPool(p);
1380      }
1381  
1382      /**
# Line 1363 | Line 1384 | public class ThreadPoolExecutorSubclassT
1384       * when given a negative value
1385       */
1386      public void testKeepAliveTimeIllegalArgumentException() {
1387 <        ThreadPoolExecutor p =
1388 <            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1389 <
1390 <        try {
1391 <            p.setKeepAliveTime(-1,MILLISECONDS);
1392 <            shouldThrow();
1393 <        } catch (IllegalArgumentException success) {
1394 <        } finally {
1395 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1387 >        final ThreadPoolExecutor p =
1388 >            new CustomTPE(2, 3,
1389 >                          LONG_DELAY_MS, MILLISECONDS,
1390 >                          new ArrayBlockingQueue<Runnable>(10));
1391 >        try (PoolCleaner cleaner = cleaner(p)) {
1392 >            try {
1393 >                p.setKeepAliveTime(-1, MILLISECONDS);
1394 >                shouldThrow();
1395 >            } catch (IllegalArgumentException success) {}
1396          }
1376        joinPool(p);
1397      }
1398  
1399      /**
# Line 1381 | Line 1401 | public class ThreadPoolExecutorSubclassT
1401       */
1402      public void testTerminated() {
1403          CustomTPE p = new CustomTPE();
1404 <        try { p.shutdown(); } catch (SecurityException ok) { return; }
1405 <        assertTrue(p.terminatedCalled());
1406 <        joinPool(p);
1404 >        try (PoolCleaner cleaner = cleaner(p)) {
1405 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1406 >            assertTrue(p.terminatedCalled());
1407 >            assertTrue(p.isShutdown());
1408 >        }
1409      }
1410  
1411      /**
# Line 1391 | Line 1413 | public class ThreadPoolExecutorSubclassT
1413       */
1414      public void testBeforeAfter() throws InterruptedException {
1415          CustomTPE p = new CustomTPE();
1416 <        try {
1416 >        try (PoolCleaner cleaner = cleaner(p)) {
1417              final CountDownLatch done = new CountDownLatch(1);
1418              p.execute(new CheckedRunnable() {
1419                  public void realRun() {
# Line 1401 | Line 1423 | public class ThreadPoolExecutorSubclassT
1423              assertEquals(0, done.getCount());
1424              assertTrue(p.afterCalled());
1425              assertTrue(p.beforeCalled());
1404            try { p.shutdown(); } catch (SecurityException ok) { return; }
1405        } finally {
1406            joinPool(p);
1426          }
1427      }
1428  
# Line 1411 | Line 1430 | public class ThreadPoolExecutorSubclassT
1430       * completed submit of callable returns result
1431       */
1432      public void testSubmitCallable() throws Exception {
1433 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1434 <        try {
1433 >        final ExecutorService e =
1434 >            new CustomTPE(2, 2,
1435 >                          LONG_DELAY_MS, MILLISECONDS,
1436 >                          new ArrayBlockingQueue<Runnable>(10));
1437 >        try (PoolCleaner cleaner = cleaner(e)) {
1438              Future<String> future = e.submit(new StringTask());
1439              String result = future.get();
1440              assertSame(TEST_STRING, result);
1419        } finally {
1420            joinPool(e);
1441          }
1442      }
1443  
# Line 1425 | Line 1445 | public class ThreadPoolExecutorSubclassT
1445       * completed submit of runnable returns successfully
1446       */
1447      public void testSubmitRunnable() throws Exception {
1448 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1449 <        try {
1448 >        final ExecutorService e =
1449 >            new CustomTPE(2, 2,
1450 >                          LONG_DELAY_MS, MILLISECONDS,
1451 >                          new ArrayBlockingQueue<Runnable>(10));
1452 >        try (PoolCleaner cleaner = cleaner(e)) {
1453              Future<?> future = e.submit(new NoOpRunnable());
1454              future.get();
1455              assertTrue(future.isDone());
1433        } finally {
1434            joinPool(e);
1456          }
1457      }
1458  
# Line 1439 | Line 1460 | public class ThreadPoolExecutorSubclassT
1460       * completed submit of (runnable, result) returns result
1461       */
1462      public void testSubmitRunnable2() throws Exception {
1463 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1464 <        try {
1463 >        final ExecutorService e =
1464 >            new CustomTPE(2, 2,
1465 >                          LONG_DELAY_MS, MILLISECONDS,
1466 >                          new ArrayBlockingQueue<Runnable>(10));
1467 >        try (PoolCleaner cleaner = cleaner(e)) {
1468              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1469              String result = future.get();
1470              assertSame(TEST_STRING, result);
1447        } finally {
1448            joinPool(e);
1471          }
1472      }
1473  
# Line 1453 | Line 1475 | public class ThreadPoolExecutorSubclassT
1475       * invokeAny(null) throws NPE
1476       */
1477      public void testInvokeAny1() throws Exception {
1478 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1479 <        try {
1480 <            e.invokeAny(null);
1481 <            shouldThrow();
1482 <        } catch (NullPointerException success) {
1483 <        } finally {
1484 <            joinPool(e);
1478 >        final ExecutorService e =
1479 >            new CustomTPE(2, 2,
1480 >                          LONG_DELAY_MS, MILLISECONDS,
1481 >                          new ArrayBlockingQueue<Runnable>(10));
1482 >        try (PoolCleaner cleaner = cleaner(e)) {
1483 >            try {
1484 >                e.invokeAny(null);
1485 >                shouldThrow();
1486 >            } catch (NullPointerException success) {}
1487          }
1488      }
1489  
# Line 1467 | Line 1491 | public class ThreadPoolExecutorSubclassT
1491       * invokeAny(empty collection) throws IAE
1492       */
1493      public void testInvokeAny2() throws Exception {
1494 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1495 <        try {
1496 <            e.invokeAny(new ArrayList<Callable<String>>());
1497 <            shouldThrow();
1498 <        } catch (IllegalArgumentException success) {
1499 <        } finally {
1500 <            joinPool(e);
1494 >        final ExecutorService e =
1495 >            new CustomTPE(2, 2,
1496 >                          LONG_DELAY_MS, MILLISECONDS,
1497 >                          new ArrayBlockingQueue<Runnable>(10));
1498 >        try (PoolCleaner cleaner = cleaner(e)) {
1499 >            try {
1500 >                e.invokeAny(new ArrayList<Callable<String>>());
1501 >                shouldThrow();
1502 >            } catch (IllegalArgumentException success) {}
1503          }
1504      }
1505  
# Line 1482 | Line 1508 | public class ThreadPoolExecutorSubclassT
1508       */
1509      public void testInvokeAny3() throws Exception {
1510          CountDownLatch latch = new CountDownLatch(1);
1511 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1512 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1513 <        l.add(latchAwaitingStringTask(latch));
1514 <        l.add(null);
1515 <        try {
1516 <            e.invokeAny(l);
1517 <            shouldThrow();
1518 <        } catch (NullPointerException success) {
1519 <        } finally {
1511 >        final ExecutorService e =
1512 >            new CustomTPE(2, 2,
1513 >                          LONG_DELAY_MS, MILLISECONDS,
1514 >                          new ArrayBlockingQueue<Runnable>(10));
1515 >        try (PoolCleaner cleaner = cleaner(e)) {
1516 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1517 >            l.add(latchAwaitingStringTask(latch));
1518 >            l.add(null);
1519 >            try {
1520 >                e.invokeAny(l);
1521 >                shouldThrow();
1522 >            } catch (NullPointerException success) {}
1523              latch.countDown();
1495            joinPool(e);
1524          }
1525      }
1526  
# Line 1500 | Line 1528 | public class ThreadPoolExecutorSubclassT
1528       * invokeAny(c) throws ExecutionException if no task completes
1529       */
1530      public void testInvokeAny4() throws Exception {
1531 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1532 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1533 <        l.add(new NPETask());
1534 <        try {
1535 <            e.invokeAny(l);
1536 <            shouldThrow();
1537 <        } catch (ExecutionException success) {
1538 <            assertTrue(success.getCause() instanceof NullPointerException);
1539 <        } finally {
1540 <            joinPool(e);
1531 >        final ExecutorService e =
1532 >            new CustomTPE(2, 2,
1533 >                          LONG_DELAY_MS, MILLISECONDS,
1534 >                          new ArrayBlockingQueue<Runnable>(10));
1535 >        try (PoolCleaner cleaner = cleaner(e)) {
1536 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1537 >            l.add(new NPETask());
1538 >            try {
1539 >                e.invokeAny(l);
1540 >                shouldThrow();
1541 >            } catch (ExecutionException success) {
1542 >                assertTrue(success.getCause() instanceof NullPointerException);
1543 >            }
1544          }
1545      }
1546  
# Line 1517 | Line 1548 | public class ThreadPoolExecutorSubclassT
1548       * invokeAny(c) returns result of some task
1549       */
1550      public void testInvokeAny5() throws Exception {
1551 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1552 <        try {
1551 >        final ExecutorService e =
1552 >            new CustomTPE(2, 2,
1553 >                          LONG_DELAY_MS, MILLISECONDS,
1554 >                          new ArrayBlockingQueue<Runnable>(10));
1555 >        try (PoolCleaner cleaner = cleaner(e)) {
1556              List<Callable<String>> l = new ArrayList<Callable<String>>();
1557              l.add(new StringTask());
1558              l.add(new StringTask());
1559              String result = e.invokeAny(l);
1560              assertSame(TEST_STRING, result);
1527        } finally {
1528            joinPool(e);
1561          }
1562      }
1563  
# Line 1533 | Line 1565 | public class ThreadPoolExecutorSubclassT
1565       * invokeAll(null) throws NPE
1566       */
1567      public void testInvokeAll1() throws Exception {
1568 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1569 <        try {
1570 <            e.invokeAll(null);
1571 <            shouldThrow();
1572 <        } catch (NullPointerException success) {
1573 <        } finally {
1574 <            joinPool(e);
1568 >        final ExecutorService e =
1569 >            new CustomTPE(2, 2,
1570 >                          LONG_DELAY_MS, MILLISECONDS,
1571 >                          new ArrayBlockingQueue<Runnable>(10));
1572 >        try (PoolCleaner cleaner = cleaner(e)) {
1573 >            try {
1574 >                e.invokeAll(null);
1575 >                shouldThrow();
1576 >            } catch (NullPointerException success) {}
1577          }
1578      }
1579  
# Line 1547 | Line 1581 | public class ThreadPoolExecutorSubclassT
1581       * invokeAll(empty collection) returns empty collection
1582       */
1583      public void testInvokeAll2() throws Exception {
1584 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1585 <        try {
1584 >        final ExecutorService e =
1585 >            new CustomTPE(2, 2,
1586 >                          LONG_DELAY_MS, MILLISECONDS,
1587 >                          new ArrayBlockingQueue<Runnable>(10));
1588 >        try (PoolCleaner cleaner = cleaner(e)) {
1589              List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1590              assertTrue(r.isEmpty());
1554        } finally {
1555            joinPool(e);
1591          }
1592      }
1593  
# Line 1560 | Line 1595 | public class ThreadPoolExecutorSubclassT
1595       * invokeAll(c) throws NPE if c has null elements
1596       */
1597      public void testInvokeAll3() throws Exception {
1598 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1599 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1600 <        l.add(new StringTask());
1601 <        l.add(null);
1602 <        try {
1603 <            e.invokeAll(l);
1604 <            shouldThrow();
1605 <        } catch (NullPointerException success) {
1606 <        } finally {
1607 <            joinPool(e);
1598 >        final ExecutorService e =
1599 >            new CustomTPE(2, 2,
1600 >                          LONG_DELAY_MS, MILLISECONDS,
1601 >                          new ArrayBlockingQueue<Runnable>(10));
1602 >        try (PoolCleaner cleaner = cleaner(e)) {
1603 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1604 >            l.add(new StringTask());
1605 >            l.add(null);
1606 >            try {
1607 >                e.invokeAll(l);
1608 >                shouldThrow();
1609 >            } catch (NullPointerException success) {}
1610          }
1611      }
1612  
# Line 1577 | Line 1614 | public class ThreadPoolExecutorSubclassT
1614       * get of element of invokeAll(c) throws exception on failed task
1615       */
1616      public void testInvokeAll4() throws Exception {
1617 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1618 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1619 <        l.add(new NPETask());
1620 <        List<Future<String>> futures = e.invokeAll(l);
1621 <        assertEquals(1, futures.size());
1622 <        try {
1623 <            futures.get(0).get();
1624 <            shouldThrow();
1625 <        } catch (ExecutionException success) {
1626 <            assertTrue(success.getCause() instanceof NullPointerException);
1627 <        } finally {
1628 <            joinPool(e);
1617 >        final ExecutorService e =
1618 >            new CustomTPE(2, 2,
1619 >                          LONG_DELAY_MS, MILLISECONDS,
1620 >                          new ArrayBlockingQueue<Runnable>(10));
1621 >        try (PoolCleaner cleaner = cleaner(e)) {
1622 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1623 >            l.add(new NPETask());
1624 >            List<Future<String>> futures = e.invokeAll(l);
1625 >            assertEquals(1, futures.size());
1626 >            try {
1627 >                futures.get(0).get();
1628 >                shouldThrow();
1629 >            } catch (ExecutionException success) {
1630 >                assertTrue(success.getCause() instanceof NullPointerException);
1631 >            }
1632          }
1633      }
1634  
# Line 1596 | Line 1636 | public class ThreadPoolExecutorSubclassT
1636       * invokeAll(c) returns results of all completed tasks
1637       */
1638      public void testInvokeAll5() throws Exception {
1639 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1640 <        try {
1639 >        final ExecutorService e =
1640 >            new CustomTPE(2, 2,
1641 >                          LONG_DELAY_MS, MILLISECONDS,
1642 >                          new ArrayBlockingQueue<Runnable>(10));
1643 >        try (PoolCleaner cleaner = cleaner(e)) {
1644              List<Callable<String>> l = new ArrayList<Callable<String>>();
1645              l.add(new StringTask());
1646              l.add(new StringTask());
# Line 1605 | Line 1648 | public class ThreadPoolExecutorSubclassT
1648              assertEquals(2, futures.size());
1649              for (Future<String> future : futures)
1650                  assertSame(TEST_STRING, future.get());
1608        } finally {
1609            joinPool(e);
1651          }
1652      }
1653  
# Line 1614 | Line 1655 | public class ThreadPoolExecutorSubclassT
1655       * timed invokeAny(null) throws NPE
1656       */
1657      public void testTimedInvokeAny1() throws Exception {
1658 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1659 <        try {
1660 <            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1661 <            shouldThrow();
1662 <        } catch (NullPointerException success) {
1663 <        } finally {
1664 <            joinPool(e);
1658 >        final ExecutorService e =
1659 >            new CustomTPE(2, 2,
1660 >                          LONG_DELAY_MS, MILLISECONDS,
1661 >                          new ArrayBlockingQueue<Runnable>(10));
1662 >        try (PoolCleaner cleaner = cleaner(e)) {
1663 >            try {
1664 >                e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1665 >                shouldThrow();
1666 >            } catch (NullPointerException success) {}
1667          }
1668      }
1669  
# Line 1628 | Line 1671 | public class ThreadPoolExecutorSubclassT
1671       * timed invokeAny(,,null) throws NPE
1672       */
1673      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1674 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1675 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1676 <        l.add(new StringTask());
1677 <        try {
1678 <            e.invokeAny(l, MEDIUM_DELAY_MS, null);
1679 <            shouldThrow();
1680 <        } catch (NullPointerException success) {
1681 <        } finally {
1682 <            joinPool(e);
1674 >        final ExecutorService e =
1675 >            new CustomTPE(2, 2,
1676 >                          LONG_DELAY_MS, MILLISECONDS,
1677 >                          new ArrayBlockingQueue<Runnable>(10));
1678 >        try (PoolCleaner cleaner = cleaner(e)) {
1679 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1680 >            l.add(new StringTask());
1681 >            try {
1682 >                e.invokeAny(l, MEDIUM_DELAY_MS, null);
1683 >                shouldThrow();
1684 >            } catch (NullPointerException success) {}
1685          }
1686      }
1687  
# Line 1644 | Line 1689 | public class ThreadPoolExecutorSubclassT
1689       * timed invokeAny(empty collection) throws IAE
1690       */
1691      public void testTimedInvokeAny2() throws Exception {
1692 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1693 <        try {
1694 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1695 <            shouldThrow();
1696 <        } catch (IllegalArgumentException success) {
1697 <        } finally {
1698 <            joinPool(e);
1692 >        final ExecutorService e =
1693 >            new CustomTPE(2, 2,
1694 >                          LONG_DELAY_MS, MILLISECONDS,
1695 >                          new ArrayBlockingQueue<Runnable>(10));
1696 >        try (PoolCleaner cleaner = cleaner(e)) {
1697 >            try {
1698 >                e.invokeAny(new ArrayList<Callable<String>>(),
1699 >                            MEDIUM_DELAY_MS, MILLISECONDS);
1700 >                shouldThrow();
1701 >            } catch (IllegalArgumentException success) {}
1702          }
1703      }
1704  
# Line 1659 | Line 1707 | public class ThreadPoolExecutorSubclassT
1707       */
1708      public void testTimedInvokeAny3() throws Exception {
1709          CountDownLatch latch = new CountDownLatch(1);
1710 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1711 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1712 <        l.add(latchAwaitingStringTask(latch));
1713 <        l.add(null);
1714 <        try {
1715 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1716 <            shouldThrow();
1717 <        } catch (NullPointerException success) {
1718 <        } finally {
1710 >        final ExecutorService e =
1711 >            new CustomTPE(2, 2,
1712 >                          LONG_DELAY_MS, MILLISECONDS,
1713 >                          new ArrayBlockingQueue<Runnable>(10));
1714 >        try (PoolCleaner cleaner = cleaner(e)) {
1715 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1716 >            l.add(latchAwaitingStringTask(latch));
1717 >            l.add(null);
1718 >            try {
1719 >                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1720 >                shouldThrow();
1721 >            } catch (NullPointerException success) {}
1722              latch.countDown();
1672            joinPool(e);
1723          }
1724      }
1725  
# Line 1677 | Line 1727 | public class ThreadPoolExecutorSubclassT
1727       * timed invokeAny(c) throws ExecutionException if no task completes
1728       */
1729      public void testTimedInvokeAny4() throws Exception {
1730 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1731 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1732 <        l.add(new NPETask());
1733 <        try {
1734 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1735 <            shouldThrow();
1736 <        } catch (ExecutionException success) {
1737 <            assertTrue(success.getCause() instanceof NullPointerException);
1738 <        } finally {
1739 <            joinPool(e);
1730 >        final ExecutorService e =
1731 >            new CustomTPE(2, 2,
1732 >                          LONG_DELAY_MS, MILLISECONDS,
1733 >                          new ArrayBlockingQueue<Runnable>(10));
1734 >        try (PoolCleaner cleaner = cleaner(e)) {
1735 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1736 >            l.add(new NPETask());
1737 >            try {
1738 >                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1739 >                shouldThrow();
1740 >            } catch (ExecutionException success) {
1741 >                assertTrue(success.getCause() instanceof NullPointerException);
1742 >            }
1743          }
1744      }
1745  
# Line 1694 | Line 1747 | public class ThreadPoolExecutorSubclassT
1747       * timed invokeAny(c) returns result of some task
1748       */
1749      public void testTimedInvokeAny5() throws Exception {
1750 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1751 <        try {
1750 >        final ExecutorService e =
1751 >            new CustomTPE(2, 2,
1752 >                          LONG_DELAY_MS, MILLISECONDS,
1753 >                          new ArrayBlockingQueue<Runnable>(10));
1754 >        try (PoolCleaner cleaner = cleaner(e)) {
1755              List<Callable<String>> l = new ArrayList<Callable<String>>();
1756              l.add(new StringTask());
1757              l.add(new StringTask());
1758              String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1759              assertSame(TEST_STRING, result);
1704        } finally {
1705            joinPool(e);
1760          }
1761      }
1762  
# Line 1710 | Line 1764 | public class ThreadPoolExecutorSubclassT
1764       * timed invokeAll(null) throws NPE
1765       */
1766      public void testTimedInvokeAll1() throws Exception {
1767 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1768 <        try {
1769 <            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1770 <            shouldThrow();
1771 <        } catch (NullPointerException success) {
1772 <        } finally {
1773 <            joinPool(e);
1767 >        final ExecutorService e =
1768 >            new CustomTPE(2, 2,
1769 >                          LONG_DELAY_MS, MILLISECONDS,
1770 >                          new ArrayBlockingQueue<Runnable>(10));
1771 >        try (PoolCleaner cleaner = cleaner(e)) {
1772 >            try {
1773 >                e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1774 >                shouldThrow();
1775 >            } catch (NullPointerException success) {}
1776          }
1777      }
1778  
# Line 1724 | Line 1780 | public class ThreadPoolExecutorSubclassT
1780       * timed invokeAll(,,null) throws NPE
1781       */
1782      public void testTimedInvokeAllNullTimeUnit() throws Exception {
1783 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1784 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1785 <        l.add(new StringTask());
1786 <        try {
1787 <            e.invokeAll(l, MEDIUM_DELAY_MS, null);
1788 <            shouldThrow();
1789 <        } catch (NullPointerException success) {
1790 <        } finally {
1791 <            joinPool(e);
1783 >        final ExecutorService e =
1784 >            new CustomTPE(2, 2,
1785 >                          LONG_DELAY_MS, MILLISECONDS,
1786 >                          new ArrayBlockingQueue<Runnable>(10));
1787 >        try (PoolCleaner cleaner = cleaner(e)) {
1788 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1789 >            l.add(new StringTask());
1790 >            try {
1791 >                e.invokeAll(l, MEDIUM_DELAY_MS, null);
1792 >                shouldThrow();
1793 >            } catch (NullPointerException success) {}
1794          }
1795      }
1796  
# Line 1740 | Line 1798 | public class ThreadPoolExecutorSubclassT
1798       * timed invokeAll(empty collection) returns empty collection
1799       */
1800      public void testTimedInvokeAll2() throws Exception {
1801 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1802 <        try {
1803 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1801 >        final ExecutorService e =
1802 >            new CustomTPE(2, 2,
1803 >                          LONG_DELAY_MS, MILLISECONDS,
1804 >                          new ArrayBlockingQueue<Runnable>(10));
1805 >        try (PoolCleaner cleaner = cleaner(e)) {
1806 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(),
1807 >                                                 MEDIUM_DELAY_MS, MILLISECONDS);
1808              assertTrue(r.isEmpty());
1747        } finally {
1748            joinPool(e);
1809          }
1810      }
1811  
# Line 1753 | Line 1813 | public class ThreadPoolExecutorSubclassT
1813       * timed invokeAll(c) throws NPE if c has null elements
1814       */
1815      public void testTimedInvokeAll3() throws Exception {
1816 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1817 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1818 <        l.add(new StringTask());
1819 <        l.add(null);
1820 <        try {
1821 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1822 <            shouldThrow();
1823 <        } catch (NullPointerException success) {
1824 <        } finally {
1825 <            joinPool(e);
1816 >        final ExecutorService e =
1817 >            new CustomTPE(2, 2,
1818 >                          LONG_DELAY_MS, MILLISECONDS,
1819 >                          new ArrayBlockingQueue<Runnable>(10));
1820 >        try (PoolCleaner cleaner = cleaner(e)) {
1821 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1822 >            l.add(new StringTask());
1823 >            l.add(null);
1824 >            try {
1825 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1826 >                shouldThrow();
1827 >            } catch (NullPointerException success) {}
1828          }
1829      }
1830  
# Line 1770 | Line 1832 | public class ThreadPoolExecutorSubclassT
1832       * get of element of invokeAll(c) throws exception on failed task
1833       */
1834      public void testTimedInvokeAll4() throws Exception {
1835 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1836 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1837 <        l.add(new NPETask());
1838 <        List<Future<String>> futures =
1839 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1840 <        assertEquals(1, futures.size());
1841 <        try {
1842 <            futures.get(0).get();
1843 <            shouldThrow();
1844 <        } catch (ExecutionException success) {
1845 <            assertTrue(success.getCause() instanceof NullPointerException);
1846 <        } finally {
1847 <            joinPool(e);
1835 >        final ExecutorService e =
1836 >            new CustomTPE(2, 2,
1837 >                          LONG_DELAY_MS, MILLISECONDS,
1838 >                          new ArrayBlockingQueue<Runnable>(10));
1839 >        try (PoolCleaner cleaner = cleaner(e)) {
1840 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1841 >            l.add(new NPETask());
1842 >            List<Future<String>> futures =
1843 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1844 >            assertEquals(1, futures.size());
1845 >            try {
1846 >                futures.get(0).get();
1847 >                shouldThrow();
1848 >            } catch (ExecutionException success) {
1849 >                assertTrue(success.getCause() instanceof NullPointerException);
1850 >            }
1851          }
1852      }
1853  
# Line 1790 | Line 1855 | public class ThreadPoolExecutorSubclassT
1855       * timed invokeAll(c) returns results of all completed tasks
1856       */
1857      public void testTimedInvokeAll5() throws Exception {
1858 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1859 <        try {
1858 >        final ExecutorService e =
1859 >            new CustomTPE(2, 2,
1860 >                          LONG_DELAY_MS, MILLISECONDS,
1861 >                          new ArrayBlockingQueue<Runnable>(10));
1862 >        try (PoolCleaner cleaner = cleaner(e)) {
1863              List<Callable<String>> l = new ArrayList<Callable<String>>();
1864              l.add(new StringTask());
1865              l.add(new StringTask());
# Line 1800 | Line 1868 | public class ThreadPoolExecutorSubclassT
1868              assertEquals(2, futures.size());
1869              for (Future<String> future : futures)
1870                  assertSame(TEST_STRING, future.get());
1803        } finally {
1804            joinPool(e);
1871          }
1872      }
1873  
# Line 1809 | Line 1875 | public class ThreadPoolExecutorSubclassT
1875       * timed invokeAll(c) cancels tasks not completed by timeout
1876       */
1877      public void testTimedInvokeAll6() throws Exception {
1878 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1879 <        try {
1878 >        final ExecutorService e =
1879 >            new CustomTPE(2, 2,
1880 >                          LONG_DELAY_MS, MILLISECONDS,
1881 >                          new ArrayBlockingQueue<Runnable>(10));
1882 >        try (PoolCleaner cleaner = cleaner(e)) {
1883              for (long timeout = timeoutMillis();;) {
1884                  List<Callable<String>> tasks = new ArrayList<>();
1885                  tasks.add(new StringTask("0"));
# Line 1834 | Line 1903 | public class ThreadPoolExecutorSubclassT
1903                          fail("expected exactly one task to be cancelled");
1904                  }
1905              }
1837        } finally {
1838            joinPool(e);
1906          }
1907      }
1908  
# Line 1849 | Line 1916 | public class ThreadPoolExecutorSubclassT
1916                            LONG_DELAY_MS, MILLISECONDS,
1917                            new LinkedBlockingQueue<Runnable>(),
1918                            new FailingThreadFactory());
1919 <        try {
1919 >        try (PoolCleaner cleaner = cleaner(e)) {
1920              final int TASKS = 100;
1921              final CountDownLatch done = new CountDownLatch(TASKS);
1922              for (int k = 0; k < TASKS; ++k)
# Line 1858 | Line 1925 | public class ThreadPoolExecutorSubclassT
1925                          done.countDown();
1926                      }});
1927              assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1861        } finally {
1862            joinPool(e);
1928          }
1929      }
1930  
# Line 1867 | Line 1932 | public class ThreadPoolExecutorSubclassT
1932       * allowsCoreThreadTimeOut is by default false.
1933       */
1934      public void testAllowsCoreThreadTimeOut() {
1935 <        ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1936 <        assertFalse(p.allowsCoreThreadTimeOut());
1937 <        joinPool(p);
1935 >        final ThreadPoolExecutor p =
1936 >            new CustomTPE(2, 2,
1937 >                          1000, MILLISECONDS,
1938 >                          new ArrayBlockingQueue<Runnable>(10));
1939 >        try (PoolCleaner cleaner = cleaner(p)) {
1940 >            assertFalse(p.allowsCoreThreadTimeOut());
1941 >        }
1942      }
1943  
1944      /**
# Line 1881 | Line 1950 | public class ThreadPoolExecutorSubclassT
1950              new CustomTPE(2, 10,
1951                            keepAliveTime, MILLISECONDS,
1952                            new ArrayBlockingQueue<Runnable>(10));
1953 <        final CountDownLatch threadStarted = new CountDownLatch(1);
1954 <        try {
1953 >        try (PoolCleaner cleaner = cleaner(p)) {
1954 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1955              p.allowCoreThreadTimeOut(true);
1956              p.execute(new CheckedRunnable() {
1957                  public void realRun() {
# Line 1897 | Line 1966 | public class ThreadPoolExecutorSubclassT
1966                  Thread.yield();
1967              assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1968              assertEquals(0, p.getPoolSize());
1900        } finally {
1901            joinPool(p);
1969          }
1970      }
1971  
# Line 1911 | Line 1978 | public class ThreadPoolExecutorSubclassT
1978              new CustomTPE(2, 10,
1979                            keepAliveTime, MILLISECONDS,
1980                            new ArrayBlockingQueue<Runnable>(10));
1981 <        final CountDownLatch threadStarted = new CountDownLatch(1);
1982 <        try {
1981 >        try (PoolCleaner cleaner = cleaner(p)) {
1982 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1983              p.allowCoreThreadTimeOut(false);
1984              p.execute(new CheckedRunnable() {
1985                  public void realRun() throws InterruptedException {
# Line 1921 | Line 1988 | public class ThreadPoolExecutorSubclassT
1988                  }});
1989              delay(2 * keepAliveTime);
1990              assertTrue(p.getPoolSize() >= 1);
1924        } finally {
1925            joinPool(p);
1991          }
1992      }
1993  
# Line 1935 | Line 2000 | public class ThreadPoolExecutorSubclassT
2000              new CustomTPE(1, 1,
2001                            LONG_DELAY_MS, MILLISECONDS,
2002                            new LinkedBlockingQueue<Runnable>());
2003 <        try {
2003 >        try (PoolCleaner cleaner = cleaner(e)) {
2004              final CountDownLatch blockerStarted = new CountDownLatch(1);
2005              final CountDownLatch done = new CountDownLatch(1);
2006              final List<Future<?>> futures = new ArrayList<>();
# Line 1962 | Line 2027 | public class ThreadPoolExecutorSubclassT
2027                  assertTrue(future.isDone());
2028              }
2029              done.countDown();
1965        } finally {
1966            joinPool(e);
2030          }
2031      }
2032  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines