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.75 by jsr166, Sun Oct 4 03:27:46 2015 UTC vs.
Revision 1.93 by jsr166, Tue Oct 6 16:39:06 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines