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.47 by jsr166, Sat Oct 3 18:17:51 2015 UTC vs.
Revision 1.105 by dl, Tue Jan 26 13:33:06 2021 UTC

# Line 10 | Line 10 | import static java.util.concurrent.TimeU
10   import static java.util.concurrent.TimeUnit.SECONDS;
11  
12   import java.util.ArrayList;
13 + import java.util.Collection;
14 + import java.util.Collections;
15   import java.util.List;
16   import java.util.concurrent.ArrayBlockingQueue;
17   import java.util.concurrent.BlockingQueue;
# Line 17 | Line 19 | import java.util.concurrent.Callable;
19   import java.util.concurrent.CancellationException;
20   import java.util.concurrent.CountDownLatch;
21   import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.Executors;
22   import java.util.concurrent.ExecutorService;
23   import java.util.concurrent.Future;
24   import java.util.concurrent.FutureTask;
25   import java.util.concurrent.LinkedBlockingQueue;
25 import java.util.concurrent.RejectedExecutionException;
26   import java.util.concurrent.RejectedExecutionHandler;
27   import java.util.concurrent.RunnableFuture;
28   import java.util.concurrent.SynchronousQueue;
29   import java.util.concurrent.ThreadFactory;
30 + import java.util.concurrent.ThreadLocalRandom;
31   import java.util.concurrent.ThreadPoolExecutor;
32   import java.util.concurrent.TimeoutException;
33   import java.util.concurrent.TimeUnit;
# Line 232 | Line 233 | public class ThreadPoolExecutorSubclassT
233      public void testExecute() throws InterruptedException {
234          final ThreadPoolExecutor p =
235              new CustomTPE(1, 1,
236 <                          LONG_DELAY_MS, MILLISECONDS,
236 >                          2 * LONG_DELAY_MS, MILLISECONDS,
237                            new ArrayBlockingQueue<Runnable>(10));
238 <        final CountDownLatch done = new CountDownLatch(1);
239 <        final Runnable task = new CheckedRunnable() {
240 <            public void realRun() {
241 <                done.countDown();
241 <            }};
242 <        try {
238 >        try (PoolCleaner cleaner = cleaner(p)) {
239 >            final CountDownLatch done = new CountDownLatch(1);
240 >            final Runnable task = new CheckedRunnable() {
241 >                public void realRun() { done.countDown(); }};
242              p.execute(task);
243 <            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
245 <        } finally {
246 <            joinPool(p);
243 >            await(done);
244          }
245      }
246  
# Line 252 | Line 249 | public class ThreadPoolExecutorSubclassT
249       * thread becomes active
250       */
251      public void testGetActiveCount() throws InterruptedException {
252 +        final CountDownLatch done = new CountDownLatch(1);
253          final ThreadPoolExecutor p =
254              new CustomTPE(2, 2,
255                            LONG_DELAY_MS, MILLISECONDS,
256                            new ArrayBlockingQueue<Runnable>(10));
257 <        final CountDownLatch threadStarted = new CountDownLatch(1);
258 <        final CountDownLatch done = new CountDownLatch(1);
261 <        try {
257 >        try (PoolCleaner cleaner = cleaner(p, done)) {
258 >            final CountDownLatch threadStarted = new CountDownLatch(1);
259              assertEquals(0, p.getActiveCount());
260              p.execute(new CheckedRunnable() {
261                  public void realRun() throws InterruptedException {
262                      threadStarted.countDown();
263                      assertEquals(1, p.getActiveCount());
264 <                    done.await();
264 >                    await(done);
265                  }});
266 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
266 >            await(threadStarted);
267              assertEquals(1, p.getActiveCount());
271        } finally {
272            done.countDown();
273            joinPool(p);
268          }
269      }
270  
# Line 278 | Line 272 | public class ThreadPoolExecutorSubclassT
272       * prestartCoreThread starts a thread if under corePoolSize, else doesn't
273       */
274      public void testPrestartCoreThread() {
275 <        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
276 <        assertEquals(0, p.getPoolSize());
277 <        assertTrue(p.prestartCoreThread());
278 <        assertEquals(1, p.getPoolSize());
279 <        assertTrue(p.prestartCoreThread());
280 <        assertEquals(2, p.getPoolSize());
281 <        assertFalse(p.prestartCoreThread());
282 <        assertEquals(2, p.getPoolSize());
283 <        joinPool(p);
275 >        final ThreadPoolExecutor p =
276 >            new CustomTPE(2, 6,
277 >                          LONG_DELAY_MS, MILLISECONDS,
278 >                          new ArrayBlockingQueue<Runnable>(10));
279 >        try (PoolCleaner cleaner = cleaner(p)) {
280 >            assertEquals(0, p.getPoolSize());
281 >            assertTrue(p.prestartCoreThread());
282 >            assertEquals(1, p.getPoolSize());
283 >            assertTrue(p.prestartCoreThread());
284 >            assertEquals(2, p.getPoolSize());
285 >            assertFalse(p.prestartCoreThread());
286 >            assertEquals(2, p.getPoolSize());
287 >            p.setCorePoolSize(4);
288 >            assertTrue(p.prestartCoreThread());
289 >            assertEquals(3, p.getPoolSize());
290 >            assertTrue(p.prestartCoreThread());
291 >            assertEquals(4, p.getPoolSize());
292 >            assertFalse(p.prestartCoreThread());
293 >            assertEquals(4, p.getPoolSize());
294 >        }
295      }
296  
297      /**
298       * prestartAllCoreThreads starts all corePoolSize threads
299       */
300      public void testPrestartAllCoreThreads() {
301 <        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
302 <        assertEquals(0, p.getPoolSize());
303 <        p.prestartAllCoreThreads();
304 <        assertEquals(2, p.getPoolSize());
305 <        p.prestartAllCoreThreads();
306 <        assertEquals(2, p.getPoolSize());
307 <        joinPool(p);
301 >        final ThreadPoolExecutor p =
302 >            new CustomTPE(2, 6,
303 >                          LONG_DELAY_MS, MILLISECONDS,
304 >                          new ArrayBlockingQueue<Runnable>(10));
305 >        try (PoolCleaner cleaner = cleaner(p)) {
306 >            assertEquals(0, p.getPoolSize());
307 >            p.prestartAllCoreThreads();
308 >            assertEquals(2, p.getPoolSize());
309 >            p.prestartAllCoreThreads();
310 >            assertEquals(2, p.getPoolSize());
311 >            p.setCorePoolSize(4);
312 >            p.prestartAllCoreThreads();
313 >            assertEquals(4, p.getPoolSize());
314 >            p.prestartAllCoreThreads();
315 >            assertEquals(4, p.getPoolSize());
316 >        }
317      }
318  
319      /**
# Line 311 | Line 325 | public class ThreadPoolExecutorSubclassT
325              new CustomTPE(2, 2,
326                            LONG_DELAY_MS, MILLISECONDS,
327                            new ArrayBlockingQueue<Runnable>(10));
328 <        final CountDownLatch threadStarted = new CountDownLatch(1);
329 <        final CountDownLatch threadProceed = new CountDownLatch(1);
330 <        final CountDownLatch threadDone = new CountDownLatch(1);
331 <        try {
328 >        try (PoolCleaner cleaner = cleaner(p)) {
329 >            final CountDownLatch threadStarted = new CountDownLatch(1);
330 >            final CountDownLatch threadProceed = new CountDownLatch(1);
331 >            final CountDownLatch threadDone = new CountDownLatch(1);
332              assertEquals(0, p.getCompletedTaskCount());
333              p.execute(new CheckedRunnable() {
334                  public void realRun() throws InterruptedException {
335                      threadStarted.countDown();
336                      assertEquals(0, p.getCompletedTaskCount());
337 <                    threadProceed.await();
337 >                    await(threadProceed);
338                      threadDone.countDown();
339                  }});
340              await(threadStarted);
341              assertEquals(0, p.getCompletedTaskCount());
342              threadProceed.countDown();
343 <            threadDone.await();
343 >            await(threadDone);
344              long startTime = System.nanoTime();
345              while (p.getCompletedTaskCount() != 1) {
346                  if (millisElapsedSince(startTime) > LONG_DELAY_MS)
347                      fail("timed out");
348                  Thread.yield();
349              }
336        } finally {
337            joinPool(p);
350          }
351      }
352  
# Line 342 | Line 354 | public class ThreadPoolExecutorSubclassT
354       * getCorePoolSize returns size given in constructor if not otherwise set
355       */
356      public void testGetCorePoolSize() {
357 <        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
358 <        assertEquals(1, p.getCorePoolSize());
359 <        joinPool(p);
357 >        final ThreadPoolExecutor p =
358 >            new CustomTPE(1, 1,
359 >                          LONG_DELAY_MS, MILLISECONDS,
360 >                          new ArrayBlockingQueue<Runnable>(10));
361 >        try (PoolCleaner cleaner = cleaner(p)) {
362 >            assertEquals(1, p.getCorePoolSize());
363 >        }
364      }
365  
366      /**
367       * getKeepAliveTime returns value given in constructor if not otherwise set
368       */
369      public void testGetKeepAliveTime() {
370 <        ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
371 <        assertEquals(1, p.getKeepAliveTime(SECONDS));
372 <        joinPool(p);
370 >        final ThreadPoolExecutor p =
371 >            new CustomTPE(2, 2,
372 >                          1000, MILLISECONDS,
373 >                          new ArrayBlockingQueue<Runnable>(10));
374 >        try (PoolCleaner cleaner = cleaner(p)) {
375 >            assertEquals(1, p.getKeepAliveTime(SECONDS));
376 >        }
377      }
378  
379      /**
380       * getThreadFactory returns factory in constructor if not set
381       */
382      public void testGetThreadFactory() {
383 <        ThreadFactory tf = new SimpleThreadFactory();
384 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler());
385 <        assertSame(tf, p.getThreadFactory());
386 <        joinPool(p);
383 >        final ThreadFactory threadFactory = new SimpleThreadFactory();
384 >        final ThreadPoolExecutor p =
385 >            new CustomTPE(1, 2,
386 >                          LONG_DELAY_MS, MILLISECONDS,
387 >                          new ArrayBlockingQueue<Runnable>(10),
388 >                          threadFactory,
389 >                          new NoOpREHandler());
390 >        try (PoolCleaner cleaner = cleaner(p)) {
391 >            assertSame(threadFactory, p.getThreadFactory());
392 >        }
393      }
394  
395      /**
396       * setThreadFactory sets the thread factory returned by getThreadFactory
397       */
398      public void testSetThreadFactory() {
399 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
400 <        ThreadFactory tf = new SimpleThreadFactory();
401 <        p.setThreadFactory(tf);
402 <        assertSame(tf, p.getThreadFactory());
403 <        joinPool(p);
399 >        final ThreadPoolExecutor p =
400 >            new CustomTPE(1, 2,
401 >                          LONG_DELAY_MS, MILLISECONDS,
402 >                          new ArrayBlockingQueue<Runnable>(10));
403 >        try (PoolCleaner cleaner = cleaner(p)) {
404 >            ThreadFactory threadFactory = new SimpleThreadFactory();
405 >            p.setThreadFactory(threadFactory);
406 >            assertSame(threadFactory, p.getThreadFactory());
407 >        }
408      }
409  
410      /**
411       * setThreadFactory(null) throws NPE
412       */
413      public void testSetThreadFactoryNull() {
414 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
415 <        try {
416 <            p.setThreadFactory(null);
417 <            shouldThrow();
418 <        } catch (NullPointerException success) {
419 <        } finally {
420 <            joinPool(p);
414 >        final ThreadPoolExecutor p =
415 >            new CustomTPE(1, 2,
416 >                          LONG_DELAY_MS, MILLISECONDS,
417 >                          new ArrayBlockingQueue<Runnable>(10));
418 >        try (PoolCleaner cleaner = cleaner(p)) {
419 >            try {
420 >                p.setThreadFactory(null);
421 >                shouldThrow();
422 >            } catch (NullPointerException success) {}
423          }
424      }
425  
# Line 395 | Line 427 | public class ThreadPoolExecutorSubclassT
427       * getRejectedExecutionHandler returns handler in constructor if not set
428       */
429      public void testGetRejectedExecutionHandler() {
430 <        RejectedExecutionHandler h = new NoOpREHandler();
431 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h);
432 <        assertSame(h, p.getRejectedExecutionHandler());
433 <        joinPool(p);
430 >        final RejectedExecutionHandler handler = new NoOpREHandler();
431 >        final ThreadPoolExecutor p =
432 >            new CustomTPE(1, 2,
433 >                          LONG_DELAY_MS, MILLISECONDS,
434 >                          new ArrayBlockingQueue<Runnable>(10),
435 >                          handler);
436 >        try (PoolCleaner cleaner = cleaner(p)) {
437 >            assertSame(handler, p.getRejectedExecutionHandler());
438 >        }
439      }
440  
441      /**
# Line 406 | Line 443 | public class ThreadPoolExecutorSubclassT
443       * getRejectedExecutionHandler
444       */
445      public void testSetRejectedExecutionHandler() {
446 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
447 <        RejectedExecutionHandler h = new NoOpREHandler();
448 <        p.setRejectedExecutionHandler(h);
449 <        assertSame(h, p.getRejectedExecutionHandler());
450 <        joinPool(p);
446 >        final ThreadPoolExecutor p =
447 >            new CustomTPE(1, 2,
448 >                          LONG_DELAY_MS, MILLISECONDS,
449 >                          new ArrayBlockingQueue<Runnable>(10));
450 >        try (PoolCleaner cleaner = cleaner(p)) {
451 >            RejectedExecutionHandler handler = new NoOpREHandler();
452 >            p.setRejectedExecutionHandler(handler);
453 >            assertSame(handler, p.getRejectedExecutionHandler());
454 >        }
455      }
456  
457      /**
458       * setRejectedExecutionHandler(null) throws NPE
459       */
460      public void testSetRejectedExecutionHandlerNull() {
461 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
462 <        try {
463 <            p.setRejectedExecutionHandler(null);
464 <            shouldThrow();
465 <        } catch (NullPointerException success) {
466 <        } finally {
467 <            joinPool(p);
461 >        final ThreadPoolExecutor p =
462 >            new CustomTPE(1, 2,
463 >                          LONG_DELAY_MS, MILLISECONDS,
464 >                          new ArrayBlockingQueue<Runnable>(10));
465 >        try (PoolCleaner cleaner = cleaner(p)) {
466 >            try {
467 >                p.setRejectedExecutionHandler(null);
468 >                shouldThrow();
469 >            } catch (NullPointerException success) {}
470          }
471      }
472  
# Line 433 | Line 476 | public class ThreadPoolExecutorSubclassT
476       */
477      public void testGetLargestPoolSize() throws InterruptedException {
478          final int THREADS = 3;
479 +        final CountDownLatch done = new CountDownLatch(1);
480          final ThreadPoolExecutor p =
481              new CustomTPE(THREADS, THREADS,
482                            LONG_DELAY_MS, MILLISECONDS,
483                            new ArrayBlockingQueue<Runnable>(10));
484 <        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
441 <        final CountDownLatch done = new CountDownLatch(1);
442 <        try {
484 >        try (PoolCleaner cleaner = cleaner(p, done)) {
485              assertEquals(0, p.getLargestPoolSize());
486 +            final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
487              for (int i = 0; i < THREADS; i++)
488                  p.execute(new CheckedRunnable() {
489                      public void realRun() throws InterruptedException {
490                          threadsStarted.countDown();
491 <                        done.await();
491 >                        await(done);
492                          assertEquals(THREADS, p.getLargestPoolSize());
493                      }});
494 <            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
452 <            assertEquals(THREADS, p.getLargestPoolSize());
453 <        } finally {
454 <            done.countDown();
455 <            joinPool(p);
494 >            await(threadsStarted);
495              assertEquals(THREADS, p.getLargestPoolSize());
496          }
497 +        assertEquals(THREADS, p.getLargestPoolSize());
498      }
499  
500      /**
# Line 462 | Line 502 | public class ThreadPoolExecutorSubclassT
502       * otherwise set
503       */
504      public void testGetMaximumPoolSize() {
505 <        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
506 <        assertEquals(2, p.getMaximumPoolSize());
507 <        joinPool(p);
505 >        final ThreadPoolExecutor p =
506 >            new CustomTPE(2, 3,
507 >                          LONG_DELAY_MS, MILLISECONDS,
508 >                          new ArrayBlockingQueue<Runnable>(10));
509 >        try (PoolCleaner cleaner = cleaner(p)) {
510 >            assertEquals(3, p.getMaximumPoolSize());
511 >            p.setMaximumPoolSize(5);
512 >            assertEquals(5, p.getMaximumPoolSize());
513 >            p.setMaximumPoolSize(4);
514 >            assertEquals(4, p.getMaximumPoolSize());
515 >        }
516      }
517  
518      /**
# Line 472 | Line 520 | public class ThreadPoolExecutorSubclassT
520       * become active
521       */
522      public void testGetPoolSize() throws InterruptedException {
523 +        final CountDownLatch done = new CountDownLatch(1);
524          final ThreadPoolExecutor p =
525              new CustomTPE(1, 1,
526                            LONG_DELAY_MS, MILLISECONDS,
527                            new ArrayBlockingQueue<Runnable>(10));
528 <        final CountDownLatch threadStarted = new CountDownLatch(1);
480 <        final CountDownLatch done = new CountDownLatch(1);
481 <        try {
528 >        try (PoolCleaner cleaner = cleaner(p, done)) {
529              assertEquals(0, p.getPoolSize());
530 +            final CountDownLatch threadStarted = new CountDownLatch(1);
531              p.execute(new CheckedRunnable() {
532                  public void realRun() throws InterruptedException {
533                      threadStarted.countDown();
534                      assertEquals(1, p.getPoolSize());
535 <                    done.await();
535 >                    await(done);
536                  }});
537 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
537 >            await(threadStarted);
538              assertEquals(1, p.getPoolSize());
491        } finally {
492            done.countDown();
493            joinPool(p);
539          }
540      }
541  
# Line 498 | Line 543 | public class ThreadPoolExecutorSubclassT
543       * getTaskCount increases, but doesn't overestimate, when tasks submitted
544       */
545      public void testGetTaskCount() throws InterruptedException {
546 +        final int TASKS = 3;
547 +        final CountDownLatch done = new CountDownLatch(1);
548          final ThreadPoolExecutor p =
549              new CustomTPE(1, 1,
550                            LONG_DELAY_MS, MILLISECONDS,
551                            new ArrayBlockingQueue<Runnable>(10));
552 <        final CountDownLatch threadStarted = new CountDownLatch(1);
553 <        final CountDownLatch done = new CountDownLatch(1);
507 <        try {
552 >        try (PoolCleaner cleaner = cleaner(p, done)) {
553 >            final CountDownLatch threadStarted = new CountDownLatch(1);
554              assertEquals(0, p.getTaskCount());
555 +            assertEquals(0, p.getCompletedTaskCount());
556              p.execute(new CheckedRunnable() {
557                  public void realRun() throws InterruptedException {
558                      threadStarted.countDown();
559 <                    assertEquals(1, p.getTaskCount());
513 <                    done.await();
559 >                    await(done);
560                  }});
561 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
561 >            await(threadStarted);
562              assertEquals(1, p.getTaskCount());
563 <        } finally {
564 <            done.countDown();
565 <            joinPool(p);
563 >            assertEquals(0, p.getCompletedTaskCount());
564 >            for (int i = 0; i < TASKS; i++) {
565 >                assertEquals(1 + i, p.getTaskCount());
566 >                p.execute(new CheckedRunnable() {
567 >                    public void realRun() throws InterruptedException {
568 >                        threadStarted.countDown();
569 >                        assertEquals(1 + TASKS, p.getTaskCount());
570 >                        await(done);
571 >                    }});
572 >            }
573 >            assertEquals(1 + TASKS, p.getTaskCount());
574 >            assertEquals(0, p.getCompletedTaskCount());
575          }
576 +        assertEquals(1 + TASKS, p.getTaskCount());
577 +        assertEquals(1 + TASKS, p.getCompletedTaskCount());
578      }
579  
580      /**
581       * isShutdown is false before shutdown, true after
582       */
583      public void testIsShutdown() {
584 <
585 <        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
586 <        assertFalse(p.isShutdown());
587 <        try { p.shutdown(); } catch (SecurityException ok) { return; }
588 <        assertTrue(p.isShutdown());
589 <        joinPool(p);
584 >        final ThreadPoolExecutor p =
585 >            new CustomTPE(1, 1,
586 >                          LONG_DELAY_MS, MILLISECONDS,
587 >                          new ArrayBlockingQueue<Runnable>(10));
588 >        try (PoolCleaner cleaner = cleaner(p)) {
589 >            assertFalse(p.isShutdown());
590 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
591 >            assertTrue(p.isShutdown());
592 >        }
593      }
594  
595      /**
# Line 540 | Line 600 | public class ThreadPoolExecutorSubclassT
600              new CustomTPE(1, 1,
601                            LONG_DELAY_MS, MILLISECONDS,
602                            new ArrayBlockingQueue<Runnable>(10));
603 <        final CountDownLatch threadStarted = new CountDownLatch(1);
604 <        final CountDownLatch done = new CountDownLatch(1);
605 <        try {
603 >        try (PoolCleaner cleaner = cleaner(p)) {
604 >            final CountDownLatch threadStarted = new CountDownLatch(1);
605 >            final CountDownLatch done = new CountDownLatch(1);
606              assertFalse(p.isTerminating());
607              p.execute(new CheckedRunnable() {
608                  public void realRun() throws InterruptedException {
609                      assertFalse(p.isTerminating());
610                      threadStarted.countDown();
611 <                    done.await();
611 >                    await(done);
612                  }});
613 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
613 >            await(threadStarted);
614              assertFalse(p.isTerminating());
615              done.countDown();
556        } finally {
616              try { p.shutdown(); } catch (SecurityException ok) { return; }
617 +            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
618 +            assertTrue(p.isTerminated());
619 +            assertFalse(p.isTerminating());
620          }
559        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
560        assertTrue(p.isTerminated());
561        assertFalse(p.isTerminating());
621      }
622  
623      /**
# Line 569 | Line 628 | public class ThreadPoolExecutorSubclassT
628              new CustomTPE(1, 1,
629                            LONG_DELAY_MS, MILLISECONDS,
630                            new ArrayBlockingQueue<Runnable>(10));
631 <        final CountDownLatch threadStarted = new CountDownLatch(1);
632 <        final CountDownLatch done = new CountDownLatch(1);
633 <        try {
631 >        try (PoolCleaner cleaner = cleaner(p)) {
632 >            final CountDownLatch threadStarted = new CountDownLatch(1);
633 >            final CountDownLatch done = new CountDownLatch(1);
634              assertFalse(p.isTerminating());
635              p.execute(new CheckedRunnable() {
636                  public void realRun() throws InterruptedException {
637                      assertFalse(p.isTerminating());
638                      threadStarted.countDown();
639 <                    done.await();
639 >                    await(done);
640                  }});
641 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
641 >            await(threadStarted);
642              assertFalse(p.isTerminating());
643              done.countDown();
585        } finally {
644              try { p.shutdown(); } catch (SecurityException ok) { return; }
645 +            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
646 +            assertTrue(p.isTerminated());
647 +            assertFalse(p.isTerminating());
648          }
588        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
589        assertTrue(p.isTerminated());
590        assertFalse(p.isTerminating());
649      }
650  
651      /**
652       * getQueue returns the work queue, which contains queued tasks
653       */
654      public void testGetQueue() throws InterruptedException {
655 <        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
655 >        final CountDownLatch done = new CountDownLatch(1);
656 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
657          final ThreadPoolExecutor p =
658              new CustomTPE(1, 1,
659                            LONG_DELAY_MS, MILLISECONDS,
660                            q);
661 <        final CountDownLatch threadStarted = new CountDownLatch(1);
662 <        final CountDownLatch done = new CountDownLatch(1);
663 <        try {
664 <            FutureTask[] tasks = new FutureTask[5];
661 >        try (PoolCleaner cleaner = cleaner(p, done)) {
662 >            final CountDownLatch threadStarted = new CountDownLatch(1);
663 >            FutureTask[] rtasks = new FutureTask[5];
664 >            @SuppressWarnings("unchecked")
665 >            FutureTask<Boolean>[] tasks = (FutureTask<Boolean>[])rtasks;
666              for (int i = 0; i < tasks.length; i++) {
667 <                Callable task = new CheckedCallable<Boolean>() {
667 >                Callable<Boolean> task = new CheckedCallable<Boolean>() {
668                      public Boolean realCall() throws InterruptedException {
669                          threadStarted.countDown();
670                          assertSame(q, p.getQueue());
671 <                        done.await();
671 >                        await(done);
672                          return Boolean.TRUE;
673                      }};
674 <                tasks[i] = new FutureTask(task);
674 >                tasks[i] = new FutureTask<Boolean>(task);
675                  p.execute(tasks[i]);
676              }
677 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
677 >            await(threadStarted);
678              assertSame(q, p.getQueue());
679              assertFalse(q.contains(tasks[0]));
680              assertTrue(q.contains(tasks[tasks.length - 1]));
681              assertEquals(tasks.length - 1, q.size());
622        } finally {
623            done.countDown();
624            joinPool(p);
682          }
683      }
684  
# Line 629 | Line 686 | public class ThreadPoolExecutorSubclassT
686       * remove(task) removes queued task, and fails to remove active task
687       */
688      public void testRemove() throws InterruptedException {
689 <        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
689 >        final CountDownLatch done = new CountDownLatch(1);
690 >        BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
691          final ThreadPoolExecutor p =
692              new CustomTPE(1, 1,
693                            LONG_DELAY_MS, MILLISECONDS,
694                            q);
695 <        Runnable[] tasks = new Runnable[6];
696 <        final CountDownLatch threadStarted = new CountDownLatch(1);
697 <        final CountDownLatch done = new CountDownLatch(1);
640 <        try {
695 >        try (PoolCleaner cleaner = cleaner(p, done)) {
696 >            Runnable[] tasks = new Runnable[6];
697 >            final CountDownLatch threadStarted = new CountDownLatch(1);
698              for (int i = 0; i < tasks.length; i++) {
699                  tasks[i] = new CheckedRunnable() {
700 <                        public void realRun() throws InterruptedException {
701 <                            threadStarted.countDown();
702 <                            done.await();
703 <                        }};
700 >                    public void realRun() throws InterruptedException {
701 >                        threadStarted.countDown();
702 >                        await(done);
703 >                    }};
704                  p.execute(tasks[i]);
705              }
706 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
706 >            await(threadStarted);
707              assertFalse(p.remove(tasks[0]));
708              assertTrue(q.contains(tasks[4]));
709              assertTrue(q.contains(tasks[3]));
# Line 656 | Line 713 | public class ThreadPoolExecutorSubclassT
713              assertTrue(q.contains(tasks[3]));
714              assertTrue(p.remove(tasks[3]));
715              assertFalse(q.contains(tasks[3]));
659        } finally {
660            done.countDown();
661            joinPool(p);
716          }
717      }
718  
# Line 668 | Line 722 | public class ThreadPoolExecutorSubclassT
722      public void testPurge() throws InterruptedException {
723          final CountDownLatch threadStarted = new CountDownLatch(1);
724          final CountDownLatch done = new CountDownLatch(1);
725 <        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
725 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
726          final ThreadPoolExecutor p =
727              new CustomTPE(1, 1,
728                            LONG_DELAY_MS, MILLISECONDS,
729                            q);
730 <        FutureTask[] tasks = new FutureTask[5];
731 <        try {
730 >        try (PoolCleaner cleaner = cleaner(p, done)) {
731 >            FutureTask[] rtasks = new FutureTask[5];
732 >            @SuppressWarnings("unchecked")
733 >            FutureTask<Boolean>[] tasks = (FutureTask<Boolean>[])rtasks;
734              for (int i = 0; i < tasks.length; i++) {
735 <                Callable task = new CheckedCallable<Boolean>() {
735 >                Callable<Boolean> task = new CheckedCallable<Boolean>() {
736                      public Boolean realCall() throws InterruptedException {
737                          threadStarted.countDown();
738 <                        done.await();
738 >                        await(done);
739                          return Boolean.TRUE;
740                      }};
741 <                tasks[i] = new FutureTask(task);
741 >                tasks[i] = new FutureTask<Boolean>(task);
742                  p.execute(tasks[i]);
743              }
744 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
744 >            await(threadStarted);
745              assertEquals(tasks.length, p.getTaskCount());
746              assertEquals(tasks.length - 1, q.size());
747              assertEquals(1L, p.getActiveCount());
# Line 698 | Line 754 | public class ThreadPoolExecutorSubclassT
754              p.purge();         // Nothing to do
755              assertEquals(tasks.length - 3, q.size());
756              assertEquals(tasks.length - 2, p.getTaskCount());
701        } finally {
702            done.countDown();
703            joinPool(p);
757          }
758      }
759  
# Line 712 | Line 765 | public class ThreadPoolExecutorSubclassT
765          final int poolSize = 2;
766          final int count = 5;
767          final AtomicInteger ran = new AtomicInteger(0);
768 <        ThreadPoolExecutor p =
769 <            new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS,
768 >        final ThreadPoolExecutor p =
769 >            new CustomTPE(poolSize, poolSize,
770 >                          LONG_DELAY_MS, MILLISECONDS,
771                            new ArrayBlockingQueue<Runnable>(10));
772 <        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
772 >        final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
773          Runnable waiter = new CheckedRunnable() { public void realRun() {
774              threadsStarted.countDown();
775              try {
776 <                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
776 >                MILLISECONDS.sleep(LONGER_DELAY_MS);
777              } catch (InterruptedException success) {}
778              ran.getAndIncrement();
779          }};
780          for (int i = 0; i < count; i++)
781              p.execute(waiter);
782 <        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
782 >        await(threadsStarted);
783          assertEquals(poolSize, p.getActiveCount());
784          assertEquals(0, p.getCompletedTaskCount());
785          final List<Runnable> queuedTasks;
# Line 1081 | Line 1135 | public class ThreadPoolExecutorSubclassT
1135      }
1136  
1137      /**
1138 <     * execute throws RejectedExecutionException if saturated.
1138 >     * Submitted tasks are rejected when saturated or shutdown
1139       */
1140 <    public void testSaturatedExecute() {
1141 <        ThreadPoolExecutor p =
1140 >    public void testSubmittedTasksRejectedWhenSaturatedOrShutdown() throws InterruptedException {
1141 >        final ThreadPoolExecutor p =
1142              new CustomTPE(1, 1,
1143                            LONG_DELAY_MS, MILLISECONDS,
1144                            new ArrayBlockingQueue<Runnable>(1));
1145 +        final int saturatedSize = saturatedSize(p);
1146 +        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
1147 +        final CountDownLatch threadsStarted = new CountDownLatch(p.getMaximumPoolSize());
1148          final CountDownLatch done = new CountDownLatch(1);
1149 <        try {
1150 <            Runnable task = new CheckedRunnable() {
1151 <                public void realRun() throws InterruptedException {
1149 >        final Runnable r = () -> {
1150 >            threadsStarted.countDown();
1151 >            for (;;) {
1152 >                try {
1153                      done.await();
1154 <                }};
1155 <            for (int i = 0; i < 2; ++i)
1156 <                p.execute(task);
1157 <            for (int i = 0; i < 2; ++i) {
1154 >                    return;
1155 >                } catch (InterruptedException shutdownNowDeliberatelyIgnored) {}
1156 >            }};
1157 >        final Callable<Boolean> c = () -> {
1158 >            threadsStarted.countDown();
1159 >            for (;;) {
1160                  try {
1161 <                    p.execute(task);
1162 <                    shouldThrow();
1163 <                } catch (RejectedExecutionException success) {}
1164 <                assertTrue(p.getTaskCount() <= 2);
1161 >                    done.await();
1162 >                    return Boolean.TRUE;
1163 >                } catch (InterruptedException shutdownNowDeliberatelyIgnored) {}
1164 >            }};
1165 >        final boolean shutdownNow = rnd.nextBoolean();
1166 >
1167 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1168 >            // saturate
1169 >            for (int i = saturatedSize; i--> 0; ) {
1170 >                switch (rnd.nextInt(4)) {
1171 >                case 0: p.execute(r); break;
1172 >                case 1: assertFalse(p.submit(r).isDone()); break;
1173 >                case 2: assertFalse(p.submit(r, Boolean.TRUE).isDone()); break;
1174 >                case 3: assertFalse(p.submit(c).isDone()); break;
1175 >                }
1176              }
1106        } finally {
1107            done.countDown();
1108            joinPool(p);
1109        }
1110    }
1177  
1178 <    /**
1179 <     * executor using CallerRunsPolicy runs task if saturated.
1180 <     */
1181 <    public void testSaturatedExecute2() {
1182 <        RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1183 <        ThreadPoolExecutor p = new CustomTPE(1, 1,
1184 <                                             LONG_DELAY_MS, MILLISECONDS,
1185 <                                             new ArrayBlockingQueue<Runnable>(1),
1186 <                                             h);
1187 <        try {
1188 <            TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1189 <            for (int i = 0; i < tasks.length; ++i)
1190 <                tasks[i] = new TrackedNoOpRunnable();
1191 <            TrackedLongRunnable mr = new TrackedLongRunnable();
1192 <            p.execute(mr);
1193 <            for (int i = 0; i < tasks.length; ++i)
1194 <                p.execute(tasks[i]);
1195 <            for (int i = 1; i < tasks.length; ++i)
1196 <                assertTrue(tasks[i].done);
1131 <            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1132 <        } finally {
1133 <            joinPool(p);
1134 <        }
1178 >            await(threadsStarted);
1179 >            assertTaskSubmissionsAreRejected(p);
1180 >
1181 >            if (shutdownNow)
1182 >                p.shutdownNow();
1183 >            else
1184 >                p.shutdown();
1185 >            // Pool is shutdown, but not yet terminated
1186 >            assertTaskSubmissionsAreRejected(p);
1187 >            assertFalse(p.isTerminated());
1188 >
1189 >            done.countDown();   // release blocking tasks
1190 >            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
1191 >
1192 >            assertTaskSubmissionsAreRejected(p);
1193 >        }
1194 >        assertEquals(saturatedSize(p)
1195 >                     - (shutdownNow ? p.getQueue().remainingCapacity() : 0),
1196 >                     p.getCompletedTaskCount());
1197      }
1198  
1199      /**
1200 <     * executor using DiscardPolicy drops task if saturated.
1200 >     * executor using DiscardOldestPolicy drops oldest task if saturated.
1201       */
1202 <    public void testSaturatedExecute3() {
1203 <        RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1204 <        ThreadPoolExecutor p =
1202 >    public void testSaturatedExecute_DiscardOldestPolicy() {
1203 >        final CountDownLatch done = new CountDownLatch(1);
1204 >        LatchAwaiter r1 = awaiter(done);
1205 >        LatchAwaiter r2 = awaiter(done);
1206 >        LatchAwaiter r3 = awaiter(done);
1207 >        final ThreadPoolExecutor p =
1208              new CustomTPE(1, 1,
1209                            LONG_DELAY_MS, MILLISECONDS,
1210                            new ArrayBlockingQueue<Runnable>(1),
1211 <                          h);
1212 <        try {
1213 <            TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1214 <            for (int i = 0; i < tasks.length; ++i)
1215 <                tasks[i] = new TrackedNoOpRunnable();
1216 <            p.execute(new TrackedLongRunnable());
1152 <            for (TrackedNoOpRunnable task : tasks)
1153 <                p.execute(task);
1154 <            for (TrackedNoOpRunnable task : tasks)
1155 <                assertFalse(task.done);
1156 <            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1157 <        } finally {
1158 <            joinPool(p);
1159 <        }
1160 <    }
1161 <
1162 <    /**
1163 <     * executor using DiscardOldestPolicy drops oldest task if saturated.
1164 <     */
1165 <    public void testSaturatedExecute4() {
1166 <        RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
1167 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1168 <        try {
1169 <            p.execute(new TrackedLongRunnable());
1170 <            TrackedLongRunnable r2 = new TrackedLongRunnable();
1211 >                          new ThreadPoolExecutor.DiscardOldestPolicy());
1212 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1213 >            assertEquals(LatchAwaiter.NEW, r1.state);
1214 >            assertEquals(LatchAwaiter.NEW, r2.state);
1215 >            assertEquals(LatchAwaiter.NEW, r3.state);
1216 >            p.execute(r1);
1217              p.execute(r2);
1218              assertTrue(p.getQueue().contains(r2));
1173            TrackedNoOpRunnable r3 = new TrackedNoOpRunnable();
1219              p.execute(r3);
1220              assertFalse(p.getQueue().contains(r2));
1221              assertTrue(p.getQueue().contains(r3));
1177            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1178        } finally {
1179            joinPool(p);
1180        }
1181    }
1182
1183    /**
1184     * execute throws RejectedExecutionException if shutdown
1185     */
1186    public void testRejectedExecutionExceptionOnShutdown() {
1187        ThreadPoolExecutor p =
1188            new CustomTPE(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
1189        try { p.shutdown(); } catch (SecurityException ok) { return; }
1190        try {
1191            p.execute(new NoOpRunnable());
1192            shouldThrow();
1193        } catch (RejectedExecutionException success) {}
1194
1195        joinPool(p);
1196    }
1197
1198    /**
1199     * execute using CallerRunsPolicy drops task on shutdown
1200     */
1201    public void testCallerRunsOnShutdown() {
1202        RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1203        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1204
1205        try { p.shutdown(); } catch (SecurityException ok) { return; }
1206        try {
1207            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1208            p.execute(r);
1209            assertFalse(r.done);
1210        } finally {
1211            joinPool(p);
1212        }
1213    }
1214
1215    /**
1216     * execute using DiscardPolicy drops task on shutdown
1217     */
1218    public void testDiscardOnShutdown() {
1219        RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1220        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1221
1222        try { p.shutdown(); } catch (SecurityException ok) { return; }
1223        try {
1224            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1225            p.execute(r);
1226            assertFalse(r.done);
1227        } finally {
1228            joinPool(p);
1222          }
1223 +        assertEquals(LatchAwaiter.DONE, r1.state);
1224 +        assertEquals(LatchAwaiter.NEW, r2.state);
1225 +        assertEquals(LatchAwaiter.DONE, r3.state);
1226      }
1227  
1228      /**
1229       * execute using DiscardOldestPolicy drops task on shutdown
1230       */
1231      public void testDiscardOldestOnShutdown() {
1232 <        RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
1233 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1232 >        final ThreadPoolExecutor p =
1233 >            new CustomTPE(1, 1,
1234 >                          LONG_DELAY_MS, MILLISECONDS,
1235 >                          new ArrayBlockingQueue<Runnable>(1),
1236 >                          new ThreadPoolExecutor.DiscardOldestPolicy());
1237  
1238          try { p.shutdown(); } catch (SecurityException ok) { return; }
1239 <        try {
1239 >        try (PoolCleaner cleaner = cleaner(p)) {
1240              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1241              p.execute(r);
1242              assertFalse(r.done);
1244        } finally {
1245            joinPool(p);
1243          }
1244      }
1245  
1246      /**
1247 <     * execute(null) throws NPE
1247 >     * Submitting null tasks throws NullPointerException
1248       */
1249 <    public void testExecuteNull() {
1250 <        ThreadPoolExecutor p =
1251 <            new CustomTPE(1, 2, 1L, SECONDS,
1249 >    public void testNullTaskSubmission() {
1250 >        final ThreadPoolExecutor p =
1251 >            new CustomTPE(1, 2,
1252 >                          1L, SECONDS,
1253                            new ArrayBlockingQueue<Runnable>(10));
1254 <        try {
1255 <            p.execute(null);
1256 <            shouldThrow();
1259 <        } catch (NullPointerException success) {}
1260 <
1261 <        joinPool(p);
1254 >        try (PoolCleaner cleaner = cleaner(p)) {
1255 >            assertNullTaskSubmissionThrowsNullPointerException(p);
1256 >        }
1257      }
1258  
1259      /**
1260       * setCorePoolSize of negative value throws IllegalArgumentException
1261       */
1262      public void testCorePoolSizeIllegalArgumentException() {
1263 <        ThreadPoolExecutor p =
1264 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1265 <        try {
1266 <            p.setCorePoolSize(-1);
1267 <            shouldThrow();
1268 <        } catch (IllegalArgumentException success) {
1269 <        } finally {
1270 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1263 >        final ThreadPoolExecutor p =
1264 >            new CustomTPE(1, 2,
1265 >                          LONG_DELAY_MS, MILLISECONDS,
1266 >                          new ArrayBlockingQueue<Runnable>(10));
1267 >        try (PoolCleaner cleaner = cleaner(p)) {
1268 >            try {
1269 >                p.setCorePoolSize(-1);
1270 >                shouldThrow();
1271 >            } catch (IllegalArgumentException success) {}
1272          }
1277        joinPool(p);
1273      }
1274  
1275      /**
# Line 1282 | Line 1277 | public class ThreadPoolExecutorSubclassT
1277       * if given a value less the core pool size
1278       */
1279      public void testMaximumPoolSizeIllegalArgumentException() {
1280 <        ThreadPoolExecutor p =
1281 <            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1282 <        try {
1283 <            p.setMaximumPoolSize(1);
1284 <            shouldThrow();
1285 <        } catch (IllegalArgumentException success) {
1286 <        } finally {
1287 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1280 >        final ThreadPoolExecutor p =
1281 >            new CustomTPE(2, 3,
1282 >                          LONG_DELAY_MS, MILLISECONDS,
1283 >                          new ArrayBlockingQueue<Runnable>(10));
1284 >        try (PoolCleaner cleaner = cleaner(p)) {
1285 >            try {
1286 >                p.setMaximumPoolSize(1);
1287 >                shouldThrow();
1288 >            } catch (IllegalArgumentException success) {}
1289          }
1294        joinPool(p);
1290      }
1291  
1292      /**
# Line 1299 | Line 1294 | public class ThreadPoolExecutorSubclassT
1294       * if given a negative value
1295       */
1296      public void testMaximumPoolSizeIllegalArgumentException2() {
1297 <        ThreadPoolExecutor p =
1298 <            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1299 <        try {
1300 <            p.setMaximumPoolSize(-1);
1301 <            shouldThrow();
1302 <        } catch (IllegalArgumentException success) {
1303 <        } finally {
1304 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1297 >        final ThreadPoolExecutor p =
1298 >            new CustomTPE(2, 3,
1299 >                          LONG_DELAY_MS, MILLISECONDS,
1300 >                          new ArrayBlockingQueue<Runnable>(10));
1301 >        try (PoolCleaner cleaner = cleaner(p)) {
1302 >            try {
1303 >                p.setMaximumPoolSize(-1);
1304 >                shouldThrow();
1305 >            } catch (IllegalArgumentException success) {}
1306          }
1311        joinPool(p);
1307      }
1308  
1309      /**
# Line 1316 | Line 1311 | public class ThreadPoolExecutorSubclassT
1311       * when given a negative value
1312       */
1313      public void testKeepAliveTimeIllegalArgumentException() {
1314 <        ThreadPoolExecutor p =
1315 <            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1316 <
1317 <        try {
1318 <            p.setKeepAliveTime(-1,MILLISECONDS);
1319 <            shouldThrow();
1320 <        } catch (IllegalArgumentException success) {
1321 <        } finally {
1322 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1314 >        final ThreadPoolExecutor p =
1315 >            new CustomTPE(2, 3,
1316 >                          LONG_DELAY_MS, MILLISECONDS,
1317 >                          new ArrayBlockingQueue<Runnable>(10));
1318 >        try (PoolCleaner cleaner = cleaner(p)) {
1319 >            try {
1320 >                p.setKeepAliveTime(-1, MILLISECONDS);
1321 >                shouldThrow();
1322 >            } catch (IllegalArgumentException success) {}
1323          }
1329        joinPool(p);
1324      }
1325  
1326      /**
# Line 1334 | Line 1328 | public class ThreadPoolExecutorSubclassT
1328       */
1329      public void testTerminated() {
1330          CustomTPE p = new CustomTPE();
1331 <        try { p.shutdown(); } catch (SecurityException ok) { return; }
1332 <        assertTrue(p.terminatedCalled());
1333 <        joinPool(p);
1331 >        try (PoolCleaner cleaner = cleaner(p)) {
1332 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1333 >            assertTrue(p.terminatedCalled());
1334 >            assertTrue(p.isShutdown());
1335 >        }
1336      }
1337  
1338      /**
# Line 1344 | Line 1340 | public class ThreadPoolExecutorSubclassT
1340       */
1341      public void testBeforeAfter() throws InterruptedException {
1342          CustomTPE p = new CustomTPE();
1343 <        try {
1343 >        try (PoolCleaner cleaner = cleaner(p)) {
1344              final CountDownLatch done = new CountDownLatch(1);
1345              p.execute(new CheckedRunnable() {
1346                  public void realRun() {
# Line 1354 | Line 1350 | public class ThreadPoolExecutorSubclassT
1350              assertEquals(0, done.getCount());
1351              assertTrue(p.afterCalled());
1352              assertTrue(p.beforeCalled());
1357            try { p.shutdown(); } catch (SecurityException ok) { return; }
1358        } finally {
1359            joinPool(p);
1353          }
1354      }
1355  
# Line 1364 | Line 1357 | public class ThreadPoolExecutorSubclassT
1357       * completed submit of callable returns result
1358       */
1359      public void testSubmitCallable() throws Exception {
1360 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1361 <        try {
1360 >        final ExecutorService e =
1361 >            new CustomTPE(2, 2,
1362 >                          LONG_DELAY_MS, MILLISECONDS,
1363 >                          new ArrayBlockingQueue<Runnable>(10));
1364 >        try (PoolCleaner cleaner = cleaner(e)) {
1365              Future<String> future = e.submit(new StringTask());
1366              String result = future.get();
1367              assertSame(TEST_STRING, result);
1372        } finally {
1373            joinPool(e);
1368          }
1369      }
1370  
# Line 1378 | Line 1372 | public class ThreadPoolExecutorSubclassT
1372       * completed submit of runnable returns successfully
1373       */
1374      public void testSubmitRunnable() throws Exception {
1375 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1376 <        try {
1375 >        final ExecutorService e =
1376 >            new CustomTPE(2, 2,
1377 >                          LONG_DELAY_MS, MILLISECONDS,
1378 >                          new ArrayBlockingQueue<Runnable>(10));
1379 >        try (PoolCleaner cleaner = cleaner(e)) {
1380              Future<?> future = e.submit(new NoOpRunnable());
1381              future.get();
1382              assertTrue(future.isDone());
1386        } finally {
1387            joinPool(e);
1383          }
1384      }
1385  
# Line 1392 | Line 1387 | public class ThreadPoolExecutorSubclassT
1387       * completed submit of (runnable, result) returns result
1388       */
1389      public void testSubmitRunnable2() throws Exception {
1390 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1391 <        try {
1390 >        final ExecutorService e =
1391 >            new CustomTPE(2, 2,
1392 >                          LONG_DELAY_MS, MILLISECONDS,
1393 >                          new ArrayBlockingQueue<Runnable>(10));
1394 >        try (PoolCleaner cleaner = cleaner(e)) {
1395              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1396              String result = future.get();
1397              assertSame(TEST_STRING, result);
1400        } finally {
1401            joinPool(e);
1398          }
1399      }
1400  
1401      /**
1402 <     * invokeAny(null) throws NPE
1402 >     * invokeAny(null) throws NullPointerException
1403       */
1404      public void testInvokeAny1() throws Exception {
1405 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1406 <        try {
1407 <            e.invokeAny(null);
1408 <            shouldThrow();
1409 <        } catch (NullPointerException success) {
1410 <        } finally {
1411 <            joinPool(e);
1405 >        final ExecutorService e =
1406 >            new CustomTPE(2, 2,
1407 >                          LONG_DELAY_MS, MILLISECONDS,
1408 >                          new ArrayBlockingQueue<Runnable>(10));
1409 >        try (PoolCleaner cleaner = cleaner(e)) {
1410 >            try {
1411 >                e.invokeAny(null);
1412 >                shouldThrow();
1413 >            } catch (NullPointerException success) {}
1414          }
1415      }
1416  
1417      /**
1418 <     * invokeAny(empty collection) throws IAE
1418 >     * invokeAny(empty collection) throws IllegalArgumentException
1419       */
1420      public void testInvokeAny2() throws Exception {
1421 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1422 <        try {
1423 <            e.invokeAny(new ArrayList<Callable<String>>());
1424 <            shouldThrow();
1425 <        } catch (IllegalArgumentException success) {
1426 <        } finally {
1427 <            joinPool(e);
1421 >        final ExecutorService e =
1422 >            new CustomTPE(2, 2,
1423 >                          LONG_DELAY_MS, MILLISECONDS,
1424 >                          new ArrayBlockingQueue<Runnable>(10));
1425 >        try (PoolCleaner cleaner = cleaner(e)) {
1426 >            try {
1427 >                e.invokeAny(new ArrayList<Callable<String>>());
1428 >                shouldThrow();
1429 >            } catch (IllegalArgumentException success) {}
1430          }
1431      }
1432  
# Line 1435 | Line 1435 | public class ThreadPoolExecutorSubclassT
1435       */
1436      public void testInvokeAny3() throws Exception {
1437          CountDownLatch latch = new CountDownLatch(1);
1438 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1439 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1440 <        l.add(latchAwaitingStringTask(latch));
1441 <        l.add(null);
1442 <        try {
1443 <            e.invokeAny(l);
1444 <            shouldThrow();
1445 <        } catch (NullPointerException success) {
1446 <        } finally {
1438 >        final ExecutorService e =
1439 >            new CustomTPE(2, 2,
1440 >                          LONG_DELAY_MS, MILLISECONDS,
1441 >                          new ArrayBlockingQueue<Runnable>(10));
1442 >        try (PoolCleaner cleaner = cleaner(e)) {
1443 >            List<Callable<String>> l = new ArrayList<>();
1444 >            l.add(latchAwaitingStringTask(latch));
1445 >            l.add(null);
1446 >            try {
1447 >                e.invokeAny(l);
1448 >                shouldThrow();
1449 >            } catch (NullPointerException success) {}
1450              latch.countDown();
1448            joinPool(e);
1451          }
1452      }
1453  
# Line 1453 | Line 1455 | public class ThreadPoolExecutorSubclassT
1455       * invokeAny(c) throws ExecutionException if no task completes
1456       */
1457      public void testInvokeAny4() throws Exception {
1458 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1459 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1460 <        l.add(new NPETask());
1461 <        try {
1462 <            e.invokeAny(l);
1463 <            shouldThrow();
1464 <        } catch (ExecutionException success) {
1465 <            assertTrue(success.getCause() instanceof NullPointerException);
1466 <        } finally {
1467 <            joinPool(e);
1458 >        final ExecutorService e =
1459 >            new CustomTPE(2, 2,
1460 >                          LONG_DELAY_MS, MILLISECONDS,
1461 >                          new ArrayBlockingQueue<Runnable>(10));
1462 >        try (PoolCleaner cleaner = cleaner(e)) {
1463 >            List<Callable<String>> l = new ArrayList<>();
1464 >            l.add(new NPETask());
1465 >            try {
1466 >                e.invokeAny(l);
1467 >                shouldThrow();
1468 >            } catch (ExecutionException success) {
1469 >                assertTrue(success.getCause() instanceof NullPointerException);
1470 >            }
1471          }
1472      }
1473  
# Line 1470 | Line 1475 | public class ThreadPoolExecutorSubclassT
1475       * invokeAny(c) returns result of some task
1476       */
1477      public void testInvokeAny5() throws Exception {
1478 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1479 <        try {
1480 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1478 >        final ExecutorService e =
1479 >            new CustomTPE(2, 2,
1480 >                          LONG_DELAY_MS, MILLISECONDS,
1481 >                          new ArrayBlockingQueue<Runnable>(10));
1482 >        try (PoolCleaner cleaner = cleaner(e)) {
1483 >            List<Callable<String>> l = new ArrayList<>();
1484              l.add(new StringTask());
1485              l.add(new StringTask());
1486              String result = e.invokeAny(l);
1487              assertSame(TEST_STRING, result);
1480        } finally {
1481            joinPool(e);
1488          }
1489      }
1490  
# Line 1486 | Line 1492 | public class ThreadPoolExecutorSubclassT
1492       * invokeAll(null) throws NPE
1493       */
1494      public void testInvokeAll1() throws Exception {
1495 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1496 <        try {
1497 <            e.invokeAll(null);
1498 <            shouldThrow();
1499 <        } catch (NullPointerException success) {
1500 <        } finally {
1501 <            joinPool(e);
1495 >        final ExecutorService e =
1496 >            new CustomTPE(2, 2,
1497 >                          LONG_DELAY_MS, MILLISECONDS,
1498 >                          new ArrayBlockingQueue<Runnable>(10));
1499 >        try (PoolCleaner cleaner = cleaner(e)) {
1500 >            try {
1501 >                e.invokeAll(null);
1502 >                shouldThrow();
1503 >            } catch (NullPointerException success) {}
1504          }
1505      }
1506  
1507      /**
1508 <     * invokeAll(empty collection) returns empty collection
1508 >     * invokeAll(empty collection) returns empty list
1509       */
1510      public void testInvokeAll2() throws Exception {
1511 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1512 <        try {
1513 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1511 >        final ExecutorService e =
1512 >            new CustomTPE(2, 2,
1513 >                          LONG_DELAY_MS, MILLISECONDS,
1514 >                          new ArrayBlockingQueue<Runnable>(10));
1515 >        final Collection<Callable<String>> emptyCollection
1516 >            = Collections.emptyList();
1517 >        try (PoolCleaner cleaner = cleaner(e)) {
1518 >            List<Future<String>> r = e.invokeAll(emptyCollection);
1519              assertTrue(r.isEmpty());
1507        } finally {
1508            joinPool(e);
1520          }
1521      }
1522  
# Line 1513 | Line 1524 | public class ThreadPoolExecutorSubclassT
1524       * invokeAll(c) throws NPE if c has null elements
1525       */
1526      public void testInvokeAll3() throws Exception {
1527 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1528 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1529 <        l.add(new StringTask());
1530 <        l.add(null);
1531 <        try {
1532 <            e.invokeAll(l);
1533 <            shouldThrow();
1534 <        } catch (NullPointerException success) {
1535 <        } finally {
1536 <            joinPool(e);
1527 >        final ExecutorService e =
1528 >            new CustomTPE(2, 2,
1529 >                          LONG_DELAY_MS, MILLISECONDS,
1530 >                          new ArrayBlockingQueue<Runnable>(10));
1531 >        try (PoolCleaner cleaner = cleaner(e)) {
1532 >            List<Callable<String>> l = new ArrayList<>();
1533 >            l.add(new StringTask());
1534 >            l.add(null);
1535 >            try {
1536 >                e.invokeAll(l);
1537 >                shouldThrow();
1538 >            } catch (NullPointerException success) {}
1539          }
1540      }
1541  
# Line 1530 | Line 1543 | public class ThreadPoolExecutorSubclassT
1543       * get of element of invokeAll(c) throws exception on failed task
1544       */
1545      public void testInvokeAll4() throws Exception {
1546 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1547 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1548 <        l.add(new NPETask());
1549 <        List<Future<String>> futures = e.invokeAll(l);
1550 <        assertEquals(1, futures.size());
1551 <        try {
1552 <            futures.get(0).get();
1553 <            shouldThrow();
1554 <        } catch (ExecutionException success) {
1555 <            assertTrue(success.getCause() instanceof NullPointerException);
1556 <        } finally {
1557 <            joinPool(e);
1546 >        final ExecutorService e =
1547 >            new CustomTPE(2, 2,
1548 >                          LONG_DELAY_MS, MILLISECONDS,
1549 >                          new ArrayBlockingQueue<Runnable>(10));
1550 >        try (PoolCleaner cleaner = cleaner(e)) {
1551 >            List<Callable<String>> l = new ArrayList<>();
1552 >            l.add(new NPETask());
1553 >            List<Future<String>> futures = e.invokeAll(l);
1554 >            assertEquals(1, futures.size());
1555 >            try {
1556 >                futures.get(0).get();
1557 >                shouldThrow();
1558 >            } catch (ExecutionException success) {
1559 >                assertTrue(success.getCause() instanceof NullPointerException);
1560 >            }
1561          }
1562      }
1563  
# Line 1549 | Line 1565 | public class ThreadPoolExecutorSubclassT
1565       * invokeAll(c) returns results of all completed tasks
1566       */
1567      public void testInvokeAll5() throws Exception {
1568 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1569 <        try {
1570 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1568 >        final ExecutorService e =
1569 >            new CustomTPE(2, 2,
1570 >                          LONG_DELAY_MS, MILLISECONDS,
1571 >                          new ArrayBlockingQueue<Runnable>(10));
1572 >        try (PoolCleaner cleaner = cleaner(e)) {
1573 >            List<Callable<String>> l = new ArrayList<>();
1574              l.add(new StringTask());
1575              l.add(new StringTask());
1576              List<Future<String>> futures = e.invokeAll(l);
1577              assertEquals(2, futures.size());
1578              for (Future<String> future : futures)
1579                  assertSame(TEST_STRING, future.get());
1561        } finally {
1562            joinPool(e);
1580          }
1581      }
1582  
# Line 1567 | Line 1584 | public class ThreadPoolExecutorSubclassT
1584       * timed invokeAny(null) throws NPE
1585       */
1586      public void testTimedInvokeAny1() throws Exception {
1587 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1588 <        try {
1589 <            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1590 <            shouldThrow();
1591 <        } catch (NullPointerException success) {
1592 <        } finally {
1593 <            joinPool(e);
1587 >        final ExecutorService e =
1588 >            new CustomTPE(2, 2,
1589 >                          LONG_DELAY_MS, MILLISECONDS,
1590 >                          new ArrayBlockingQueue<Runnable>(10));
1591 >        try (PoolCleaner cleaner = cleaner(e)) {
1592 >            try {
1593 >                e.invokeAny(null, randomTimeout(), randomTimeUnit());
1594 >                shouldThrow();
1595 >            } catch (NullPointerException success) {}
1596          }
1597      }
1598  
# Line 1581 | Line 1600 | public class ThreadPoolExecutorSubclassT
1600       * timed invokeAny(,,null) throws NPE
1601       */
1602      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1603 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1604 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1605 <        l.add(new StringTask());
1606 <        try {
1607 <            e.invokeAny(l, MEDIUM_DELAY_MS, null);
1608 <            shouldThrow();
1609 <        } catch (NullPointerException success) {
1610 <        } finally {
1611 <            joinPool(e);
1603 >        final ExecutorService e =
1604 >            new CustomTPE(2, 2,
1605 >                          LONG_DELAY_MS, MILLISECONDS,
1606 >                          new ArrayBlockingQueue<Runnable>(10));
1607 >        try (PoolCleaner cleaner = cleaner(e)) {
1608 >            List<Callable<String>> l = new ArrayList<>();
1609 >            l.add(new StringTask());
1610 >            try {
1611 >                e.invokeAny(l, randomTimeout(), null);
1612 >                shouldThrow();
1613 >            } catch (NullPointerException success) {}
1614          }
1615      }
1616  
1617      /**
1618 <     * timed invokeAny(empty collection) throws IAE
1618 >     * timed invokeAny(empty collection) throws IllegalArgumentException
1619       */
1620      public void testTimedInvokeAny2() throws Exception {
1621 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1622 <        try {
1623 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1624 <            shouldThrow();
1625 <        } catch (IllegalArgumentException success) {
1626 <        } finally {
1627 <            joinPool(e);
1621 >        final ExecutorService e =
1622 >            new CustomTPE(2, 2,
1623 >                          LONG_DELAY_MS, MILLISECONDS,
1624 >                          new ArrayBlockingQueue<Runnable>(10));
1625 >        final Collection<Callable<String>> emptyCollection
1626 >            = Collections.emptyList();
1627 >        try (PoolCleaner cleaner = cleaner(e)) {
1628 >            try {
1629 >                e.invokeAny(emptyCollection, randomTimeout(), randomTimeUnit());
1630 >                shouldThrow();
1631 >            } catch (IllegalArgumentException success) {}
1632          }
1633      }
1634  
# Line 1612 | Line 1637 | public class ThreadPoolExecutorSubclassT
1637       */
1638      public void testTimedInvokeAny3() throws Exception {
1639          CountDownLatch latch = new CountDownLatch(1);
1640 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1641 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1642 <        l.add(latchAwaitingStringTask(latch));
1643 <        l.add(null);
1644 <        try {
1645 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1646 <            shouldThrow();
1647 <        } catch (NullPointerException success) {
1648 <        } finally {
1640 >        final ExecutorService e =
1641 >            new CustomTPE(2, 2,
1642 >                          LONG_DELAY_MS, MILLISECONDS,
1643 >                          new ArrayBlockingQueue<Runnable>(10));
1644 >        try (PoolCleaner cleaner = cleaner(e)) {
1645 >            List<Callable<String>> l = new ArrayList<>();
1646 >            l.add(latchAwaitingStringTask(latch));
1647 >            l.add(null);
1648 >            try {
1649 >                e.invokeAny(l, randomTimeout(), randomTimeUnit());
1650 >                shouldThrow();
1651 >            } catch (NullPointerException success) {}
1652              latch.countDown();
1625            joinPool(e);
1653          }
1654      }
1655  
# Line 1630 | Line 1657 | public class ThreadPoolExecutorSubclassT
1657       * timed invokeAny(c) throws ExecutionException if no task completes
1658       */
1659      public void testTimedInvokeAny4() throws Exception {
1660 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1661 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1662 <        l.add(new NPETask());
1663 <        try {
1664 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1665 <            shouldThrow();
1666 <        } catch (ExecutionException success) {
1667 <            assertTrue(success.getCause() instanceof NullPointerException);
1668 <        } finally {
1669 <            joinPool(e);
1660 >        final ExecutorService e =
1661 >            new CustomTPE(2, 2,
1662 >                          LONG_DELAY_MS, MILLISECONDS,
1663 >                          new ArrayBlockingQueue<Runnable>(10));
1664 >        try (PoolCleaner cleaner = cleaner(e)) {
1665 >            long startTime = System.nanoTime();
1666 >            List<Callable<String>> l = new ArrayList<>();
1667 >            l.add(new NPETask());
1668 >            try {
1669 >                e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1670 >                shouldThrow();
1671 >            } catch (ExecutionException success) {
1672 >                assertTrue(success.getCause() instanceof NullPointerException);
1673 >            }
1674 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1675          }
1676      }
1677  
# Line 1647 | Line 1679 | public class ThreadPoolExecutorSubclassT
1679       * timed invokeAny(c) returns result of some task
1680       */
1681      public void testTimedInvokeAny5() throws Exception {
1682 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1683 <        try {
1684 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1682 >        final ExecutorService e =
1683 >            new CustomTPE(2, 2,
1684 >                          LONG_DELAY_MS, MILLISECONDS,
1685 >                          new ArrayBlockingQueue<Runnable>(10));
1686 >        try (PoolCleaner cleaner = cleaner(e)) {
1687 >            long startTime = System.nanoTime();
1688 >            List<Callable<String>> l = new ArrayList<>();
1689              l.add(new StringTask());
1690              l.add(new StringTask());
1691 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1691 >            String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1692              assertSame(TEST_STRING, result);
1693 <        } finally {
1658 <            joinPool(e);
1693 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1694          }
1695      }
1696  
# Line 1663 | Line 1698 | public class ThreadPoolExecutorSubclassT
1698       * timed invokeAll(null) throws NPE
1699       */
1700      public void testTimedInvokeAll1() throws Exception {
1701 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1702 <        try {
1703 <            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1704 <            shouldThrow();
1705 <        } catch (NullPointerException success) {
1706 <        } finally {
1707 <            joinPool(e);
1701 >        final ExecutorService e =
1702 >            new CustomTPE(2, 2,
1703 >                          LONG_DELAY_MS, MILLISECONDS,
1704 >                          new ArrayBlockingQueue<Runnable>(10));
1705 >        try (PoolCleaner cleaner = cleaner(e)) {
1706 >            try {
1707 >                e.invokeAll(null, randomTimeout(), randomTimeUnit());
1708 >                shouldThrow();
1709 >            } catch (NullPointerException success) {}
1710          }
1711      }
1712  
# Line 1677 | Line 1714 | public class ThreadPoolExecutorSubclassT
1714       * timed invokeAll(,,null) throws NPE
1715       */
1716      public void testTimedInvokeAllNullTimeUnit() throws Exception {
1717 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1718 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1719 <        l.add(new StringTask());
1720 <        try {
1721 <            e.invokeAll(l, MEDIUM_DELAY_MS, null);
1722 <            shouldThrow();
1723 <        } catch (NullPointerException success) {
1724 <        } finally {
1725 <            joinPool(e);
1717 >        final ExecutorService e =
1718 >            new CustomTPE(2, 2,
1719 >                          LONG_DELAY_MS, MILLISECONDS,
1720 >                          new ArrayBlockingQueue<Runnable>(10));
1721 >        try (PoolCleaner cleaner = cleaner(e)) {
1722 >            List<Callable<String>> l = new ArrayList<>();
1723 >            l.add(new StringTask());
1724 >            try {
1725 >                e.invokeAll(l, randomTimeout(), null);
1726 >                shouldThrow();
1727 >            } catch (NullPointerException success) {}
1728          }
1729      }
1730  
1731      /**
1732 <     * timed invokeAll(empty collection) returns empty collection
1732 >     * timed invokeAll(empty collection) returns empty list
1733       */
1734      public void testTimedInvokeAll2() throws Exception {
1735 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1736 <        try {
1737 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1735 >        final ExecutorService e =
1736 >            new CustomTPE(2, 2,
1737 >                          LONG_DELAY_MS, MILLISECONDS,
1738 >                          new ArrayBlockingQueue<Runnable>(10));
1739 >        final Collection<Callable<String>> emptyCollection
1740 >            = Collections.emptyList();
1741 >        try (PoolCleaner cleaner = cleaner(e)) {
1742 >            List<Future<String>> r =
1743 >                e.invokeAll(emptyCollection, randomTimeout(), randomTimeUnit());
1744              assertTrue(r.isEmpty());
1700        } finally {
1701            joinPool(e);
1745          }
1746      }
1747  
# Line 1706 | Line 1749 | public class ThreadPoolExecutorSubclassT
1749       * timed invokeAll(c) throws NPE if c has null elements
1750       */
1751      public void testTimedInvokeAll3() throws Exception {
1752 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1753 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1754 <        l.add(new StringTask());
1755 <        l.add(null);
1756 <        try {
1757 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1758 <            shouldThrow();
1759 <        } catch (NullPointerException success) {
1760 <        } finally {
1761 <            joinPool(e);
1752 >        final ExecutorService e =
1753 >            new CustomTPE(2, 2,
1754 >                          LONG_DELAY_MS, MILLISECONDS,
1755 >                          new ArrayBlockingQueue<Runnable>(10));
1756 >        try (PoolCleaner cleaner = cleaner(e)) {
1757 >            List<Callable<String>> l = new ArrayList<>();
1758 >            l.add(new StringTask());
1759 >            l.add(null);
1760 >            try {
1761 >                e.invokeAll(l, randomTimeout(), randomTimeUnit());
1762 >                shouldThrow();
1763 >            } catch (NullPointerException success) {}
1764          }
1765      }
1766  
# Line 1723 | Line 1768 | public class ThreadPoolExecutorSubclassT
1768       * get of element of invokeAll(c) throws exception on failed task
1769       */
1770      public void testTimedInvokeAll4() throws Exception {
1771 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1772 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1773 <        l.add(new NPETask());
1774 <        List<Future<String>> futures =
1775 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1776 <        assertEquals(1, futures.size());
1777 <        try {
1778 <            futures.get(0).get();
1779 <            shouldThrow();
1780 <        } catch (ExecutionException success) {
1781 <            assertTrue(success.getCause() instanceof NullPointerException);
1782 <        } finally {
1783 <            joinPool(e);
1771 >        final ExecutorService e =
1772 >            new CustomTPE(2, 2,
1773 >                          LONG_DELAY_MS, MILLISECONDS,
1774 >                          new ArrayBlockingQueue<Runnable>(10));
1775 >        try (PoolCleaner cleaner = cleaner(e)) {
1776 >            List<Callable<String>> l = new ArrayList<>();
1777 >            l.add(new NPETask());
1778 >            List<Future<String>> futures =
1779 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1780 >            assertEquals(1, futures.size());
1781 >            try {
1782 >                futures.get(0).get();
1783 >                shouldThrow();
1784 >            } catch (ExecutionException success) {
1785 >                assertTrue(success.getCause() instanceof NullPointerException);
1786 >            }
1787          }
1788      }
1789  
# Line 1743 | Line 1791 | public class ThreadPoolExecutorSubclassT
1791       * timed invokeAll(c) returns results of all completed tasks
1792       */
1793      public void testTimedInvokeAll5() throws Exception {
1794 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1795 <        try {
1796 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
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<Callable<String>> l = new ArrayList<>();
1800              l.add(new StringTask());
1801              l.add(new StringTask());
1802              List<Future<String>> futures =
# Line 1753 | Line 1804 | public class ThreadPoolExecutorSubclassT
1804              assertEquals(2, futures.size());
1805              for (Future<String> future : futures)
1806                  assertSame(TEST_STRING, future.get());
1756        } finally {
1757            joinPool(e);
1807          }
1808      }
1809  
# Line 1762 | Line 1811 | public class ThreadPoolExecutorSubclassT
1811       * timed invokeAll(c) cancels tasks not completed by timeout
1812       */
1813      public void testTimedInvokeAll6() throws Exception {
1814 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1815 <        try {
1816 <            for (long timeout = timeoutMillis();;) {
1814 >        for (long timeout = timeoutMillis();;) {
1815 >            final CountDownLatch done = new CountDownLatch(1);
1816 >            final Callable<String> waiter = new CheckedCallable<String>() {
1817 >                public String realCall() {
1818 >                    try { done.await(LONG_DELAY_MS, MILLISECONDS); }
1819 >                    catch (InterruptedException ok) {}
1820 >                    return "1"; }};
1821 >            final ExecutorService p =
1822 >                new CustomTPE(2, 2,
1823 >                              LONG_DELAY_MS, MILLISECONDS,
1824 >                              new ArrayBlockingQueue<Runnable>(10));
1825 >            try (PoolCleaner cleaner = cleaner(p, done)) {
1826                  List<Callable<String>> tasks = new ArrayList<>();
1827                  tasks.add(new StringTask("0"));
1828 <                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1828 >                tasks.add(waiter);
1829                  tasks.add(new StringTask("2"));
1830                  long startTime = System.nanoTime();
1831                  List<Future<String>> futures =
1832 <                    e.invokeAll(tasks, timeout, MILLISECONDS);
1832 >                    p.invokeAll(tasks, timeout, MILLISECONDS);
1833                  assertEquals(tasks.size(), futures.size());
1834                  assertTrue(millisElapsedSince(startTime) >= timeout);
1835 <                for (Future future : futures)
1835 >                for (Future<?> future : futures)
1836                      assertTrue(future.isDone());
1837                  assertTrue(futures.get(1).isCancelled());
1838                  try {
# Line 1787 | Line 1845 | public class ThreadPoolExecutorSubclassT
1845                          fail("expected exactly one task to be cancelled");
1846                  }
1847              }
1790        } finally {
1791            joinPool(e);
1848          }
1849      }
1850  
# Line 1802 | Line 1858 | public class ThreadPoolExecutorSubclassT
1858                            LONG_DELAY_MS, MILLISECONDS,
1859                            new LinkedBlockingQueue<Runnable>(),
1860                            new FailingThreadFactory());
1861 <        try {
1861 >        try (PoolCleaner cleaner = cleaner(e)) {
1862              final int TASKS = 100;
1863              final CountDownLatch done = new CountDownLatch(TASKS);
1864              for (int k = 0; k < TASKS; ++k)
# Line 1810 | Line 1866 | public class ThreadPoolExecutorSubclassT
1866                      public void realRun() {
1867                          done.countDown();
1868                      }});
1869 <            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1814 <        } finally {
1815 <            joinPool(e);
1869 >            await(done);
1870          }
1871      }
1872  
# Line 1820 | Line 1874 | public class ThreadPoolExecutorSubclassT
1874       * allowsCoreThreadTimeOut is by default false.
1875       */
1876      public void testAllowsCoreThreadTimeOut() {
1877 <        ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1878 <        assertFalse(p.allowsCoreThreadTimeOut());
1879 <        joinPool(p);
1877 >        final ThreadPoolExecutor p =
1878 >            new CustomTPE(2, 2,
1879 >                          1000, MILLISECONDS,
1880 >                          new ArrayBlockingQueue<Runnable>(10));
1881 >        try (PoolCleaner cleaner = cleaner(p)) {
1882 >            assertFalse(p.allowsCoreThreadTimeOut());
1883 >        }
1884      }
1885  
1886      /**
# Line 1834 | Line 1892 | public class ThreadPoolExecutorSubclassT
1892              new CustomTPE(2, 10,
1893                            keepAliveTime, MILLISECONDS,
1894                            new ArrayBlockingQueue<Runnable>(10));
1895 <        final CountDownLatch threadStarted = new CountDownLatch(1);
1896 <        try {
1895 >        try (PoolCleaner cleaner = cleaner(p)) {
1896 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1897              p.allowCoreThreadTimeOut(true);
1898              p.execute(new CheckedRunnable() {
1899                  public void realRun() {
# Line 1850 | Line 1908 | public class ThreadPoolExecutorSubclassT
1908                  Thread.yield();
1909              assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1910              assertEquals(0, p.getPoolSize());
1853        } finally {
1854            joinPool(p);
1911          }
1912      }
1913  
# Line 1864 | Line 1920 | public class ThreadPoolExecutorSubclassT
1920              new CustomTPE(2, 10,
1921                            keepAliveTime, MILLISECONDS,
1922                            new ArrayBlockingQueue<Runnable>(10));
1923 <        final CountDownLatch threadStarted = new CountDownLatch(1);
1924 <        try {
1923 >        try (PoolCleaner cleaner = cleaner(p)) {
1924 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1925              p.allowCoreThreadTimeOut(false);
1926              p.execute(new CheckedRunnable() {
1927                  public void realRun() throws InterruptedException {
# Line 1874 | Line 1930 | public class ThreadPoolExecutorSubclassT
1930                  }});
1931              delay(2 * keepAliveTime);
1932              assertTrue(p.getPoolSize() >= 1);
1877        } finally {
1878            joinPool(p);
1933          }
1934      }
1935  
# Line 1884 | Line 1938 | public class ThreadPoolExecutorSubclassT
1938       * (in part, a test of CustomTPE itself)
1939       */
1940      public void testGet_cancelled() throws Exception {
1941 +        final CountDownLatch done = new CountDownLatch(1);
1942          final ExecutorService e =
1943              new CustomTPE(1, 1,
1944                            LONG_DELAY_MS, MILLISECONDS,
1945                            new LinkedBlockingQueue<Runnable>());
1946 <        try {
1946 >        try (PoolCleaner cleaner = cleaner(e, done)) {
1947              final CountDownLatch blockerStarted = new CountDownLatch(1);
1893            final CountDownLatch done = new CountDownLatch(1);
1948              final List<Future<?>> futures = new ArrayList<>();
1949              for (int i = 0; i < 2; i++) {
1950                  Runnable r = new CheckedRunnable() { public void realRun()
# Line 1900 | Line 1954 | public class ThreadPoolExecutorSubclassT
1954                  }};
1955                  futures.add(e.submit(r));
1956              }
1957 <            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
1957 >            await(blockerStarted);
1958              for (Future<?> future : futures) future.cancel(false);
1959              for (Future<?> future : futures) {
1960                  try {
# Line 1914 | Line 1968 | public class ThreadPoolExecutorSubclassT
1968                  assertTrue(future.isCancelled());
1969                  assertTrue(future.isDone());
1970              }
1917            done.countDown();
1918        } finally {
1919            joinPool(e);
1971          }
1972      }
1973 +    @SuppressWarnings("deprecation")
1974 +    public void testFinalizeMethodCallsSuperFinalize() {
1975 +        new CustomTPE(1, 1,
1976 +                      LONG_DELAY_MS, MILLISECONDS,
1977 +                      new LinkedBlockingQueue<Runnable>()) {
1978 +
1979 +            /**
1980 +             * A finalize method without "throws Throwable", that
1981 +             * calls super.finalize().
1982 +             */
1983 +            protected void finalize() {
1984 +                super.finalize();
1985 +            }
1986 +        }.shutdown();
1987 +    }
1988  
1989   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines