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.77 by jsr166, Sun Oct 4 03:51:35 2015 UTC vs.
Revision 1.98 by jsr166, Sat Mar 25 21:41:10 2017 UTC

# Line 17 | Line 17 | import java.util.concurrent.Callable;
17   import java.util.concurrent.CancellationException;
18   import java.util.concurrent.CountDownLatch;
19   import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.Executors;
20   import java.util.concurrent.ExecutorService;
21   import java.util.concurrent.Future;
22   import java.util.concurrent.FutureTask;
# Line 239 | Line 238 | public class ThreadPoolExecutorSubclassT
238              final Runnable task = new CheckedRunnable() {
239                  public void realRun() { done.countDown(); }};
240              p.execute(task);
241 <            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
241 >            await(done);
242          }
243      }
244  
# Line 248 | Line 247 | public class ThreadPoolExecutorSubclassT
247       * thread becomes active
248       */
249      public void testGetActiveCount() throws InterruptedException {
250 +        final CountDownLatch done = new CountDownLatch(1);
251          final ThreadPoolExecutor p =
252              new CustomTPE(2, 2,
253                            LONG_DELAY_MS, MILLISECONDS,
254                            new ArrayBlockingQueue<Runnable>(10));
255 <        try (PoolCleaner cleaner = cleaner(p)) {
255 >        try (PoolCleaner cleaner = cleaner(p, done)) {
256              final CountDownLatch threadStarted = new CountDownLatch(1);
257            final CountDownLatch done = new CountDownLatch(1);
257              assertEquals(0, p.getActiveCount());
258              p.execute(new CheckedRunnable() {
259                  public void realRun() throws InterruptedException {
260                      threadStarted.countDown();
261                      assertEquals(1, p.getActiveCount());
262 <                    done.await();
262 >                    await(done);
263                  }});
264 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
264 >            await(threadStarted);
265              assertEquals(1, p.getActiveCount());
267            done.countDown();
266          }
267      }
268  
# Line 272 | Line 270 | public class ThreadPoolExecutorSubclassT
270       * prestartCoreThread starts a thread if under corePoolSize, else doesn't
271       */
272      public void testPrestartCoreThread() {
273 <        ThreadPoolExecutor p =
273 >        final ThreadPoolExecutor p =
274              new CustomTPE(2, 6,
275                            LONG_DELAY_MS, MILLISECONDS,
276                            new ArrayBlockingQueue<Runnable>(10));
# Line 298 | Line 296 | public class ThreadPoolExecutorSubclassT
296       * prestartAllCoreThreads starts all corePoolSize threads
297       */
298      public void testPrestartAllCoreThreads() {
299 <        ThreadPoolExecutor p =
299 >        final ThreadPoolExecutor p =
300              new CustomTPE(2, 6,
301                            LONG_DELAY_MS, MILLISECONDS,
302                            new ArrayBlockingQueue<Runnable>(10));
# Line 334 | Line 332 | public class ThreadPoolExecutorSubclassT
332                  public void realRun() throws InterruptedException {
333                      threadStarted.countDown();
334                      assertEquals(0, p.getCompletedTaskCount());
335 <                    threadProceed.await();
335 >                    await(threadProceed);
336                      threadDone.countDown();
337                  }});
338              await(threadStarted);
339              assertEquals(0, p.getCompletedTaskCount());
340              threadProceed.countDown();
341 <            threadDone.await();
341 >            await(threadDone);
342              long startTime = System.nanoTime();
343              while (p.getCompletedTaskCount() != 1) {
344                  if (millisElapsedSince(startTime) > LONG_DELAY_MS)
# Line 476 | Line 474 | public class ThreadPoolExecutorSubclassT
474       */
475      public void testGetLargestPoolSize() throws InterruptedException {
476          final int THREADS = 3;
477 +        final CountDownLatch done = new CountDownLatch(1);
478          final ThreadPoolExecutor p =
479              new CustomTPE(THREADS, THREADS,
480                            LONG_DELAY_MS, MILLISECONDS,
481                            new ArrayBlockingQueue<Runnable>(10));
482 <        try (PoolCleaner cleaner = cleaner(p)) {
484 <            final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
485 <            final CountDownLatch done = new CountDownLatch(1);
482 >        try (PoolCleaner cleaner = cleaner(p, done)) {
483              assertEquals(0, p.getLargestPoolSize());
484 +            final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
485              for (int i = 0; i < THREADS; i++)
486                  p.execute(new CheckedRunnable() {
487                      public void realRun() throws InterruptedException {
488                          threadsStarted.countDown();
489 <                        done.await();
489 >                        await(done);
490                          assertEquals(THREADS, p.getLargestPoolSize());
491                      }});
492 <            assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
492 >            await(threadsStarted);
493              assertEquals(THREADS, p.getLargestPoolSize());
496            done.countDown();   // release pool
494          }
495          assertEquals(THREADS, p.getLargestPoolSize());
496      }
# Line 521 | Line 518 | public class ThreadPoolExecutorSubclassT
518       * become active
519       */
520      public void testGetPoolSize() throws InterruptedException {
521 +        final CountDownLatch done = new CountDownLatch(1);
522          final ThreadPoolExecutor p =
523              new CustomTPE(1, 1,
524                            LONG_DELAY_MS, MILLISECONDS,
525                            new ArrayBlockingQueue<Runnable>(10));
526 <        try (PoolCleaner cleaner = cleaner(p)) {
529 <            final CountDownLatch threadStarted = new CountDownLatch(1);
530 <            final CountDownLatch done = new CountDownLatch(1);
526 >        try (PoolCleaner cleaner = cleaner(p, done)) {
527              assertEquals(0, p.getPoolSize());
528 +            final CountDownLatch threadStarted = new CountDownLatch(1);
529              p.execute(new CheckedRunnable() {
530                  public void realRun() throws InterruptedException {
531                      threadStarted.countDown();
532                      assertEquals(1, p.getPoolSize());
533 <                    done.await();
533 >                    await(done);
534                  }});
535 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
535 >            await(threadStarted);
536              assertEquals(1, p.getPoolSize());
540            done.countDown();   // release pool
537          }
538      }
539  
# Line 545 | Line 541 | public class ThreadPoolExecutorSubclassT
541       * getTaskCount increases, but doesn't overestimate, when tasks submitted
542       */
543      public void testGetTaskCount() throws InterruptedException {
544 +        final int TASKS = 3;
545 +        final CountDownLatch done = new CountDownLatch(1);
546          final ThreadPoolExecutor p =
547              new CustomTPE(1, 1,
548                            LONG_DELAY_MS, MILLISECONDS,
549                            new ArrayBlockingQueue<Runnable>(10));
550 <        try (PoolCleaner cleaner = cleaner(p)) {
550 >        try (PoolCleaner cleaner = cleaner(p, done)) {
551              final CountDownLatch threadStarted = new CountDownLatch(1);
554            final CountDownLatch done = new CountDownLatch(1);
552              assertEquals(0, p.getTaskCount());
553 +            assertEquals(0, p.getCompletedTaskCount());
554              p.execute(new CheckedRunnable() {
555                  public void realRun() throws InterruptedException {
556                      threadStarted.countDown();
557 <                    assertEquals(1, p.getTaskCount());
560 <                    done.await();
557 >                    await(done);
558                  }});
559 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
559 >            await(threadStarted);
560              assertEquals(1, p.getTaskCount());
561 <            done.countDown();
561 >            assertEquals(0, p.getCompletedTaskCount());
562 >            for (int i = 0; i < TASKS; i++) {
563 >                assertEquals(1 + i, p.getTaskCount());
564 >                p.execute(new CheckedRunnable() {
565 >                    public void realRun() throws InterruptedException {
566 >                        threadStarted.countDown();
567 >                        assertEquals(1 + TASKS, p.getTaskCount());
568 >                        await(done);
569 >                    }});
570 >            }
571 >            assertEquals(1 + TASKS, p.getTaskCount());
572 >            assertEquals(0, p.getCompletedTaskCount());
573          }
574 +        assertEquals(1 + TASKS, p.getTaskCount());
575 +        assertEquals(1 + TASKS, p.getCompletedTaskCount());
576      }
577  
578      /**
# Line 596 | Line 606 | public class ThreadPoolExecutorSubclassT
606                  public void realRun() throws InterruptedException {
607                      assertFalse(p.isTerminating());
608                      threadStarted.countDown();
609 <                    done.await();
609 >                    await(done);
610                  }});
611 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
611 >            await(threadStarted);
612              assertFalse(p.isTerminating());
613              done.countDown();
614              try { p.shutdown(); } catch (SecurityException ok) { return; }
# Line 624 | Line 634 | public class ThreadPoolExecutorSubclassT
634                  public void realRun() throws InterruptedException {
635                      assertFalse(p.isTerminating());
636                      threadStarted.countDown();
637 <                    done.await();
637 >                    await(done);
638                  }});
639 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
639 >            await(threadStarted);
640              assertFalse(p.isTerminating());
641              done.countDown();
642              try { p.shutdown(); } catch (SecurityException ok) { return; }
# Line 640 | Line 650 | public class ThreadPoolExecutorSubclassT
650       * getQueue returns the work queue, which contains queued tasks
651       */
652      public void testGetQueue() throws InterruptedException {
653 <        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
653 >        final CountDownLatch done = new CountDownLatch(1);
654 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
655          final ThreadPoolExecutor p =
656              new CustomTPE(1, 1,
657                            LONG_DELAY_MS, MILLISECONDS,
658                            q);
659 <        try (PoolCleaner cleaner = cleaner(p)) {
659 >        try (PoolCleaner cleaner = cleaner(p, done)) {
660              final CountDownLatch threadStarted = new CountDownLatch(1);
650            final CountDownLatch done = new CountDownLatch(1);
661              FutureTask[] tasks = new FutureTask[5];
662              for (int i = 0; i < tasks.length; i++) {
663 <                Callable task = new CheckedCallable<Boolean>() {
663 >                Callable<Boolean> task = new CheckedCallable<Boolean>() {
664                      public Boolean realCall() throws InterruptedException {
665                          threadStarted.countDown();
666                          assertSame(q, p.getQueue());
667 <                        done.await();
667 >                        await(done);
668                          return Boolean.TRUE;
669                      }};
670                  tasks[i] = new FutureTask(task);
671                  p.execute(tasks[i]);
672              }
673 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
673 >            await(threadStarted);
674              assertSame(q, p.getQueue());
675              assertFalse(q.contains(tasks[0]));
676              assertTrue(q.contains(tasks[tasks.length - 1]));
677              assertEquals(tasks.length - 1, q.size());
668            done.countDown();
678          }
679      }
680  
# Line 673 | Line 682 | public class ThreadPoolExecutorSubclassT
682       * remove(task) removes queued task, and fails to remove active task
683       */
684      public void testRemove() throws InterruptedException {
685 <        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
685 >        final CountDownLatch done = new CountDownLatch(1);
686 >        BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
687          final ThreadPoolExecutor p =
688              new CustomTPE(1, 1,
689                            LONG_DELAY_MS, MILLISECONDS,
690                            q);
691 <        try (PoolCleaner cleaner = cleaner(p)) {
691 >        try (PoolCleaner cleaner = cleaner(p, done)) {
692              Runnable[] tasks = new Runnable[6];
693              final CountDownLatch threadStarted = new CountDownLatch(1);
684            final CountDownLatch done = new CountDownLatch(1);
694              for (int i = 0; i < tasks.length; i++) {
695                  tasks[i] = new CheckedRunnable() {
696                      public void realRun() throws InterruptedException {
697                          threadStarted.countDown();
698 <                        done.await();
698 >                        await(done);
699                      }};
700                  p.execute(tasks[i]);
701              }
702 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
702 >            await(threadStarted);
703              assertFalse(p.remove(tasks[0]));
704              assertTrue(q.contains(tasks[4]));
705              assertTrue(q.contains(tasks[3]));
# Line 700 | Line 709 | public class ThreadPoolExecutorSubclassT
709              assertTrue(q.contains(tasks[3]));
710              assertTrue(p.remove(tasks[3]));
711              assertFalse(q.contains(tasks[3]));
703            done.countDown();
712          }
713      }
714  
# Line 710 | Line 718 | public class ThreadPoolExecutorSubclassT
718      public void testPurge() throws InterruptedException {
719          final CountDownLatch threadStarted = new CountDownLatch(1);
720          final CountDownLatch done = new CountDownLatch(1);
721 <        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
721 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
722          final ThreadPoolExecutor p =
723              new CustomTPE(1, 1,
724                            LONG_DELAY_MS, MILLISECONDS,
725                            q);
726 <        try (PoolCleaner cleaner = cleaner(p)) {
726 >        try (PoolCleaner cleaner = cleaner(p, done)) {
727              FutureTask[] tasks = new FutureTask[5];
728              for (int i = 0; i < tasks.length; i++) {
729 <                Callable task = new CheckedCallable<Boolean>() {
729 >                Callable<Boolean> task = new CheckedCallable<Boolean>() {
730                      public Boolean realCall() throws InterruptedException {
731                          threadStarted.countDown();
732 <                        done.await();
732 >                        await(done);
733                          return Boolean.TRUE;
734                      }};
735                  tasks[i] = new FutureTask(task);
736                  p.execute(tasks[i]);
737              }
738 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
738 >            await(threadStarted);
739              assertEquals(tasks.length, p.getTaskCount());
740              assertEquals(tasks.length - 1, q.size());
741              assertEquals(1L, p.getActiveCount());
# Line 740 | Line 748 | public class ThreadPoolExecutorSubclassT
748              p.purge();         // Nothing to do
749              assertEquals(tasks.length - 3, q.size());
750              assertEquals(tasks.length - 2, p.getTaskCount());
743            done.countDown();
751          }
752      }
753  
# Line 752 | Line 759 | public class ThreadPoolExecutorSubclassT
759          final int poolSize = 2;
760          final int count = 5;
761          final AtomicInteger ran = new AtomicInteger(0);
762 <        ThreadPoolExecutor p =
763 <            new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS,
762 >        final ThreadPoolExecutor p =
763 >            new CustomTPE(poolSize, poolSize,
764 >                          LONG_DELAY_MS, MILLISECONDS,
765                            new ArrayBlockingQueue<Runnable>(10));
766 <        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
766 >        final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
767          Runnable waiter = new CheckedRunnable() { public void realRun() {
768              threadsStarted.countDown();
769              try {
# Line 765 | Line 773 | public class ThreadPoolExecutorSubclassT
773          }};
774          for (int i = 0; i < count; i++)
775              p.execute(waiter);
776 <        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
776 >        await(threadsStarted);
777          assertEquals(poolSize, p.getActiveCount());
778          assertEquals(0, p.getCompletedTaskCount());
779          final List<Runnable> queuedTasks;
# Line 1124 | Line 1132 | public class ThreadPoolExecutorSubclassT
1132       * execute throws RejectedExecutionException if saturated.
1133       */
1134      public void testSaturatedExecute() {
1135 +        final CountDownLatch done = new CountDownLatch(1);
1136          final ThreadPoolExecutor p =
1137              new CustomTPE(1, 1,
1138                            LONG_DELAY_MS, MILLISECONDS,
1139                            new ArrayBlockingQueue<Runnable>(1));
1140 <        try (PoolCleaner cleaner = cleaner(p)) {
1132 <            final CountDownLatch done = new CountDownLatch(1);
1140 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1141              Runnable task = new CheckedRunnable() {
1142                  public void realRun() throws InterruptedException {
1143 <                    done.await();
1143 >                    await(done);
1144                  }};
1145              for (int i = 0; i < 2; ++i)
1146                  p.execute(task);
# Line 1143 | Line 1151 | public class ThreadPoolExecutorSubclassT
1151                  } catch (RejectedExecutionException success) {}
1152                  assertTrue(p.getTaskCount() <= 2);
1153              }
1146            done.countDown();
1154          }
1155      }
1156  
# Line 1151 | Line 1158 | public class ThreadPoolExecutorSubclassT
1158       * executor using CallerRunsPolicy runs task if saturated.
1159       */
1160      public void testSaturatedExecute2() {
1161 +        final CountDownLatch done = new CountDownLatch(1);
1162          final ThreadPoolExecutor p =
1163              new CustomTPE(1, 1,
1164                            LONG_DELAY_MS, MILLISECONDS,
1165                            new ArrayBlockingQueue<Runnable>(1),
1166                            new CustomTPE.CallerRunsPolicy());
1167 <        try (PoolCleaner cleaner = cleaner(p)) {
1160 <            final CountDownLatch done = new CountDownLatch(1);
1167 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1168              Runnable blocker = new CheckedRunnable() {
1169                  public void realRun() throws InterruptedException {
1170 <                    done.await();
1170 >                    await(done);
1171                  }};
1172              p.execute(blocker);
1173              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
# Line 1171 | Line 1178 | public class ThreadPoolExecutorSubclassT
1178              for (int i = 1; i < tasks.length; i++)
1179                  assertTrue(tasks[i].done);
1180              assertFalse(tasks[0].done); // waiting in queue
1174            done.countDown();
1181          }
1182      }
1183  
# Line 1182 | Line 1188 | public class ThreadPoolExecutorSubclassT
1188          final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1189          for (int i = 0; i < tasks.length; ++i)
1190              tasks[i] = new TrackedNoOpRunnable();
1191 +        final CountDownLatch done = new CountDownLatch(1);
1192          final ThreadPoolExecutor p =
1193              new CustomTPE(1, 1,
1194                            LONG_DELAY_MS, MILLISECONDS,
1195                            new ArrayBlockingQueue<Runnable>(1),
1196                            new CustomTPE.DiscardPolicy());
1197 <        try (PoolCleaner cleaner = cleaner(p)) {
1191 <            final CountDownLatch done = new CountDownLatch(1);
1197 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1198              p.execute(awaiter(done));
1199  
1200              for (TrackedNoOpRunnable task : tasks)
1201                  p.execute(task);
1202              for (int i = 1; i < tasks.length; i++)
1203                  assertFalse(tasks[i].done);
1198            done.countDown();
1204          }
1205          for (int i = 1; i < tasks.length; i++)
1206              assertFalse(tasks[i].done);
# Line 1215 | Line 1220 | public class ThreadPoolExecutorSubclassT
1220                            LONG_DELAY_MS, MILLISECONDS,
1221                            new ArrayBlockingQueue<Runnable>(1),
1222                            new CustomTPE.DiscardOldestPolicy());
1223 <        try (PoolCleaner cleaner = cleaner(p)) {
1223 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1224              assertEquals(LatchAwaiter.NEW, r1.state);
1225              assertEquals(LatchAwaiter.NEW, r2.state);
1226              assertEquals(LatchAwaiter.NEW, r3.state);
# Line 1225 | Line 1230 | public class ThreadPoolExecutorSubclassT
1230              p.execute(r3);
1231              assertFalse(p.getQueue().contains(r2));
1232              assertTrue(p.getQueue().contains(r3));
1228            done.countDown();
1233          }
1234          assertEquals(LatchAwaiter.DONE, r1.state);
1235          assertEquals(LatchAwaiter.NEW, r2.state);
# Line 1253 | Line 1257 | public class ThreadPoolExecutorSubclassT
1257       * execute using CallerRunsPolicy drops task on shutdown
1258       */
1259      public void testCallerRunsOnShutdown() {
1260 <        RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1261 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1262 <
1260 >        final ThreadPoolExecutor p =
1261 >            new CustomTPE(1, 1,
1262 >                          LONG_DELAY_MS, MILLISECONDS,
1263 >                          new ArrayBlockingQueue<Runnable>(1),
1264 >                          new CustomTPE.CallerRunsPolicy());
1265          try { p.shutdown(); } catch (SecurityException ok) { return; }
1266 <        try {
1266 >        try (PoolCleaner cleaner = cleaner(p)) {
1267              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1268              p.execute(r);
1269              assertFalse(r.done);
1264        } finally {
1265            joinPool(p);
1270          }
1271      }
1272  
# Line 1270 | Line 1274 | public class ThreadPoolExecutorSubclassT
1274       * execute using DiscardPolicy drops task on shutdown
1275       */
1276      public void testDiscardOnShutdown() {
1277 <        RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1278 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1279 <
1277 >        final ThreadPoolExecutor p =
1278 >            new CustomTPE(1, 1,
1279 >                          LONG_DELAY_MS, MILLISECONDS,
1280 >                          new ArrayBlockingQueue<Runnable>(1),
1281 >                          new CustomTPE.DiscardPolicy());
1282          try { p.shutdown(); } catch (SecurityException ok) { return; }
1283 <        try {
1283 >        try (PoolCleaner cleaner = cleaner(p)) {
1284              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1285              p.execute(r);
1286              assertFalse(r.done);
1281        } finally {
1282            joinPool(p);
1287          }
1288      }
1289  
# Line 1287 | Line 1291 | public class ThreadPoolExecutorSubclassT
1291       * execute using DiscardOldestPolicy drops task on shutdown
1292       */
1293      public void testDiscardOldestOnShutdown() {
1294 <        RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
1295 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1294 >        final ThreadPoolExecutor p =
1295 >            new CustomTPE(1, 1,
1296 >                          LONG_DELAY_MS, MILLISECONDS,
1297 >                          new ArrayBlockingQueue<Runnable>(1),
1298 >                          new CustomTPE.DiscardOldestPolicy());
1299  
1300          try { p.shutdown(); } catch (SecurityException ok) { return; }
1301 <        try {
1301 >        try (PoolCleaner cleaner = cleaner(p)) {
1302              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1303              p.execute(r);
1304              assertFalse(r.done);
1298        } finally {
1299            joinPool(p);
1305          }
1306      }
1307  
# Line 1304 | Line 1309 | public class ThreadPoolExecutorSubclassT
1309       * execute(null) throws NPE
1310       */
1311      public void testExecuteNull() {
1312 <        ThreadPoolExecutor p =
1313 <            new CustomTPE(1, 2, 1L, SECONDS,
1312 >        final ThreadPoolExecutor p =
1313 >            new CustomTPE(1, 2,
1314 >                          1L, SECONDS,
1315                            new ArrayBlockingQueue<Runnable>(10));
1316 <        try {
1317 <            p.execute(null);
1318 <            shouldThrow();
1319 <        } catch (NullPointerException success) {}
1320 <
1321 <        joinPool(p);
1316 >        try (PoolCleaner cleaner = cleaner(p)) {
1317 >            try {
1318 >                p.execute(null);
1319 >                shouldThrow();
1320 >            } catch (NullPointerException success) {}
1321 >        }
1322      }
1323  
1324      /**
1325       * setCorePoolSize of negative value throws IllegalArgumentException
1326       */
1327      public void testCorePoolSizeIllegalArgumentException() {
1328 <        ThreadPoolExecutor p =
1329 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1330 <        try {
1331 <            p.setCorePoolSize(-1);
1332 <            shouldThrow();
1333 <        } catch (IllegalArgumentException success) {
1334 <        } finally {
1335 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1328 >        final ThreadPoolExecutor p =
1329 >            new CustomTPE(1, 2,
1330 >                          LONG_DELAY_MS, MILLISECONDS,
1331 >                          new ArrayBlockingQueue<Runnable>(10));
1332 >        try (PoolCleaner cleaner = cleaner(p)) {
1333 >            try {
1334 >                p.setCorePoolSize(-1);
1335 >                shouldThrow();
1336 >            } catch (IllegalArgumentException success) {}
1337          }
1331        joinPool(p);
1338      }
1339  
1340      /**
# Line 1336 | Line 1342 | public class ThreadPoolExecutorSubclassT
1342       * if given a value less the core pool size
1343       */
1344      public void testMaximumPoolSizeIllegalArgumentException() {
1345 <        ThreadPoolExecutor p =
1346 <            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1347 <        try {
1348 <            p.setMaximumPoolSize(1);
1349 <            shouldThrow();
1350 <        } catch (IllegalArgumentException success) {
1351 <        } finally {
1352 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1345 >        final ThreadPoolExecutor p =
1346 >            new CustomTPE(2, 3,
1347 >                          LONG_DELAY_MS, MILLISECONDS,
1348 >                          new ArrayBlockingQueue<Runnable>(10));
1349 >        try (PoolCleaner cleaner = cleaner(p)) {
1350 >            try {
1351 >                p.setMaximumPoolSize(1);
1352 >                shouldThrow();
1353 >            } catch (IllegalArgumentException success) {}
1354          }
1348        joinPool(p);
1355      }
1356  
1357      /**
# Line 1353 | Line 1359 | public class ThreadPoolExecutorSubclassT
1359       * if given a negative value
1360       */
1361      public void testMaximumPoolSizeIllegalArgumentException2() {
1362 <        ThreadPoolExecutor p =
1363 <            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1364 <        try {
1365 <            p.setMaximumPoolSize(-1);
1366 <            shouldThrow();
1367 <        } catch (IllegalArgumentException success) {
1368 <        } finally {
1369 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1362 >        final ThreadPoolExecutor p =
1363 >            new CustomTPE(2, 3,
1364 >                          LONG_DELAY_MS,
1365 >                          MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1366 >        try (PoolCleaner cleaner = cleaner(p)) {
1367 >            try {
1368 >                p.setMaximumPoolSize(-1);
1369 >                shouldThrow();
1370 >            } catch (IllegalArgumentException success) {}
1371          }
1365        joinPool(p);
1372      }
1373  
1374      /**
# Line 1370 | Line 1376 | public class ThreadPoolExecutorSubclassT
1376       * when given a negative value
1377       */
1378      public void testKeepAliveTimeIllegalArgumentException() {
1379 <        ThreadPoolExecutor p =
1380 <            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1381 <
1382 <        try {
1383 <            p.setKeepAliveTime(-1,MILLISECONDS);
1384 <            shouldThrow();
1385 <        } catch (IllegalArgumentException success) {
1386 <        } finally {
1387 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1379 >        final ThreadPoolExecutor p =
1380 >            new CustomTPE(2, 3,
1381 >                          LONG_DELAY_MS, MILLISECONDS,
1382 >                          new ArrayBlockingQueue<Runnable>(10));
1383 >        try (PoolCleaner cleaner = cleaner(p)) {
1384 >            try {
1385 >                p.setKeepAliveTime(-1, MILLISECONDS);
1386 >                shouldThrow();
1387 >            } catch (IllegalArgumentException success) {}
1388          }
1383        joinPool(p);
1389      }
1390  
1391      /**
# Line 1388 | Line 1393 | public class ThreadPoolExecutorSubclassT
1393       */
1394      public void testTerminated() {
1395          CustomTPE p = new CustomTPE();
1396 <        try { p.shutdown(); } catch (SecurityException ok) { return; }
1397 <        assertTrue(p.terminatedCalled());
1398 <        joinPool(p);
1396 >        try (PoolCleaner cleaner = cleaner(p)) {
1397 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1398 >            assertTrue(p.terminatedCalled());
1399 >            assertTrue(p.isShutdown());
1400 >        }
1401      }
1402  
1403      /**
# Line 1398 | Line 1405 | public class ThreadPoolExecutorSubclassT
1405       */
1406      public void testBeforeAfter() throws InterruptedException {
1407          CustomTPE p = new CustomTPE();
1408 <        try {
1408 >        try (PoolCleaner cleaner = cleaner(p)) {
1409              final CountDownLatch done = new CountDownLatch(1);
1410              p.execute(new CheckedRunnable() {
1411                  public void realRun() {
# Line 1408 | Line 1415 | public class ThreadPoolExecutorSubclassT
1415              assertEquals(0, done.getCount());
1416              assertTrue(p.afterCalled());
1417              assertTrue(p.beforeCalled());
1411            try { p.shutdown(); } catch (SecurityException ok) { return; }
1412        } finally {
1413            joinPool(p);
1418          }
1419      }
1420  
# Line 1418 | Line 1422 | public class ThreadPoolExecutorSubclassT
1422       * completed submit of callable returns result
1423       */
1424      public void testSubmitCallable() throws Exception {
1425 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1426 <        try {
1425 >        final ExecutorService e =
1426 >            new CustomTPE(2, 2,
1427 >                          LONG_DELAY_MS, MILLISECONDS,
1428 >                          new ArrayBlockingQueue<Runnable>(10));
1429 >        try (PoolCleaner cleaner = cleaner(e)) {
1430              Future<String> future = e.submit(new StringTask());
1431              String result = future.get();
1432              assertSame(TEST_STRING, result);
1426        } finally {
1427            joinPool(e);
1433          }
1434      }
1435  
# Line 1432 | Line 1437 | public class ThreadPoolExecutorSubclassT
1437       * completed submit of runnable returns successfully
1438       */
1439      public void testSubmitRunnable() throws Exception {
1440 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1441 <        try {
1440 >        final ExecutorService e =
1441 >            new CustomTPE(2, 2,
1442 >                          LONG_DELAY_MS, MILLISECONDS,
1443 >                          new ArrayBlockingQueue<Runnable>(10));
1444 >        try (PoolCleaner cleaner = cleaner(e)) {
1445              Future<?> future = e.submit(new NoOpRunnable());
1446              future.get();
1447              assertTrue(future.isDone());
1440        } finally {
1441            joinPool(e);
1448          }
1449      }
1450  
# Line 1446 | Line 1452 | public class ThreadPoolExecutorSubclassT
1452       * completed submit of (runnable, result) returns result
1453       */
1454      public void testSubmitRunnable2() throws Exception {
1455 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1456 <        try {
1455 >        final ExecutorService e =
1456 >            new CustomTPE(2, 2,
1457 >                          LONG_DELAY_MS, MILLISECONDS,
1458 >                          new ArrayBlockingQueue<Runnable>(10));
1459 >        try (PoolCleaner cleaner = cleaner(e)) {
1460              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1461              String result = future.get();
1462              assertSame(TEST_STRING, result);
1454        } finally {
1455            joinPool(e);
1463          }
1464      }
1465  
# Line 1460 | Line 1467 | public class ThreadPoolExecutorSubclassT
1467       * invokeAny(null) throws NPE
1468       */
1469      public void testInvokeAny1() throws Exception {
1470 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1471 <        try {
1472 <            e.invokeAny(null);
1473 <            shouldThrow();
1474 <        } catch (NullPointerException success) {
1475 <        } finally {
1476 <            joinPool(e);
1470 >        final ExecutorService e =
1471 >            new CustomTPE(2, 2,
1472 >                          LONG_DELAY_MS, MILLISECONDS,
1473 >                          new ArrayBlockingQueue<Runnable>(10));
1474 >        try (PoolCleaner cleaner = cleaner(e)) {
1475 >            try {
1476 >                e.invokeAny(null);
1477 >                shouldThrow();
1478 >            } catch (NullPointerException success) {}
1479          }
1480      }
1481  
# Line 1474 | Line 1483 | public class ThreadPoolExecutorSubclassT
1483       * invokeAny(empty collection) throws IAE
1484       */
1485      public void testInvokeAny2() throws Exception {
1486 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1487 <        try {
1488 <            e.invokeAny(new ArrayList<Callable<String>>());
1489 <            shouldThrow();
1490 <        } catch (IllegalArgumentException success) {
1491 <        } finally {
1492 <            joinPool(e);
1486 >        final ExecutorService e =
1487 >            new CustomTPE(2, 2,
1488 >                          LONG_DELAY_MS, MILLISECONDS,
1489 >                          new ArrayBlockingQueue<Runnable>(10));
1490 >        try (PoolCleaner cleaner = cleaner(e)) {
1491 >            try {
1492 >                e.invokeAny(new ArrayList<Callable<String>>());
1493 >                shouldThrow();
1494 >            } catch (IllegalArgumentException success) {}
1495          }
1496      }
1497  
# Line 1489 | Line 1500 | public class ThreadPoolExecutorSubclassT
1500       */
1501      public void testInvokeAny3() throws Exception {
1502          CountDownLatch latch = new CountDownLatch(1);
1503 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1504 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1505 <        l.add(latchAwaitingStringTask(latch));
1506 <        l.add(null);
1507 <        try {
1508 <            e.invokeAny(l);
1509 <            shouldThrow();
1510 <        } catch (NullPointerException success) {
1511 <        } finally {
1503 >        final ExecutorService e =
1504 >            new CustomTPE(2, 2,
1505 >                          LONG_DELAY_MS, MILLISECONDS,
1506 >                          new ArrayBlockingQueue<Runnable>(10));
1507 >        try (PoolCleaner cleaner = cleaner(e)) {
1508 >            List<Callable<String>> l = new ArrayList<>();
1509 >            l.add(latchAwaitingStringTask(latch));
1510 >            l.add(null);
1511 >            try {
1512 >                e.invokeAny(l);
1513 >                shouldThrow();
1514 >            } catch (NullPointerException success) {}
1515              latch.countDown();
1502            joinPool(e);
1516          }
1517      }
1518  
# Line 1507 | Line 1520 | public class ThreadPoolExecutorSubclassT
1520       * invokeAny(c) throws ExecutionException if no task completes
1521       */
1522      public void testInvokeAny4() throws Exception {
1523 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1524 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1525 <        l.add(new NPETask());
1526 <        try {
1527 <            e.invokeAny(l);
1528 <            shouldThrow();
1529 <        } catch (ExecutionException success) {
1530 <            assertTrue(success.getCause() instanceof NullPointerException);
1531 <        } finally {
1532 <            joinPool(e);
1523 >        final ExecutorService e =
1524 >            new CustomTPE(2, 2,
1525 >                          LONG_DELAY_MS, MILLISECONDS,
1526 >                          new ArrayBlockingQueue<Runnable>(10));
1527 >        try (PoolCleaner cleaner = cleaner(e)) {
1528 >            List<Callable<String>> l = new ArrayList<>();
1529 >            l.add(new NPETask());
1530 >            try {
1531 >                e.invokeAny(l);
1532 >                shouldThrow();
1533 >            } catch (ExecutionException success) {
1534 >                assertTrue(success.getCause() instanceof NullPointerException);
1535 >            }
1536          }
1537      }
1538  
# Line 1524 | Line 1540 | public class ThreadPoolExecutorSubclassT
1540       * invokeAny(c) returns result of some task
1541       */
1542      public void testInvokeAny5() throws Exception {
1543 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1544 <        try {
1545 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1543 >        final ExecutorService e =
1544 >            new CustomTPE(2, 2,
1545 >                          LONG_DELAY_MS, MILLISECONDS,
1546 >                          new ArrayBlockingQueue<Runnable>(10));
1547 >        try (PoolCleaner cleaner = cleaner(e)) {
1548 >            List<Callable<String>> l = new ArrayList<>();
1549              l.add(new StringTask());
1550              l.add(new StringTask());
1551              String result = e.invokeAny(l);
1552              assertSame(TEST_STRING, result);
1534        } finally {
1535            joinPool(e);
1553          }
1554      }
1555  
# Line 1540 | Line 1557 | public class ThreadPoolExecutorSubclassT
1557       * invokeAll(null) throws NPE
1558       */
1559      public void testInvokeAll1() throws Exception {
1560 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1561 <        try {
1562 <            e.invokeAll(null);
1563 <            shouldThrow();
1564 <        } catch (NullPointerException success) {
1565 <        } finally {
1566 <            joinPool(e);
1560 >        final ExecutorService e =
1561 >            new CustomTPE(2, 2,
1562 >                          LONG_DELAY_MS, MILLISECONDS,
1563 >                          new ArrayBlockingQueue<Runnable>(10));
1564 >        try (PoolCleaner cleaner = cleaner(e)) {
1565 >            try {
1566 >                e.invokeAll(null);
1567 >                shouldThrow();
1568 >            } catch (NullPointerException success) {}
1569          }
1570      }
1571  
# Line 1554 | Line 1573 | public class ThreadPoolExecutorSubclassT
1573       * invokeAll(empty collection) returns empty collection
1574       */
1575      public void testInvokeAll2() throws Exception {
1576 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1577 <        try {
1576 >        final ExecutorService e =
1577 >            new CustomTPE(2, 2,
1578 >                          LONG_DELAY_MS, MILLISECONDS,
1579 >                          new ArrayBlockingQueue<Runnable>(10));
1580 >        try (PoolCleaner cleaner = cleaner(e)) {
1581              List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1582              assertTrue(r.isEmpty());
1561        } finally {
1562            joinPool(e);
1583          }
1584      }
1585  
# Line 1567 | Line 1587 | public class ThreadPoolExecutorSubclassT
1587       * invokeAll(c) throws NPE if c has null elements
1588       */
1589      public void testInvokeAll3() throws Exception {
1590 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1591 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1592 <        l.add(new StringTask());
1593 <        l.add(null);
1594 <        try {
1595 <            e.invokeAll(l);
1596 <            shouldThrow();
1597 <        } catch (NullPointerException success) {
1598 <        } finally {
1599 <            joinPool(e);
1590 >        final ExecutorService e =
1591 >            new CustomTPE(2, 2,
1592 >                          LONG_DELAY_MS, MILLISECONDS,
1593 >                          new ArrayBlockingQueue<Runnable>(10));
1594 >        try (PoolCleaner cleaner = cleaner(e)) {
1595 >            List<Callable<String>> l = new ArrayList<>();
1596 >            l.add(new StringTask());
1597 >            l.add(null);
1598 >            try {
1599 >                e.invokeAll(l);
1600 >                shouldThrow();
1601 >            } catch (NullPointerException success) {}
1602          }
1603      }
1604  
# Line 1584 | Line 1606 | public class ThreadPoolExecutorSubclassT
1606       * get of element of invokeAll(c) throws exception on failed task
1607       */
1608      public void testInvokeAll4() throws Exception {
1609 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1610 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1611 <        l.add(new NPETask());
1612 <        List<Future<String>> futures = e.invokeAll(l);
1613 <        assertEquals(1, futures.size());
1614 <        try {
1615 <            futures.get(0).get();
1616 <            shouldThrow();
1617 <        } catch (ExecutionException success) {
1618 <            assertTrue(success.getCause() instanceof NullPointerException);
1619 <        } finally {
1620 <            joinPool(e);
1609 >        final ExecutorService e =
1610 >            new CustomTPE(2, 2,
1611 >                          LONG_DELAY_MS, MILLISECONDS,
1612 >                          new ArrayBlockingQueue<Runnable>(10));
1613 >        try (PoolCleaner cleaner = cleaner(e)) {
1614 >            List<Callable<String>> l = new ArrayList<>();
1615 >            l.add(new NPETask());
1616 >            List<Future<String>> futures = e.invokeAll(l);
1617 >            assertEquals(1, futures.size());
1618 >            try {
1619 >                futures.get(0).get();
1620 >                shouldThrow();
1621 >            } catch (ExecutionException success) {
1622 >                assertTrue(success.getCause() instanceof NullPointerException);
1623 >            }
1624          }
1625      }
1626  
# Line 1603 | Line 1628 | public class ThreadPoolExecutorSubclassT
1628       * invokeAll(c) returns results of all completed tasks
1629       */
1630      public void testInvokeAll5() throws Exception {
1631 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1632 <        try {
1633 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1631 >        final ExecutorService e =
1632 >            new CustomTPE(2, 2,
1633 >                          LONG_DELAY_MS, MILLISECONDS,
1634 >                          new ArrayBlockingQueue<Runnable>(10));
1635 >        try (PoolCleaner cleaner = cleaner(e)) {
1636 >            List<Callable<String>> l = new ArrayList<>();
1637              l.add(new StringTask());
1638              l.add(new StringTask());
1639              List<Future<String>> futures = e.invokeAll(l);
1640              assertEquals(2, futures.size());
1641              for (Future<String> future : futures)
1642                  assertSame(TEST_STRING, future.get());
1615        } finally {
1616            joinPool(e);
1643          }
1644      }
1645  
# Line 1621 | Line 1647 | public class ThreadPoolExecutorSubclassT
1647       * timed invokeAny(null) throws NPE
1648       */
1649      public void testTimedInvokeAny1() throws Exception {
1650 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1651 <        try {
1652 <            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1653 <            shouldThrow();
1654 <        } catch (NullPointerException success) {
1655 <        } finally {
1656 <            joinPool(e);
1650 >        final ExecutorService e =
1651 >            new CustomTPE(2, 2,
1652 >                          LONG_DELAY_MS, MILLISECONDS,
1653 >                          new ArrayBlockingQueue<Runnable>(10));
1654 >        try (PoolCleaner cleaner = cleaner(e)) {
1655 >            try {
1656 >                e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1657 >                shouldThrow();
1658 >            } catch (NullPointerException success) {}
1659          }
1660      }
1661  
# Line 1635 | Line 1663 | public class ThreadPoolExecutorSubclassT
1663       * timed invokeAny(,,null) throws NPE
1664       */
1665      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1666 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1667 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1668 <        l.add(new StringTask());
1669 <        try {
1670 <            e.invokeAny(l, MEDIUM_DELAY_MS, null);
1671 <            shouldThrow();
1672 <        } catch (NullPointerException success) {
1673 <        } finally {
1674 <            joinPool(e);
1666 >        final ExecutorService e =
1667 >            new CustomTPE(2, 2,
1668 >                          LONG_DELAY_MS, MILLISECONDS,
1669 >                          new ArrayBlockingQueue<Runnable>(10));
1670 >        try (PoolCleaner cleaner = cleaner(e)) {
1671 >            List<Callable<String>> l = new ArrayList<>();
1672 >            l.add(new StringTask());
1673 >            try {
1674 >                e.invokeAny(l, MEDIUM_DELAY_MS, null);
1675 >                shouldThrow();
1676 >            } catch (NullPointerException success) {}
1677          }
1678      }
1679  
# Line 1651 | Line 1681 | public class ThreadPoolExecutorSubclassT
1681       * timed invokeAny(empty collection) throws IAE
1682       */
1683      public void testTimedInvokeAny2() throws Exception {
1684 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1685 <        try {
1686 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1687 <            shouldThrow();
1688 <        } catch (IllegalArgumentException success) {
1689 <        } finally {
1690 <            joinPool(e);
1684 >        final ExecutorService e =
1685 >            new CustomTPE(2, 2,
1686 >                          LONG_DELAY_MS, MILLISECONDS,
1687 >                          new ArrayBlockingQueue<Runnable>(10));
1688 >        try (PoolCleaner cleaner = cleaner(e)) {
1689 >            try {
1690 >                e.invokeAny(new ArrayList<Callable<String>>(),
1691 >                            MEDIUM_DELAY_MS, MILLISECONDS);
1692 >                shouldThrow();
1693 >            } catch (IllegalArgumentException success) {}
1694          }
1695      }
1696  
# Line 1666 | Line 1699 | public class ThreadPoolExecutorSubclassT
1699       */
1700      public void testTimedInvokeAny3() throws Exception {
1701          CountDownLatch latch = new CountDownLatch(1);
1702 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1703 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1704 <        l.add(latchAwaitingStringTask(latch));
1705 <        l.add(null);
1706 <        try {
1707 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1708 <            shouldThrow();
1709 <        } catch (NullPointerException success) {
1710 <        } finally {
1702 >        final ExecutorService e =
1703 >            new CustomTPE(2, 2,
1704 >                          LONG_DELAY_MS, MILLISECONDS,
1705 >                          new ArrayBlockingQueue<Runnable>(10));
1706 >        try (PoolCleaner cleaner = cleaner(e)) {
1707 >            List<Callable<String>> l = new ArrayList<>();
1708 >            l.add(latchAwaitingStringTask(latch));
1709 >            l.add(null);
1710 >            try {
1711 >                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1712 >                shouldThrow();
1713 >            } catch (NullPointerException success) {}
1714              latch.countDown();
1679            joinPool(e);
1715          }
1716      }
1717  
# Line 1684 | Line 1719 | public class ThreadPoolExecutorSubclassT
1719       * timed invokeAny(c) throws ExecutionException if no task completes
1720       */
1721      public void testTimedInvokeAny4() throws Exception {
1722 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1723 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1724 <        l.add(new NPETask());
1725 <        try {
1726 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1727 <            shouldThrow();
1728 <        } catch (ExecutionException success) {
1729 <            assertTrue(success.getCause() instanceof NullPointerException);
1730 <        } finally {
1731 <            joinPool(e);
1722 >        final ExecutorService e =
1723 >            new CustomTPE(2, 2,
1724 >                          LONG_DELAY_MS, MILLISECONDS,
1725 >                          new ArrayBlockingQueue<Runnable>(10));
1726 >        try (PoolCleaner cleaner = cleaner(e)) {
1727 >            long startTime = System.nanoTime();
1728 >            List<Callable<String>> l = new ArrayList<>();
1729 >            l.add(new NPETask());
1730 >            try {
1731 >                e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1732 >                shouldThrow();
1733 >            } catch (ExecutionException success) {
1734 >                assertTrue(success.getCause() instanceof NullPointerException);
1735 >            }
1736 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1737          }
1738      }
1739  
# Line 1701 | Line 1741 | public class ThreadPoolExecutorSubclassT
1741       * timed invokeAny(c) returns result of some task
1742       */
1743      public void testTimedInvokeAny5() throws Exception {
1744 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1745 <        try {
1746 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1744 >        final ExecutorService e =
1745 >            new CustomTPE(2, 2,
1746 >                          LONG_DELAY_MS, MILLISECONDS,
1747 >                          new ArrayBlockingQueue<Runnable>(10));
1748 >        try (PoolCleaner cleaner = cleaner(e)) {
1749 >            long startTime = System.nanoTime();
1750 >            List<Callable<String>> l = new ArrayList<>();
1751              l.add(new StringTask());
1752              l.add(new StringTask());
1753 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1753 >            String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1754              assertSame(TEST_STRING, result);
1755 <        } finally {
1712 <            joinPool(e);
1755 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1756          }
1757      }
1758  
# Line 1717 | Line 1760 | public class ThreadPoolExecutorSubclassT
1760       * timed invokeAll(null) throws NPE
1761       */
1762      public void testTimedInvokeAll1() throws Exception {
1763 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1764 <        try {
1765 <            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1766 <            shouldThrow();
1767 <        } catch (NullPointerException success) {
1768 <        } finally {
1769 <            joinPool(e);
1763 >        final ExecutorService e =
1764 >            new CustomTPE(2, 2,
1765 >                          LONG_DELAY_MS, MILLISECONDS,
1766 >                          new ArrayBlockingQueue<Runnable>(10));
1767 >        try (PoolCleaner cleaner = cleaner(e)) {
1768 >            try {
1769 >                e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1770 >                shouldThrow();
1771 >            } catch (NullPointerException success) {}
1772          }
1773      }
1774  
# Line 1731 | Line 1776 | public class ThreadPoolExecutorSubclassT
1776       * timed invokeAll(,,null) throws NPE
1777       */
1778      public void testTimedInvokeAllNullTimeUnit() throws Exception {
1779 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1780 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1781 <        l.add(new StringTask());
1782 <        try {
1783 <            e.invokeAll(l, MEDIUM_DELAY_MS, null);
1784 <            shouldThrow();
1785 <        } catch (NullPointerException success) {
1786 <        } finally {
1787 <            joinPool(e);
1779 >        final ExecutorService e =
1780 >            new CustomTPE(2, 2,
1781 >                          LONG_DELAY_MS, MILLISECONDS,
1782 >                          new ArrayBlockingQueue<Runnable>(10));
1783 >        try (PoolCleaner cleaner = cleaner(e)) {
1784 >            List<Callable<String>> l = new ArrayList<>();
1785 >            l.add(new StringTask());
1786 >            try {
1787 >                e.invokeAll(l, MEDIUM_DELAY_MS, null);
1788 >                shouldThrow();
1789 >            } catch (NullPointerException success) {}
1790          }
1791      }
1792  
# Line 1747 | Line 1794 | public class ThreadPoolExecutorSubclassT
1794       * timed invokeAll(empty collection) returns empty collection
1795       */
1796      public void testTimedInvokeAll2() throws Exception {
1797 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1798 <        try {
1799 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1797 >        final ExecutorService e =
1798 >            new CustomTPE(2, 2,
1799 >                          LONG_DELAY_MS, MILLISECONDS,
1800 >                          new ArrayBlockingQueue<Runnable>(10));
1801 >        try (PoolCleaner cleaner = cleaner(e)) {
1802 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(),
1803 >                                                 MEDIUM_DELAY_MS, MILLISECONDS);
1804              assertTrue(r.isEmpty());
1754        } finally {
1755            joinPool(e);
1805          }
1806      }
1807  
# Line 1760 | Line 1809 | public class ThreadPoolExecutorSubclassT
1809       * timed invokeAll(c) throws NPE if c has null elements
1810       */
1811      public void testTimedInvokeAll3() throws Exception {
1812 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1813 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1814 <        l.add(new StringTask());
1815 <        l.add(null);
1816 <        try {
1817 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1818 <            shouldThrow();
1819 <        } catch (NullPointerException success) {
1820 <        } finally {
1821 <            joinPool(e);
1812 >        final ExecutorService e =
1813 >            new CustomTPE(2, 2,
1814 >                          LONG_DELAY_MS, MILLISECONDS,
1815 >                          new ArrayBlockingQueue<Runnable>(10));
1816 >        try (PoolCleaner cleaner = cleaner(e)) {
1817 >            List<Callable<String>> l = new ArrayList<>();
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          }
1825      }
1826  
# Line 1777 | Line 1828 | public class ThreadPoolExecutorSubclassT
1828       * get of element of invokeAll(c) throws exception on failed task
1829       */
1830      public void testTimedInvokeAll4() throws Exception {
1831 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1832 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1833 <        l.add(new NPETask());
1834 <        List<Future<String>> futures =
1835 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1836 <        assertEquals(1, futures.size());
1837 <        try {
1838 <            futures.get(0).get();
1839 <            shouldThrow();
1840 <        } catch (ExecutionException success) {
1841 <            assertTrue(success.getCause() instanceof NullPointerException);
1842 <        } finally {
1843 <            joinPool(e);
1831 >        final ExecutorService e =
1832 >            new CustomTPE(2, 2,
1833 >                          LONG_DELAY_MS, MILLISECONDS,
1834 >                          new ArrayBlockingQueue<Runnable>(10));
1835 >        try (PoolCleaner cleaner = cleaner(e)) {
1836 >            List<Callable<String>> l = new ArrayList<>();
1837 >            l.add(new NPETask());
1838 >            List<Future<String>> futures =
1839 >                e.invokeAll(l, LONG_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 >            }
1847          }
1848      }
1849  
# Line 1797 | Line 1851 | public class ThreadPoolExecutorSubclassT
1851       * timed invokeAll(c) returns results of all completed tasks
1852       */
1853      public void testTimedInvokeAll5() throws Exception {
1854 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1855 <        try {
1856 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1854 >        final ExecutorService e =
1855 >            new CustomTPE(2, 2,
1856 >                          LONG_DELAY_MS, MILLISECONDS,
1857 >                          new ArrayBlockingQueue<Runnable>(10));
1858 >        try (PoolCleaner cleaner = cleaner(e)) {
1859 >            List<Callable<String>> l = new ArrayList<>();
1860              l.add(new StringTask());
1861              l.add(new StringTask());
1862              List<Future<String>> futures =
# Line 1807 | Line 1864 | public class ThreadPoolExecutorSubclassT
1864              assertEquals(2, futures.size());
1865              for (Future<String> future : futures)
1866                  assertSame(TEST_STRING, future.get());
1810        } finally {
1811            joinPool(e);
1867          }
1868      }
1869  
# Line 1816 | Line 1871 | public class ThreadPoolExecutorSubclassT
1871       * timed invokeAll(c) cancels tasks not completed by timeout
1872       */
1873      public void testTimedInvokeAll6() throws Exception {
1874 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1875 <        try {
1876 <            for (long timeout = timeoutMillis();;) {
1874 >        for (long timeout = timeoutMillis();;) {
1875 >            final CountDownLatch done = new CountDownLatch(1);
1876 >            final Callable<String> waiter = new CheckedCallable<String>() {
1877 >                public String realCall() {
1878 >                    try { done.await(LONG_DELAY_MS, MILLISECONDS); }
1879 >                    catch (InterruptedException ok) {}
1880 >                    return "1"; }};
1881 >            final ExecutorService p =
1882 >                new CustomTPE(2, 2,
1883 >                              LONG_DELAY_MS, MILLISECONDS,
1884 >                              new ArrayBlockingQueue<Runnable>(10));
1885 >            try (PoolCleaner cleaner = cleaner(p, done)) {
1886                  List<Callable<String>> tasks = new ArrayList<>();
1887                  tasks.add(new StringTask("0"));
1888 <                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1888 >                tasks.add(waiter);
1889                  tasks.add(new StringTask("2"));
1890                  long startTime = System.nanoTime();
1891                  List<Future<String>> futures =
1892 <                    e.invokeAll(tasks, timeout, MILLISECONDS);
1892 >                    p.invokeAll(tasks, timeout, MILLISECONDS);
1893                  assertEquals(tasks.size(), futures.size());
1894                  assertTrue(millisElapsedSince(startTime) >= timeout);
1895                  for (Future future : futures)
# Line 1841 | Line 1905 | public class ThreadPoolExecutorSubclassT
1905                          fail("expected exactly one task to be cancelled");
1906                  }
1907              }
1844        } finally {
1845            joinPool(e);
1908          }
1909      }
1910  
# Line 1856 | Line 1918 | public class ThreadPoolExecutorSubclassT
1918                            LONG_DELAY_MS, MILLISECONDS,
1919                            new LinkedBlockingQueue<Runnable>(),
1920                            new FailingThreadFactory());
1921 <        try {
1921 >        try (PoolCleaner cleaner = cleaner(e)) {
1922              final int TASKS = 100;
1923              final CountDownLatch done = new CountDownLatch(TASKS);
1924              for (int k = 0; k < TASKS; ++k)
# Line 1864 | Line 1926 | public class ThreadPoolExecutorSubclassT
1926                      public void realRun() {
1927                          done.countDown();
1928                      }});
1929 <            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1868 <        } finally {
1869 <            joinPool(e);
1929 >            await(done);
1930          }
1931      }
1932  
# Line 1874 | Line 1934 | public class ThreadPoolExecutorSubclassT
1934       * allowsCoreThreadTimeOut is by default false.
1935       */
1936      public void testAllowsCoreThreadTimeOut() {
1937 <        ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1938 <        assertFalse(p.allowsCoreThreadTimeOut());
1939 <        joinPool(p);
1937 >        final ThreadPoolExecutor p =
1938 >            new CustomTPE(2, 2,
1939 >                          1000, MILLISECONDS,
1940 >                          new ArrayBlockingQueue<Runnable>(10));
1941 >        try (PoolCleaner cleaner = cleaner(p)) {
1942 >            assertFalse(p.allowsCoreThreadTimeOut());
1943 >        }
1944      }
1945  
1946      /**
# Line 1888 | Line 1952 | public class ThreadPoolExecutorSubclassT
1952              new CustomTPE(2, 10,
1953                            keepAliveTime, MILLISECONDS,
1954                            new ArrayBlockingQueue<Runnable>(10));
1955 <        final CountDownLatch threadStarted = new CountDownLatch(1);
1956 <        try {
1955 >        try (PoolCleaner cleaner = cleaner(p)) {
1956 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1957              p.allowCoreThreadTimeOut(true);
1958              p.execute(new CheckedRunnable() {
1959                  public void realRun() {
# Line 1904 | Line 1968 | public class ThreadPoolExecutorSubclassT
1968                  Thread.yield();
1969              assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1970              assertEquals(0, p.getPoolSize());
1907        } finally {
1908            joinPool(p);
1971          }
1972      }
1973  
# Line 1918 | Line 1980 | public class ThreadPoolExecutorSubclassT
1980              new CustomTPE(2, 10,
1981                            keepAliveTime, MILLISECONDS,
1982                            new ArrayBlockingQueue<Runnable>(10));
1983 <        final CountDownLatch threadStarted = new CountDownLatch(1);
1984 <        try {
1983 >        try (PoolCleaner cleaner = cleaner(p)) {
1984 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1985              p.allowCoreThreadTimeOut(false);
1986              p.execute(new CheckedRunnable() {
1987                  public void realRun() throws InterruptedException {
# Line 1928 | Line 1990 | public class ThreadPoolExecutorSubclassT
1990                  }});
1991              delay(2 * keepAliveTime);
1992              assertTrue(p.getPoolSize() >= 1);
1931        } finally {
1932            joinPool(p);
1993          }
1994      }
1995  
# Line 1938 | Line 1998 | public class ThreadPoolExecutorSubclassT
1998       * (in part, a test of CustomTPE itself)
1999       */
2000      public void testGet_cancelled() throws Exception {
2001 +        final CountDownLatch done = new CountDownLatch(1);
2002          final ExecutorService e =
2003              new CustomTPE(1, 1,
2004                            LONG_DELAY_MS, MILLISECONDS,
2005                            new LinkedBlockingQueue<Runnable>());
2006 <        try {
2006 >        try (PoolCleaner cleaner = cleaner(e, done)) {
2007              final CountDownLatch blockerStarted = new CountDownLatch(1);
1947            final CountDownLatch done = new CountDownLatch(1);
2008              final List<Future<?>> futures = new ArrayList<>();
2009              for (int i = 0; i < 2; i++) {
2010                  Runnable r = new CheckedRunnable() { public void realRun()
# Line 1954 | Line 2014 | public class ThreadPoolExecutorSubclassT
2014                  }};
2015                  futures.add(e.submit(r));
2016              }
2017 <            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
2017 >            await(blockerStarted);
2018              for (Future<?> future : futures) future.cancel(false);
2019              for (Future<?> future : futures) {
2020                  try {
# Line 1968 | Line 2028 | public class ThreadPoolExecutorSubclassT
2028                  assertTrue(future.isCancelled());
2029                  assertTrue(future.isDone());
2030              }
1971            done.countDown();
1972        } finally {
1973            joinPool(e);
2031          }
2032      }
2033  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines