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

Comparing jsr166/src/test/tck/ThreadPoolExecutorTest.java (file contents):
Revision 1.93 by jsr166, Sun Oct 4 03:51:35 2015 UTC vs.
Revision 1.107 by jsr166, Tue Oct 6 05:22:25 2015 UTC

# Line 113 | Line 113 | public class ThreadPoolExecutorTest exte
113                  public void realRun() throws InterruptedException {
114                      threadStarted.countDown();
115                      assertEquals(1, p.getActiveCount());
116 <                    done.await();
116 >                    await(done);
117                  }});
118 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
118 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
119              assertEquals(1, p.getActiveCount());
120              done.countDown();
121          }
# Line 329 | Line 329 | public class ThreadPoolExecutorTest exte
329       */
330      public void testGetLargestPoolSize() throws InterruptedException {
331          final int THREADS = 3;
332 +        final CountDownLatch done = new CountDownLatch(1);
333          final ThreadPoolExecutor p =
334              new ThreadPoolExecutor(THREADS, THREADS,
335                                     LONG_DELAY_MS, MILLISECONDS,
336                                     new ArrayBlockingQueue<Runnable>(10));
337 <        try (PoolCleaner cleaner = cleaner(p)) {
337 <            final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
338 <            final CountDownLatch done = new CountDownLatch(1);
337 >        try (PoolCleaner cleaner = cleaner(p, done)) {
338              assertEquals(0, p.getLargestPoolSize());
339 +            final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
340              for (int i = 0; i < THREADS; i++)
341                  p.execute(new CheckedRunnable() {
342                      public void realRun() throws InterruptedException {
343                          threadsStarted.countDown();
344 <                        done.await();
344 >                        await(done);
345                          assertEquals(THREADS, p.getLargestPoolSize());
346                      }});
347 <            assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
347 >            await(threadsStarted);
348              assertEquals(THREADS, p.getLargestPoolSize());
349            done.countDown();   // release pool
349          }
350          assertEquals(THREADS, p.getLargestPoolSize());
351      }
# Line 374 | Line 373 | public class ThreadPoolExecutorTest exte
373       * become active
374       */
375      public void testGetPoolSize() throws InterruptedException {
376 +        final CountDownLatch done = new CountDownLatch(1);
377          final ThreadPoolExecutor p =
378              new ThreadPoolExecutor(1, 1,
379                                     LONG_DELAY_MS, MILLISECONDS,
380                                     new ArrayBlockingQueue<Runnable>(10));
381 <        try (PoolCleaner cleaner = cleaner(p)) {
382 <            final CountDownLatch threadStarted = new CountDownLatch(1);
383 <            final CountDownLatch done = new CountDownLatch(1);
381 >        try (PoolCleaner cleaner = cleaner(p, done)) {
382              assertEquals(0, p.getPoolSize());
383 +            final CountDownLatch threadStarted = new CountDownLatch(1);
384              p.execute(new CheckedRunnable() {
385                  public void realRun() throws InterruptedException {
386                      threadStarted.countDown();
387                      assertEquals(1, p.getPoolSize());
388 <                    done.await();
388 >                    await(done);
389                  }});
390 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
390 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
391              assertEquals(1, p.getPoolSize());
393            done.countDown();   // release pool
392          }
393      }
394  
# Line 398 | Line 396 | public class ThreadPoolExecutorTest exte
396       * getTaskCount increases, but doesn't overestimate, when tasks submitted
397       */
398      public void testGetTaskCount() throws InterruptedException {
399 +        final int TASKS = 3;
400 +        final CountDownLatch done = new CountDownLatch(1);
401          final ThreadPoolExecutor p =
402              new ThreadPoolExecutor(1, 1,
403                                     LONG_DELAY_MS, MILLISECONDS,
404                                     new ArrayBlockingQueue<Runnable>(10));
405 <        try (PoolCleaner cleaner = cleaner(p)) {
405 >        try (PoolCleaner cleaner = cleaner(p, done)) {
406              final CountDownLatch threadStarted = new CountDownLatch(1);
407            final CountDownLatch done = new CountDownLatch(1);
407              assertEquals(0, p.getTaskCount());
408 +            assertEquals(0, p.getCompletedTaskCount());
409              p.execute(new CheckedRunnable() {
410                  public void realRun() throws InterruptedException {
411                      threadStarted.countDown();
412 <                    assertEquals(1, p.getTaskCount());
413 <                    done.await();
412 >                    await(done);
413                  }});
414 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
414 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
415              assertEquals(1, p.getTaskCount());
416 <            done.countDown();
416 >            assertEquals(0, p.getCompletedTaskCount());
417 >            for (int i = 0; i < TASKS; i++) {
418 >                assertEquals(1 + i, p.getTaskCount());
419 >                p.execute(new CheckedRunnable() {
420 >                    public void realRun() throws InterruptedException {
421 >                        threadStarted.countDown();
422 >                        assertEquals(1 + TASKS, p.getTaskCount());
423 >                        await(done);
424 >                    }});
425 >            }
426 >            assertEquals(1 + TASKS, p.getTaskCount());
427 >            assertEquals(0, p.getCompletedTaskCount());
428          }
429 +        assertEquals(1 + TASKS, p.getTaskCount());
430 +        assertEquals(1 + TASKS, p.getCompletedTaskCount());
431      }
432  
433      /**
# Line 481 | Line 493 | public class ThreadPoolExecutorTest exte
493                  public void realRun() throws InterruptedException {
494                      assertFalse(p.isTerminating());
495                      threadStarted.countDown();
496 <                    done.await();
496 >                    await(done);
497                  }});
498 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
498 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
499              assertFalse(p.isTerminating());
500              done.countDown();
501              try { p.shutdown(); } catch (SecurityException ok) { return; }
# Line 509 | Line 521 | public class ThreadPoolExecutorTest exte
521                  public void realRun() throws InterruptedException {
522                      assertFalse(p.isTerminating());
523                      threadStarted.countDown();
524 <                    done.await();
524 >                    await(done);
525                  }});
526 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
526 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
527              assertFalse(p.isTerminating());
528              done.countDown();
529              try { p.shutdown(); } catch (SecurityException ok) { return; }
# Line 539 | Line 551 | public class ThreadPoolExecutorTest exte
551                      public Boolean realCall() throws InterruptedException {
552                          threadStarted.countDown();
553                          assertSame(q, p.getQueue());
554 <                        done.await();
554 >                        await(done);
555                          return Boolean.TRUE;
556                      }};
557                  tasks[i] = new FutureTask(task);
558                  p.execute(tasks[i]);
559              }
560 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
560 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
561              assertSame(q, p.getQueue());
562              assertFalse(q.contains(tasks[0]));
563              assertTrue(q.contains(tasks[tasks.length - 1]));
# Line 571 | Line 583 | public class ThreadPoolExecutorTest exte
583                  tasks[i] = new CheckedRunnable() {
584                      public void realRun() throws InterruptedException {
585                          threadStarted.countDown();
586 <                        done.await();
586 >                        await(done);
587                      }};
588                  p.execute(tasks[i]);
589              }
590 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
590 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
591              assertFalse(p.remove(tasks[0]));
592              assertTrue(q.contains(tasks[4]));
593              assertTrue(q.contains(tasks[3]));
# Line 600 | Line 612 | public class ThreadPoolExecutorTest exte
612              new ThreadPoolExecutor(1, 1,
613                                     LONG_DELAY_MS, MILLISECONDS,
614                                     q);
615 <        try (PoolCleaner cleaner = cleaner(p)) {
615 >        try (PoolCleaner cleaner = cleaner(p, done)) {
616              FutureTask[] tasks = new FutureTask[5];
617              for (int i = 0; i < tasks.length; i++) {
618                  Callable task = new CheckedCallable<Boolean>() {
619                      public Boolean realCall() throws InterruptedException {
620                          threadStarted.countDown();
621 <                        done.await();
621 >                        await(done);
622                          return Boolean.TRUE;
623                      }};
624                  tasks[i] = new FutureTask(task);
625                  p.execute(tasks[i]);
626              }
627 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
627 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
628              assertEquals(tasks.length, p.getTaskCount());
629              assertEquals(tasks.length - 1, q.size());
630              assertEquals(1L, p.getActiveCount());
# Line 625 | Line 637 | public class ThreadPoolExecutorTest exte
637              p.purge();         // Nothing to do
638              assertEquals(tasks.length - 3, q.size());
639              assertEquals(tasks.length - 2, p.getTaskCount());
628            done.countDown();
640          }
641      }
642  
# Line 641 | Line 652 | public class ThreadPoolExecutorTest exte
652              new ThreadPoolExecutor(poolSize, poolSize,
653                                     LONG_DELAY_MS, MILLISECONDS,
654                                     new ArrayBlockingQueue<Runnable>(10));
655 <        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
655 >        final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
656          Runnable waiter = new CheckedRunnable() { public void realRun() {
657              threadsStarted.countDown();
658              try {
# Line 651 | Line 662 | public class ThreadPoolExecutorTest exte
662          }};
663          for (int i = 0; i < count; i++)
664              p.execute(waiter);
665 <        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
665 >        await(threadsStarted);
666          assertEquals(poolSize, p.getActiveCount());
667          assertEquals(0, p.getCompletedTaskCount());
668          final List<Runnable> queuedTasks;
# Line 1013 | Line 1024 | public class ThreadPoolExecutorTest exte
1024       * get of submitted callable throws InterruptedException if interrupted
1025       */
1026      public void testInterruptedSubmit() throws InterruptedException {
1027 +        final CountDownLatch done = new CountDownLatch(1);
1028          final ThreadPoolExecutor p =
1029              new ThreadPoolExecutor(1, 1,
1030                                     60, SECONDS,
1031                                     new ArrayBlockingQueue<Runnable>(10));
1032  
1033 <        try (PoolCleaner cleaner = cleaner(p)) {
1033 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1034              final CountDownLatch threadStarted = new CountDownLatch(1);
1023            final CountDownLatch done = new CountDownLatch(1);
1035              Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1036                  public void realRun() throws Exception {
1037                      Callable task = new CheckedCallable<Boolean>() {
1038                          public Boolean realCall() throws InterruptedException {
1039                              threadStarted.countDown();
1040 <                            done.await();
1040 >                            await(done);
1041                              return Boolean.TRUE;
1042                          }};
1043                      p.submit(task).get();
1044                  }});
1045  
1046 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
1046 >            await(threadStarted);
1047              t.interrupt();
1048 <            awaitTermination(t, MEDIUM_DELAY_MS);
1038 <            done.countDown();
1048 >            awaitTermination(t);
1049          }
1050      }
1051  
# Line 1051 | Line 1061 | public class ThreadPoolExecutorTest exte
1061              final CountDownLatch done = new CountDownLatch(1);
1062              Runnable task = new CheckedRunnable() {
1063                  public void realRun() throws InterruptedException {
1064 <                    done.await();
1064 >                    await(done);
1065                  }};
1066              for (int i = 0; i < 2; ++i)
1067                  p.execute(task);
# Line 1070 | Line 1080 | public class ThreadPoolExecutorTest exte
1080       * submit(runnable) throws RejectedExecutionException if saturated.
1081       */
1082      public void testSaturatedSubmitRunnable() {
1083 <        ThreadPoolExecutor p =
1083 >        final ThreadPoolExecutor p =
1084              new ThreadPoolExecutor(1, 1,
1085                                     LONG_DELAY_MS, MILLISECONDS,
1086                                     new ArrayBlockingQueue<Runnable>(1));
1087 <        final CountDownLatch done = new CountDownLatch(1);
1088 <        try {
1087 >        try (PoolCleaner cleaner = cleaner(p)) {
1088 >            final CountDownLatch done = new CountDownLatch(1);
1089              Runnable task = new CheckedRunnable() {
1090                  public void realRun() throws InterruptedException {
1091 <                    done.await();
1091 >                    await(done);
1092                  }};
1093              for (int i = 0; i < 2; ++i)
1094                  p.submit(task);
# Line 1089 | Line 1099 | public class ThreadPoolExecutorTest exte
1099                  } catch (RejectedExecutionException success) {}
1100                  assertTrue(p.getTaskCount() <= 2);
1101              }
1092        } finally {
1102              done.countDown();
1094            joinPool(p);
1103          }
1104      }
1105  
# Line 1099 | Line 1107 | public class ThreadPoolExecutorTest exte
1107       * submit(callable) throws RejectedExecutionException if saturated.
1108       */
1109      public void testSaturatedSubmitCallable() {
1110 <        ThreadPoolExecutor p =
1110 >        final ThreadPoolExecutor p =
1111              new ThreadPoolExecutor(1, 1,
1112                                     LONG_DELAY_MS, MILLISECONDS,
1113                                     new ArrayBlockingQueue<Runnable>(1));
1114 <        final CountDownLatch done = new CountDownLatch(1);
1115 <        try {
1114 >        try (PoolCleaner cleaner = cleaner(p)) {
1115 >            final CountDownLatch done = new CountDownLatch(1);
1116              Runnable task = new CheckedRunnable() {
1117                  public void realRun() throws InterruptedException {
1118 <                    done.await();
1118 >                    await(done);
1119                  }};
1120              for (int i = 0; i < 2; ++i)
1121                  p.submit(Executors.callable(task));
# Line 1118 | Line 1126 | public class ThreadPoolExecutorTest exte
1126                  } catch (RejectedExecutionException success) {}
1127                  assertTrue(p.getTaskCount() <= 2);
1128              }
1121        } finally {
1129              done.countDown();
1123            joinPool(p);
1130          }
1131      }
1132  
# Line 1138 | Line 1144 | public class ThreadPoolExecutorTest exte
1144              final CountDownLatch done = new CountDownLatch(1);
1145              Runnable blocker = new CheckedRunnable() {
1146                  public void realRun() throws InterruptedException {
1147 <                    done.await();
1147 >                    await(done);
1148                  }};
1149              p.execute(blocker);
1150              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
# Line 1214 | Line 1220 | public class ThreadPoolExecutorTest exte
1220       * execute throws RejectedExecutionException if shutdown
1221       */
1222      public void testRejectedExecutionExceptionOnShutdown() {
1223 <        ThreadPoolExecutor p =
1223 >        final ThreadPoolExecutor p =
1224              new ThreadPoolExecutor(1, 1,
1225                                     LONG_DELAY_MS, MILLISECONDS,
1226                                     new ArrayBlockingQueue<Runnable>(1));
# Line 1238 | Line 1244 | public class ThreadPoolExecutorTest exte
1244                                     new ArrayBlockingQueue<Runnable>(1), h);
1245  
1246          try { p.shutdown(); } catch (SecurityException ok) { return; }
1247 <        try {
1247 >        try (PoolCleaner cleaner = cleaner(p)) {
1248              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1249              p.execute(r);
1250              assertFalse(r.done);
1245        } finally {
1246            joinPool(p);
1251          }
1252      }
1253  
# Line 1251 | Line 1255 | public class ThreadPoolExecutorTest exte
1255       * execute using DiscardPolicy drops task on shutdown
1256       */
1257      public void testDiscardOnShutdown() {
1258 <        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
1255 <        ThreadPoolExecutor p =
1258 >        final ThreadPoolExecutor p =
1259              new ThreadPoolExecutor(1, 1,
1260                                     LONG_DELAY_MS, MILLISECONDS,
1261                                     new ArrayBlockingQueue<Runnable>(1),
1262 <                                   h);
1262 >                                   new ThreadPoolExecutor.DiscardPolicy());
1263  
1264          try { p.shutdown(); } catch (SecurityException ok) { return; }
1265 <        try {
1265 >        try (PoolCleaner cleaner = cleaner(p)) {
1266              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1267              p.execute(r);
1268              assertFalse(r.done);
1266        } finally {
1267            joinPool(p);
1269          }
1270      }
1271  
# Line 1272 | Line 1273 | public class ThreadPoolExecutorTest exte
1273       * execute using DiscardOldestPolicy drops task on shutdown
1274       */
1275      public void testDiscardOldestOnShutdown() {
1276 <        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
1276 <        ThreadPoolExecutor p =
1276 >        final ThreadPoolExecutor p =
1277              new ThreadPoolExecutor(1, 1,
1278                                     LONG_DELAY_MS, MILLISECONDS,
1279                                     new ArrayBlockingQueue<Runnable>(1),
1280 <                                   h);
1280 >                                   new ThreadPoolExecutor.DiscardOldestPolicy());
1281  
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);
1287        } finally {
1288            joinPool(p);
1287          }
1288      }
1289  
# Line 1293 | Line 1291 | public class ThreadPoolExecutorTest exte
1291       * execute(null) throws NPE
1292       */
1293      public void testExecuteNull() {
1294 <        ThreadPoolExecutor p =
1295 <            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1294 >        final ThreadPoolExecutor p =
1295 >            new ThreadPoolExecutor(1, 2,
1296 >                                   1L, SECONDS,
1297                                     new ArrayBlockingQueue<Runnable>(10));
1298 <        try {
1299 <            p.execute(null);
1300 <            shouldThrow();
1301 <        } catch (NullPointerException success) {}
1302 <
1303 <        joinPool(p);
1298 >        try (PoolCleaner cleaner = cleaner(p)) {
1299 >            try {
1300 >                p.execute(null);
1301 >                shouldThrow();
1302 >            } catch (NullPointerException success) {}
1303 >        }
1304      }
1305  
1306      /**
1307       * setCorePoolSize of negative value throws IllegalArgumentException
1308       */
1309      public void testCorePoolSizeIllegalArgumentException() {
1310 <        ThreadPoolExecutor p =
1310 >        final ThreadPoolExecutor p =
1311              new ThreadPoolExecutor(1, 2,
1312                                     LONG_DELAY_MS, MILLISECONDS,
1313                                     new ArrayBlockingQueue<Runnable>(10));
1314 <        try {
1315 <            p.setCorePoolSize(-1);
1316 <            shouldThrow();
1317 <        } catch (IllegalArgumentException success) {
1318 <        } finally {
1320 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1314 >        try (PoolCleaner cleaner = cleaner(p)) {
1315 >            try {
1316 >                p.setCorePoolSize(-1);
1317 >                shouldThrow();
1318 >            } catch (IllegalArgumentException success) {}
1319          }
1322        joinPool(p);
1320      }
1321  
1322      /**
# Line 1327 | Line 1324 | public class ThreadPoolExecutorTest exte
1324       * given a value less the core pool size
1325       */
1326      public void testMaximumPoolSizeIllegalArgumentException() {
1327 <        ThreadPoolExecutor p =
1327 >        final ThreadPoolExecutor p =
1328              new ThreadPoolExecutor(2, 3,
1329                                     LONG_DELAY_MS, MILLISECONDS,
1330                                     new ArrayBlockingQueue<Runnable>(10));
1331 <        try {
1332 <            p.setMaximumPoolSize(1);
1333 <            shouldThrow();
1334 <        } catch (IllegalArgumentException success) {
1335 <        } finally {
1339 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1331 >        try (PoolCleaner cleaner = cleaner(p)) {
1332 >            try {
1333 >                p.setMaximumPoolSize(1);
1334 >                shouldThrow();
1335 >            } catch (IllegalArgumentException success) {}
1336          }
1341        joinPool(p);
1337      }
1338  
1339      /**
# Line 1346 | Line 1341 | public class ThreadPoolExecutorTest exte
1341       * if given a negative value
1342       */
1343      public void testMaximumPoolSizeIllegalArgumentException2() {
1344 <        ThreadPoolExecutor p =
1344 >        final ThreadPoolExecutor p =
1345              new ThreadPoolExecutor(2, 3,
1346                                     LONG_DELAY_MS, MILLISECONDS,
1347                                     new ArrayBlockingQueue<Runnable>(10));
1348 <        try {
1349 <            p.setMaximumPoolSize(-1);
1350 <            shouldThrow();
1351 <        } catch (IllegalArgumentException success) {
1352 <        } finally {
1358 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1348 >        try (PoolCleaner cleaner = cleaner(p)) {
1349 >            try {
1350 >                p.setMaximumPoolSize(-1);
1351 >                shouldThrow();
1352 >            } catch (IllegalArgumentException success) {}
1353          }
1360        joinPool(p);
1354      }
1355  
1356      /**
# Line 1365 | Line 1358 | public class ThreadPoolExecutorTest exte
1358       * max pool size result in IllegalArgumentException.
1359       */
1360      public void testPoolSizeInvariants() {
1361 <        ThreadPoolExecutor p =
1361 >        final ThreadPoolExecutor p =
1362              new ThreadPoolExecutor(1, 1,
1363                                     LONG_DELAY_MS, MILLISECONDS,
1364                                     new ArrayBlockingQueue<Runnable>(10));
1365 <        for (int s = 1; s < 5; s++) {
1366 <            p.setMaximumPoolSize(s);
1367 <            p.setCorePoolSize(s);
1368 <            try {
1369 <                p.setMaximumPoolSize(s - 1);
1370 <                shouldThrow();
1371 <            } catch (IllegalArgumentException success) {}
1372 <            assertEquals(s, p.getCorePoolSize());
1373 <            assertEquals(s, p.getMaximumPoolSize());
1374 <            try {
1375 <                p.setCorePoolSize(s + 1);
1376 <                shouldThrow();
1377 <            } catch (IllegalArgumentException success) {}
1378 <            assertEquals(s, p.getCorePoolSize());
1379 <            assertEquals(s, p.getMaximumPoolSize());
1365 >        try (PoolCleaner cleaner = cleaner(p)) {
1366 >            for (int s = 1; s < 5; s++) {
1367 >                p.setMaximumPoolSize(s);
1368 >                p.setCorePoolSize(s);
1369 >                try {
1370 >                    p.setMaximumPoolSize(s - 1);
1371 >                    shouldThrow();
1372 >                } catch (IllegalArgumentException success) {}
1373 >                assertEquals(s, p.getCorePoolSize());
1374 >                assertEquals(s, p.getMaximumPoolSize());
1375 >                try {
1376 >                    p.setCorePoolSize(s + 1);
1377 >                    shouldThrow();
1378 >                } catch (IllegalArgumentException success) {}
1379 >                assertEquals(s, p.getCorePoolSize());
1380 >                assertEquals(s, p.getMaximumPoolSize());
1381 >            }
1382          }
1388        joinPool(p);
1383      }
1384  
1385      /**
# Line 1393 | Line 1387 | public class ThreadPoolExecutorTest exte
1387       * when given a negative value
1388       */
1389      public void testKeepAliveTimeIllegalArgumentException() {
1390 <        ThreadPoolExecutor p =
1390 >        final ThreadPoolExecutor p =
1391              new ThreadPoolExecutor(2, 3,
1392                                     LONG_DELAY_MS, MILLISECONDS,
1393                                     new ArrayBlockingQueue<Runnable>(10));
1394 <        try {
1395 <            p.setKeepAliveTime(-1,MILLISECONDS);
1396 <            shouldThrow();
1397 <        } catch (IllegalArgumentException success) {
1398 <        } finally {
1405 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1394 >        try (PoolCleaner cleaner = cleaner(p)) {
1395 >            try {
1396 >                p.setKeepAliveTime(-1, MILLISECONDS);
1397 >                shouldThrow();
1398 >            } catch (IllegalArgumentException success) {}
1399          }
1407        joinPool(p);
1400      }
1401  
1402      /**
# Line 1412 | Line 1404 | public class ThreadPoolExecutorTest exte
1404       */
1405      public void testTerminated() {
1406          ExtendedTPE p = new ExtendedTPE();
1407 <        try { p.shutdown(); } catch (SecurityException ok) { return; }
1408 <        assertTrue(p.terminatedCalled());
1409 <        joinPool(p);
1407 >        try (PoolCleaner cleaner = cleaner(p)) {
1408 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1409 >            assertTrue(p.terminatedCalled());
1410 >            assertTrue(p.isShutdown());
1411 >        }
1412      }
1413  
1414      /**
# Line 1422 | Line 1416 | public class ThreadPoolExecutorTest exte
1416       */
1417      public void testBeforeAfter() throws InterruptedException {
1418          ExtendedTPE p = new ExtendedTPE();
1419 <        try {
1419 >        try (PoolCleaner cleaner = cleaner(p)) {
1420              final CountDownLatch done = new CountDownLatch(1);
1421              p.execute(new CheckedRunnable() {
1422                  public void realRun() {
# Line 1432 | Line 1426 | public class ThreadPoolExecutorTest exte
1426              assertEquals(0, done.getCount());
1427              assertTrue(p.afterCalled());
1428              assertTrue(p.beforeCalled());
1435            try { p.shutdown(); } catch (SecurityException ok) { return; }
1436        } finally {
1437            joinPool(p);
1429          }
1430      }
1431  
# Line 1442 | Line 1433 | public class ThreadPoolExecutorTest exte
1433       * completed submit of callable returns result
1434       */
1435      public void testSubmitCallable() throws Exception {
1436 <        ExecutorService e =
1436 >        final ExecutorService e =
1437              new ThreadPoolExecutor(2, 2,
1438                                     LONG_DELAY_MS, MILLISECONDS,
1439                                     new ArrayBlockingQueue<Runnable>(10));
1440 <        try {
1440 >        try (PoolCleaner cleaner = cleaner(e)) {
1441              Future<String> future = e.submit(new StringTask());
1442              String result = future.get();
1443              assertSame(TEST_STRING, result);
1453        } finally {
1454            joinPool(e);
1444          }
1445      }
1446  
# Line 1459 | Line 1448 | public class ThreadPoolExecutorTest exte
1448       * completed submit of runnable returns successfully
1449       */
1450      public void testSubmitRunnable() throws Exception {
1451 <        ExecutorService e =
1451 >        final ExecutorService e =
1452              new ThreadPoolExecutor(2, 2,
1453                                     LONG_DELAY_MS, MILLISECONDS,
1454                                     new ArrayBlockingQueue<Runnable>(10));
1455 <        try {
1455 >        try (PoolCleaner cleaner = cleaner(e)) {
1456              Future<?> future = e.submit(new NoOpRunnable());
1457              future.get();
1458              assertTrue(future.isDone());
1470        } finally {
1471            joinPool(e);
1459          }
1460      }
1461  
# Line 1476 | Line 1463 | public class ThreadPoolExecutorTest exte
1463       * completed submit of (runnable, result) returns result
1464       */
1465      public void testSubmitRunnable2() throws Exception {
1466 <        ExecutorService e =
1466 >        final ExecutorService e =
1467              new ThreadPoolExecutor(2, 2,
1468                                     LONG_DELAY_MS, MILLISECONDS,
1469                                     new ArrayBlockingQueue<Runnable>(10));
1470 <        try {
1470 >        try (PoolCleaner cleaner = cleaner(e)) {
1471              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1472              String result = future.get();
1473              assertSame(TEST_STRING, result);
1487        } finally {
1488            joinPool(e);
1474          }
1475      }
1476  
# Line 1493 | Line 1478 | public class ThreadPoolExecutorTest exte
1478       * invokeAny(null) throws NPE
1479       */
1480      public void testInvokeAny1() throws Exception {
1481 <        ExecutorService e =
1481 >        final ExecutorService e =
1482              new ThreadPoolExecutor(2, 2,
1483                                     LONG_DELAY_MS, MILLISECONDS,
1484                                     new ArrayBlockingQueue<Runnable>(10));
1485 <        try {
1486 <            e.invokeAny(null);
1487 <            shouldThrow();
1488 <        } catch (NullPointerException success) {
1489 <        } finally {
1505 <            joinPool(e);
1485 >        try (PoolCleaner cleaner = cleaner(e)) {
1486 >            try {
1487 >                e.invokeAny(null);
1488 >                shouldThrow();
1489 >            } catch (NullPointerException success) {}
1490          }
1491      }
1492  
# Line 1510 | Line 1494 | public class ThreadPoolExecutorTest exte
1494       * invokeAny(empty collection) throws IAE
1495       */
1496      public void testInvokeAny2() throws Exception {
1497 <        ExecutorService e =
1497 >        final ExecutorService e =
1498              new ThreadPoolExecutor(2, 2,
1499                                     LONG_DELAY_MS, MILLISECONDS,
1500                                     new ArrayBlockingQueue<Runnable>(10));
1501 <        try {
1502 <            e.invokeAny(new ArrayList<Callable<String>>());
1503 <            shouldThrow();
1504 <        } catch (IllegalArgumentException success) {
1505 <        } finally {
1522 <            joinPool(e);
1501 >        try (PoolCleaner cleaner = cleaner(e)) {
1502 >            try {
1503 >                e.invokeAny(new ArrayList<Callable<String>>());
1504 >                shouldThrow();
1505 >            } catch (IllegalArgumentException success) {}
1506          }
1507      }
1508  
# Line 1532 | Line 1515 | public class ThreadPoolExecutorTest exte
1515              new ThreadPoolExecutor(2, 2,
1516                                     LONG_DELAY_MS, MILLISECONDS,
1517                                     new ArrayBlockingQueue<Runnable>(10));
1518 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1519 <        l.add(latchAwaitingStringTask(latch));
1520 <        l.add(null);
1521 <        try {
1522 <            e.invokeAny(l);
1523 <            shouldThrow();
1524 <        } catch (NullPointerException success) {
1525 <        } finally {
1518 >        try (PoolCleaner cleaner = cleaner(e)) {
1519 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1520 >            l.add(latchAwaitingStringTask(latch));
1521 >            l.add(null);
1522 >            try {
1523 >                e.invokeAny(l);
1524 >                shouldThrow();
1525 >            } catch (NullPointerException success) {}
1526              latch.countDown();
1544            joinPool(e);
1527          }
1528      }
1529  
# Line 1549 | Line 1531 | public class ThreadPoolExecutorTest exte
1531       * invokeAny(c) throws ExecutionException if no task completes
1532       */
1533      public void testInvokeAny4() throws Exception {
1534 <        ExecutorService e =
1534 >        final ExecutorService e =
1535              new ThreadPoolExecutor(2, 2,
1536                                     LONG_DELAY_MS, MILLISECONDS,
1537                                     new ArrayBlockingQueue<Runnable>(10));
1538 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1539 <        l.add(new NPETask());
1540 <        try {
1541 <            e.invokeAny(l);
1542 <            shouldThrow();
1543 <        } catch (ExecutionException success) {
1544 <            assertTrue(success.getCause() instanceof NullPointerException);
1545 <        } finally {
1546 <            joinPool(e);
1538 >        try (PoolCleaner cleaner = cleaner(e)) {
1539 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1540 >            l.add(new NPETask());
1541 >            try {
1542 >                e.invokeAny(l);
1543 >                shouldThrow();
1544 >            } catch (ExecutionException success) {
1545 >                assertTrue(success.getCause() instanceof NullPointerException);
1546 >            }
1547          }
1548      }
1549  
# Line 1569 | Line 1551 | public class ThreadPoolExecutorTest exte
1551       * invokeAny(c) returns result of some task
1552       */
1553      public void testInvokeAny5() throws Exception {
1554 <        ExecutorService e =
1554 >        final ExecutorService e =
1555              new ThreadPoolExecutor(2, 2,
1556                                     LONG_DELAY_MS, MILLISECONDS,
1557                                     new ArrayBlockingQueue<Runnable>(10));
1558 <        try {
1558 >        try (PoolCleaner cleaner = cleaner(e)) {
1559              List<Callable<String>> l = new ArrayList<Callable<String>>();
1560              l.add(new StringTask());
1561              l.add(new StringTask());
1562              String result = e.invokeAny(l);
1563              assertSame(TEST_STRING, result);
1582        } finally {
1583            joinPool(e);
1564          }
1565      }
1566  
# Line 1588 | Line 1568 | public class ThreadPoolExecutorTest exte
1568       * invokeAll(null) throws NPE
1569       */
1570      public void testInvokeAll1() throws Exception {
1571 <        ExecutorService e =
1571 >        final ExecutorService e =
1572              new ThreadPoolExecutor(2, 2,
1573                                     LONG_DELAY_MS, MILLISECONDS,
1574                                     new ArrayBlockingQueue<Runnable>(10));
1575 <        try {
1576 <            e.invokeAll(null);
1577 <            shouldThrow();
1578 <        } catch (NullPointerException success) {
1579 <        } finally {
1600 <            joinPool(e);
1575 >        try (PoolCleaner cleaner = cleaner(e)) {
1576 >            try {
1577 >                e.invokeAll(null);
1578 >                shouldThrow();
1579 >            } catch (NullPointerException success) {}
1580          }
1581      }
1582  
# Line 1605 | Line 1584 | public class ThreadPoolExecutorTest exte
1584       * invokeAll(empty collection) returns empty collection
1585       */
1586      public void testInvokeAll2() throws InterruptedException {
1587 <        ExecutorService e =
1587 >        final ExecutorService e =
1588              new ThreadPoolExecutor(2, 2,
1589                                     LONG_DELAY_MS, MILLISECONDS,
1590                                     new ArrayBlockingQueue<Runnable>(10));
1591 <        try {
1591 >        try (PoolCleaner cleaner = cleaner(e)) {
1592              List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1593              assertTrue(r.isEmpty());
1615        } finally {
1616            joinPool(e);
1594          }
1595      }
1596  
# Line 1621 | Line 1598 | public class ThreadPoolExecutorTest exte
1598       * invokeAll(c) throws NPE if c has null elements
1599       */
1600      public void testInvokeAll3() throws Exception {
1601 <        ExecutorService e =
1601 >        final ExecutorService e =
1602              new ThreadPoolExecutor(2, 2,
1603                                     LONG_DELAY_MS, MILLISECONDS,
1604                                     new ArrayBlockingQueue<Runnable>(10));
1605 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1606 <        l.add(new StringTask());
1607 <        l.add(null);
1608 <        try {
1609 <            e.invokeAll(l);
1610 <            shouldThrow();
1611 <        } catch (NullPointerException success) {
1612 <        } finally {
1636 <            joinPool(e);
1605 >        try (PoolCleaner cleaner = cleaner(e)) {
1606 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1607 >            l.add(new StringTask());
1608 >            l.add(null);
1609 >            try {
1610 >                e.invokeAll(l);
1611 >                shouldThrow();
1612 >            } catch (NullPointerException success) {}
1613          }
1614      }
1615  
# Line 1641 | Line 1617 | public class ThreadPoolExecutorTest exte
1617       * get of element of invokeAll(c) throws exception on failed task
1618       */
1619      public void testInvokeAll4() throws Exception {
1620 <        ExecutorService e =
1620 >        final ExecutorService e =
1621              new ThreadPoolExecutor(2, 2,
1622                                     LONG_DELAY_MS, MILLISECONDS,
1623                                     new ArrayBlockingQueue<Runnable>(10));
1624 <        try {
1624 >        try (PoolCleaner cleaner = cleaner(e)) {
1625              List<Callable<String>> l = new ArrayList<Callable<String>>();
1626              l.add(new NPETask());
1627              List<Future<String>> futures = e.invokeAll(l);
# Line 1656 | Line 1632 | public class ThreadPoolExecutorTest exte
1632              } catch (ExecutionException success) {
1633                  assertTrue(success.getCause() instanceof NullPointerException);
1634              }
1659        } finally {
1660            joinPool(e);
1635          }
1636      }
1637  
# Line 1665 | Line 1639 | public class ThreadPoolExecutorTest exte
1639       * invokeAll(c) returns results of all completed tasks
1640       */
1641      public void testInvokeAll5() throws Exception {
1642 <        ExecutorService e =
1642 >        final ExecutorService e =
1643              new ThreadPoolExecutor(2, 2,
1644                                     LONG_DELAY_MS, MILLISECONDS,
1645                                     new ArrayBlockingQueue<Runnable>(10));
1646 <        try {
1646 >        try (PoolCleaner cleaner = cleaner(e)) {
1647              List<Callable<String>> l = new ArrayList<Callable<String>>();
1648              l.add(new StringTask());
1649              l.add(new StringTask());
# Line 1677 | Line 1651 | public class ThreadPoolExecutorTest exte
1651              assertEquals(2, futures.size());
1652              for (Future<String> future : futures)
1653                  assertSame(TEST_STRING, future.get());
1680        } finally {
1681            joinPool(e);
1654          }
1655      }
1656  
# Line 1686 | Line 1658 | public class ThreadPoolExecutorTest exte
1658       * timed invokeAny(null) throws NPE
1659       */
1660      public void testTimedInvokeAny1() throws Exception {
1661 <        ExecutorService e =
1661 >        final ExecutorService e =
1662              new ThreadPoolExecutor(2, 2,
1663                                     LONG_DELAY_MS, MILLISECONDS,
1664                                     new ArrayBlockingQueue<Runnable>(10));
1665 <        try {
1666 <            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1667 <            shouldThrow();
1668 <        } catch (NullPointerException success) {
1669 <        } finally {
1698 <            joinPool(e);
1665 >        try (PoolCleaner cleaner = cleaner(e)) {
1666 >            try {
1667 >                e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1668 >                shouldThrow();
1669 >            } catch (NullPointerException success) {}
1670          }
1671      }
1672  
# Line 1703 | Line 1674 | public class ThreadPoolExecutorTest exte
1674       * timed invokeAny(,,null) throws NPE
1675       */
1676      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1677 <        ExecutorService e =
1677 >        final ExecutorService e =
1678              new ThreadPoolExecutor(2, 2,
1679                                     LONG_DELAY_MS, MILLISECONDS,
1680                                     new ArrayBlockingQueue<Runnable>(10));
1681 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1682 <        l.add(new StringTask());
1683 <        try {
1684 <            e.invokeAny(l, MEDIUM_DELAY_MS, null);
1685 <            shouldThrow();
1686 <        } catch (NullPointerException success) {
1687 <        } finally {
1717 <            joinPool(e);
1681 >        try (PoolCleaner cleaner = cleaner(e)) {
1682 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1683 >            l.add(new StringTask());
1684 >            try {
1685 >                e.invokeAny(l, MEDIUM_DELAY_MS, null);
1686 >                shouldThrow();
1687 >            } catch (NullPointerException success) {}
1688          }
1689      }
1690  
# Line 1722 | Line 1692 | public class ThreadPoolExecutorTest exte
1692       * timed invokeAny(empty collection) throws IAE
1693       */
1694      public void testTimedInvokeAny2() throws Exception {
1695 <        ExecutorService e =
1695 >        final ExecutorService e =
1696              new ThreadPoolExecutor(2, 2,
1697                                     LONG_DELAY_MS, MILLISECONDS,
1698                                     new ArrayBlockingQueue<Runnable>(10));
1699 <        try {
1700 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1701 <            shouldThrow();
1702 <        } catch (IllegalArgumentException success) {
1703 <        } finally {
1704 <            joinPool(e);
1699 >        try (PoolCleaner cleaner = cleaner(e)) {
1700 >            try {
1701 >                e.invokeAny(new ArrayList<Callable<String>>(),
1702 >                            MEDIUM_DELAY_MS, MILLISECONDS);
1703 >                shouldThrow();
1704 >            } catch (IllegalArgumentException success) {}
1705          }
1706      }
1707  
# Line 1744 | Line 1714 | public class ThreadPoolExecutorTest exte
1714              new ThreadPoolExecutor(2, 2,
1715                                     LONG_DELAY_MS, MILLISECONDS,
1716                                     new ArrayBlockingQueue<Runnable>(10));
1717 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1718 <        l.add(latchAwaitingStringTask(latch));
1719 <        l.add(null);
1720 <        try {
1721 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1722 <            shouldThrow();
1723 <        } catch (NullPointerException success) {
1724 <        } finally {
1717 >        try (PoolCleaner cleaner = cleaner(e)) {
1718 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1719 >            l.add(latchAwaitingStringTask(latch));
1720 >            l.add(null);
1721 >            try {
1722 >                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1723 >                shouldThrow();
1724 >            } catch (NullPointerException success) {}
1725              latch.countDown();
1756            joinPool(e);
1726          }
1727      }
1728  
# Line 1761 | Line 1730 | public class ThreadPoolExecutorTest exte
1730       * timed invokeAny(c) throws ExecutionException if no task completes
1731       */
1732      public void testTimedInvokeAny4() throws Exception {
1733 <        ExecutorService e =
1733 >        final ExecutorService e =
1734              new ThreadPoolExecutor(2, 2,
1735                                     LONG_DELAY_MS, MILLISECONDS,
1736                                     new ArrayBlockingQueue<Runnable>(10));
1737 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1738 <        l.add(new NPETask());
1739 <        try {
1740 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1741 <            shouldThrow();
1742 <        } catch (ExecutionException success) {
1743 <            assertTrue(success.getCause() instanceof NullPointerException);
1744 <        } finally {
1745 <            joinPool(e);
1737 >        try (PoolCleaner cleaner = cleaner(e)) {
1738 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1739 >            l.add(new NPETask());
1740 >            try {
1741 >                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1742 >                shouldThrow();
1743 >            } catch (ExecutionException success) {
1744 >                assertTrue(success.getCause() instanceof NullPointerException);
1745 >            }
1746          }
1747      }
1748  
# Line 1781 | Line 1750 | public class ThreadPoolExecutorTest exte
1750       * timed invokeAny(c) returns result of some task
1751       */
1752      public void testTimedInvokeAny5() throws Exception {
1753 <        ExecutorService e =
1753 >        final ExecutorService e =
1754              new ThreadPoolExecutor(2, 2,
1755                                     LONG_DELAY_MS, MILLISECONDS,
1756                                     new ArrayBlockingQueue<Runnable>(10));
1757 <        try {
1757 >        try (PoolCleaner cleaner = cleaner(e)) {
1758              List<Callable<String>> l = new ArrayList<Callable<String>>();
1759              l.add(new StringTask());
1760              l.add(new StringTask());
1761              String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1762              assertSame(TEST_STRING, result);
1794        } finally {
1795            joinPool(e);
1763          }
1764      }
1765  
# Line 1800 | Line 1767 | public class ThreadPoolExecutorTest exte
1767       * timed invokeAll(null) throws NPE
1768       */
1769      public void testTimedInvokeAll1() throws Exception {
1770 <        ExecutorService e =
1770 >        final ExecutorService e =
1771              new ThreadPoolExecutor(2, 2,
1772                                     LONG_DELAY_MS, MILLISECONDS,
1773                                     new ArrayBlockingQueue<Runnable>(10));
1774 <        try {
1775 <            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1776 <            shouldThrow();
1777 <        } catch (NullPointerException success) {
1778 <        } finally {
1812 <            joinPool(e);
1774 >        try (PoolCleaner cleaner = cleaner(e)) {
1775 >            try {
1776 >                e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1777 >                shouldThrow();
1778 >            } catch (NullPointerException success) {}
1779          }
1780      }
1781  
# Line 1817 | Line 1783 | public class ThreadPoolExecutorTest exte
1783       * timed invokeAll(,,null) throws NPE
1784       */
1785      public void testTimedInvokeAllNullTimeUnit() throws Exception {
1786 <        ExecutorService e =
1786 >        final ExecutorService e =
1787              new ThreadPoolExecutor(2, 2,
1788                                     LONG_DELAY_MS, MILLISECONDS,
1789                                     new ArrayBlockingQueue<Runnable>(10));
1790 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1791 <        l.add(new StringTask());
1792 <        try {
1793 <            e.invokeAll(l, MEDIUM_DELAY_MS, null);
1794 <            shouldThrow();
1795 <        } catch (NullPointerException success) {
1796 <        } finally {
1831 <            joinPool(e);
1790 >        try (PoolCleaner cleaner = cleaner(e)) {
1791 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1792 >            l.add(new StringTask());
1793 >            try {
1794 >                e.invokeAll(l, MEDIUM_DELAY_MS, null);
1795 >                shouldThrow();
1796 >            } catch (NullPointerException success) {}
1797          }
1798      }
1799  
# Line 1836 | Line 1801 | public class ThreadPoolExecutorTest exte
1801       * timed invokeAll(empty collection) returns empty collection
1802       */
1803      public void testTimedInvokeAll2() throws InterruptedException {
1804 <        ExecutorService e =
1804 >        final ExecutorService e =
1805              new ThreadPoolExecutor(2, 2,
1806                                     LONG_DELAY_MS, MILLISECONDS,
1807                                     new ArrayBlockingQueue<Runnable>(10));
1808 <        try {
1809 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1808 >        try (PoolCleaner cleaner = cleaner(e)) {
1809 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(),
1810 >                                                 MEDIUM_DELAY_MS, MILLISECONDS);
1811              assertTrue(r.isEmpty());
1846        } finally {
1847            joinPool(e);
1812          }
1813      }
1814  
# Line 1852 | Line 1816 | public class ThreadPoolExecutorTest exte
1816       * timed invokeAll(c) throws NPE if c has null elements
1817       */
1818      public void testTimedInvokeAll3() throws Exception {
1819 <        ExecutorService e =
1819 >        final ExecutorService e =
1820              new ThreadPoolExecutor(2, 2,
1821                                     LONG_DELAY_MS, MILLISECONDS,
1822                                     new ArrayBlockingQueue<Runnable>(10));
1823 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1824 <        l.add(new StringTask());
1825 <        l.add(null);
1826 <        try {
1827 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1828 <            shouldThrow();
1829 <        } catch (NullPointerException success) {
1830 <        } finally {
1867 <            joinPool(e);
1823 >        try (PoolCleaner cleaner = cleaner(e)) {
1824 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1825 >            l.add(new StringTask());
1826 >            l.add(null);
1827 >            try {
1828 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1829 >                shouldThrow();
1830 >            } catch (NullPointerException success) {}
1831          }
1832      }
1833  
# Line 1872 | Line 1835 | public class ThreadPoolExecutorTest exte
1835       * get of element of invokeAll(c) throws exception on failed task
1836       */
1837      public void testTimedInvokeAll4() throws Exception {
1838 <        ExecutorService e =
1838 >        final ExecutorService e =
1839              new ThreadPoolExecutor(2, 2,
1840                                     LONG_DELAY_MS, MILLISECONDS,
1841                                     new ArrayBlockingQueue<Runnable>(10));
1842 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1843 <        l.add(new NPETask());
1844 <        List<Future<String>> futures =
1845 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1846 <        assertEquals(1, futures.size());
1847 <        try {
1848 <            futures.get(0).get();
1849 <            shouldThrow();
1850 <        } catch (ExecutionException success) {
1851 <            assertTrue(success.getCause() instanceof NullPointerException);
1852 <        } finally {
1853 <            joinPool(e);
1842 >        try (PoolCleaner cleaner = cleaner(e)) {
1843 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1844 >            l.add(new NPETask());
1845 >            List<Future<String>> futures =
1846 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1847 >            assertEquals(1, futures.size());
1848 >            try {
1849 >                futures.get(0).get();
1850 >                shouldThrow();
1851 >            } catch (ExecutionException success) {
1852 >                assertTrue(success.getCause() instanceof NullPointerException);
1853 >            }
1854          }
1855      }
1856  
# Line 1895 | Line 1858 | public class ThreadPoolExecutorTest exte
1858       * timed invokeAll(c) returns results of all completed tasks
1859       */
1860      public void testTimedInvokeAll5() throws Exception {
1861 <        ExecutorService e =
1861 >        final ExecutorService e =
1862              new ThreadPoolExecutor(2, 2,
1863                                     LONG_DELAY_MS, MILLISECONDS,
1864                                     new ArrayBlockingQueue<Runnable>(10));
1865 <        try {
1865 >        try (PoolCleaner cleaner = cleaner(e)) {
1866              List<Callable<String>> l = new ArrayList<Callable<String>>();
1867              l.add(new StringTask());
1868              l.add(new StringTask());
# Line 1908 | Line 1871 | public class ThreadPoolExecutorTest exte
1871              assertEquals(2, futures.size());
1872              for (Future<String> future : futures)
1873                  assertSame(TEST_STRING, future.get());
1911        } finally {
1912            joinPool(e);
1874          }
1875      }
1876  
# Line 1917 | Line 1878 | public class ThreadPoolExecutorTest exte
1878       * timed invokeAll(c) cancels tasks not completed by timeout
1879       */
1880      public void testTimedInvokeAll6() throws Exception {
1881 <        ExecutorService e =
1881 >        final ExecutorService e =
1882              new ThreadPoolExecutor(2, 2,
1883                                     LONG_DELAY_MS, MILLISECONDS,
1884                                     new ArrayBlockingQueue<Runnable>(10));
1885 <        try {
1885 >        try (PoolCleaner cleaner = cleaner(e)) {
1886              for (long timeout = timeoutMillis();;) {
1887                  List<Callable<String>> tasks = new ArrayList<>();
1888                  tasks.add(new StringTask("0"));
# Line 1945 | Line 1906 | public class ThreadPoolExecutorTest exte
1906                          fail("expected exactly one task to be cancelled");
1907                  }
1908              }
1948        } finally {
1949            joinPool(e);
1909          }
1910      }
1911  
# Line 1960 | Line 1919 | public class ThreadPoolExecutorTest exte
1919                                     LONG_DELAY_MS, MILLISECONDS,
1920                                     new LinkedBlockingQueue<Runnable>(),
1921                                     new FailingThreadFactory());
1922 <        try {
1922 >        try (PoolCleaner cleaner = cleaner(e)) {
1923              final int TASKS = 100;
1924              final CountDownLatch done = new CountDownLatch(TASKS);
1925              for (int k = 0; k < TASKS; ++k)
# Line 1969 | Line 1928 | public class ThreadPoolExecutorTest exte
1928                          done.countDown();
1929                      }});
1930              assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1972        } finally {
1973            joinPool(e);
1931          }
1932      }
1933  
# Line 1982 | Line 1939 | public class ThreadPoolExecutorTest exte
1939              new ThreadPoolExecutor(2, 2,
1940                                     1000, MILLISECONDS,
1941                                     new ArrayBlockingQueue<Runnable>(10));
1942 <        assertFalse(p.allowsCoreThreadTimeOut());
1943 <        joinPool(p);
1942 >        try (PoolCleaner cleaner = cleaner(p)) {
1943 >            assertFalse(p.allowsCoreThreadTimeOut());
1944 >        }
1945      }
1946  
1947      /**
# Line 1995 | Line 1953 | public class ThreadPoolExecutorTest exte
1953              new ThreadPoolExecutor(2, 10,
1954                                     keepAliveTime, MILLISECONDS,
1955                                     new ArrayBlockingQueue<Runnable>(10));
1956 <        final CountDownLatch threadStarted = new CountDownLatch(1);
1957 <        try {
1956 >        try (PoolCleaner cleaner = cleaner(p)) {
1957 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1958              p.allowCoreThreadTimeOut(true);
1959              p.execute(new CheckedRunnable() {
1960                  public void realRun() {
# Line 2011 | Line 1969 | public class ThreadPoolExecutorTest exte
1969                  Thread.yield();
1970              assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1971              assertEquals(0, p.getPoolSize());
2014        } finally {
2015            joinPool(p);
1972          }
1973      }
1974  
# Line 2025 | Line 1981 | public class ThreadPoolExecutorTest exte
1981              new ThreadPoolExecutor(2, 10,
1982                                     keepAliveTime, MILLISECONDS,
1983                                     new ArrayBlockingQueue<Runnable>(10));
1984 <        final CountDownLatch threadStarted = new CountDownLatch(1);
1985 <        try {
1984 >        try (PoolCleaner cleaner = cleaner(p)) {
1985 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1986              p.allowCoreThreadTimeOut(false);
1987              p.execute(new CheckedRunnable() {
1988                  public void realRun() throws InterruptedException {
# Line 2035 | Line 1991 | public class ThreadPoolExecutorTest exte
1991                  }});
1992              delay(2 * keepAliveTime);
1993              assertTrue(p.getPoolSize() >= 1);
2038        } finally {
2039            joinPool(p);
1994          }
1995      }
1996  
# Line 2055 | Line 2009 | public class ThreadPoolExecutorTest exte
2009              new ThreadPoolExecutor(1, 30,
2010                                     60, SECONDS,
2011                                     new ArrayBlockingQueue(30));
2012 <        try {
2012 >        try (PoolCleaner cleaner = cleaner(p)) {
2013              for (int i = 0; i < nTasks; ++i) {
2014                  for (;;) {
2015                      try {
# Line 2067 | Line 2021 | public class ThreadPoolExecutorTest exte
2021              }
2022              // enough time to run all tasks
2023              assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS));
2070        } finally {
2071            joinPool(p);
2024          }
2025      }
2026  
# Line 2080 | Line 2032 | public class ThreadPoolExecutorTest exte
2032              new ThreadPoolExecutor(1, 1,
2033                                     LONG_DELAY_MS, MILLISECONDS,
2034                                     new LinkedBlockingQueue<Runnable>());
2035 <        try {
2035 >        try (PoolCleaner cleaner = cleaner(e)) {
2036              final CountDownLatch blockerStarted = new CountDownLatch(1);
2037              final CountDownLatch done = new CountDownLatch(1);
2038              final List<Future<?>> futures = new ArrayList<>();
# Line 2107 | Line 2059 | public class ThreadPoolExecutorTest exte
2059                  assertTrue(future.isDone());
2060              }
2061              done.countDown();
2110        } finally {
2111            joinPool(e);
2062          }
2063      }
2064  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines