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.33 by jsr166, Wed Dec 31 19:05:43 2014 UTC vs.
Revision 1.86 by jsr166, Mon Oct 5 22:09:02 2015 UTC

# Line 7 | Line 7
7   */
8  
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 + import static java.util.concurrent.TimeUnit.SECONDS;
11  
12   import java.util.ArrayList;
13   import java.util.List;
14   import java.util.concurrent.ArrayBlockingQueue;
15   import java.util.concurrent.BlockingQueue;
16   import java.util.concurrent.Callable;
17 + import java.util.concurrent.CancellationException;
18   import java.util.concurrent.CountDownLatch;
19   import java.util.concurrent.ExecutionException;
20   import java.util.concurrent.Executors;
# Line 28 | Line 30 | import java.util.concurrent.ThreadFactor
30   import java.util.concurrent.ThreadPoolExecutor;
31   import java.util.concurrent.TimeoutException;
32   import java.util.concurrent.TimeUnit;
33 + import java.util.concurrent.atomic.AtomicInteger;
34   import java.util.concurrent.locks.Condition;
35   import java.util.concurrent.locks.ReentrantLock;
36  
# Line 36 | Line 39 | import junit.framework.TestSuite;
39  
40   public class ThreadPoolExecutorSubclassTest extends JSR166TestCase {
41      public static void main(String[] args) {
42 <        junit.textui.TestRunner.run(suite());
42 >        main(suite(), args);
43      }
44      public static Test suite() {
45          return new TestSuite(ThreadPoolExecutorSubclassTest.class);
# Line 58 | Line 61 | public class ThreadPoolExecutorSubclassT
61          CustomTask(final Runnable r, final V res) {
62              if (r == null) throw new NullPointerException();
63              callable = new Callable<V>() {
64 <            public V call() throws Exception { r.run(); return res; }};
64 >                public V call() throws Exception { r.run(); return res; }};
65          }
66          public boolean isDone() {
67              lock.lock(); try { return done; } finally { lock.unlock() ; }
# Line 98 | Line 101 | public class ThreadPoolExecutorSubclassT
101              }
102              lock.lock();
103              try {
104 <                result = v;
105 <                exception = e;
106 <                done = true;
107 <                thread = null;
108 <                cond.signalAll();
104 >                if (!done) {
105 >                    result = v;
106 >                    exception = e;
107 >                    done = true;
108 >                    thread = null;
109 >                    cond.signalAll();
110 >                }
111              }
112              finally { lock.unlock(); }
113          }
# Line 111 | Line 116 | public class ThreadPoolExecutorSubclassT
116              try {
117                  while (!done)
118                      cond.await();
119 +                if (cancelled)
120 +                    throw new CancellationException();
121                  if (exception != null)
122                      throw new ExecutionException(exception);
123                  return result;
# Line 122 | Line 129 | public class ThreadPoolExecutorSubclassT
129              long nanos = unit.toNanos(timeout);
130              lock.lock();
131              try {
132 <                for (;;) {
133 <                    if (done) break;
127 <                    if (nanos < 0)
132 >                while (!done) {
133 >                    if (nanos <= 0L)
134                          throw new TimeoutException();
135                      nanos = cond.awaitNanos(nanos);
136                  }
137 +                if (cancelled)
138 +                    throw new CancellationException();
139                  if (exception != null)
140                      throw new ExecutionException(exception);
141                  return result;
# Line 224 | Line 232 | public class ThreadPoolExecutorSubclassT
232      public void testExecute() throws InterruptedException {
233          final ThreadPoolExecutor p =
234              new CustomTPE(1, 1,
235 <                          LONG_DELAY_MS, MILLISECONDS,
235 >                          2 * LONG_DELAY_MS, MILLISECONDS,
236                            new ArrayBlockingQueue<Runnable>(10));
237 <        final CountDownLatch done = new CountDownLatch(1);
238 <        final Runnable task = new CheckedRunnable() {
239 <            public void realRun() {
240 <                done.countDown();
233 <            }};
234 <        try {
237 >        try (PoolCleaner cleaner = cleaner(p)) {
238 >            final CountDownLatch done = new CountDownLatch(1);
239 >            final Runnable task = new CheckedRunnable() {
240 >                public void realRun() { done.countDown(); }};
241              p.execute(task);
242 <            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
237 <        } finally {
238 <            joinPool(p);
242 >            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
243          }
244      }
245  
# Line 248 | Line 252 | public class ThreadPoolExecutorSubclassT
252              new CustomTPE(2, 2,
253                            LONG_DELAY_MS, MILLISECONDS,
254                            new ArrayBlockingQueue<Runnable>(10));
255 <        final CountDownLatch threadStarted = new CountDownLatch(1);
256 <        final CountDownLatch done = new CountDownLatch(1);
257 <        try {
255 >        try (PoolCleaner cleaner = cleaner(p)) {
256 >            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 {
# Line 258 | Line 262 | public class ThreadPoolExecutorSubclassT
262                      assertEquals(1, p.getActiveCount());
263                      done.await();
264                  }});
265 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
265 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
266              assertEquals(1, p.getActiveCount());
263        } finally {
267              done.countDown();
265            joinPool(p);
268          }
269      }
270  
# Line 270 | 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 303 | 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 {
# Line 325 | Line 347 | public class ThreadPoolExecutorSubclassT
347                      fail("timed out");
348                  Thread.yield();
349              }
328        } finally {
329            joinPool(p);
350          }
351      }
352  
# Line 334 | 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(TimeUnit.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 387 | 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 398 | 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 429 | Line 480 | public class ThreadPoolExecutorSubclassT
480              new CustomTPE(THREADS, THREADS,
481                            LONG_DELAY_MS, MILLISECONDS,
482                            new ArrayBlockingQueue<Runnable>(10));
483 <        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
484 <        final CountDownLatch done = new CountDownLatch(1);
485 <        try {
483 >        try (PoolCleaner cleaner = cleaner(p)) {
484 >            final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
485 >            final CountDownLatch done = new CountDownLatch(1);
486              assertEquals(0, p.getLargestPoolSize());
487              for (int i = 0; i < THREADS; i++)
488                  p.execute(new CheckedRunnable() {
# Line 440 | Line 491 | public class ThreadPoolExecutorSubclassT
491                          done.await();
492                          assertEquals(THREADS, p.getLargestPoolSize());
493                      }});
494 <            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
444 <            assertEquals(THREADS, p.getLargestPoolSize());
445 <        } finally {
446 <            done.countDown();
447 <            joinPool(p);
494 >            assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
495              assertEquals(THREADS, p.getLargestPoolSize());
496 +            done.countDown();   // release pool
497          }
498 +        assertEquals(THREADS, p.getLargestPoolSize());
499      }
500  
501      /**
# Line 454 | Line 503 | public class ThreadPoolExecutorSubclassT
503       * otherwise set
504       */
505      public void testGetMaximumPoolSize() {
506 <        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
507 <        assertEquals(2, p.getMaximumPoolSize());
508 <        joinPool(p);
506 >        final ThreadPoolExecutor p =
507 >            new CustomTPE(2, 3,
508 >                          LONG_DELAY_MS, MILLISECONDS,
509 >                          new ArrayBlockingQueue<Runnable>(10));
510 >        try (PoolCleaner cleaner = cleaner(p)) {
511 >            assertEquals(3, p.getMaximumPoolSize());
512 >            p.setMaximumPoolSize(5);
513 >            assertEquals(5, p.getMaximumPoolSize());
514 >            p.setMaximumPoolSize(4);
515 >            assertEquals(4, p.getMaximumPoolSize());
516 >        }
517      }
518  
519      /**
# Line 468 | Line 525 | public class ThreadPoolExecutorSubclassT
525              new CustomTPE(1, 1,
526                            LONG_DELAY_MS, MILLISECONDS,
527                            new ArrayBlockingQueue<Runnable>(10));
528 <        final CountDownLatch threadStarted = new CountDownLatch(1);
529 <        final CountDownLatch done = new CountDownLatch(1);
530 <        try {
528 >        try (PoolCleaner cleaner = cleaner(p)) {
529 >            final CountDownLatch threadStarted = new CountDownLatch(1);
530 >            final CountDownLatch done = new CountDownLatch(1);
531              assertEquals(0, p.getPoolSize());
532              p.execute(new CheckedRunnable() {
533                  public void realRun() throws InterruptedException {
# Line 478 | Line 535 | public class ThreadPoolExecutorSubclassT
535                      assertEquals(1, p.getPoolSize());
536                      done.await();
537                  }});
538 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
538 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
539              assertEquals(1, p.getPoolSize());
540 <        } finally {
484 <            done.countDown();
485 <            joinPool(p);
540 >            done.countDown();   // release pool
541          }
542      }
543  
# Line 490 | Line 545 | public class ThreadPoolExecutorSubclassT
545       * getTaskCount increases, but doesn't overestimate, when tasks submitted
546       */
547      public void testGetTaskCount() throws InterruptedException {
548 +        final int TASKS = 3;
549 +        final CountDownLatch done = new CountDownLatch(1);
550          final ThreadPoolExecutor p =
551              new CustomTPE(1, 1,
552                            LONG_DELAY_MS, MILLISECONDS,
553                            new ArrayBlockingQueue<Runnable>(10));
554 <        final CountDownLatch threadStarted = new CountDownLatch(1);
555 <        final CountDownLatch done = new CountDownLatch(1);
499 <        try {
554 >        try (PoolCleaner cleaner = cleaner(p, done)) {
555 >            final CountDownLatch threadStarted = new CountDownLatch(1);
556              assertEquals(0, p.getTaskCount());
557 +            assertEquals(0, p.getCompletedTaskCount());
558              p.execute(new CheckedRunnable() {
559                  public void realRun() throws InterruptedException {
560                      threadStarted.countDown();
504                    assertEquals(1, p.getTaskCount());
561                      done.await();
562                  }});
563 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
563 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
564              assertEquals(1, p.getTaskCount());
565 <        } finally {
566 <            done.countDown();
567 <            joinPool(p);
565 >            assertEquals(0, p.getCompletedTaskCount());
566 >            for (int i = 0; i < TASKS; i++) {
567 >                assertEquals(1 + i, p.getTaskCount());
568 >                p.execute(new CheckedRunnable() {
569 >                    public void realRun() throws InterruptedException {
570 >                        threadStarted.countDown();
571 >                        assertEquals(1 + TASKS, p.getTaskCount());
572 >                        done.await();
573 >                    }});
574 >            }
575 >            assertEquals(1 + TASKS, p.getTaskCount());
576 >            assertEquals(0, p.getCompletedTaskCount());
577          }
578 +        assertEquals(1 + TASKS, p.getTaskCount());
579 +        assertEquals(1 + TASKS, p.getCompletedTaskCount());
580      }
581  
582      /**
583       * isShutdown is false before shutdown, true after
584       */
585      public void testIsShutdown() {
586 <
587 <        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
588 <        assertFalse(p.isShutdown());
589 <        try { p.shutdown(); } catch (SecurityException ok) { return; }
590 <        assertTrue(p.isShutdown());
591 <        joinPool(p);
586 >        final ThreadPoolExecutor p =
587 >            new CustomTPE(1, 1,
588 >                          LONG_DELAY_MS, MILLISECONDS,
589 >                          new ArrayBlockingQueue<Runnable>(10));
590 >        try (PoolCleaner cleaner = cleaner(p)) {
591 >            assertFalse(p.isShutdown());
592 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
593 >            assertTrue(p.isShutdown());
594 >        }
595      }
596  
597      /**
# Line 532 | Line 602 | public class ThreadPoolExecutorSubclassT
602              new CustomTPE(1, 1,
603                            LONG_DELAY_MS, MILLISECONDS,
604                            new ArrayBlockingQueue<Runnable>(10));
605 <        final CountDownLatch threadStarted = new CountDownLatch(1);
606 <        final CountDownLatch done = new CountDownLatch(1);
607 <        try {
605 >        try (PoolCleaner cleaner = cleaner(p)) {
606 >            final CountDownLatch threadStarted = new CountDownLatch(1);
607 >            final CountDownLatch done = new CountDownLatch(1);
608              assertFalse(p.isTerminating());
609              p.execute(new CheckedRunnable() {
610                  public void realRun() throws InterruptedException {
# Line 542 | Line 612 | public class ThreadPoolExecutorSubclassT
612                      threadStarted.countDown();
613                      done.await();
614                  }});
615 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
615 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
616              assertFalse(p.isTerminating());
617              done.countDown();
548        } finally {
618              try { p.shutdown(); } catch (SecurityException ok) { return; }
619 +            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
620 +            assertTrue(p.isTerminated());
621 +            assertFalse(p.isTerminating());
622          }
551        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
552        assertTrue(p.isTerminated());
553        assertFalse(p.isTerminating());
623      }
624  
625      /**
# Line 561 | Line 630 | public class ThreadPoolExecutorSubclassT
630              new CustomTPE(1, 1,
631                            LONG_DELAY_MS, MILLISECONDS,
632                            new ArrayBlockingQueue<Runnable>(10));
633 <        final CountDownLatch threadStarted = new CountDownLatch(1);
634 <        final CountDownLatch done = new CountDownLatch(1);
635 <        try {
633 >        try (PoolCleaner cleaner = cleaner(p)) {
634 >            final CountDownLatch threadStarted = new CountDownLatch(1);
635 >            final CountDownLatch done = new CountDownLatch(1);
636              assertFalse(p.isTerminating());
637              p.execute(new CheckedRunnable() {
638                  public void realRun() throws InterruptedException {
# Line 571 | Line 640 | public class ThreadPoolExecutorSubclassT
640                      threadStarted.countDown();
641                      done.await();
642                  }});
643 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
643 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
644              assertFalse(p.isTerminating());
645              done.countDown();
577        } finally {
646              try { p.shutdown(); } catch (SecurityException ok) { return; }
647 +            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
648 +            assertTrue(p.isTerminated());
649 +            assertFalse(p.isTerminating());
650          }
580        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
581        assertTrue(p.isTerminated());
582        assertFalse(p.isTerminating());
651      }
652  
653      /**
# Line 591 | Line 659 | public class ThreadPoolExecutorSubclassT
659              new CustomTPE(1, 1,
660                            LONG_DELAY_MS, MILLISECONDS,
661                            q);
662 <        final CountDownLatch threadStarted = new CountDownLatch(1);
663 <        final CountDownLatch done = new CountDownLatch(1);
664 <        try {
662 >        try (PoolCleaner cleaner = cleaner(p)) {
663 >            final CountDownLatch threadStarted = new CountDownLatch(1);
664 >            final CountDownLatch done = new CountDownLatch(1);
665              FutureTask[] tasks = new FutureTask[5];
666              for (int i = 0; i < tasks.length; i++) {
667                  Callable task = new CheckedCallable<Boolean>() {
# Line 606 | Line 674 | public class ThreadPoolExecutorSubclassT
674                  tasks[i] = new FutureTask(task);
675                  p.execute(tasks[i]);
676              }
677 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
677 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
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());
614        } finally {
682              done.countDown();
616            joinPool(p);
683          }
684      }
685  
# Line 626 | Line 692 | public class ThreadPoolExecutorSubclassT
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);
698 <        try {
695 >        try (PoolCleaner cleaner = cleaner(p)) {
696 >            Runnable[] tasks = new Runnable[6];
697 >            final CountDownLatch threadStarted = new CountDownLatch(1);
698 >            final CountDownLatch done = new CountDownLatch(1);
699              for (int i = 0; i < tasks.length; i++) {
700                  tasks[i] = new CheckedRunnable() {
701 <                        public void realRun() throws InterruptedException {
702 <                            threadStarted.countDown();
703 <                            done.await();
704 <                        }};
701 >                    public void realRun() throws InterruptedException {
702 >                        threadStarted.countDown();
703 >                        done.await();
704 >                    }};
705                  p.execute(tasks[i]);
706              }
707 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
707 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
708              assertFalse(p.remove(tasks[0]));
709              assertTrue(q.contains(tasks[4]));
710              assertTrue(q.contains(tasks[3]));
# Line 648 | Line 714 | public class ThreadPoolExecutorSubclassT
714              assertTrue(q.contains(tasks[3]));
715              assertTrue(p.remove(tasks[3]));
716              assertFalse(q.contains(tasks[3]));
651        } finally {
717              done.countDown();
653            joinPool(p);
718          }
719      }
720  
# Line 665 | Line 729 | public class ThreadPoolExecutorSubclassT
729              new CustomTPE(1, 1,
730                            LONG_DELAY_MS, MILLISECONDS,
731                            q);
732 <        FutureTask[] tasks = new FutureTask[5];
733 <        try {
732 >        try (PoolCleaner cleaner = cleaner(p, done)) {
733 >            FutureTask[] tasks = new FutureTask[5];
734              for (int i = 0; i < tasks.length; i++) {
735                  Callable task = new CheckedCallable<Boolean>() {
736                      public Boolean realCall() throws InterruptedException {
# Line 677 | Line 741 | public class ThreadPoolExecutorSubclassT
741                  tasks[i] = new FutureTask(task);
742                  p.execute(tasks[i]);
743              }
744 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
744 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
745              assertEquals(tasks.length, p.getTaskCount());
746              assertEquals(tasks.length - 1, q.size());
747              assertEquals(1L, p.getActiveCount());
# Line 690 | 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());
693        } finally {
694            done.countDown();
695            joinPool(p);
757          }
758      }
759  
760      /**
761 <     * shutdownNow returns a list containing tasks that were not run
761 >     * shutdownNow returns a list containing tasks that were not run,
762 >     * and those tasks are drained from the queue
763       */
764 <    public void testShutdownNow() {
765 <        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
766 <        List l;
767 <        try {
768 <            for (int i = 0; i < 5; i++)
769 <                p.execute(new MediumPossiblyInterruptedRunnable());
770 <        }
771 <        finally {
764 >    public void testShutdownNow() throws InterruptedException {
765 >        final int poolSize = 2;
766 >        final int count = 5;
767 >        final AtomicInteger ran = new AtomicInteger(0);
768 >        final ThreadPoolExecutor p =
769 >            new CustomTPE(poolSize, poolSize,
770 >                          LONG_DELAY_MS, MILLISECONDS,
771 >                          new ArrayBlockingQueue<Runnable>(10));
772 >        final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
773 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
774 >            threadsStarted.countDown();
775              try {
776 <                l = p.shutdownNow();
777 <            } catch (SecurityException ok) { return; }
776 >                MILLISECONDS.sleep(2 * LONG_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));
783 >        assertEquals(poolSize, p.getActiveCount());
784 >        assertEquals(0, p.getCompletedTaskCount());
785 >        final List<Runnable> queuedTasks;
786 >        try {
787 >            queuedTasks = p.shutdownNow();
788 >        } catch (SecurityException ok) {
789 >            return; // Allowed in case test doesn't have privs
790          }
791          assertTrue(p.isShutdown());
792 <        assertTrue(l.size() <= 4);
792 >        assertTrue(p.getQueue().isEmpty());
793 >        assertEquals(count - poolSize, queuedTasks.size());
794 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
795 >        assertTrue(p.isTerminated());
796 >        assertEquals(poolSize, ran.get());
797 >        assertEquals(poolSize, p.getCompletedTaskCount());
798      }
799  
800      // Exception Tests
# Line 722 | Line 804 | public class ThreadPoolExecutorSubclassT
804       */
805      public void testConstructor1() {
806          try {
807 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
807 >            new CustomTPE(-1, 1, 1L, SECONDS,
808 >                          new ArrayBlockingQueue<Runnable>(10));
809              shouldThrow();
810          } catch (IllegalArgumentException success) {}
811      }
# Line 732 | Line 815 | public class ThreadPoolExecutorSubclassT
815       */
816      public void testConstructor2() {
817          try {
818 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
818 >            new CustomTPE(1, -1, 1L, SECONDS,
819 >                          new ArrayBlockingQueue<Runnable>(10));
820              shouldThrow();
821          } catch (IllegalArgumentException success) {}
822      }
# Line 742 | Line 826 | public class ThreadPoolExecutorSubclassT
826       */
827      public void testConstructor3() {
828          try {
829 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
829 >            new CustomTPE(1, 0, 1L, SECONDS,
830 >                          new ArrayBlockingQueue<Runnable>(10));
831              shouldThrow();
832          } catch (IllegalArgumentException success) {}
833      }
# Line 752 | Line 837 | public class ThreadPoolExecutorSubclassT
837       */
838      public void testConstructor4() {
839          try {
840 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
840 >            new CustomTPE(1, 2, -1L, SECONDS,
841 >                          new ArrayBlockingQueue<Runnable>(10));
842              shouldThrow();
843          } catch (IllegalArgumentException success) {}
844      }
# Line 762 | Line 848 | public class ThreadPoolExecutorSubclassT
848       */
849      public void testConstructor5() {
850          try {
851 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
851 >            new CustomTPE(2, 1, 1L, SECONDS,
852 >                          new ArrayBlockingQueue<Runnable>(10));
853              shouldThrow();
854          } catch (IllegalArgumentException success) {}
855      }
# Line 772 | Line 859 | public class ThreadPoolExecutorSubclassT
859       */
860      public void testConstructorNullPointerException() {
861          try {
862 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null);
862 >            new CustomTPE(1, 2, 1L, SECONDS, null);
863              shouldThrow();
864          } catch (NullPointerException success) {}
865      }
# Line 782 | Line 869 | public class ThreadPoolExecutorSubclassT
869       */
870      public void testConstructor6() {
871          try {
872 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
872 >            new CustomTPE(-1, 1, 1L, SECONDS,
873 >                          new ArrayBlockingQueue<Runnable>(10),
874 >                          new SimpleThreadFactory());
875              shouldThrow();
876          } catch (IllegalArgumentException success) {}
877      }
# Line 792 | Line 881 | public class ThreadPoolExecutorSubclassT
881       */
882      public void testConstructor7() {
883          try {
884 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
884 >            new CustomTPE(1,-1, 1L, SECONDS,
885 >                          new ArrayBlockingQueue<Runnable>(10),
886 >                          new SimpleThreadFactory());
887              shouldThrow();
888          } catch (IllegalArgumentException success) {}
889      }
# Line 802 | Line 893 | public class ThreadPoolExecutorSubclassT
893       */
894      public void testConstructor8() {
895          try {
896 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
896 >            new CustomTPE(1, 0, 1L, SECONDS,
897 >                          new ArrayBlockingQueue<Runnable>(10),
898 >                          new SimpleThreadFactory());
899              shouldThrow();
900          } catch (IllegalArgumentException success) {}
901      }
# Line 812 | Line 905 | public class ThreadPoolExecutorSubclassT
905       */
906      public void testConstructor9() {
907          try {
908 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
908 >            new CustomTPE(1, 2, -1L, SECONDS,
909 >                          new ArrayBlockingQueue<Runnable>(10),
910 >                          new SimpleThreadFactory());
911              shouldThrow();
912          } catch (IllegalArgumentException success) {}
913      }
# Line 822 | Line 917 | public class ThreadPoolExecutorSubclassT
917       */
918      public void testConstructor10() {
919          try {
920 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
920 >            new CustomTPE(2, 1, 1L, SECONDS,
921 >                          new ArrayBlockingQueue<Runnable>(10),
922 >                          new SimpleThreadFactory());
923              shouldThrow();
924          } catch (IllegalArgumentException success) {}
925      }
# Line 832 | Line 929 | public class ThreadPoolExecutorSubclassT
929       */
930      public void testConstructorNullPointerException2() {
931          try {
932 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
932 >            new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory());
933              shouldThrow();
934          } catch (NullPointerException success) {}
935      }
# Line 842 | Line 939 | public class ThreadPoolExecutorSubclassT
939       */
940      public void testConstructorNullPointerException3() {
941          try {
942 <            ThreadFactory f = null;
943 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
942 >            new CustomTPE(1, 2, 1L, SECONDS,
943 >                          new ArrayBlockingQueue<Runnable>(10),
944 >                          (ThreadFactory) null);
945              shouldThrow();
946          } catch (NullPointerException success) {}
947      }
# Line 853 | Line 951 | public class ThreadPoolExecutorSubclassT
951       */
952      public void testConstructor11() {
953          try {
954 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
954 >            new CustomTPE(-1, 1, 1L, SECONDS,
955 >                          new ArrayBlockingQueue<Runnable>(10),
956 >                          new NoOpREHandler());
957              shouldThrow();
958          } catch (IllegalArgumentException success) {}
959      }
# Line 863 | Line 963 | public class ThreadPoolExecutorSubclassT
963       */
964      public void testConstructor12() {
965          try {
966 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
966 >            new CustomTPE(1, -1, 1L, SECONDS,
967 >                          new ArrayBlockingQueue<Runnable>(10),
968 >                          new NoOpREHandler());
969              shouldThrow();
970          } catch (IllegalArgumentException success) {}
971      }
# Line 873 | Line 975 | public class ThreadPoolExecutorSubclassT
975       */
976      public void testConstructor13() {
977          try {
978 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
978 >            new CustomTPE(1, 0, 1L, SECONDS,
979 >                          new ArrayBlockingQueue<Runnable>(10),
980 >                          new NoOpREHandler());
981              shouldThrow();
982          } catch (IllegalArgumentException success) {}
983      }
# Line 883 | Line 987 | public class ThreadPoolExecutorSubclassT
987       */
988      public void testConstructor14() {
989          try {
990 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
990 >            new CustomTPE(1, 2, -1L, SECONDS,
991 >                          new ArrayBlockingQueue<Runnable>(10),
992 >                          new NoOpREHandler());
993              shouldThrow();
994          } catch (IllegalArgumentException success) {}
995      }
# Line 893 | Line 999 | public class ThreadPoolExecutorSubclassT
999       */
1000      public void testConstructor15() {
1001          try {
1002 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
1002 >            new CustomTPE(2, 1, 1L, SECONDS,
1003 >                          new ArrayBlockingQueue<Runnable>(10),
1004 >                          new NoOpREHandler());
1005              shouldThrow();
1006          } catch (IllegalArgumentException success) {}
1007      }
# Line 903 | Line 1011 | public class ThreadPoolExecutorSubclassT
1011       */
1012      public void testConstructorNullPointerException4() {
1013          try {
1014 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
1014 >            new CustomTPE(1, 2, 1L, SECONDS,
1015 >                          null,
1016 >                          new NoOpREHandler());
1017              shouldThrow();
1018          } catch (NullPointerException success) {}
1019      }
# Line 913 | Line 1023 | public class ThreadPoolExecutorSubclassT
1023       */
1024      public void testConstructorNullPointerException5() {
1025          try {
1026 <            RejectedExecutionHandler r = null;
1027 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
1026 >            new CustomTPE(1, 2, 1L, SECONDS,
1027 >                          new ArrayBlockingQueue<Runnable>(10),
1028 >                          (RejectedExecutionHandler) null);
1029              shouldThrow();
1030          } catch (NullPointerException success) {}
1031      }
# Line 924 | Line 1035 | public class ThreadPoolExecutorSubclassT
1035       */
1036      public void testConstructor16() {
1037          try {
1038 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1038 >            new CustomTPE(-1, 1, 1L, SECONDS,
1039 >                          new ArrayBlockingQueue<Runnable>(10),
1040 >                          new SimpleThreadFactory(),
1041 >                          new NoOpREHandler());
1042              shouldThrow();
1043          } catch (IllegalArgumentException success) {}
1044      }
# Line 934 | Line 1048 | public class ThreadPoolExecutorSubclassT
1048       */
1049      public void testConstructor17() {
1050          try {
1051 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1051 >            new CustomTPE(1, -1, 1L, SECONDS,
1052 >                          new ArrayBlockingQueue<Runnable>(10),
1053 >                          new SimpleThreadFactory(),
1054 >                          new NoOpREHandler());
1055              shouldThrow();
1056          } catch (IllegalArgumentException success) {}
1057      }
# Line 944 | Line 1061 | public class ThreadPoolExecutorSubclassT
1061       */
1062      public void testConstructor18() {
1063          try {
1064 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1064 >            new CustomTPE(1, 0, 1L, SECONDS,
1065 >                          new ArrayBlockingQueue<Runnable>(10),
1066 >                          new SimpleThreadFactory(),
1067 >                          new NoOpREHandler());
1068              shouldThrow();
1069          } catch (IllegalArgumentException success) {}
1070      }
# Line 954 | Line 1074 | public class ThreadPoolExecutorSubclassT
1074       */
1075      public void testConstructor19() {
1076          try {
1077 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1077 >            new CustomTPE(1, 2, -1L, SECONDS,
1078 >                          new ArrayBlockingQueue<Runnable>(10),
1079 >                          new SimpleThreadFactory(),
1080 >                          new NoOpREHandler());
1081              shouldThrow();
1082          } catch (IllegalArgumentException success) {}
1083      }
# Line 964 | Line 1087 | public class ThreadPoolExecutorSubclassT
1087       */
1088      public void testConstructor20() {
1089          try {
1090 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1090 >            new CustomTPE(2, 1, 1L, SECONDS,
1091 >                          new ArrayBlockingQueue<Runnable>(10),
1092 >                          new SimpleThreadFactory(),
1093 >                          new NoOpREHandler());
1094              shouldThrow();
1095          } catch (IllegalArgumentException success) {}
1096      }
# Line 974 | Line 1100 | public class ThreadPoolExecutorSubclassT
1100       */
1101      public void testConstructorNullPointerException6() {
1102          try {
1103 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
1103 >            new CustomTPE(1, 2, 1L, SECONDS,
1104 >                          null,
1105 >                          new SimpleThreadFactory(),
1106 >                          new NoOpREHandler());
1107              shouldThrow();
1108          } catch (NullPointerException success) {}
1109      }
# Line 984 | Line 1113 | public class ThreadPoolExecutorSubclassT
1113       */
1114      public void testConstructorNullPointerException7() {
1115          try {
1116 <            RejectedExecutionHandler r = null;
1117 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
1116 >            new CustomTPE(1, 2, 1L, SECONDS,
1117 >                          new ArrayBlockingQueue<Runnable>(10),
1118 >                          new SimpleThreadFactory(),
1119 >                          (RejectedExecutionHandler) null);
1120              shouldThrow();
1121          } catch (NullPointerException success) {}
1122      }
# Line 995 | Line 1126 | public class ThreadPoolExecutorSubclassT
1126       */
1127      public void testConstructorNullPointerException8() {
1128          try {
1129 <            new CustomTPE(1, 2,
999 <                          LONG_DELAY_MS, MILLISECONDS,
1129 >            new CustomTPE(1, 2, 1L, SECONDS,
1130                            new ArrayBlockingQueue<Runnable>(10),
1131                            (ThreadFactory) null,
1132                            new NoOpREHandler());
# Line 1008 | Line 1138 | public class ThreadPoolExecutorSubclassT
1138       * execute throws RejectedExecutionException if saturated.
1139       */
1140      public void testSaturatedExecute() {
1141 <        ThreadPoolExecutor p =
1141 >        final ThreadPoolExecutor p =
1142              new CustomTPE(1, 1,
1143                            LONG_DELAY_MS, MILLISECONDS,
1144                            new ArrayBlockingQueue<Runnable>(1));
1145 <        final CountDownLatch done = new CountDownLatch(1);
1146 <        try {
1145 >        try (PoolCleaner cleaner = cleaner(p)) {
1146 >            final CountDownLatch done = new CountDownLatch(1);
1147              Runnable task = new CheckedRunnable() {
1148                  public void realRun() throws InterruptedException {
1149                      done.await();
# Line 1027 | Line 1157 | public class ThreadPoolExecutorSubclassT
1157                  } catch (RejectedExecutionException success) {}
1158                  assertTrue(p.getTaskCount() <= 2);
1159              }
1030        } finally {
1160              done.countDown();
1032            joinPool(p);
1161          }
1162      }
1163  
# Line 1037 | Line 1165 | public class ThreadPoolExecutorSubclassT
1165       * executor using CallerRunsPolicy runs task if saturated.
1166       */
1167      public void testSaturatedExecute2() {
1168 <        RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1169 <        ThreadPoolExecutor p = new CustomTPE(1, 1,
1170 <                                             LONG_DELAY_MS, MILLISECONDS,
1171 <                                             new ArrayBlockingQueue<Runnable>(1),
1172 <                                             h);
1173 <        try {
1168 >        final ThreadPoolExecutor p =
1169 >            new CustomTPE(1, 1,
1170 >                          LONG_DELAY_MS, MILLISECONDS,
1171 >                          new ArrayBlockingQueue<Runnable>(1),
1172 >                          new CustomTPE.CallerRunsPolicy());
1173 >        try (PoolCleaner cleaner = cleaner(p)) {
1174 >            final CountDownLatch done = new CountDownLatch(1);
1175 >            Runnable blocker = new CheckedRunnable() {
1176 >                public void realRun() throws InterruptedException {
1177 >                    done.await();
1178 >                }};
1179 >            p.execute(blocker);
1180              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1181 <            for (int i = 0; i < tasks.length; ++i)
1181 >            for (int i = 0; i < tasks.length; i++)
1182                  tasks[i] = new TrackedNoOpRunnable();
1183 <            TrackedLongRunnable mr = new TrackedLongRunnable();
1050 <            p.execute(mr);
1051 <            for (int i = 0; i < tasks.length; ++i)
1183 >            for (int i = 0; i < tasks.length; i++)
1184                  p.execute(tasks[i]);
1185 <            for (int i = 1; i < tasks.length; ++i)
1185 >            for (int i = 1; i < tasks.length; i++)
1186                  assertTrue(tasks[i].done);
1187 <            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1188 <        } finally {
1057 <            joinPool(p);
1187 >            assertFalse(tasks[0].done); // waiting in queue
1188 >            done.countDown();
1189          }
1190      }
1191  
# Line 1062 | Line 1193 | public class ThreadPoolExecutorSubclassT
1193       * executor using DiscardPolicy drops task if saturated.
1194       */
1195      public void testSaturatedExecute3() {
1196 <        RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1197 <        ThreadPoolExecutor p =
1196 >        final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1197 >        for (int i = 0; i < tasks.length; ++i)
1198 >            tasks[i] = new TrackedNoOpRunnable();
1199 >        final ThreadPoolExecutor p =
1200              new CustomTPE(1, 1,
1201                            LONG_DELAY_MS, MILLISECONDS,
1202                            new ArrayBlockingQueue<Runnable>(1),
1203 <                          h);
1204 <        try {
1205 <            TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1206 <            for (int i = 0; i < tasks.length; ++i)
1207 <                tasks[i] = new TrackedNoOpRunnable();
1075 <            p.execute(new TrackedLongRunnable());
1203 >                          new CustomTPE.DiscardPolicy());
1204 >        try (PoolCleaner cleaner = cleaner(p)) {
1205 >            final CountDownLatch done = new CountDownLatch(1);
1206 >            p.execute(awaiter(done));
1207 >
1208              for (TrackedNoOpRunnable task : tasks)
1209                  p.execute(task);
1210 <            for (TrackedNoOpRunnable task : tasks)
1211 <                assertFalse(task.done);
1212 <            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1081 <        } finally {
1082 <            joinPool(p);
1210 >            for (int i = 1; i < tasks.length; i++)
1211 >                assertFalse(tasks[i].done);
1212 >            done.countDown();
1213          }
1214 +        for (int i = 1; i < tasks.length; i++)
1215 +            assertFalse(tasks[i].done);
1216 +        assertTrue(tasks[0].done); // was waiting in queue
1217      }
1218  
1219      /**
1220       * executor using DiscardOldestPolicy drops oldest task if saturated.
1221       */
1222      public void testSaturatedExecute4() {
1223 <        RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
1224 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1225 <        try {
1226 <            p.execute(new TrackedLongRunnable());
1227 <            TrackedLongRunnable r2 = new TrackedLongRunnable();
1223 >        final CountDownLatch done = new CountDownLatch(1);
1224 >        LatchAwaiter r1 = awaiter(done);
1225 >        LatchAwaiter r2 = awaiter(done);
1226 >        LatchAwaiter r3 = awaiter(done);
1227 >        final ThreadPoolExecutor p =
1228 >            new CustomTPE(1, 1,
1229 >                          LONG_DELAY_MS, MILLISECONDS,
1230 >                          new ArrayBlockingQueue<Runnable>(1),
1231 >                          new CustomTPE.DiscardOldestPolicy());
1232 >        try (PoolCleaner cleaner = cleaner(p)) {
1233 >            assertEquals(LatchAwaiter.NEW, r1.state);
1234 >            assertEquals(LatchAwaiter.NEW, r2.state);
1235 >            assertEquals(LatchAwaiter.NEW, r3.state);
1236 >            p.execute(r1);
1237              p.execute(r2);
1238              assertTrue(p.getQueue().contains(r2));
1097            TrackedNoOpRunnable r3 = new TrackedNoOpRunnable();
1239              p.execute(r3);
1240              assertFalse(p.getQueue().contains(r2));
1241              assertTrue(p.getQueue().contains(r3));
1242 <            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1102 <        } finally {
1103 <            joinPool(p);
1242 >            done.countDown();
1243          }
1244 +        assertEquals(LatchAwaiter.DONE, r1.state);
1245 +        assertEquals(LatchAwaiter.NEW, r2.state);
1246 +        assertEquals(LatchAwaiter.DONE, r3.state);
1247      }
1248  
1249      /**
1250       * execute throws RejectedExecutionException if shutdown
1251       */
1252      public void testRejectedExecutionExceptionOnShutdown() {
1253 <        ThreadPoolExecutor p =
1254 <            new CustomTPE(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
1253 >        final ThreadPoolExecutor p =
1254 >            new CustomTPE(1, 1,
1255 >                          LONG_DELAY_MS, MILLISECONDS,
1256 >                          new ArrayBlockingQueue<Runnable>(1));
1257          try { p.shutdown(); } catch (SecurityException ok) { return; }
1258 <        try {
1259 <            p.execute(new NoOpRunnable());
1260 <            shouldThrow();
1261 <        } catch (RejectedExecutionException success) {}
1262 <
1263 <        joinPool(p);
1258 >        try (PoolCleaner cleaner = cleaner(p)) {
1259 >            try {
1260 >                p.execute(new NoOpRunnable());
1261 >                shouldThrow();
1262 >            } catch (RejectedExecutionException success) {}
1263 >        }
1264      }
1265  
1266      /**
1267       * execute using CallerRunsPolicy drops task on shutdown
1268       */
1269      public void testCallerRunsOnShutdown() {
1270 <        RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1271 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1272 <
1270 >        final ThreadPoolExecutor p =
1271 >            new CustomTPE(1, 1,
1272 >                          LONG_DELAY_MS, MILLISECONDS,
1273 >                          new ArrayBlockingQueue<Runnable>(1),
1274 >                          new CustomTPE.CallerRunsPolicy());
1275          try { p.shutdown(); } catch (SecurityException ok) { return; }
1276 <        try {
1276 >        try (PoolCleaner cleaner = cleaner(p)) {
1277              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1278              p.execute(r);
1279              assertFalse(r.done);
1134        } finally {
1135            joinPool(p);
1280          }
1281      }
1282  
# Line 1140 | Line 1284 | public class ThreadPoolExecutorSubclassT
1284       * execute using DiscardPolicy drops task on shutdown
1285       */
1286      public void testDiscardOnShutdown() {
1287 <        RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1288 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1289 <
1287 >        final ThreadPoolExecutor p =
1288 >            new CustomTPE(1, 1,
1289 >                          LONG_DELAY_MS, MILLISECONDS,
1290 >                          new ArrayBlockingQueue<Runnable>(1),
1291 >                          new CustomTPE.DiscardPolicy());
1292          try { p.shutdown(); } catch (SecurityException ok) { return; }
1293 <        try {
1293 >        try (PoolCleaner cleaner = cleaner(p)) {
1294              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1295              p.execute(r);
1296              assertFalse(r.done);
1151        } finally {
1152            joinPool(p);
1297          }
1298      }
1299  
# Line 1157 | Line 1301 | public class ThreadPoolExecutorSubclassT
1301       * execute using DiscardOldestPolicy drops task on shutdown
1302       */
1303      public void testDiscardOldestOnShutdown() {
1304 <        RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
1305 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1304 >        final ThreadPoolExecutor p =
1305 >            new CustomTPE(1, 1,
1306 >                          LONG_DELAY_MS, MILLISECONDS,
1307 >                          new ArrayBlockingQueue<Runnable>(1),
1308 >                          new CustomTPE.DiscardOldestPolicy());
1309  
1310          try { p.shutdown(); } catch (SecurityException ok) { return; }
1311 <        try {
1311 >        try (PoolCleaner cleaner = cleaner(p)) {
1312              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1313              p.execute(r);
1314              assertFalse(r.done);
1168        } finally {
1169            joinPool(p);
1315          }
1316      }
1317  
# Line 1174 | Line 1319 | public class ThreadPoolExecutorSubclassT
1319       * execute(null) throws NPE
1320       */
1321      public void testExecuteNull() {
1322 <        ThreadPoolExecutor p = null;
1323 <        try {
1324 <            p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1325 <            p.execute(null);
1326 <            shouldThrow();
1327 <        } catch (NullPointerException success) {}
1328 <
1329 <        joinPool(p);
1322 >        final ThreadPoolExecutor p =
1323 >            new CustomTPE(1, 2,
1324 >                          1L, SECONDS,
1325 >                          new ArrayBlockingQueue<Runnable>(10));
1326 >        try (PoolCleaner cleaner = cleaner(p)) {
1327 >            try {
1328 >                p.execute(null);
1329 >                shouldThrow();
1330 >            } catch (NullPointerException success) {}
1331 >        }
1332      }
1333  
1334      /**
1335       * setCorePoolSize of negative value throws IllegalArgumentException
1336       */
1337      public void testCorePoolSizeIllegalArgumentException() {
1338 <        ThreadPoolExecutor p =
1339 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1340 <        try {
1341 <            p.setCorePoolSize(-1);
1342 <            shouldThrow();
1343 <        } catch (IllegalArgumentException success) {
1344 <        } finally {
1345 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1338 >        final ThreadPoolExecutor p =
1339 >            new CustomTPE(1, 2,
1340 >                          LONG_DELAY_MS, MILLISECONDS,
1341 >                          new ArrayBlockingQueue<Runnable>(10));
1342 >        try (PoolCleaner cleaner = cleaner(p)) {
1343 >            try {
1344 >                p.setCorePoolSize(-1);
1345 >                shouldThrow();
1346 >            } catch (IllegalArgumentException success) {}
1347          }
1200        joinPool(p);
1348      }
1349  
1350      /**
# Line 1205 | Line 1352 | public class ThreadPoolExecutorSubclassT
1352       * if given a value less the core pool size
1353       */
1354      public void testMaximumPoolSizeIllegalArgumentException() {
1355 <        ThreadPoolExecutor p =
1356 <            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1357 <        try {
1358 <            p.setMaximumPoolSize(1);
1359 <            shouldThrow();
1360 <        } catch (IllegalArgumentException success) {
1361 <        } finally {
1362 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1355 >        final ThreadPoolExecutor p =
1356 >            new CustomTPE(2, 3,
1357 >                          LONG_DELAY_MS, MILLISECONDS,
1358 >                          new ArrayBlockingQueue<Runnable>(10));
1359 >        try (PoolCleaner cleaner = cleaner(p)) {
1360 >            try {
1361 >                p.setMaximumPoolSize(1);
1362 >                shouldThrow();
1363 >            } catch (IllegalArgumentException success) {}
1364          }
1217        joinPool(p);
1365      }
1366  
1367      /**
# Line 1222 | Line 1369 | public class ThreadPoolExecutorSubclassT
1369       * if given a negative value
1370       */
1371      public void testMaximumPoolSizeIllegalArgumentException2() {
1372 <        ThreadPoolExecutor p =
1373 <            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1374 <        try {
1375 <            p.setMaximumPoolSize(-1);
1376 <            shouldThrow();
1377 <        } catch (IllegalArgumentException success) {
1378 <        } finally {
1379 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1372 >        final ThreadPoolExecutor p =
1373 >            new CustomTPE(2, 3,
1374 >                          LONG_DELAY_MS,
1375 >                          MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1376 >        try (PoolCleaner cleaner = cleaner(p)) {
1377 >            try {
1378 >                p.setMaximumPoolSize(-1);
1379 >                shouldThrow();
1380 >            } catch (IllegalArgumentException success) {}
1381          }
1234        joinPool(p);
1382      }
1383  
1384      /**
# Line 1239 | Line 1386 | public class ThreadPoolExecutorSubclassT
1386       * when given a negative value
1387       */
1388      public void testKeepAliveTimeIllegalArgumentException() {
1389 <        ThreadPoolExecutor p =
1390 <            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1391 <
1392 <        try {
1393 <            p.setKeepAliveTime(-1,MILLISECONDS);
1394 <            shouldThrow();
1395 <        } catch (IllegalArgumentException success) {
1396 <        } finally {
1397 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1389 >        final ThreadPoolExecutor p =
1390 >            new CustomTPE(2, 3,
1391 >                          LONG_DELAY_MS, MILLISECONDS,
1392 >                          new ArrayBlockingQueue<Runnable>(10));
1393 >        try (PoolCleaner cleaner = cleaner(p)) {
1394 >            try {
1395 >                p.setKeepAliveTime(-1, MILLISECONDS);
1396 >                shouldThrow();
1397 >            } catch (IllegalArgumentException success) {}
1398          }
1252        joinPool(p);
1399      }
1400  
1401      /**
# Line 1257 | Line 1403 | public class ThreadPoolExecutorSubclassT
1403       */
1404      public void testTerminated() {
1405          CustomTPE p = new CustomTPE();
1406 <        try { p.shutdown(); } catch (SecurityException ok) { return; }
1407 <        assertTrue(p.terminatedCalled());
1408 <        joinPool(p);
1406 >        try (PoolCleaner cleaner = cleaner(p)) {
1407 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1408 >            assertTrue(p.terminatedCalled());
1409 >            assertTrue(p.isShutdown());
1410 >        }
1411      }
1412  
1413      /**
# Line 1267 | Line 1415 | public class ThreadPoolExecutorSubclassT
1415       */
1416      public void testBeforeAfter() throws InterruptedException {
1417          CustomTPE p = new CustomTPE();
1418 <        try {
1418 >        try (PoolCleaner cleaner = cleaner(p)) {
1419              final CountDownLatch done = new CountDownLatch(1);
1420              p.execute(new CheckedRunnable() {
1421                  public void realRun() {
# Line 1277 | Line 1425 | public class ThreadPoolExecutorSubclassT
1425              assertEquals(0, done.getCount());
1426              assertTrue(p.afterCalled());
1427              assertTrue(p.beforeCalled());
1280            try { p.shutdown(); } catch (SecurityException ok) { return; }
1281        } finally {
1282            joinPool(p);
1428          }
1429      }
1430  
# Line 1287 | Line 1432 | public class ThreadPoolExecutorSubclassT
1432       * completed submit of callable returns result
1433       */
1434      public void testSubmitCallable() throws Exception {
1435 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1436 <        try {
1435 >        final ExecutorService e =
1436 >            new CustomTPE(2, 2,
1437 >                          LONG_DELAY_MS, MILLISECONDS,
1438 >                          new ArrayBlockingQueue<Runnable>(10));
1439 >        try (PoolCleaner cleaner = cleaner(e)) {
1440              Future<String> future = e.submit(new StringTask());
1441              String result = future.get();
1442              assertSame(TEST_STRING, result);
1295        } finally {
1296            joinPool(e);
1443          }
1444      }
1445  
# Line 1301 | Line 1447 | public class ThreadPoolExecutorSubclassT
1447       * completed submit of runnable returns successfully
1448       */
1449      public void testSubmitRunnable() throws Exception {
1450 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1451 <        try {
1450 >        final ExecutorService e =
1451 >            new CustomTPE(2, 2,
1452 >                          LONG_DELAY_MS, MILLISECONDS,
1453 >                          new ArrayBlockingQueue<Runnable>(10));
1454 >        try (PoolCleaner cleaner = cleaner(e)) {
1455              Future<?> future = e.submit(new NoOpRunnable());
1456              future.get();
1457              assertTrue(future.isDone());
1309        } finally {
1310            joinPool(e);
1458          }
1459      }
1460  
# Line 1315 | Line 1462 | public class ThreadPoolExecutorSubclassT
1462       * completed submit of (runnable, result) returns result
1463       */
1464      public void testSubmitRunnable2() throws Exception {
1465 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1466 <        try {
1465 >        final ExecutorService e =
1466 >            new CustomTPE(2, 2,
1467 >                          LONG_DELAY_MS, MILLISECONDS,
1468 >                          new ArrayBlockingQueue<Runnable>(10));
1469 >        try (PoolCleaner cleaner = cleaner(e)) {
1470              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1471              String result = future.get();
1472              assertSame(TEST_STRING, result);
1323        } finally {
1324            joinPool(e);
1473          }
1474      }
1475  
# Line 1329 | Line 1477 | public class ThreadPoolExecutorSubclassT
1477       * invokeAny(null) throws NPE
1478       */
1479      public void testInvokeAny1() throws Exception {
1480 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1481 <        try {
1482 <            e.invokeAny(null);
1483 <            shouldThrow();
1484 <        } catch (NullPointerException success) {
1485 <        } finally {
1486 <            joinPool(e);
1480 >        final ExecutorService e =
1481 >            new CustomTPE(2, 2,
1482 >                          LONG_DELAY_MS, MILLISECONDS,
1483 >                          new ArrayBlockingQueue<Runnable>(10));
1484 >        try (PoolCleaner cleaner = cleaner(e)) {
1485 >            try {
1486 >                e.invokeAny(null);
1487 >                shouldThrow();
1488 >            } catch (NullPointerException success) {}
1489          }
1490      }
1491  
# Line 1343 | Line 1493 | public class ThreadPoolExecutorSubclassT
1493       * invokeAny(empty collection) throws IAE
1494       */
1495      public void testInvokeAny2() throws Exception {
1496 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1497 <        try {
1498 <            e.invokeAny(new ArrayList<Callable<String>>());
1499 <            shouldThrow();
1500 <        } catch (IllegalArgumentException success) {
1501 <        } finally {
1502 <            joinPool(e);
1496 >        final ExecutorService e =
1497 >            new CustomTPE(2, 2,
1498 >                          LONG_DELAY_MS, MILLISECONDS,
1499 >                          new ArrayBlockingQueue<Runnable>(10));
1500 >        try (PoolCleaner cleaner = cleaner(e)) {
1501 >            try {
1502 >                e.invokeAny(new ArrayList<Callable<String>>());
1503 >                shouldThrow();
1504 >            } catch (IllegalArgumentException success) {}
1505          }
1506      }
1507  
# Line 1358 | Line 1510 | public class ThreadPoolExecutorSubclassT
1510       */
1511      public void testInvokeAny3() throws Exception {
1512          CountDownLatch latch = new CountDownLatch(1);
1513 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1514 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1515 <        l.add(latchAwaitingStringTask(latch));
1516 <        l.add(null);
1517 <        try {
1518 <            e.invokeAny(l);
1519 <            shouldThrow();
1520 <        } catch (NullPointerException success) {
1521 <        } finally {
1513 >        final ExecutorService e =
1514 >            new CustomTPE(2, 2,
1515 >                          LONG_DELAY_MS, MILLISECONDS,
1516 >                          new ArrayBlockingQueue<Runnable>(10));
1517 >        try (PoolCleaner cleaner = cleaner(e)) {
1518 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1519 >            l.add(latchAwaitingStringTask(latch));
1520 >            l.add(null);
1521 >            try {
1522 >                e.invokeAny(l);
1523 >                shouldThrow();
1524 >            } catch (NullPointerException success) {}
1525              latch.countDown();
1371            joinPool(e);
1526          }
1527      }
1528  
# Line 1376 | Line 1530 | public class ThreadPoolExecutorSubclassT
1530       * invokeAny(c) throws ExecutionException if no task completes
1531       */
1532      public void testInvokeAny4() throws Exception {
1533 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1534 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1535 <        l.add(new NPETask());
1536 <        try {
1537 <            e.invokeAny(l);
1538 <            shouldThrow();
1539 <        } catch (ExecutionException success) {
1540 <            assertTrue(success.getCause() instanceof NullPointerException);
1541 <        } finally {
1542 <            joinPool(e);
1533 >        final ExecutorService e =
1534 >            new CustomTPE(2, 2,
1535 >                          LONG_DELAY_MS, MILLISECONDS,
1536 >                          new ArrayBlockingQueue<Runnable>(10));
1537 >        try (PoolCleaner cleaner = cleaner(e)) {
1538 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1539 >            l.add(new NPETask());
1540 >            try {
1541 >                e.invokeAny(l);
1542 >                shouldThrow();
1543 >            } catch (ExecutionException success) {
1544 >                assertTrue(success.getCause() instanceof NullPointerException);
1545 >            }
1546          }
1547      }
1548  
# Line 1393 | Line 1550 | public class ThreadPoolExecutorSubclassT
1550       * invokeAny(c) returns result of some task
1551       */
1552      public void testInvokeAny5() throws Exception {
1553 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1554 <        try {
1553 >        final ExecutorService e =
1554 >            new CustomTPE(2, 2,
1555 >                          LONG_DELAY_MS, MILLISECONDS,
1556 >                          new ArrayBlockingQueue<Runnable>(10));
1557 >        try (PoolCleaner cleaner = cleaner(e)) {
1558              List<Callable<String>> l = new ArrayList<Callable<String>>();
1559              l.add(new StringTask());
1560              l.add(new StringTask());
1561              String result = e.invokeAny(l);
1562              assertSame(TEST_STRING, result);
1403        } finally {
1404            joinPool(e);
1563          }
1564      }
1565  
# Line 1409 | Line 1567 | public class ThreadPoolExecutorSubclassT
1567       * invokeAll(null) throws NPE
1568       */
1569      public void testInvokeAll1() throws Exception {
1570 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1571 <        try {
1572 <            e.invokeAll(null);
1573 <            shouldThrow();
1574 <        } catch (NullPointerException success) {
1575 <        } finally {
1576 <            joinPool(e);
1570 >        final ExecutorService e =
1571 >            new CustomTPE(2, 2,
1572 >                          LONG_DELAY_MS, MILLISECONDS,
1573 >                          new ArrayBlockingQueue<Runnable>(10));
1574 >        try (PoolCleaner cleaner = cleaner(e)) {
1575 >            try {
1576 >                e.invokeAll(null);
1577 >                shouldThrow();
1578 >            } catch (NullPointerException success) {}
1579          }
1580      }
1581  
# Line 1423 | Line 1583 | public class ThreadPoolExecutorSubclassT
1583       * invokeAll(empty collection) returns empty collection
1584       */
1585      public void testInvokeAll2() throws Exception {
1586 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1587 <        try {
1586 >        final ExecutorService e =
1587 >            new CustomTPE(2, 2,
1588 >                          LONG_DELAY_MS, MILLISECONDS,
1589 >                          new ArrayBlockingQueue<Runnable>(10));
1590 >        try (PoolCleaner cleaner = cleaner(e)) {
1591              List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1592              assertTrue(r.isEmpty());
1430        } finally {
1431            joinPool(e);
1593          }
1594      }
1595  
# Line 1436 | Line 1597 | public class ThreadPoolExecutorSubclassT
1597       * invokeAll(c) throws NPE if c has null elements
1598       */
1599      public void testInvokeAll3() throws Exception {
1600 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1601 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1602 <        l.add(new StringTask());
1603 <        l.add(null);
1604 <        try {
1605 <            e.invokeAll(l);
1606 <            shouldThrow();
1607 <        } catch (NullPointerException success) {
1608 <        } finally {
1609 <            joinPool(e);
1600 >        final ExecutorService e =
1601 >            new CustomTPE(2, 2,
1602 >                          LONG_DELAY_MS, MILLISECONDS,
1603 >                          new ArrayBlockingQueue<Runnable>(10));
1604 >        try (PoolCleaner cleaner = cleaner(e)) {
1605 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1606 >            l.add(new StringTask());
1607 >            l.add(null);
1608 >            try {
1609 >                e.invokeAll(l);
1610 >                shouldThrow();
1611 >            } catch (NullPointerException success) {}
1612          }
1613      }
1614  
# Line 1453 | Line 1616 | public class ThreadPoolExecutorSubclassT
1616       * get of element of invokeAll(c) throws exception on failed task
1617       */
1618      public void testInvokeAll4() throws Exception {
1619 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1620 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1621 <        l.add(new NPETask());
1622 <        List<Future<String>> futures = e.invokeAll(l);
1623 <        assertEquals(1, futures.size());
1624 <        try {
1625 <            futures.get(0).get();
1626 <            shouldThrow();
1627 <        } catch (ExecutionException success) {
1628 <            assertTrue(success.getCause() instanceof NullPointerException);
1629 <        } finally {
1630 <            joinPool(e);
1619 >        final ExecutorService e =
1620 >            new CustomTPE(2, 2,
1621 >                          LONG_DELAY_MS, MILLISECONDS,
1622 >                          new ArrayBlockingQueue<Runnable>(10));
1623 >        try (PoolCleaner cleaner = cleaner(e)) {
1624 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1625 >            l.add(new NPETask());
1626 >            List<Future<String>> futures = e.invokeAll(l);
1627 >            assertEquals(1, futures.size());
1628 >            try {
1629 >                futures.get(0).get();
1630 >                shouldThrow();
1631 >            } catch (ExecutionException success) {
1632 >                assertTrue(success.getCause() instanceof NullPointerException);
1633 >            }
1634          }
1635      }
1636  
# Line 1472 | Line 1638 | public class ThreadPoolExecutorSubclassT
1638       * invokeAll(c) returns results of all completed tasks
1639       */
1640      public void testInvokeAll5() throws Exception {
1641 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1642 <        try {
1641 >        final ExecutorService e =
1642 >            new CustomTPE(2, 2,
1643 >                          LONG_DELAY_MS, MILLISECONDS,
1644 >                          new ArrayBlockingQueue<Runnable>(10));
1645 >        try (PoolCleaner cleaner = cleaner(e)) {
1646              List<Callable<String>> l = new ArrayList<Callable<String>>();
1647              l.add(new StringTask());
1648              l.add(new StringTask());
# Line 1481 | Line 1650 | public class ThreadPoolExecutorSubclassT
1650              assertEquals(2, futures.size());
1651              for (Future<String> future : futures)
1652                  assertSame(TEST_STRING, future.get());
1484        } finally {
1485            joinPool(e);
1653          }
1654      }
1655  
# Line 1490 | Line 1657 | public class ThreadPoolExecutorSubclassT
1657       * timed invokeAny(null) throws NPE
1658       */
1659      public void testTimedInvokeAny1() throws Exception {
1660 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1661 <        try {
1662 <            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1663 <            shouldThrow();
1664 <        } catch (NullPointerException success) {
1665 <        } finally {
1666 <            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 >            try {
1666 >                e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1667 >                shouldThrow();
1668 >            } catch (NullPointerException success) {}
1669          }
1670      }
1671  
# Line 1504 | Line 1673 | public class ThreadPoolExecutorSubclassT
1673       * timed invokeAny(,,null) throws NPE
1674       */
1675      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1676 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1677 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1678 <        l.add(new StringTask());
1679 <        try {
1680 <            e.invokeAny(l, MEDIUM_DELAY_MS, null);
1681 <            shouldThrow();
1682 <        } catch (NullPointerException success) {
1683 <        } finally {
1684 <            joinPool(e);
1676 >        final ExecutorService e =
1677 >            new CustomTPE(2, 2,
1678 >                          LONG_DELAY_MS, MILLISECONDS,
1679 >                          new ArrayBlockingQueue<Runnable>(10));
1680 >        try (PoolCleaner cleaner = cleaner(e)) {
1681 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1682 >            l.add(new StringTask());
1683 >            try {
1684 >                e.invokeAny(l, MEDIUM_DELAY_MS, null);
1685 >                shouldThrow();
1686 >            } catch (NullPointerException success) {}
1687          }
1688      }
1689  
# Line 1520 | Line 1691 | public class ThreadPoolExecutorSubclassT
1691       * timed invokeAny(empty collection) throws IAE
1692       */
1693      public void testTimedInvokeAny2() throws Exception {
1694 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1695 <        try {
1696 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1697 <            shouldThrow();
1698 <        } catch (IllegalArgumentException success) {
1699 <        } finally {
1700 <            joinPool(e);
1694 >        final ExecutorService e =
1695 >            new CustomTPE(2, 2,
1696 >                          LONG_DELAY_MS, MILLISECONDS,
1697 >                          new ArrayBlockingQueue<Runnable>(10));
1698 >        try (PoolCleaner cleaner = cleaner(e)) {
1699 >            try {
1700 >                e.invokeAny(new ArrayList<Callable<String>>(),
1701 >                            MEDIUM_DELAY_MS, MILLISECONDS);
1702 >                shouldThrow();
1703 >            } catch (IllegalArgumentException success) {}
1704          }
1705      }
1706  
# Line 1535 | Line 1709 | public class ThreadPoolExecutorSubclassT
1709       */
1710      public void testTimedInvokeAny3() throws Exception {
1711          CountDownLatch latch = new CountDownLatch(1);
1712 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1713 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1714 <        l.add(latchAwaitingStringTask(latch));
1715 <        l.add(null);
1716 <        try {
1717 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1718 <            shouldThrow();
1719 <        } catch (NullPointerException success) {
1720 <        } finally {
1712 >        final ExecutorService e =
1713 >            new CustomTPE(2, 2,
1714 >                          LONG_DELAY_MS, MILLISECONDS,
1715 >                          new ArrayBlockingQueue<Runnable>(10));
1716 >        try (PoolCleaner cleaner = cleaner(e)) {
1717 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1718 >            l.add(latchAwaitingStringTask(latch));
1719 >            l.add(null);
1720 >            try {
1721 >                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1722 >                shouldThrow();
1723 >            } catch (NullPointerException success) {}
1724              latch.countDown();
1548            joinPool(e);
1725          }
1726      }
1727  
# Line 1553 | Line 1729 | public class ThreadPoolExecutorSubclassT
1729       * timed invokeAny(c) throws ExecutionException if no task completes
1730       */
1731      public void testTimedInvokeAny4() throws Exception {
1732 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1733 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1734 <        l.add(new NPETask());
1735 <        try {
1736 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1737 <            shouldThrow();
1738 <        } catch (ExecutionException success) {
1739 <            assertTrue(success.getCause() instanceof NullPointerException);
1740 <        } finally {
1741 <            joinPool(e);
1732 >        final ExecutorService e =
1733 >            new CustomTPE(2, 2,
1734 >                          LONG_DELAY_MS, MILLISECONDS,
1735 >                          new ArrayBlockingQueue<Runnable>(10));
1736 >        try (PoolCleaner cleaner = cleaner(e)) {
1737 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1738 >            l.add(new NPETask());
1739 >            try {
1740 >                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1741 >                shouldThrow();
1742 >            } catch (ExecutionException success) {
1743 >                assertTrue(success.getCause() instanceof NullPointerException);
1744 >            }
1745          }
1746      }
1747  
# Line 1570 | Line 1749 | public class ThreadPoolExecutorSubclassT
1749       * timed invokeAny(c) returns result of some task
1750       */
1751      public void testTimedInvokeAny5() throws Exception {
1752 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1753 <        try {
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<Callable<String>>();
1758              l.add(new StringTask());
1759              l.add(new StringTask());
1760              String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1761              assertSame(TEST_STRING, result);
1580        } finally {
1581            joinPool(e);
1762          }
1763      }
1764  
# Line 1586 | Line 1766 | public class ThreadPoolExecutorSubclassT
1766       * timed invokeAll(null) throws NPE
1767       */
1768      public void testTimedInvokeAll1() throws Exception {
1769 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1770 <        try {
1771 <            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1772 <            shouldThrow();
1773 <        } catch (NullPointerException success) {
1774 <        } finally {
1775 <            joinPool(e);
1769 >        final ExecutorService e =
1770 >            new CustomTPE(2, 2,
1771 >                          LONG_DELAY_MS, MILLISECONDS,
1772 >                          new ArrayBlockingQueue<Runnable>(10));
1773 >        try (PoolCleaner cleaner = cleaner(e)) {
1774 >            try {
1775 >                e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1776 >                shouldThrow();
1777 >            } catch (NullPointerException success) {}
1778          }
1779      }
1780  
# Line 1600 | Line 1782 | public class ThreadPoolExecutorSubclassT
1782       * timed invokeAll(,,null) throws NPE
1783       */
1784      public void testTimedInvokeAllNullTimeUnit() throws Exception {
1785 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1786 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1787 <        l.add(new StringTask());
1788 <        try {
1789 <            e.invokeAll(l, MEDIUM_DELAY_MS, null);
1790 <            shouldThrow();
1791 <        } catch (NullPointerException success) {
1792 <        } finally {
1793 <            joinPool(e);
1785 >        final ExecutorService e =
1786 >            new CustomTPE(2, 2,
1787 >                          LONG_DELAY_MS, MILLISECONDS,
1788 >                          new ArrayBlockingQueue<Runnable>(10));
1789 >        try (PoolCleaner cleaner = cleaner(e)) {
1790 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1791 >            l.add(new StringTask());
1792 >            try {
1793 >                e.invokeAll(l, MEDIUM_DELAY_MS, null);
1794 >                shouldThrow();
1795 >            } catch (NullPointerException success) {}
1796          }
1797      }
1798  
# Line 1616 | Line 1800 | public class ThreadPoolExecutorSubclassT
1800       * timed invokeAll(empty collection) returns empty collection
1801       */
1802      public void testTimedInvokeAll2() throws Exception {
1803 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1804 <        try {
1805 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1803 >        final ExecutorService e =
1804 >            new CustomTPE(2, 2,
1805 >                          LONG_DELAY_MS, MILLISECONDS,
1806 >                          new ArrayBlockingQueue<Runnable>(10));
1807 >        try (PoolCleaner cleaner = cleaner(e)) {
1808 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(),
1809 >                                                 MEDIUM_DELAY_MS, MILLISECONDS);
1810              assertTrue(r.isEmpty());
1623        } finally {
1624            joinPool(e);
1811          }
1812      }
1813  
# Line 1629 | Line 1815 | public class ThreadPoolExecutorSubclassT
1815       * timed invokeAll(c) throws NPE if c has null elements
1816       */
1817      public void testTimedInvokeAll3() throws Exception {
1818 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1819 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1820 <        l.add(new StringTask());
1821 <        l.add(null);
1822 <        try {
1823 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1824 <            shouldThrow();
1825 <        } catch (NullPointerException success) {
1826 <        } finally {
1827 <            joinPool(e);
1818 >        final ExecutorService e =
1819 >            new CustomTPE(2, 2,
1820 >                          LONG_DELAY_MS, MILLISECONDS,
1821 >                          new ArrayBlockingQueue<Runnable>(10));
1822 >        try (PoolCleaner cleaner = cleaner(e)) {
1823 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1824 >            l.add(new StringTask());
1825 >            l.add(null);
1826 >            try {
1827 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1828 >                shouldThrow();
1829 >            } catch (NullPointerException success) {}
1830          }
1831      }
1832  
# Line 1646 | Line 1834 | public class ThreadPoolExecutorSubclassT
1834       * get of element of invokeAll(c) throws exception on failed task
1835       */
1836      public void testTimedInvokeAll4() throws Exception {
1837 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1838 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1839 <        l.add(new NPETask());
1840 <        List<Future<String>> futures =
1841 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1842 <        assertEquals(1, futures.size());
1843 <        try {
1844 <            futures.get(0).get();
1845 <            shouldThrow();
1846 <        } catch (ExecutionException success) {
1847 <            assertTrue(success.getCause() instanceof NullPointerException);
1848 <        } finally {
1849 <            joinPool(e);
1837 >        final ExecutorService e =
1838 >            new CustomTPE(2, 2,
1839 >                          LONG_DELAY_MS, MILLISECONDS,
1840 >                          new ArrayBlockingQueue<Runnable>(10));
1841 >        try (PoolCleaner cleaner = cleaner(e)) {
1842 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1843 >            l.add(new NPETask());
1844 >            List<Future<String>> futures =
1845 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1846 >            assertEquals(1, futures.size());
1847 >            try {
1848 >                futures.get(0).get();
1849 >                shouldThrow();
1850 >            } catch (ExecutionException success) {
1851 >                assertTrue(success.getCause() instanceof NullPointerException);
1852 >            }
1853          }
1854      }
1855  
# Line 1666 | Line 1857 | public class ThreadPoolExecutorSubclassT
1857       * timed invokeAll(c) returns results of all completed tasks
1858       */
1859      public void testTimedInvokeAll5() throws Exception {
1860 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1861 <        try {
1860 >        final ExecutorService e =
1861 >            new CustomTPE(2, 2,
1862 >                          LONG_DELAY_MS, MILLISECONDS,
1863 >                          new ArrayBlockingQueue<Runnable>(10));
1864 >        try (PoolCleaner cleaner = cleaner(e)) {
1865              List<Callable<String>> l = new ArrayList<Callable<String>>();
1866              l.add(new StringTask());
1867              l.add(new StringTask());
1868              List<Future<String>> futures =
1869 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1869 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1870              assertEquals(2, futures.size());
1871              for (Future<String> future : futures)
1872                  assertSame(TEST_STRING, future.get());
1679        } finally {
1680            joinPool(e);
1873          }
1874      }
1875  
# Line 1685 | Line 1877 | public class ThreadPoolExecutorSubclassT
1877       * timed invokeAll(c) cancels tasks not completed by timeout
1878       */
1879      public void testTimedInvokeAll6() throws Exception {
1880 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1881 <        try {
1882 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1883 <            l.add(new StringTask());
1884 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1885 <            l.add(new StringTask());
1886 <            List<Future<String>> futures =
1887 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1888 <            assertEquals(l.size(), futures.size());
1889 <            for (Future future : futures)
1890 <                assertTrue(future.isDone());
1891 <            assertFalse(futures.get(0).isCancelled());
1892 <            assertTrue(futures.get(1).isCancelled());
1893 <        } finally {
1894 <            joinPool(e);
1880 >        final ExecutorService e =
1881 >            new CustomTPE(2, 2,
1882 >                          LONG_DELAY_MS, MILLISECONDS,
1883 >                          new ArrayBlockingQueue<Runnable>(10));
1884 >        try (PoolCleaner cleaner = cleaner(e)) {
1885 >            for (long timeout = timeoutMillis();;) {
1886 >                List<Callable<String>> tasks = new ArrayList<>();
1887 >                tasks.add(new StringTask("0"));
1888 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1889 >                tasks.add(new StringTask("2"));
1890 >                long startTime = System.nanoTime();
1891 >                List<Future<String>> futures =
1892 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1893 >                assertEquals(tasks.size(), futures.size());
1894 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1895 >                for (Future future : futures)
1896 >                    assertTrue(future.isDone());
1897 >                assertTrue(futures.get(1).isCancelled());
1898 >                try {
1899 >                    assertEquals("0", futures.get(0).get());
1900 >                    assertEquals("2", futures.get(2).get());
1901 >                    break;
1902 >                } catch (CancellationException retryWithLongerTimeout) {
1903 >                    timeout *= 2;
1904 >                    if (timeout >= LONG_DELAY_MS / 2)
1905 >                        fail("expected exactly one task to be cancelled");
1906 >                }
1907 >            }
1908          }
1909      }
1910  
# Line 1713 | Line 1918 | public class ThreadPoolExecutorSubclassT
1918                            LONG_DELAY_MS, MILLISECONDS,
1919                            new LinkedBlockingQueue<Runnable>(),
1920                            new FailingThreadFactory());
1921 <        try {
1921 >        try (PoolCleaner cleaner = cleaner(e)) {
1922              final int TASKS = 100;
1923              final CountDownLatch done = new CountDownLatch(TASKS);
1924              for (int k = 0; k < TASKS; ++k)
# Line 1722 | Line 1927 | public class ThreadPoolExecutorSubclassT
1927                          done.countDown();
1928                      }});
1929              assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1725        } finally {
1726            joinPool(e);
1930          }
1931      }
1932  
# Line 1731 | Line 1934 | public class ThreadPoolExecutorSubclassT
1934       * allowsCoreThreadTimeOut is by default false.
1935       */
1936      public void testAllowsCoreThreadTimeOut() {
1937 <        ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1938 <        assertFalse(p.allowsCoreThreadTimeOut());
1939 <        joinPool(p);
1937 >        final ThreadPoolExecutor p =
1938 >            new CustomTPE(2, 2,
1939 >                          1000, MILLISECONDS,
1940 >                          new ArrayBlockingQueue<Runnable>(10));
1941 >        try (PoolCleaner cleaner = cleaner(p)) {
1942 >            assertFalse(p.allowsCoreThreadTimeOut());
1943 >        }
1944      }
1945  
1946      /**
1947       * allowCoreThreadTimeOut(true) causes idle threads to time out
1948       */
1949      public void testAllowCoreThreadTimeOut_true() throws Exception {
1950 <        long coreThreadTimeOut = SHORT_DELAY_MS;
1950 >        long keepAliveTime = timeoutMillis();
1951          final ThreadPoolExecutor p =
1952              new CustomTPE(2, 10,
1953 <                          coreThreadTimeOut, MILLISECONDS,
1953 >                          keepAliveTime, MILLISECONDS,
1954                            new ArrayBlockingQueue<Runnable>(10));
1955 <        final CountDownLatch threadStarted = new CountDownLatch(1);
1956 <        try {
1955 >        try (PoolCleaner cleaner = cleaner(p)) {
1956 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1957              p.allowCoreThreadTimeOut(true);
1958              p.execute(new CheckedRunnable() {
1959 <                public void realRun() throws InterruptedException {
1959 >                public void realRun() {
1960                      threadStarted.countDown();
1961                      assertEquals(1, p.getPoolSize());
1962                  }});
1963              await(threadStarted);
1964 <            delay(coreThreadTimeOut);
1964 >            delay(keepAliveTime);
1965              long startTime = System.nanoTime();
1966              while (p.getPoolSize() > 0
1967                     && millisElapsedSince(startTime) < LONG_DELAY_MS)
1968                  Thread.yield();
1969              assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1970              assertEquals(0, p.getPoolSize());
1764        } finally {
1765            joinPool(p);
1971          }
1972      }
1973  
# Line 1770 | Line 1975 | public class ThreadPoolExecutorSubclassT
1975       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1976       */
1977      public void testAllowCoreThreadTimeOut_false() throws Exception {
1978 <        long coreThreadTimeOut = SHORT_DELAY_MS;
1978 >        long keepAliveTime = timeoutMillis();
1979          final ThreadPoolExecutor p =
1980              new CustomTPE(2, 10,
1981 <                          coreThreadTimeOut, MILLISECONDS,
1981 >                          keepAliveTime, MILLISECONDS,
1982                            new ArrayBlockingQueue<Runnable>(10));
1983 <        final CountDownLatch threadStarted = new CountDownLatch(1);
1984 <        try {
1983 >        try (PoolCleaner cleaner = cleaner(p)) {
1984 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1985              p.allowCoreThreadTimeOut(false);
1986              p.execute(new CheckedRunnable() {
1987                  public void realRun() throws InterruptedException {
1988                      threadStarted.countDown();
1989                      assertTrue(p.getPoolSize() >= 1);
1990                  }});
1991 <            delay(2 * coreThreadTimeOut);
1991 >            delay(2 * keepAliveTime);
1992              assertTrue(p.getPoolSize() >= 1);
1993 <        } finally {
1994 <            joinPool(p);
1993 >        }
1994 >    }
1995 >
1996 >    /**
1997 >     * get(cancelled task) throws CancellationException
1998 >     * (in part, a test of CustomTPE itself)
1999 >     */
2000 >    public void testGet_cancelled() throws Exception {
2001 >        final ExecutorService e =
2002 >            new CustomTPE(1, 1,
2003 >                          LONG_DELAY_MS, MILLISECONDS,
2004 >                          new LinkedBlockingQueue<Runnable>());
2005 >        try (PoolCleaner cleaner = cleaner(e)) {
2006 >            final CountDownLatch blockerStarted = new CountDownLatch(1);
2007 >            final CountDownLatch done = new CountDownLatch(1);
2008 >            final List<Future<?>> futures = new ArrayList<>();
2009 >            for (int i = 0; i < 2; i++) {
2010 >                Runnable r = new CheckedRunnable() { public void realRun()
2011 >                                                         throws Throwable {
2012 >                    blockerStarted.countDown();
2013 >                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
2014 >                }};
2015 >                futures.add(e.submit(r));
2016 >            }
2017 >            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
2018 >            for (Future<?> future : futures) future.cancel(false);
2019 >            for (Future<?> future : futures) {
2020 >                try {
2021 >                    future.get();
2022 >                    shouldThrow();
2023 >                } catch (CancellationException success) {}
2024 >                try {
2025 >                    future.get(LONG_DELAY_MS, MILLISECONDS);
2026 >                    shouldThrow();
2027 >                } catch (CancellationException success) {}
2028 >                assertTrue(future.isCancelled());
2029 >                assertTrue(future.isDone());
2030 >            }
2031 >            done.countDown();
2032          }
2033      }
2034  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines