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.30 by jsr166, Sun May 29 07:01:17 2011 UTC vs.
Revision 1.83 by jsr166, Mon Oct 5 20:45:41 2015 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 import java.util.concurrent.*;
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 < import java.util.concurrent.locks.*;
10 > import static java.util.concurrent.TimeUnit.SECONDS;
11  
12 < import junit.framework.*;
13 < import java.util.*;
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;
21 > import java.util.concurrent.ExecutorService;
22 > import java.util.concurrent.Future;
23 > import java.util.concurrent.FutureTask;
24 > import java.util.concurrent.LinkedBlockingQueue;
25 > import java.util.concurrent.RejectedExecutionException;
26 > import java.util.concurrent.RejectedExecutionHandler;
27 > import java.util.concurrent.RunnableFuture;
28 > import java.util.concurrent.SynchronousQueue;
29 > import java.util.concurrent.ThreadFactory;
30 > import java.util.concurrent.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 >
37 > import junit.framework.Test;
38 > 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 37 | 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 77 | 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 90 | 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 101 | Line 129 | public class ThreadPoolExecutorSubclassT
129              long nanos = unit.toNanos(timeout);
130              lock.lock();
131              try {
132 <                for (;;) {
133 <                    if (done) break;
106 <                    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 203 | 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();
212 <            }};
213 <        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));
216 <        } finally {
217 <            joinPool(p);
242 >            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
243          }
244      }
245  
# Line 227 | 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 237 | 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(MEDIUM_DELAY_MS, MILLISECONDS));
266              assertEquals(1, p.getActiveCount());
242        } finally {
267              done.countDown();
244            joinPool(p);
268          }
269      }
270  
# Line 249 | 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 282 | 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 304 | Line 347 | public class ThreadPoolExecutorSubclassT
347                      fail("timed out");
348                  Thread.yield();
349              }
307        } finally {
308            joinPool(p);
350          }
351      }
352  
# Line 313 | 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 366 | 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 377 | 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 408 | 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 419 | Line 491 | public class ThreadPoolExecutorSubclassT
491                          done.await();
492                          assertEquals(THREADS, p.getLargestPoolSize());
493                      }});
494 <            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
423 <            assertEquals(THREADS, p.getLargestPoolSize());
424 <        } finally {
425 <            done.countDown();
426 <            joinPool(p);
494 >            assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
495              assertEquals(THREADS, p.getLargestPoolSize());
496 +            done.countDown();   // release pool
497          }
498 +        assertEquals(THREADS, p.getLargestPoolSize());
499      }
500  
501      /**
# Line 433 | 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 447 | 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 457 | 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(MEDIUM_DELAY_MS, MILLISECONDS));
539              assertEquals(1, p.getPoolSize());
540 <        } finally {
463 <            done.countDown();
464 <            joinPool(p);
540 >            done.countDown();   // release pool
541          }
542      }
543  
# Line 473 | Line 549 | public class ThreadPoolExecutorSubclassT
549              new CustomTPE(1, 1,
550                            LONG_DELAY_MS, MILLISECONDS,
551                            new ArrayBlockingQueue<Runnable>(10));
552 <        final CountDownLatch threadStarted = new CountDownLatch(1);
553 <        final CountDownLatch done = new CountDownLatch(1);
554 <        try {
552 >        try (PoolCleaner cleaner = cleaner(p)) {
553 >            final CountDownLatch threadStarted = new CountDownLatch(1);
554 >            final CountDownLatch done = new CountDownLatch(1);
555              assertEquals(0, p.getTaskCount());
556              p.execute(new CheckedRunnable() {
557                  public void realRun() throws InterruptedException {
# Line 483 | Line 559 | public class ThreadPoolExecutorSubclassT
559                      assertEquals(1, p.getTaskCount());
560                      done.await();
561                  }});
562 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
562 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
563              assertEquals(1, p.getTaskCount());
488        } finally {
564              done.countDown();
490            joinPool(p);
565          }
566      }
567  
# Line 495 | Line 569 | public class ThreadPoolExecutorSubclassT
569       * isShutdown is false before shutdown, true after
570       */
571      public void testIsShutdown() {
572 <
573 <        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
574 <        assertFalse(p.isShutdown());
575 <        try { p.shutdown(); } catch (SecurityException ok) { return; }
576 <        assertTrue(p.isShutdown());
577 <        joinPool(p);
572 >        final ThreadPoolExecutor p =
573 >            new CustomTPE(1, 1,
574 >                          LONG_DELAY_MS, MILLISECONDS,
575 >                          new ArrayBlockingQueue<Runnable>(10));
576 >        try (PoolCleaner cleaner = cleaner(p)) {
577 >            assertFalse(p.isShutdown());
578 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
579 >            assertTrue(p.isShutdown());
580 >        }
581      }
582  
583      /**
# Line 511 | Line 588 | public class ThreadPoolExecutorSubclassT
588              new CustomTPE(1, 1,
589                            LONG_DELAY_MS, MILLISECONDS,
590                            new ArrayBlockingQueue<Runnable>(10));
591 <        final CountDownLatch threadStarted = new CountDownLatch(1);
592 <        final CountDownLatch done = new CountDownLatch(1);
593 <        try {
591 >        try (PoolCleaner cleaner = cleaner(p)) {
592 >            final CountDownLatch threadStarted = new CountDownLatch(1);
593 >            final CountDownLatch done = new CountDownLatch(1);
594              assertFalse(p.isTerminating());
595              p.execute(new CheckedRunnable() {
596                  public void realRun() throws InterruptedException {
# Line 521 | Line 598 | public class ThreadPoolExecutorSubclassT
598                      threadStarted.countDown();
599                      done.await();
600                  }});
601 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
601 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
602              assertFalse(p.isTerminating());
603              done.countDown();
527        } finally {
604              try { p.shutdown(); } catch (SecurityException ok) { return; }
605 +            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
606 +            assertTrue(p.isTerminated());
607 +            assertFalse(p.isTerminating());
608          }
530        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
531        assertTrue(p.isTerminated());
532        assertFalse(p.isTerminating());
609      }
610  
611      /**
# Line 540 | Line 616 | public class ThreadPoolExecutorSubclassT
616              new CustomTPE(1, 1,
617                            LONG_DELAY_MS, MILLISECONDS,
618                            new ArrayBlockingQueue<Runnable>(10));
619 <        final CountDownLatch threadStarted = new CountDownLatch(1);
620 <        final CountDownLatch done = new CountDownLatch(1);
621 <        try {
619 >        try (PoolCleaner cleaner = cleaner(p)) {
620 >            final CountDownLatch threadStarted = new CountDownLatch(1);
621 >            final CountDownLatch done = new CountDownLatch(1);
622              assertFalse(p.isTerminating());
623              p.execute(new CheckedRunnable() {
624                  public void realRun() throws InterruptedException {
# Line 550 | Line 626 | public class ThreadPoolExecutorSubclassT
626                      threadStarted.countDown();
627                      done.await();
628                  }});
629 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
629 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
630              assertFalse(p.isTerminating());
631              done.countDown();
556        } finally {
632              try { p.shutdown(); } catch (SecurityException ok) { return; }
633 +            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
634 +            assertTrue(p.isTerminated());
635 +            assertFalse(p.isTerminating());
636          }
559        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
560        assertTrue(p.isTerminated());
561        assertFalse(p.isTerminating());
637      }
638  
639      /**
# Line 570 | Line 645 | public class ThreadPoolExecutorSubclassT
645              new CustomTPE(1, 1,
646                            LONG_DELAY_MS, MILLISECONDS,
647                            q);
648 <        final CountDownLatch threadStarted = new CountDownLatch(1);
649 <        final CountDownLatch done = new CountDownLatch(1);
650 <        try {
648 >        try (PoolCleaner cleaner = cleaner(p)) {
649 >            final CountDownLatch threadStarted = new CountDownLatch(1);
650 >            final CountDownLatch done = new CountDownLatch(1);
651              FutureTask[] tasks = new FutureTask[5];
652              for (int i = 0; i < tasks.length; i++) {
653                  Callable task = new CheckedCallable<Boolean>() {
# Line 585 | Line 660 | public class ThreadPoolExecutorSubclassT
660                  tasks[i] = new FutureTask(task);
661                  p.execute(tasks[i]);
662              }
663 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
663 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
664              assertSame(q, p.getQueue());
665              assertFalse(q.contains(tasks[0]));
666              assertTrue(q.contains(tasks[tasks.length - 1]));
667              assertEquals(tasks.length - 1, q.size());
593        } finally {
668              done.countDown();
595            joinPool(p);
669          }
670      }
671  
# Line 605 | Line 678 | public class ThreadPoolExecutorSubclassT
678              new CustomTPE(1, 1,
679                            LONG_DELAY_MS, MILLISECONDS,
680                            q);
681 <        Runnable[] tasks = new Runnable[6];
682 <        final CountDownLatch threadStarted = new CountDownLatch(1);
683 <        final CountDownLatch done = new CountDownLatch(1);
684 <        try {
681 >        try (PoolCleaner cleaner = cleaner(p)) {
682 >            Runnable[] tasks = new Runnable[6];
683 >            final CountDownLatch threadStarted = new CountDownLatch(1);
684 >            final CountDownLatch done = new CountDownLatch(1);
685              for (int i = 0; i < tasks.length; i++) {
686                  tasks[i] = new CheckedRunnable() {
687 <                        public void realRun() throws InterruptedException {
688 <                            threadStarted.countDown();
689 <                            done.await();
690 <                        }};
687 >                    public void realRun() throws InterruptedException {
688 >                        threadStarted.countDown();
689 >                        done.await();
690 >                    }};
691                  p.execute(tasks[i]);
692              }
693 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
693 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
694              assertFalse(p.remove(tasks[0]));
695              assertTrue(q.contains(tasks[4]));
696              assertTrue(q.contains(tasks[3]));
# Line 627 | Line 700 | public class ThreadPoolExecutorSubclassT
700              assertTrue(q.contains(tasks[3]));
701              assertTrue(p.remove(tasks[3]));
702              assertFalse(q.contains(tasks[3]));
630        } finally {
703              done.countDown();
632            joinPool(p);
704          }
705      }
706  
# Line 644 | Line 715 | public class ThreadPoolExecutorSubclassT
715              new CustomTPE(1, 1,
716                            LONG_DELAY_MS, MILLISECONDS,
717                            q);
718 <        FutureTask[] tasks = new FutureTask[5];
719 <        try {
718 >        try (PoolCleaner cleaner = cleaner(p)) {
719 >            FutureTask[] tasks = new FutureTask[5];
720              for (int i = 0; i < tasks.length; i++) {
721                  Callable task = new CheckedCallable<Boolean>() {
722                      public Boolean realCall() throws InterruptedException {
# Line 656 | Line 727 | public class ThreadPoolExecutorSubclassT
727                  tasks[i] = new FutureTask(task);
728                  p.execute(tasks[i]);
729              }
730 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
730 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
731              assertEquals(tasks.length, p.getTaskCount());
732              assertEquals(tasks.length - 1, q.size());
733              assertEquals(1L, p.getActiveCount());
# Line 669 | Line 740 | public class ThreadPoolExecutorSubclassT
740              p.purge();         // Nothing to do
741              assertEquals(tasks.length - 3, q.size());
742              assertEquals(tasks.length - 2, p.getTaskCount());
672        } finally {
743              done.countDown();
674            joinPool(p);
744          }
745      }
746  
747      /**
748 <     * shutdownNow returns a list containing tasks that were not run
748 >     * shutdownNow returns a list containing tasks that were not run,
749 >     * and those tasks are drained from the queue
750       */
751 <    public void testShutdownNow() {
752 <        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
753 <        List l;
754 <        try {
755 <            for (int i = 0; i < 5; i++)
756 <                p.execute(new MediumPossiblyInterruptedRunnable());
757 <        }
758 <        finally {
751 >    public void testShutdownNow() throws InterruptedException {
752 >        final int poolSize = 2;
753 >        final int count = 5;
754 >        final AtomicInteger ran = new AtomicInteger(0);
755 >        final ThreadPoolExecutor p =
756 >            new CustomTPE(poolSize, poolSize,
757 >                          LONG_DELAY_MS, MILLISECONDS,
758 >                          new ArrayBlockingQueue<Runnable>(10));
759 >        final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
760 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
761 >            threadsStarted.countDown();
762              try {
763 <                l = p.shutdownNow();
764 <            } catch (SecurityException ok) { return; }
763 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
764 >            } catch (InterruptedException success) {}
765 >            ran.getAndIncrement();
766 >        }};
767 >        for (int i = 0; i < count; i++)
768 >            p.execute(waiter);
769 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
770 >        assertEquals(poolSize, p.getActiveCount());
771 >        assertEquals(0, p.getCompletedTaskCount());
772 >        final List<Runnable> queuedTasks;
773 >        try {
774 >            queuedTasks = p.shutdownNow();
775 >        } catch (SecurityException ok) {
776 >            return; // Allowed in case test doesn't have privs
777          }
778          assertTrue(p.isShutdown());
779 <        assertTrue(l.size() <= 4);
779 >        assertTrue(p.getQueue().isEmpty());
780 >        assertEquals(count - poolSize, queuedTasks.size());
781 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
782 >        assertTrue(p.isTerminated());
783 >        assertEquals(poolSize, ran.get());
784 >        assertEquals(poolSize, p.getCompletedTaskCount());
785      }
786  
787      // Exception Tests
# Line 701 | Line 791 | public class ThreadPoolExecutorSubclassT
791       */
792      public void testConstructor1() {
793          try {
794 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
794 >            new CustomTPE(-1, 1, 1L, SECONDS,
795 >                          new ArrayBlockingQueue<Runnable>(10));
796              shouldThrow();
797          } catch (IllegalArgumentException success) {}
798      }
# Line 711 | Line 802 | public class ThreadPoolExecutorSubclassT
802       */
803      public void testConstructor2() {
804          try {
805 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
805 >            new CustomTPE(1, -1, 1L, SECONDS,
806 >                          new ArrayBlockingQueue<Runnable>(10));
807              shouldThrow();
808          } catch (IllegalArgumentException success) {}
809      }
# Line 721 | Line 813 | public class ThreadPoolExecutorSubclassT
813       */
814      public void testConstructor3() {
815          try {
816 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
816 >            new CustomTPE(1, 0, 1L, SECONDS,
817 >                          new ArrayBlockingQueue<Runnable>(10));
818              shouldThrow();
819          } catch (IllegalArgumentException success) {}
820      }
# Line 731 | Line 824 | public class ThreadPoolExecutorSubclassT
824       */
825      public void testConstructor4() {
826          try {
827 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
827 >            new CustomTPE(1, 2, -1L, SECONDS,
828 >                          new ArrayBlockingQueue<Runnable>(10));
829              shouldThrow();
830          } catch (IllegalArgumentException success) {}
831      }
# Line 741 | Line 835 | public class ThreadPoolExecutorSubclassT
835       */
836      public void testConstructor5() {
837          try {
838 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
838 >            new CustomTPE(2, 1, 1L, SECONDS,
839 >                          new ArrayBlockingQueue<Runnable>(10));
840              shouldThrow();
841          } catch (IllegalArgumentException success) {}
842      }
# Line 751 | Line 846 | public class ThreadPoolExecutorSubclassT
846       */
847      public void testConstructorNullPointerException() {
848          try {
849 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null);
849 >            new CustomTPE(1, 2, 1L, SECONDS, null);
850              shouldThrow();
851          } catch (NullPointerException success) {}
852      }
# Line 761 | Line 856 | public class ThreadPoolExecutorSubclassT
856       */
857      public void testConstructor6() {
858          try {
859 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
859 >            new CustomTPE(-1, 1, 1L, SECONDS,
860 >                          new ArrayBlockingQueue<Runnable>(10),
861 >                          new SimpleThreadFactory());
862              shouldThrow();
863          } catch (IllegalArgumentException success) {}
864      }
# Line 771 | Line 868 | public class ThreadPoolExecutorSubclassT
868       */
869      public void testConstructor7() {
870          try {
871 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
871 >            new CustomTPE(1,-1, 1L, SECONDS,
872 >                          new ArrayBlockingQueue<Runnable>(10),
873 >                          new SimpleThreadFactory());
874              shouldThrow();
875          } catch (IllegalArgumentException success) {}
876      }
# Line 781 | Line 880 | public class ThreadPoolExecutorSubclassT
880       */
881      public void testConstructor8() {
882          try {
883 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
883 >            new CustomTPE(1, 0, 1L, SECONDS,
884 >                          new ArrayBlockingQueue<Runnable>(10),
885 >                          new SimpleThreadFactory());
886              shouldThrow();
887          } catch (IllegalArgumentException success) {}
888      }
# Line 791 | Line 892 | public class ThreadPoolExecutorSubclassT
892       */
893      public void testConstructor9() {
894          try {
895 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
895 >            new CustomTPE(1, 2, -1L, SECONDS,
896 >                          new ArrayBlockingQueue<Runnable>(10),
897 >                          new SimpleThreadFactory());
898              shouldThrow();
899          } catch (IllegalArgumentException success) {}
900      }
# Line 801 | Line 904 | public class ThreadPoolExecutorSubclassT
904       */
905      public void testConstructor10() {
906          try {
907 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
907 >            new CustomTPE(2, 1, 1L, SECONDS,
908 >                          new ArrayBlockingQueue<Runnable>(10),
909 >                          new SimpleThreadFactory());
910              shouldThrow();
911          } catch (IllegalArgumentException success) {}
912      }
# Line 811 | Line 916 | public class ThreadPoolExecutorSubclassT
916       */
917      public void testConstructorNullPointerException2() {
918          try {
919 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
919 >            new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory());
920              shouldThrow();
921          } catch (NullPointerException success) {}
922      }
# Line 821 | Line 926 | public class ThreadPoolExecutorSubclassT
926       */
927      public void testConstructorNullPointerException3() {
928          try {
929 <            ThreadFactory f = null;
930 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
929 >            new CustomTPE(1, 2, 1L, SECONDS,
930 >                          new ArrayBlockingQueue<Runnable>(10),
931 >                          (ThreadFactory) null);
932              shouldThrow();
933          } catch (NullPointerException success) {}
934      }
# Line 832 | Line 938 | public class ThreadPoolExecutorSubclassT
938       */
939      public void testConstructor11() {
940          try {
941 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
941 >            new CustomTPE(-1, 1, 1L, SECONDS,
942 >                          new ArrayBlockingQueue<Runnable>(10),
943 >                          new NoOpREHandler());
944              shouldThrow();
945          } catch (IllegalArgumentException success) {}
946      }
# Line 842 | Line 950 | public class ThreadPoolExecutorSubclassT
950       */
951      public void testConstructor12() {
952          try {
953 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
953 >            new CustomTPE(1, -1, 1L, SECONDS,
954 >                          new ArrayBlockingQueue<Runnable>(10),
955 >                          new NoOpREHandler());
956              shouldThrow();
957          } catch (IllegalArgumentException success) {}
958      }
# Line 852 | Line 962 | public class ThreadPoolExecutorSubclassT
962       */
963      public void testConstructor13() {
964          try {
965 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
965 >            new CustomTPE(1, 0, 1L, SECONDS,
966 >                          new ArrayBlockingQueue<Runnable>(10),
967 >                          new NoOpREHandler());
968              shouldThrow();
969          } catch (IllegalArgumentException success) {}
970      }
# Line 862 | Line 974 | public class ThreadPoolExecutorSubclassT
974       */
975      public void testConstructor14() {
976          try {
977 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
977 >            new CustomTPE(1, 2, -1L, SECONDS,
978 >                          new ArrayBlockingQueue<Runnable>(10),
979 >                          new NoOpREHandler());
980              shouldThrow();
981          } catch (IllegalArgumentException success) {}
982      }
# Line 872 | Line 986 | public class ThreadPoolExecutorSubclassT
986       */
987      public void testConstructor15() {
988          try {
989 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
989 >            new CustomTPE(2, 1, 1L, SECONDS,
990 >                          new ArrayBlockingQueue<Runnable>(10),
991 >                          new NoOpREHandler());
992              shouldThrow();
993          } catch (IllegalArgumentException success) {}
994      }
# Line 882 | Line 998 | public class ThreadPoolExecutorSubclassT
998       */
999      public void testConstructorNullPointerException4() {
1000          try {
1001 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
1001 >            new CustomTPE(1, 2, 1L, SECONDS,
1002 >                          null,
1003 >                          new NoOpREHandler());
1004              shouldThrow();
1005          } catch (NullPointerException success) {}
1006      }
# Line 892 | Line 1010 | public class ThreadPoolExecutorSubclassT
1010       */
1011      public void testConstructorNullPointerException5() {
1012          try {
1013 <            RejectedExecutionHandler r = null;
1014 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
1013 >            new CustomTPE(1, 2, 1L, SECONDS,
1014 >                          new ArrayBlockingQueue<Runnable>(10),
1015 >                          (RejectedExecutionHandler) null);
1016              shouldThrow();
1017          } catch (NullPointerException success) {}
1018      }
# Line 903 | Line 1022 | public class ThreadPoolExecutorSubclassT
1022       */
1023      public void testConstructor16() {
1024          try {
1025 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1025 >            new CustomTPE(-1, 1, 1L, SECONDS,
1026 >                          new ArrayBlockingQueue<Runnable>(10),
1027 >                          new SimpleThreadFactory(),
1028 >                          new NoOpREHandler());
1029              shouldThrow();
1030          } catch (IllegalArgumentException success) {}
1031      }
# Line 913 | Line 1035 | public class ThreadPoolExecutorSubclassT
1035       */
1036      public void testConstructor17() {
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 923 | Line 1048 | public class ThreadPoolExecutorSubclassT
1048       */
1049      public void testConstructor18() {
1050          try {
1051 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1051 >            new CustomTPE(1, 0, 1L, SECONDS,
1052 >                          new ArrayBlockingQueue<Runnable>(10),
1053 >                          new SimpleThreadFactory(),
1054 >                          new NoOpREHandler());
1055              shouldThrow();
1056          } catch (IllegalArgumentException success) {}
1057      }
# Line 933 | Line 1061 | public class ThreadPoolExecutorSubclassT
1061       */
1062      public void testConstructor19() {
1063          try {
1064 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1064 >            new CustomTPE(1, 2, -1L, SECONDS,
1065 >                          new ArrayBlockingQueue<Runnable>(10),
1066 >                          new SimpleThreadFactory(),
1067 >                          new NoOpREHandler());
1068              shouldThrow();
1069          } catch (IllegalArgumentException success) {}
1070      }
# Line 943 | Line 1074 | public class ThreadPoolExecutorSubclassT
1074       */
1075      public void testConstructor20() {
1076          try {
1077 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1077 >            new CustomTPE(2, 1, 1L, SECONDS,
1078 >                          new ArrayBlockingQueue<Runnable>(10),
1079 >                          new SimpleThreadFactory(),
1080 >                          new NoOpREHandler());
1081              shouldThrow();
1082          } catch (IllegalArgumentException success) {}
1083      }
# Line 953 | Line 1087 | public class ThreadPoolExecutorSubclassT
1087       */
1088      public void testConstructorNullPointerException6() {
1089          try {
1090 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
1090 >            new CustomTPE(1, 2, 1L, SECONDS,
1091 >                          null,
1092 >                          new SimpleThreadFactory(),
1093 >                          new NoOpREHandler());
1094              shouldThrow();
1095          } catch (NullPointerException success) {}
1096      }
# Line 963 | Line 1100 | public class ThreadPoolExecutorSubclassT
1100       */
1101      public void testConstructorNullPointerException7() {
1102          try {
1103 <            RejectedExecutionHandler r = null;
1104 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
1103 >            new CustomTPE(1, 2, 1L, SECONDS,
1104 >                          new ArrayBlockingQueue<Runnable>(10),
1105 >                          new SimpleThreadFactory(),
1106 >                          (RejectedExecutionHandler) null);
1107              shouldThrow();
1108          } catch (NullPointerException success) {}
1109      }
# Line 974 | Line 1113 | public class ThreadPoolExecutorSubclassT
1113       */
1114      public void testConstructorNullPointerException8() {
1115          try {
1116 <            new CustomTPE(1, 2,
978 <                          LONG_DELAY_MS, MILLISECONDS,
1116 >            new CustomTPE(1, 2, 1L, SECONDS,
1117                            new ArrayBlockingQueue<Runnable>(10),
1118                            (ThreadFactory) null,
1119                            new NoOpREHandler());
# Line 987 | Line 1125 | public class ThreadPoolExecutorSubclassT
1125       * execute throws RejectedExecutionException if saturated.
1126       */
1127      public void testSaturatedExecute() {
1128 <        ThreadPoolExecutor p =
1128 >        final ThreadPoolExecutor p =
1129              new CustomTPE(1, 1,
1130                            LONG_DELAY_MS, MILLISECONDS,
1131                            new ArrayBlockingQueue<Runnable>(1));
1132 <        final CountDownLatch done = new CountDownLatch(1);
1133 <        try {
1132 >        try (PoolCleaner cleaner = cleaner(p)) {
1133 >            final CountDownLatch done = new CountDownLatch(1);
1134              Runnable task = new CheckedRunnable() {
1135                  public void realRun() throws InterruptedException {
1136                      done.await();
# Line 1006 | Line 1144 | public class ThreadPoolExecutorSubclassT
1144                  } catch (RejectedExecutionException success) {}
1145                  assertTrue(p.getTaskCount() <= 2);
1146              }
1009        } finally {
1147              done.countDown();
1011            joinPool(p);
1148          }
1149      }
1150  
# Line 1016 | Line 1152 | public class ThreadPoolExecutorSubclassT
1152       * executor using CallerRunsPolicy runs task if saturated.
1153       */
1154      public void testSaturatedExecute2() {
1155 <        RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1156 <        ThreadPoolExecutor p = new CustomTPE(1, 1,
1157 <                                             LONG_DELAY_MS, MILLISECONDS,
1158 <                                             new ArrayBlockingQueue<Runnable>(1),
1159 <                                             h);
1160 <        try {
1155 >        final ThreadPoolExecutor p =
1156 >            new CustomTPE(1, 1,
1157 >                          LONG_DELAY_MS, MILLISECONDS,
1158 >                          new ArrayBlockingQueue<Runnable>(1),
1159 >                          new CustomTPE.CallerRunsPolicy());
1160 >        try (PoolCleaner cleaner = cleaner(p)) {
1161 >            final CountDownLatch done = new CountDownLatch(1);
1162 >            Runnable blocker = new CheckedRunnable() {
1163 >                public void realRun() throws InterruptedException {
1164 >                    done.await();
1165 >                }};
1166 >            p.execute(blocker);
1167              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1168 <            for (int i = 0; i < tasks.length; ++i)
1168 >            for (int i = 0; i < tasks.length; i++)
1169                  tasks[i] = new TrackedNoOpRunnable();
1170 <            TrackedLongRunnable mr = new TrackedLongRunnable();
1029 <            p.execute(mr);
1030 <            for (int i = 0; i < tasks.length; ++i)
1170 >            for (int i = 0; i < tasks.length; i++)
1171                  p.execute(tasks[i]);
1172 <            for (int i = 1; i < tasks.length; ++i)
1172 >            for (int i = 1; i < tasks.length; i++)
1173                  assertTrue(tasks[i].done);
1174 <            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1175 <        } finally {
1036 <            joinPool(p);
1174 >            assertFalse(tasks[0].done); // waiting in queue
1175 >            done.countDown();
1176          }
1177      }
1178  
# Line 1041 | Line 1180 | public class ThreadPoolExecutorSubclassT
1180       * executor using DiscardPolicy drops task if saturated.
1181       */
1182      public void testSaturatedExecute3() {
1183 <        RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1184 <        ThreadPoolExecutor p =
1183 >        final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1184 >        for (int i = 0; i < tasks.length; ++i)
1185 >            tasks[i] = new TrackedNoOpRunnable();
1186 >        final ThreadPoolExecutor p =
1187              new CustomTPE(1, 1,
1188                            LONG_DELAY_MS, MILLISECONDS,
1189                            new ArrayBlockingQueue<Runnable>(1),
1190 <                          h);
1191 <        try {
1192 <            TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1193 <            for (int i = 0; i < tasks.length; ++i)
1194 <                tasks[i] = new TrackedNoOpRunnable();
1054 <            p.execute(new TrackedLongRunnable());
1190 >                          new CustomTPE.DiscardPolicy());
1191 >        try (PoolCleaner cleaner = cleaner(p)) {
1192 >            final CountDownLatch done = new CountDownLatch(1);
1193 >            p.execute(awaiter(done));
1194 >
1195              for (TrackedNoOpRunnable task : tasks)
1196                  p.execute(task);
1197 <            for (TrackedNoOpRunnable task : tasks)
1198 <                assertFalse(task.done);
1199 <            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1060 <        } finally {
1061 <            joinPool(p);
1197 >            for (int i = 1; i < tasks.length; i++)
1198 >                assertFalse(tasks[i].done);
1199 >            done.countDown();
1200          }
1201 +        for (int i = 1; i < tasks.length; i++)
1202 +            assertFalse(tasks[i].done);
1203 +        assertTrue(tasks[0].done); // was waiting in queue
1204      }
1205  
1206      /**
1207       * executor using DiscardOldestPolicy drops oldest task if saturated.
1208       */
1209      public void testSaturatedExecute4() {
1210 <        RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
1211 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1212 <        try {
1213 <            p.execute(new TrackedLongRunnable());
1214 <            TrackedLongRunnable r2 = new TrackedLongRunnable();
1210 >        final CountDownLatch done = new CountDownLatch(1);
1211 >        LatchAwaiter r1 = awaiter(done);
1212 >        LatchAwaiter r2 = awaiter(done);
1213 >        LatchAwaiter r3 = awaiter(done);
1214 >        final ThreadPoolExecutor p =
1215 >            new CustomTPE(1, 1,
1216 >                          LONG_DELAY_MS, MILLISECONDS,
1217 >                          new ArrayBlockingQueue<Runnable>(1),
1218 >                          new CustomTPE.DiscardOldestPolicy());
1219 >        try (PoolCleaner cleaner = cleaner(p)) {
1220 >            assertEquals(LatchAwaiter.NEW, r1.state);
1221 >            assertEquals(LatchAwaiter.NEW, r2.state);
1222 >            assertEquals(LatchAwaiter.NEW, r3.state);
1223 >            p.execute(r1);
1224              p.execute(r2);
1225              assertTrue(p.getQueue().contains(r2));
1076            TrackedNoOpRunnable r3 = new TrackedNoOpRunnable();
1226              p.execute(r3);
1227              assertFalse(p.getQueue().contains(r2));
1228              assertTrue(p.getQueue().contains(r3));
1229 <            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1081 <        } finally {
1082 <            joinPool(p);
1229 >            done.countDown();
1230          }
1231 +        assertEquals(LatchAwaiter.DONE, r1.state);
1232 +        assertEquals(LatchAwaiter.NEW, r2.state);
1233 +        assertEquals(LatchAwaiter.DONE, r3.state);
1234      }
1235  
1236      /**
1237       * execute throws RejectedExecutionException if shutdown
1238       */
1239      public void testRejectedExecutionExceptionOnShutdown() {
1240 <        ThreadPoolExecutor p =
1241 <            new CustomTPE(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
1240 >        final ThreadPoolExecutor p =
1241 >            new CustomTPE(1, 1,
1242 >                          LONG_DELAY_MS, MILLISECONDS,
1243 >                          new ArrayBlockingQueue<Runnable>(1));
1244          try { p.shutdown(); } catch (SecurityException ok) { return; }
1245 <        try {
1246 <            p.execute(new NoOpRunnable());
1247 <            shouldThrow();
1248 <        } catch (RejectedExecutionException success) {}
1249 <
1250 <        joinPool(p);
1245 >        try (PoolCleaner cleaner = cleaner(p)) {
1246 >            try {
1247 >                p.execute(new NoOpRunnable());
1248 >                shouldThrow();
1249 >            } catch (RejectedExecutionException success) {}
1250 >        }
1251      }
1252  
1253      /**
1254       * execute using CallerRunsPolicy drops task on shutdown
1255       */
1256      public void testCallerRunsOnShutdown() {
1257 <        RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1258 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1259 <
1257 >        final ThreadPoolExecutor p =
1258 >            new CustomTPE(1, 1,
1259 >                          LONG_DELAY_MS, MILLISECONDS,
1260 >                          new ArrayBlockingQueue<Runnable>(1),
1261 >                          new CustomTPE.CallerRunsPolicy());
1262          try { p.shutdown(); } catch (SecurityException ok) { return; }
1263 <        try {
1263 >        try (PoolCleaner cleaner = cleaner(p)) {
1264              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1265              p.execute(r);
1266              assertFalse(r.done);
1113        } finally {
1114            joinPool(p);
1267          }
1268      }
1269  
# Line 1119 | Line 1271 | public class ThreadPoolExecutorSubclassT
1271       * execute using DiscardPolicy drops task on shutdown
1272       */
1273      public void testDiscardOnShutdown() {
1274 <        RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1275 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1276 <
1274 >        final ThreadPoolExecutor p =
1275 >            new CustomTPE(1, 1,
1276 >                          LONG_DELAY_MS, MILLISECONDS,
1277 >                          new ArrayBlockingQueue<Runnable>(1),
1278 >                          new CustomTPE.DiscardPolicy());
1279          try { p.shutdown(); } catch (SecurityException ok) { return; }
1280 <        try {
1280 >        try (PoolCleaner cleaner = cleaner(p)) {
1281              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1282              p.execute(r);
1283              assertFalse(r.done);
1130        } finally {
1131            joinPool(p);
1284          }
1285      }
1286  
# Line 1136 | Line 1288 | public class ThreadPoolExecutorSubclassT
1288       * execute using DiscardOldestPolicy drops task on shutdown
1289       */
1290      public void testDiscardOldestOnShutdown() {
1291 <        RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
1292 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1291 >        final ThreadPoolExecutor p =
1292 >            new CustomTPE(1, 1,
1293 >                          LONG_DELAY_MS, MILLISECONDS,
1294 >                          new ArrayBlockingQueue<Runnable>(1),
1295 >                          new CustomTPE.DiscardOldestPolicy());
1296  
1297          try { p.shutdown(); } catch (SecurityException ok) { return; }
1298 <        try {
1298 >        try (PoolCleaner cleaner = cleaner(p)) {
1299              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1300              p.execute(r);
1301              assertFalse(r.done);
1147        } finally {
1148            joinPool(p);
1302          }
1303      }
1304  
# Line 1153 | Line 1306 | public class ThreadPoolExecutorSubclassT
1306       * execute(null) throws NPE
1307       */
1308      public void testExecuteNull() {
1309 <        ThreadPoolExecutor p = null;
1310 <        try {
1311 <            p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1312 <            p.execute(null);
1313 <            shouldThrow();
1314 <        } catch (NullPointerException success) {}
1315 <
1316 <        joinPool(p);
1309 >        final ThreadPoolExecutor p =
1310 >            new CustomTPE(1, 2,
1311 >                          1L, SECONDS,
1312 >                          new ArrayBlockingQueue<Runnable>(10));
1313 >        try (PoolCleaner cleaner = cleaner(p)) {
1314 >            try {
1315 >                p.execute(null);
1316 >                shouldThrow();
1317 >            } catch (NullPointerException success) {}
1318 >        }
1319      }
1320  
1321      /**
1322       * setCorePoolSize of negative value throws IllegalArgumentException
1323       */
1324      public void testCorePoolSizeIllegalArgumentException() {
1325 <        ThreadPoolExecutor p =
1326 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1327 <        try {
1328 <            p.setCorePoolSize(-1);
1329 <            shouldThrow();
1330 <        } catch (IllegalArgumentException success) {
1331 <        } finally {
1332 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1325 >        final ThreadPoolExecutor p =
1326 >            new CustomTPE(1, 2,
1327 >                          LONG_DELAY_MS, MILLISECONDS,
1328 >                          new ArrayBlockingQueue<Runnable>(10));
1329 >        try (PoolCleaner cleaner = cleaner(p)) {
1330 >            try {
1331 >                p.setCorePoolSize(-1);
1332 >                shouldThrow();
1333 >            } catch (IllegalArgumentException success) {}
1334          }
1179        joinPool(p);
1335      }
1336  
1337      /**
# Line 1184 | Line 1339 | public class ThreadPoolExecutorSubclassT
1339       * if given a value less the core pool size
1340       */
1341      public void testMaximumPoolSizeIllegalArgumentException() {
1342 <        ThreadPoolExecutor p =
1343 <            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1344 <        try {
1345 <            p.setMaximumPoolSize(1);
1346 <            shouldThrow();
1347 <        } catch (IllegalArgumentException success) {
1348 <        } finally {
1349 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1342 >        final ThreadPoolExecutor p =
1343 >            new CustomTPE(2, 3,
1344 >                          LONG_DELAY_MS, MILLISECONDS,
1345 >                          new ArrayBlockingQueue<Runnable>(10));
1346 >        try (PoolCleaner cleaner = cleaner(p)) {
1347 >            try {
1348 >                p.setMaximumPoolSize(1);
1349 >                shouldThrow();
1350 >            } catch (IllegalArgumentException success) {}
1351          }
1196        joinPool(p);
1352      }
1353  
1354      /**
# Line 1201 | Line 1356 | public class ThreadPoolExecutorSubclassT
1356       * if given a negative value
1357       */
1358      public void testMaximumPoolSizeIllegalArgumentException2() {
1359 <        ThreadPoolExecutor p =
1360 <            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1361 <        try {
1362 <            p.setMaximumPoolSize(-1);
1363 <            shouldThrow();
1364 <        } catch (IllegalArgumentException success) {
1365 <        } finally {
1366 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1359 >        final ThreadPoolExecutor p =
1360 >            new CustomTPE(2, 3,
1361 >                          LONG_DELAY_MS,
1362 >                          MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1363 >        try (PoolCleaner cleaner = cleaner(p)) {
1364 >            try {
1365 >                p.setMaximumPoolSize(-1);
1366 >                shouldThrow();
1367 >            } catch (IllegalArgumentException success) {}
1368          }
1213        joinPool(p);
1369      }
1370  
1371      /**
# Line 1218 | Line 1373 | public class ThreadPoolExecutorSubclassT
1373       * when given a negative value
1374       */
1375      public void testKeepAliveTimeIllegalArgumentException() {
1376 <        ThreadPoolExecutor p =
1377 <            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1378 <
1379 <        try {
1380 <            p.setKeepAliveTime(-1,MILLISECONDS);
1381 <            shouldThrow();
1382 <        } catch (IllegalArgumentException success) {
1383 <        } finally {
1384 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1376 >        final ThreadPoolExecutor p =
1377 >            new CustomTPE(2, 3,
1378 >                          LONG_DELAY_MS, MILLISECONDS,
1379 >                          new ArrayBlockingQueue<Runnable>(10));
1380 >        try (PoolCleaner cleaner = cleaner(p)) {
1381 >            try {
1382 >                p.setKeepAliveTime(-1, MILLISECONDS);
1383 >                shouldThrow();
1384 >            } catch (IllegalArgumentException success) {}
1385          }
1231        joinPool(p);
1386      }
1387  
1388      /**
# Line 1236 | Line 1390 | public class ThreadPoolExecutorSubclassT
1390       */
1391      public void testTerminated() {
1392          CustomTPE p = new CustomTPE();
1393 <        try { p.shutdown(); } catch (SecurityException ok) { return; }
1394 <        assertTrue(p.terminatedCalled());
1395 <        joinPool(p);
1393 >        try (PoolCleaner cleaner = cleaner(p)) {
1394 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1395 >            assertTrue(p.terminatedCalled());
1396 >            assertTrue(p.isShutdown());
1397 >        }
1398      }
1399  
1400      /**
# Line 1246 | Line 1402 | public class ThreadPoolExecutorSubclassT
1402       */
1403      public void testBeforeAfter() throws InterruptedException {
1404          CustomTPE p = new CustomTPE();
1405 <        try {
1405 >        try (PoolCleaner cleaner = cleaner(p)) {
1406              final CountDownLatch done = new CountDownLatch(1);
1407 <            final CheckedRunnable task = new CheckedRunnable() {
1407 >            p.execute(new CheckedRunnable() {
1408                  public void realRun() {
1409                      done.countDown();
1410 <                }};
1255 <            p.execute(task);
1410 >                }});
1411              await(p.afterCalled);
1412              assertEquals(0, done.getCount());
1413              assertTrue(p.afterCalled());
1414              assertTrue(p.beforeCalled());
1260            try { p.shutdown(); } catch (SecurityException ok) { return; }
1261        } finally {
1262            joinPool(p);
1415          }
1416      }
1417  
# Line 1267 | Line 1419 | public class ThreadPoolExecutorSubclassT
1419       * completed submit of callable returns result
1420       */
1421      public void testSubmitCallable() throws Exception {
1422 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1423 <        try {
1422 >        final ExecutorService e =
1423 >            new CustomTPE(2, 2,
1424 >                          LONG_DELAY_MS, MILLISECONDS,
1425 >                          new ArrayBlockingQueue<Runnable>(10));
1426 >        try (PoolCleaner cleaner = cleaner(e)) {
1427              Future<String> future = e.submit(new StringTask());
1428              String result = future.get();
1429              assertSame(TEST_STRING, result);
1275        } finally {
1276            joinPool(e);
1430          }
1431      }
1432  
# Line 1281 | Line 1434 | public class ThreadPoolExecutorSubclassT
1434       * completed submit of runnable returns successfully
1435       */
1436      public void testSubmitRunnable() throws Exception {
1437 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1438 <        try {
1437 >        final ExecutorService e =
1438 >            new CustomTPE(2, 2,
1439 >                          LONG_DELAY_MS, MILLISECONDS,
1440 >                          new ArrayBlockingQueue<Runnable>(10));
1441 >        try (PoolCleaner cleaner = cleaner(e)) {
1442              Future<?> future = e.submit(new NoOpRunnable());
1443              future.get();
1444              assertTrue(future.isDone());
1289        } finally {
1290            joinPool(e);
1445          }
1446      }
1447  
# Line 1295 | Line 1449 | public class ThreadPoolExecutorSubclassT
1449       * completed submit of (runnable, result) returns result
1450       */
1451      public void testSubmitRunnable2() throws Exception {
1452 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1453 <        try {
1452 >        final ExecutorService e =
1453 >            new CustomTPE(2, 2,
1454 >                          LONG_DELAY_MS, MILLISECONDS,
1455 >                          new ArrayBlockingQueue<Runnable>(10));
1456 >        try (PoolCleaner cleaner = cleaner(e)) {
1457              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1458              String result = future.get();
1459              assertSame(TEST_STRING, result);
1303        } finally {
1304            joinPool(e);
1460          }
1461      }
1462  
# Line 1309 | Line 1464 | public class ThreadPoolExecutorSubclassT
1464       * invokeAny(null) throws NPE
1465       */
1466      public void testInvokeAny1() throws Exception {
1467 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1468 <        try {
1469 <            e.invokeAny(null);
1470 <            shouldThrow();
1471 <        } catch (NullPointerException success) {
1472 <        } finally {
1473 <            joinPool(e);
1467 >        final ExecutorService e =
1468 >            new CustomTPE(2, 2,
1469 >                          LONG_DELAY_MS, MILLISECONDS,
1470 >                          new ArrayBlockingQueue<Runnable>(10));
1471 >        try (PoolCleaner cleaner = cleaner(e)) {
1472 >            try {
1473 >                e.invokeAny(null);
1474 >                shouldThrow();
1475 >            } catch (NullPointerException success) {}
1476          }
1477      }
1478  
# Line 1323 | Line 1480 | public class ThreadPoolExecutorSubclassT
1480       * invokeAny(empty collection) throws IAE
1481       */
1482      public void testInvokeAny2() throws Exception {
1483 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1484 <        try {
1485 <            e.invokeAny(new ArrayList<Callable<String>>());
1486 <            shouldThrow();
1487 <        } catch (IllegalArgumentException success) {
1488 <        } finally {
1489 <            joinPool(e);
1483 >        final ExecutorService e =
1484 >            new CustomTPE(2, 2,
1485 >                          LONG_DELAY_MS, MILLISECONDS,
1486 >                          new ArrayBlockingQueue<Runnable>(10));
1487 >        try (PoolCleaner cleaner = cleaner(e)) {
1488 >            try {
1489 >                e.invokeAny(new ArrayList<Callable<String>>());
1490 >                shouldThrow();
1491 >            } catch (IllegalArgumentException success) {}
1492          }
1493      }
1494  
# Line 1338 | Line 1497 | public class ThreadPoolExecutorSubclassT
1497       */
1498      public void testInvokeAny3() throws Exception {
1499          CountDownLatch latch = new CountDownLatch(1);
1500 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1501 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1502 <        l.add(latchAwaitingStringTask(latch));
1503 <        l.add(null);
1504 <        try {
1505 <            e.invokeAny(l);
1506 <            shouldThrow();
1507 <        } catch (NullPointerException success) {
1508 <        } finally {
1500 >        final ExecutorService e =
1501 >            new CustomTPE(2, 2,
1502 >                          LONG_DELAY_MS, MILLISECONDS,
1503 >                          new ArrayBlockingQueue<Runnable>(10));
1504 >        try (PoolCleaner cleaner = cleaner(e)) {
1505 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1506 >            l.add(latchAwaitingStringTask(latch));
1507 >            l.add(null);
1508 >            try {
1509 >                e.invokeAny(l);
1510 >                shouldThrow();
1511 >            } catch (NullPointerException success) {}
1512              latch.countDown();
1351            joinPool(e);
1513          }
1514      }
1515  
# Line 1356 | Line 1517 | public class ThreadPoolExecutorSubclassT
1517       * invokeAny(c) throws ExecutionException if no task completes
1518       */
1519      public void testInvokeAny4() throws Exception {
1520 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1521 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1522 <        l.add(new NPETask());
1523 <        try {
1524 <            e.invokeAny(l);
1525 <            shouldThrow();
1526 <        } catch (ExecutionException success) {
1527 <            assertTrue(success.getCause() instanceof NullPointerException);
1528 <        } finally {
1529 <            joinPool(e);
1520 >        final ExecutorService e =
1521 >            new CustomTPE(2, 2,
1522 >                          LONG_DELAY_MS, MILLISECONDS,
1523 >                          new ArrayBlockingQueue<Runnable>(10));
1524 >        try (PoolCleaner cleaner = cleaner(e)) {
1525 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1526 >            l.add(new NPETask());
1527 >            try {
1528 >                e.invokeAny(l);
1529 >                shouldThrow();
1530 >            } catch (ExecutionException success) {
1531 >                assertTrue(success.getCause() instanceof NullPointerException);
1532 >            }
1533          }
1534      }
1535  
# Line 1373 | Line 1537 | public class ThreadPoolExecutorSubclassT
1537       * invokeAny(c) returns result of some task
1538       */
1539      public void testInvokeAny5() throws Exception {
1540 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1541 <        try {
1540 >        final ExecutorService e =
1541 >            new CustomTPE(2, 2,
1542 >                          LONG_DELAY_MS, MILLISECONDS,
1543 >                          new ArrayBlockingQueue<Runnable>(10));
1544 >        try (PoolCleaner cleaner = cleaner(e)) {
1545              List<Callable<String>> l = new ArrayList<Callable<String>>();
1546              l.add(new StringTask());
1547              l.add(new StringTask());
1548              String result = e.invokeAny(l);
1549              assertSame(TEST_STRING, result);
1383        } finally {
1384            joinPool(e);
1550          }
1551      }
1552  
# Line 1389 | Line 1554 | public class ThreadPoolExecutorSubclassT
1554       * invokeAll(null) throws NPE
1555       */
1556      public void testInvokeAll1() throws Exception {
1557 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1558 <        try {
1559 <            e.invokeAll(null);
1560 <            shouldThrow();
1561 <        } catch (NullPointerException success) {
1562 <        } finally {
1563 <            joinPool(e);
1557 >        final ExecutorService e =
1558 >            new CustomTPE(2, 2,
1559 >                          LONG_DELAY_MS, MILLISECONDS,
1560 >                          new ArrayBlockingQueue<Runnable>(10));
1561 >        try (PoolCleaner cleaner = cleaner(e)) {
1562 >            try {
1563 >                e.invokeAll(null);
1564 >                shouldThrow();
1565 >            } catch (NullPointerException success) {}
1566          }
1567      }
1568  
# Line 1403 | Line 1570 | public class ThreadPoolExecutorSubclassT
1570       * invokeAll(empty collection) returns empty collection
1571       */
1572      public void testInvokeAll2() throws Exception {
1573 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1574 <        try {
1573 >        final ExecutorService e =
1574 >            new CustomTPE(2, 2,
1575 >                          LONG_DELAY_MS, MILLISECONDS,
1576 >                          new ArrayBlockingQueue<Runnable>(10));
1577 >        try (PoolCleaner cleaner = cleaner(e)) {
1578              List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1579              assertTrue(r.isEmpty());
1410        } finally {
1411            joinPool(e);
1580          }
1581      }
1582  
# Line 1416 | Line 1584 | public class ThreadPoolExecutorSubclassT
1584       * invokeAll(c) throws NPE if c has null elements
1585       */
1586      public void testInvokeAll3() throws Exception {
1587 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1588 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1589 <        l.add(new StringTask());
1590 <        l.add(null);
1591 <        try {
1592 <            e.invokeAll(l);
1593 <            shouldThrow();
1594 <        } catch (NullPointerException success) {
1595 <        } finally {
1596 <            joinPool(e);
1587 >        final ExecutorService e =
1588 >            new CustomTPE(2, 2,
1589 >                          LONG_DELAY_MS, MILLISECONDS,
1590 >                          new ArrayBlockingQueue<Runnable>(10));
1591 >        try (PoolCleaner cleaner = cleaner(e)) {
1592 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1593 >            l.add(new StringTask());
1594 >            l.add(null);
1595 >            try {
1596 >                e.invokeAll(l);
1597 >                shouldThrow();
1598 >            } catch (NullPointerException success) {}
1599          }
1600      }
1601  
# Line 1433 | Line 1603 | public class ThreadPoolExecutorSubclassT
1603       * get of element of invokeAll(c) throws exception on failed task
1604       */
1605      public void testInvokeAll4() throws Exception {
1606 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1607 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1608 <        l.add(new NPETask());
1609 <        List<Future<String>> futures = e.invokeAll(l);
1610 <        assertEquals(1, futures.size());
1611 <        try {
1612 <            futures.get(0).get();
1613 <            shouldThrow();
1614 <        } catch (ExecutionException success) {
1615 <            assertTrue(success.getCause() instanceof NullPointerException);
1616 <        } finally {
1617 <            joinPool(e);
1606 >        final ExecutorService e =
1607 >            new CustomTPE(2, 2,
1608 >                          LONG_DELAY_MS, MILLISECONDS,
1609 >                          new ArrayBlockingQueue<Runnable>(10));
1610 >        try (PoolCleaner cleaner = cleaner(e)) {
1611 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1612 >            l.add(new NPETask());
1613 >            List<Future<String>> futures = e.invokeAll(l);
1614 >            assertEquals(1, futures.size());
1615 >            try {
1616 >                futures.get(0).get();
1617 >                shouldThrow();
1618 >            } catch (ExecutionException success) {
1619 >                assertTrue(success.getCause() instanceof NullPointerException);
1620 >            }
1621          }
1622      }
1623  
# Line 1452 | Line 1625 | public class ThreadPoolExecutorSubclassT
1625       * invokeAll(c) returns results of all completed tasks
1626       */
1627      public void testInvokeAll5() throws Exception {
1628 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1629 <        try {
1628 >        final ExecutorService e =
1629 >            new CustomTPE(2, 2,
1630 >                          LONG_DELAY_MS, MILLISECONDS,
1631 >                          new ArrayBlockingQueue<Runnable>(10));
1632 >        try (PoolCleaner cleaner = cleaner(e)) {
1633              List<Callable<String>> l = new ArrayList<Callable<String>>();
1634              l.add(new StringTask());
1635              l.add(new StringTask());
# Line 1461 | Line 1637 | public class ThreadPoolExecutorSubclassT
1637              assertEquals(2, futures.size());
1638              for (Future<String> future : futures)
1639                  assertSame(TEST_STRING, future.get());
1464        } finally {
1465            joinPool(e);
1640          }
1641      }
1642  
# Line 1470 | Line 1644 | public class ThreadPoolExecutorSubclassT
1644       * timed invokeAny(null) throws NPE
1645       */
1646      public void testTimedInvokeAny1() throws Exception {
1647 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1648 <        try {
1649 <            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1650 <            shouldThrow();
1651 <        } catch (NullPointerException success) {
1652 <        } finally {
1653 <            joinPool(e);
1647 >        final ExecutorService e =
1648 >            new CustomTPE(2, 2,
1649 >                          LONG_DELAY_MS, MILLISECONDS,
1650 >                          new ArrayBlockingQueue<Runnable>(10));
1651 >        try (PoolCleaner cleaner = cleaner(e)) {
1652 >            try {
1653 >                e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1654 >                shouldThrow();
1655 >            } catch (NullPointerException success) {}
1656          }
1657      }
1658  
# Line 1484 | Line 1660 | public class ThreadPoolExecutorSubclassT
1660       * timed invokeAny(,,null) throws NPE
1661       */
1662      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1663 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1664 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1665 <        l.add(new StringTask());
1666 <        try {
1667 <            e.invokeAny(l, MEDIUM_DELAY_MS, null);
1668 <            shouldThrow();
1669 <        } catch (NullPointerException success) {
1670 <        } finally {
1671 <            joinPool(e);
1663 >        final ExecutorService e =
1664 >            new CustomTPE(2, 2,
1665 >                          LONG_DELAY_MS, MILLISECONDS,
1666 >                          new ArrayBlockingQueue<Runnable>(10));
1667 >        try (PoolCleaner cleaner = cleaner(e)) {
1668 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1669 >            l.add(new StringTask());
1670 >            try {
1671 >                e.invokeAny(l, MEDIUM_DELAY_MS, null);
1672 >                shouldThrow();
1673 >            } catch (NullPointerException success) {}
1674          }
1675      }
1676  
# Line 1500 | Line 1678 | public class ThreadPoolExecutorSubclassT
1678       * timed invokeAny(empty collection) throws IAE
1679       */
1680      public void testTimedInvokeAny2() throws Exception {
1681 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1682 <        try {
1683 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1684 <            shouldThrow();
1685 <        } catch (IllegalArgumentException success) {
1686 <        } finally {
1687 <            joinPool(e);
1681 >        final ExecutorService e =
1682 >            new CustomTPE(2, 2,
1683 >                          LONG_DELAY_MS, MILLISECONDS,
1684 >                          new ArrayBlockingQueue<Runnable>(10));
1685 >        try (PoolCleaner cleaner = cleaner(e)) {
1686 >            try {
1687 >                e.invokeAny(new ArrayList<Callable<String>>(),
1688 >                            MEDIUM_DELAY_MS, MILLISECONDS);
1689 >                shouldThrow();
1690 >            } catch (IllegalArgumentException success) {}
1691          }
1692      }
1693  
# Line 1515 | Line 1696 | public class ThreadPoolExecutorSubclassT
1696       */
1697      public void testTimedInvokeAny3() throws Exception {
1698          CountDownLatch latch = new CountDownLatch(1);
1699 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1700 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1701 <        l.add(latchAwaitingStringTask(latch));
1702 <        l.add(null);
1703 <        try {
1704 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1705 <            shouldThrow();
1706 <        } catch (NullPointerException success) {
1707 <        } finally {
1699 >        final ExecutorService e =
1700 >            new CustomTPE(2, 2,
1701 >                          LONG_DELAY_MS, MILLISECONDS,
1702 >                          new ArrayBlockingQueue<Runnable>(10));
1703 >        try (PoolCleaner cleaner = cleaner(e)) {
1704 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1705 >            l.add(latchAwaitingStringTask(latch));
1706 >            l.add(null);
1707 >            try {
1708 >                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1709 >                shouldThrow();
1710 >            } catch (NullPointerException success) {}
1711              latch.countDown();
1528            joinPool(e);
1712          }
1713      }
1714  
# Line 1533 | Line 1716 | public class ThreadPoolExecutorSubclassT
1716       * timed invokeAny(c) throws ExecutionException if no task completes
1717       */
1718      public void testTimedInvokeAny4() throws Exception {
1719 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1720 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1721 <        l.add(new NPETask());
1722 <        try {
1723 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1724 <            shouldThrow();
1725 <        } catch (ExecutionException success) {
1726 <            assertTrue(success.getCause() instanceof NullPointerException);
1727 <        } finally {
1728 <            joinPool(e);
1719 >        final ExecutorService e =
1720 >            new CustomTPE(2, 2,
1721 >                          LONG_DELAY_MS, MILLISECONDS,
1722 >                          new ArrayBlockingQueue<Runnable>(10));
1723 >        try (PoolCleaner cleaner = cleaner(e)) {
1724 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1725 >            l.add(new NPETask());
1726 >            try {
1727 >                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1728 >                shouldThrow();
1729 >            } catch (ExecutionException success) {
1730 >                assertTrue(success.getCause() instanceof NullPointerException);
1731 >            }
1732          }
1733      }
1734  
# Line 1550 | Line 1736 | public class ThreadPoolExecutorSubclassT
1736       * timed invokeAny(c) returns result of some task
1737       */
1738      public void testTimedInvokeAny5() throws Exception {
1739 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1740 <        try {
1739 >        final ExecutorService e =
1740 >            new CustomTPE(2, 2,
1741 >                          LONG_DELAY_MS, MILLISECONDS,
1742 >                          new ArrayBlockingQueue<Runnable>(10));
1743 >        try (PoolCleaner cleaner = cleaner(e)) {
1744              List<Callable<String>> l = new ArrayList<Callable<String>>();
1745              l.add(new StringTask());
1746              l.add(new StringTask());
1747              String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1748              assertSame(TEST_STRING, result);
1560        } finally {
1561            joinPool(e);
1749          }
1750      }
1751  
# Line 1566 | Line 1753 | public class ThreadPoolExecutorSubclassT
1753       * timed invokeAll(null) throws NPE
1754       */
1755      public void testTimedInvokeAll1() throws Exception {
1756 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1757 <        try {
1758 <            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1759 <            shouldThrow();
1760 <        } catch (NullPointerException success) {
1761 <        } finally {
1762 <            joinPool(e);
1756 >        final ExecutorService e =
1757 >            new CustomTPE(2, 2,
1758 >                          LONG_DELAY_MS, MILLISECONDS,
1759 >                          new ArrayBlockingQueue<Runnable>(10));
1760 >        try (PoolCleaner cleaner = cleaner(e)) {
1761 >            try {
1762 >                e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1763 >                shouldThrow();
1764 >            } catch (NullPointerException success) {}
1765          }
1766      }
1767  
# Line 1580 | Line 1769 | public class ThreadPoolExecutorSubclassT
1769       * timed invokeAll(,,null) throws NPE
1770       */
1771      public void testTimedInvokeAllNullTimeUnit() throws Exception {
1772 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1773 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1774 <        l.add(new StringTask());
1775 <        try {
1776 <            e.invokeAll(l, MEDIUM_DELAY_MS, null);
1777 <            shouldThrow();
1778 <        } catch (NullPointerException success) {
1779 <        } finally {
1780 <            joinPool(e);
1772 >        final ExecutorService e =
1773 >            new CustomTPE(2, 2,
1774 >                          LONG_DELAY_MS, MILLISECONDS,
1775 >                          new ArrayBlockingQueue<Runnable>(10));
1776 >        try (PoolCleaner cleaner = cleaner(e)) {
1777 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1778 >            l.add(new StringTask());
1779 >            try {
1780 >                e.invokeAll(l, MEDIUM_DELAY_MS, null);
1781 >                shouldThrow();
1782 >            } catch (NullPointerException success) {}
1783          }
1784      }
1785  
# Line 1596 | Line 1787 | public class ThreadPoolExecutorSubclassT
1787       * timed invokeAll(empty collection) returns empty collection
1788       */
1789      public void testTimedInvokeAll2() throws Exception {
1790 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1791 <        try {
1792 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1790 >        final ExecutorService e =
1791 >            new CustomTPE(2, 2,
1792 >                          LONG_DELAY_MS, MILLISECONDS,
1793 >                          new ArrayBlockingQueue<Runnable>(10));
1794 >        try (PoolCleaner cleaner = cleaner(e)) {
1795 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(),
1796 >                                                 MEDIUM_DELAY_MS, MILLISECONDS);
1797              assertTrue(r.isEmpty());
1603        } finally {
1604            joinPool(e);
1798          }
1799      }
1800  
# Line 1609 | Line 1802 | public class ThreadPoolExecutorSubclassT
1802       * timed invokeAll(c) throws NPE if c has null elements
1803       */
1804      public void testTimedInvokeAll3() throws Exception {
1805 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1806 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1807 <        l.add(new StringTask());
1808 <        l.add(null);
1809 <        try {
1810 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1811 <            shouldThrow();
1812 <        } catch (NullPointerException success) {
1813 <        } finally {
1814 <            joinPool(e);
1805 >        final ExecutorService e =
1806 >            new CustomTPE(2, 2,
1807 >                          LONG_DELAY_MS, MILLISECONDS,
1808 >                          new ArrayBlockingQueue<Runnable>(10));
1809 >        try (PoolCleaner cleaner = cleaner(e)) {
1810 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1811 >            l.add(new StringTask());
1812 >            l.add(null);
1813 >            try {
1814 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1815 >                shouldThrow();
1816 >            } catch (NullPointerException success) {}
1817          }
1818      }
1819  
# Line 1626 | Line 1821 | public class ThreadPoolExecutorSubclassT
1821       * get of element of invokeAll(c) throws exception on failed task
1822       */
1823      public void testTimedInvokeAll4() throws Exception {
1824 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1825 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1826 <        l.add(new NPETask());
1827 <        List<Future<String>> futures =
1828 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1829 <        assertEquals(1, futures.size());
1830 <        try {
1831 <            futures.get(0).get();
1832 <            shouldThrow();
1833 <        } catch (ExecutionException success) {
1834 <            assertTrue(success.getCause() instanceof NullPointerException);
1835 <        } finally {
1836 <            joinPool(e);
1824 >        final ExecutorService e =
1825 >            new CustomTPE(2, 2,
1826 >                          LONG_DELAY_MS, MILLISECONDS,
1827 >                          new ArrayBlockingQueue<Runnable>(10));
1828 >        try (PoolCleaner cleaner = cleaner(e)) {
1829 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1830 >            l.add(new NPETask());
1831 >            List<Future<String>> futures =
1832 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1833 >            assertEquals(1, futures.size());
1834 >            try {
1835 >                futures.get(0).get();
1836 >                shouldThrow();
1837 >            } catch (ExecutionException success) {
1838 >                assertTrue(success.getCause() instanceof NullPointerException);
1839 >            }
1840          }
1841      }
1842  
# Line 1646 | Line 1844 | public class ThreadPoolExecutorSubclassT
1844       * timed invokeAll(c) returns results of all completed tasks
1845       */
1846      public void testTimedInvokeAll5() throws Exception {
1847 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1848 <        try {
1847 >        final ExecutorService e =
1848 >            new CustomTPE(2, 2,
1849 >                          LONG_DELAY_MS, MILLISECONDS,
1850 >                          new ArrayBlockingQueue<Runnable>(10));
1851 >        try (PoolCleaner cleaner = cleaner(e)) {
1852              List<Callable<String>> l = new ArrayList<Callable<String>>();
1853              l.add(new StringTask());
1854              l.add(new StringTask());
1855              List<Future<String>> futures =
1856 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1856 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1857              assertEquals(2, futures.size());
1858              for (Future<String> future : futures)
1859                  assertSame(TEST_STRING, future.get());
1659        } finally {
1660            joinPool(e);
1860          }
1861      }
1862  
# Line 1665 | Line 1864 | public class ThreadPoolExecutorSubclassT
1864       * timed invokeAll(c) cancels tasks not completed by timeout
1865       */
1866      public void testTimedInvokeAll6() throws Exception {
1867 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1868 <        try {
1869 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1870 <            l.add(new StringTask());
1871 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1872 <            l.add(new StringTask());
1873 <            List<Future<String>> futures =
1874 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1875 <            assertEquals(l.size(), futures.size());
1876 <            for (Future future : futures)
1877 <                assertTrue(future.isDone());
1878 <            assertFalse(futures.get(0).isCancelled());
1879 <            assertTrue(futures.get(1).isCancelled());
1880 <        } finally {
1881 <            joinPool(e);
1867 >        final ExecutorService e =
1868 >            new CustomTPE(2, 2,
1869 >                          LONG_DELAY_MS, MILLISECONDS,
1870 >                          new ArrayBlockingQueue<Runnable>(10));
1871 >        try (PoolCleaner cleaner = cleaner(e)) {
1872 >            for (long timeout = timeoutMillis();;) {
1873 >                List<Callable<String>> tasks = new ArrayList<>();
1874 >                tasks.add(new StringTask("0"));
1875 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1876 >                tasks.add(new StringTask("2"));
1877 >                long startTime = System.nanoTime();
1878 >                List<Future<String>> futures =
1879 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1880 >                assertEquals(tasks.size(), futures.size());
1881 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1882 >                for (Future future : futures)
1883 >                    assertTrue(future.isDone());
1884 >                assertTrue(futures.get(1).isCancelled());
1885 >                try {
1886 >                    assertEquals("0", futures.get(0).get());
1887 >                    assertEquals("2", futures.get(2).get());
1888 >                    break;
1889 >                } catch (CancellationException retryWithLongerTimeout) {
1890 >                    timeout *= 2;
1891 >                    if (timeout >= LONG_DELAY_MS / 2)
1892 >                        fail("expected exactly one task to be cancelled");
1893 >                }
1894 >            }
1895          }
1896      }
1897  
# Line 1693 | Line 1905 | public class ThreadPoolExecutorSubclassT
1905                            LONG_DELAY_MS, MILLISECONDS,
1906                            new LinkedBlockingQueue<Runnable>(),
1907                            new FailingThreadFactory());
1908 <        try {
1908 >        try (PoolCleaner cleaner = cleaner(e)) {
1909              final int TASKS = 100;
1910              final CountDownLatch done = new CountDownLatch(TASKS);
1911              for (int k = 0; k < TASKS; ++k)
# Line 1702 | Line 1914 | public class ThreadPoolExecutorSubclassT
1914                          done.countDown();
1915                      }});
1916              assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1705        } finally {
1706            joinPool(e);
1917          }
1918      }
1919  
# Line 1711 | Line 1921 | public class ThreadPoolExecutorSubclassT
1921       * allowsCoreThreadTimeOut is by default false.
1922       */
1923      public void testAllowsCoreThreadTimeOut() {
1924 <        ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1925 <        assertFalse(p.allowsCoreThreadTimeOut());
1926 <        joinPool(p);
1924 >        final ThreadPoolExecutor p =
1925 >            new CustomTPE(2, 2,
1926 >                          1000, MILLISECONDS,
1927 >                          new ArrayBlockingQueue<Runnable>(10));
1928 >        try (PoolCleaner cleaner = cleaner(p)) {
1929 >            assertFalse(p.allowsCoreThreadTimeOut());
1930 >        }
1931      }
1932  
1933      /**
1934       * allowCoreThreadTimeOut(true) causes idle threads to time out
1935       */
1936      public void testAllowCoreThreadTimeOut_true() throws Exception {
1937 <        long coreThreadTimeOut = SHORT_DELAY_MS;
1937 >        long keepAliveTime = timeoutMillis();
1938          final ThreadPoolExecutor p =
1939              new CustomTPE(2, 10,
1940 <                          coreThreadTimeOut, MILLISECONDS,
1940 >                          keepAliveTime, MILLISECONDS,
1941                            new ArrayBlockingQueue<Runnable>(10));
1942 <        final CountDownLatch threadStarted = new CountDownLatch(1);
1943 <        try {
1942 >        try (PoolCleaner cleaner = cleaner(p)) {
1943 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1944              p.allowCoreThreadTimeOut(true);
1945              p.execute(new CheckedRunnable() {
1946 <                public void realRun() throws InterruptedException {
1946 >                public void realRun() {
1947                      threadStarted.countDown();
1948                      assertEquals(1, p.getPoolSize());
1949                  }});
1950              await(threadStarted);
1951 <            delay(coreThreadTimeOut);
1951 >            delay(keepAliveTime);
1952              long startTime = System.nanoTime();
1953              while (p.getPoolSize() > 0
1954                     && millisElapsedSince(startTime) < LONG_DELAY_MS)
1955                  Thread.yield();
1956              assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1957              assertEquals(0, p.getPoolSize());
1744        } finally {
1745            joinPool(p);
1958          }
1959      }
1960  
# Line 1750 | Line 1962 | public class ThreadPoolExecutorSubclassT
1962       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1963       */
1964      public void testAllowCoreThreadTimeOut_false() throws Exception {
1965 <        long coreThreadTimeOut = SHORT_DELAY_MS;
1965 >        long keepAliveTime = timeoutMillis();
1966          final ThreadPoolExecutor p =
1967              new CustomTPE(2, 10,
1968 <                          coreThreadTimeOut, MILLISECONDS,
1968 >                          keepAliveTime, MILLISECONDS,
1969                            new ArrayBlockingQueue<Runnable>(10));
1970 <        final CountDownLatch threadStarted = new CountDownLatch(1);
1971 <        try {
1970 >        try (PoolCleaner cleaner = cleaner(p)) {
1971 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1972              p.allowCoreThreadTimeOut(false);
1973              p.execute(new CheckedRunnable() {
1974                  public void realRun() throws InterruptedException {
1975                      threadStarted.countDown();
1976                      assertTrue(p.getPoolSize() >= 1);
1977                  }});
1978 <            delay(2 * coreThreadTimeOut);
1978 >            delay(2 * keepAliveTime);
1979              assertTrue(p.getPoolSize() >= 1);
1980 <        } finally {
1981 <            joinPool(p);
1980 >        }
1981 >    }
1982 >
1983 >    /**
1984 >     * get(cancelled task) throws CancellationException
1985 >     * (in part, a test of CustomTPE itself)
1986 >     */
1987 >    public void testGet_cancelled() throws Exception {
1988 >        final ExecutorService e =
1989 >            new CustomTPE(1, 1,
1990 >                          LONG_DELAY_MS, MILLISECONDS,
1991 >                          new LinkedBlockingQueue<Runnable>());
1992 >        try (PoolCleaner cleaner = cleaner(e)) {
1993 >            final CountDownLatch blockerStarted = new CountDownLatch(1);
1994 >            final CountDownLatch done = new CountDownLatch(1);
1995 >            final List<Future<?>> futures = new ArrayList<>();
1996 >            for (int i = 0; i < 2; i++) {
1997 >                Runnable r = new CheckedRunnable() { public void realRun()
1998 >                                                         throws Throwable {
1999 >                    blockerStarted.countDown();
2000 >                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
2001 >                }};
2002 >                futures.add(e.submit(r));
2003 >            }
2004 >            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
2005 >            for (Future<?> future : futures) future.cancel(false);
2006 >            for (Future<?> future : futures) {
2007 >                try {
2008 >                    future.get();
2009 >                    shouldThrow();
2010 >                } catch (CancellationException success) {}
2011 >                try {
2012 >                    future.get(LONG_DELAY_MS, MILLISECONDS);
2013 >                    shouldThrow();
2014 >                } catch (CancellationException success) {}
2015 >                assertTrue(future.isCancelled());
2016 >                assertTrue(future.isDone());
2017 >            }
2018 >            done.countDown();
2019          }
2020      }
2021  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines