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.5 by jsr166, Wed Nov 18 16:51:26 2009 UTC vs.
Revision 1.87 by jsr166, Mon Oct 5 22:19:00 2015 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
8  
9 < import java.util.concurrent.*;
10 < import java.util.concurrent.locks.*;
9 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
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(ThreadPoolExecutorTest.class);
45 >        return new TestSuite(ThreadPoolExecutorSubclassTest.class);
46      }
47  
48      static class CustomTask<V> implements RunnableFuture<V> {
# Line 29 | Line 54 | public class ThreadPoolExecutorSubclassT
54          V result;
55          Thread thread;
56          Exception exception;
57 <        CustomTask(Callable<V> c) { callable = c; }
58 <        CustomTask(final Runnable r, final V res) { callable = new Callable<V>() {
59 <            public V call() throws Exception { r.run(); return res; }};
57 >        CustomTask(Callable<V> c) {
58 >            if (c == null) throw new NullPointerException();
59 >            callable = c;
60 >        }
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; }};
65          }
66          public boolean isDone() {
67              lock.lock(); try { return done; } finally { lock.unlock() ; }
# Line 54 | Line 84 | public class ThreadPoolExecutorSubclassT
84              finally { lock.unlock() ; }
85          }
86          public void run() {
57            boolean runme;
87              lock.lock();
88              try {
89 <                runme = !done;
90 <                if (!runme)
91 <                    thread = Thread.currentThread();
89 >                if (done)
90 >                    return;
91 >                thread = Thread.currentThread();
92              }
93              finally { lock.unlock() ; }
65            if (!runme) return;
94              V v = null;
95              Exception e = null;
96              try {
# Line 73 | 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 86 | 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 97 | Line 129 | public class ThreadPoolExecutorSubclassT
129              long nanos = unit.toNanos(timeout);
130              lock.lock();
131              try {
132 <                for (;;) {
133 <                    if (done) break;
102 <                    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 111 | Line 144 | public class ThreadPoolExecutorSubclassT
144          }
145      }
146  
114
147      static class CustomTPE extends ThreadPoolExecutor {
148          protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
149              return new CustomTask<V>(c);
# Line 158 | Line 190 | public class ThreadPoolExecutorSubclassT
190                workQueue, threadFactory, handler);
191          }
192  
193 <        volatile boolean beforeCalled = false;
194 <        volatile boolean afterCalled = false;
195 <        volatile boolean terminatedCalled = false;
193 >        final CountDownLatch beforeCalled = new CountDownLatch(1);
194 >        final CountDownLatch afterCalled = new CountDownLatch(1);
195 >        final CountDownLatch terminatedCalled = new CountDownLatch(1);
196 >
197          public CustomTPE() {
198 <            super(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>());
198 >            super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
199          }
200          protected void beforeExecute(Thread t, Runnable r) {
201 <            beforeCalled = true;
201 >            beforeCalled.countDown();
202          }
203          protected void afterExecute(Runnable r, Throwable t) {
204 <            afterCalled = true;
204 >            afterCalled.countDown();
205          }
206          protected void terminated() {
207 <            terminatedCalled = true;
207 >            terminatedCalled.countDown();
208          }
209  
210 +        public boolean beforeCalled() {
211 +            return beforeCalled.getCount() == 0;
212 +        }
213 +        public boolean afterCalled() {
214 +            return afterCalled.getCount() == 0;
215 +        }
216 +        public boolean terminatedCalled() {
217 +            return terminatedCalled.getCount() == 0;
218 +        }
219      }
220  
221      static class FailingThreadFactory implements ThreadFactory {
# Line 184 | Line 226 | public class ThreadPoolExecutorSubclassT
226          }
227      }
228  
187
229      /**
230 <     *  execute successfully executes a runnable
230 >     * execute successfully executes a runnable
231       */
232      public void testExecute() throws InterruptedException {
233 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
234 <        try {
235 <            p1.execute(new ShortRunnable());
236 <            Thread.sleep(SMALL_DELAY_MS);
237 <        } finally {
238 <            joinPool(p1);
233 >        final ThreadPoolExecutor p =
234 >            new CustomTPE(1, 1,
235 >                          2 * LONG_DELAY_MS, MILLISECONDS,
236 >                          new ArrayBlockingQueue<Runnable>(10));
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(LONG_DELAY_MS, MILLISECONDS));
243          }
244      }
245  
246      /**
247 <     *  getActiveCount increases but doesn't overestimate, when a
248 <     *  thread becomes active
247 >     * getActiveCount increases but doesn't overestimate, when a
248 >     * thread becomes active
249       */
250      public void testGetActiveCount() throws InterruptedException {
251 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
252 <        assertEquals(0, p2.getActiveCount());
253 <        p2.execute(new MediumRunnable());
254 <        Thread.sleep(SHORT_DELAY_MS);
255 <        assertEquals(1, p2.getActiveCount());
256 <        joinPool(p2);
251 >        final ThreadPoolExecutor p =
252 >            new CustomTPE(2, 2,
253 >                          LONG_DELAY_MS, MILLISECONDS,
254 >                          new ArrayBlockingQueue<Runnable>(10));
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 {
261 >                    threadStarted.countDown();
262 >                    assertEquals(1, p.getActiveCount());
263 >                    done.await();
264 >                }});
265 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
266 >            assertEquals(1, p.getActiveCount());
267 >            done.countDown();
268 >        }
269      }
270  
271      /**
272 <     *  prestartCoreThread starts a thread if under corePoolSize, else doesn't
272 >     * prestartCoreThread starts a thread if under corePoolSize, else doesn't
273       */
274      public void testPrestartCoreThread() {
275 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
276 <        assertEquals(0, p2.getPoolSize());
277 <        assertTrue(p2.prestartCoreThread());
278 <        assertEquals(1, p2.getPoolSize());
279 <        assertTrue(p2.prestartCoreThread());
280 <        assertEquals(2, p2.getPoolSize());
281 <        assertFalse(p2.prestartCoreThread());
282 <        assertEquals(2, p2.getPoolSize());
283 <        joinPool(p2);
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
298 >     * prestartAllCoreThreads starts all corePoolSize threads
299       */
300      public void testPrestartAllCoreThreads() {
301 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
302 <        assertEquals(0, p2.getPoolSize());
303 <        p2.prestartAllCoreThreads();
304 <        assertEquals(2, p2.getPoolSize());
305 <        p2.prestartAllCoreThreads();
306 <        assertEquals(2, p2.getPoolSize());
307 <        joinPool(p2);
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      /**
320 <     *   getCompletedTaskCount increases, but doesn't overestimate,
321 <     *   when tasks complete
320 >     * getCompletedTaskCount increases, but doesn't overestimate,
321 >     * when tasks complete
322       */
323      public void testGetCompletedTaskCount() throws InterruptedException {
324 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
325 <        assertEquals(0, p2.getCompletedTaskCount());
326 <        p2.execute(new ShortRunnable());
327 <        Thread.sleep(SMALL_DELAY_MS);
328 <        assertEquals(1, p2.getCompletedTaskCount());
329 <        try { p2.shutdown(); } catch (SecurityException ok) { return; }
330 <        joinPool(p2);
324 >        final ThreadPoolExecutor p =
325 >            new CustomTPE(2, 2,
326 >                          LONG_DELAY_MS, MILLISECONDS,
327 >                          new ArrayBlockingQueue<Runnable>(10));
328 >        try (PoolCleaner cleaner = cleaner(p)) {
329 >            final CountDownLatch threadStarted = new CountDownLatch(1);
330 >            final CountDownLatch threadProceed = new CountDownLatch(1);
331 >            final CountDownLatch threadDone = new CountDownLatch(1);
332 >            assertEquals(0, p.getCompletedTaskCount());
333 >            p.execute(new CheckedRunnable() {
334 >                public void realRun() throws InterruptedException {
335 >                    threadStarted.countDown();
336 >                    assertEquals(0, p.getCompletedTaskCount());
337 >                    threadProceed.await();
338 >                    threadDone.countDown();
339 >                }});
340 >            await(threadStarted);
341 >            assertEquals(0, p.getCompletedTaskCount());
342 >            threadProceed.countDown();
343 >            threadDone.await();
344 >            long startTime = System.nanoTime();
345 >            while (p.getCompletedTaskCount() != 1) {
346 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
347 >                    fail("timed out");
348 >                Thread.yield();
349 >            }
350 >        }
351      }
352  
353      /**
354 <     *   getCorePoolSize returns size given in constructor if not otherwise set
354 >     * getCorePoolSize returns size given in constructor if not otherwise set
355       */
356      public void testGetCorePoolSize() {
357 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
358 <        assertEquals(1, p1.getCorePoolSize());
359 <        joinPool(p1);
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
367 >     * getKeepAliveTime returns value given in constructor if not otherwise set
368       */
369      public void testGetKeepAliveTime() {
370 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
371 <        assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS));
372 <        joinPool(p2);
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  
274
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, TimeUnit.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, TimeUnit.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  
296
410      /**
411       * setThreadFactory(null) throws NPE
412       */
413      public void testSetThreadFactoryNull() {
414 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.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 312 | 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, TimeUnit.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 323 | Line 443 | public class ThreadPoolExecutorSubclassT
443       * getRejectedExecutionHandler
444       */
445      public void testSetRejectedExecutionHandler() {
446 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.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  
333
457      /**
458       * setRejectedExecutionHandler(null) throws NPE
459       */
460      public void testSetRejectedExecutionHandlerNull() {
461 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.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  
348
473      /**
474 <     *   getLargestPoolSize increases, but doesn't overestimate, when
475 <     *   multiple threads active
474 >     * getLargestPoolSize increases, but doesn't overestimate, when
475 >     * multiple threads active
476       */
477      public void testGetLargestPoolSize() throws InterruptedException {
478 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
479 <        assertEquals(0, p2.getLargestPoolSize());
480 <        p2.execute(new MediumRunnable());
481 <        p2.execute(new MediumRunnable());
482 <        Thread.sleep(SHORT_DELAY_MS);
483 <        assertEquals(2, p2.getLargestPoolSize());
484 <        joinPool(p2);
478 >        final int THREADS = 3;
479 >        final CountDownLatch done = new CountDownLatch(1);
480 >        final ThreadPoolExecutor p =
481 >            new CustomTPE(THREADS, THREADS,
482 >                          LONG_DELAY_MS, MILLISECONDS,
483 >                          new ArrayBlockingQueue<Runnable>(10));
484 >        try (PoolCleaner cleaner = cleaner(p, done)) {
485 >            assertEquals(0, p.getLargestPoolSize());
486 >            final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
487 >            for (int i = 0; i < THREADS; i++)
488 >                p.execute(new CheckedRunnable() {
489 >                    public void realRun() throws InterruptedException {
490 >                        threadsStarted.countDown();
491 >                        done.await();
492 >                        assertEquals(THREADS, p.getLargestPoolSize());
493 >                    }});
494 >            assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
495 >            assertEquals(THREADS, p.getLargestPoolSize());
496 >        }
497 >        assertEquals(THREADS, p.getLargestPoolSize());
498      }
499  
500      /**
501 <     *   getMaximumPoolSize returns value given in constructor if not
502 <     *   otherwise set
501 >     * getMaximumPoolSize returns value given in constructor if not
502 >     * otherwise set
503       */
504      public void testGetMaximumPoolSize() {
505 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
506 <        assertEquals(2, p2.getMaximumPoolSize());
507 <        joinPool(p2);
505 >        final ThreadPoolExecutor p =
506 >            new CustomTPE(2, 3,
507 >                          LONG_DELAY_MS, MILLISECONDS,
508 >                          new ArrayBlockingQueue<Runnable>(10));
509 >        try (PoolCleaner cleaner = cleaner(p)) {
510 >            assertEquals(3, p.getMaximumPoolSize());
511 >            p.setMaximumPoolSize(5);
512 >            assertEquals(5, p.getMaximumPoolSize());
513 >            p.setMaximumPoolSize(4);
514 >            assertEquals(4, p.getMaximumPoolSize());
515 >        }
516      }
517  
518      /**
519 <     *   getPoolSize increases, but doesn't overestimate, when threads
520 <     *   become active
521 <     */
522 <    public void testGetPoolSize() {
523 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
524 <        assertEquals(0, p1.getPoolSize());
525 <        p1.execute(new MediumRunnable());
526 <        assertEquals(1, p1.getPoolSize());
527 <        joinPool(p1);
519 >     * getPoolSize increases, but doesn't overestimate, when threads
520 >     * become active
521 >     */
522 >    public void testGetPoolSize() throws InterruptedException {
523 >        final CountDownLatch done = new CountDownLatch(1);
524 >        final ThreadPoolExecutor p =
525 >            new CustomTPE(1, 1,
526 >                          LONG_DELAY_MS, MILLISECONDS,
527 >                          new ArrayBlockingQueue<Runnable>(10));
528 >        try (PoolCleaner cleaner = cleaner(p, done)) {
529 >            assertEquals(0, p.getPoolSize());
530 >            final CountDownLatch threadStarted = new CountDownLatch(1);
531 >            p.execute(new CheckedRunnable() {
532 >                public void realRun() throws InterruptedException {
533 >                    threadStarted.countDown();
534 >                    assertEquals(1, p.getPoolSize());
535 >                    done.await();
536 >                }});
537 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
538 >            assertEquals(1, p.getPoolSize());
539 >        }
540      }
541  
542      /**
543 <     *  getTaskCount increases, but doesn't overestimate, when tasks submitted
543 >     * getTaskCount increases, but doesn't overestimate, when tasks submitted
544       */
545      public void testGetTaskCount() throws InterruptedException {
546 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
547 <        assertEquals(0, p1.getTaskCount());
548 <        p1.execute(new MediumRunnable());
549 <        Thread.sleep(SHORT_DELAY_MS);
550 <        assertEquals(1, p1.getTaskCount());
551 <        joinPool(p1);
546 >        final int TASKS = 3;
547 >        final CountDownLatch done = new CountDownLatch(1);
548 >        final ThreadPoolExecutor p =
549 >            new CustomTPE(1, 1,
550 >                          LONG_DELAY_MS, MILLISECONDS,
551 >                          new ArrayBlockingQueue<Runnable>(10));
552 >        try (PoolCleaner cleaner = cleaner(p, done)) {
553 >            final CountDownLatch threadStarted = new CountDownLatch(1);
554 >            assertEquals(0, p.getTaskCount());
555 >            assertEquals(0, p.getCompletedTaskCount());
556 >            p.execute(new CheckedRunnable() {
557 >                public void realRun() throws InterruptedException {
558 >                    threadStarted.countDown();
559 >                    done.await();
560 >                }});
561 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
562 >            assertEquals(1, p.getTaskCount());
563 >            assertEquals(0, p.getCompletedTaskCount());
564 >            for (int i = 0; i < TASKS; i++) {
565 >                assertEquals(1 + i, p.getTaskCount());
566 >                p.execute(new CheckedRunnable() {
567 >                    public void realRun() throws InterruptedException {
568 >                        threadStarted.countDown();
569 >                        assertEquals(1 + TASKS, p.getTaskCount());
570 >                        done.await();
571 >                    }});
572 >            }
573 >            assertEquals(1 + TASKS, p.getTaskCount());
574 >            assertEquals(0, p.getCompletedTaskCount());
575 >        }
576 >        assertEquals(1 + TASKS, p.getTaskCount());
577 >        assertEquals(1 + TASKS, p.getCompletedTaskCount());
578      }
579  
580      /**
581 <     *   isShutDown is false before shutdown, true after
581 >     * isShutdown is false before shutdown, true after
582       */
583      public void testIsShutdown() {
584 <
585 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
586 <        assertFalse(p1.isShutdown());
587 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
588 <        assertTrue(p1.isShutdown());
589 <        joinPool(p1);
584 >        final ThreadPoolExecutor p =
585 >            new CustomTPE(1, 1,
586 >                          LONG_DELAY_MS, MILLISECONDS,
587 >                          new ArrayBlockingQueue<Runnable>(10));
588 >        try (PoolCleaner cleaner = cleaner(p)) {
589 >            assertFalse(p.isShutdown());
590 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
591 >            assertTrue(p.isShutdown());
592 >        }
593      }
594  
409
595      /**
596 <     *  isTerminated is false before termination, true after
596 >     * isTerminated is false before termination, true after
597       */
598      public void testIsTerminated() throws InterruptedException {
599 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
600 <        assertFalse(p1.isTerminated());
601 <        try {
602 <            p1.execute(new MediumRunnable());
603 <        } finally {
604 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
599 >        final ThreadPoolExecutor p =
600 >            new CustomTPE(1, 1,
601 >                          LONG_DELAY_MS, MILLISECONDS,
602 >                          new ArrayBlockingQueue<Runnable>(10));
603 >        try (PoolCleaner cleaner = cleaner(p)) {
604 >            final CountDownLatch threadStarted = new CountDownLatch(1);
605 >            final CountDownLatch done = new CountDownLatch(1);
606 >            assertFalse(p.isTerminating());
607 >            p.execute(new CheckedRunnable() {
608 >                public void realRun() throws InterruptedException {
609 >                    assertFalse(p.isTerminating());
610 >                    threadStarted.countDown();
611 >                    done.await();
612 >                }});
613 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
614 >            assertFalse(p.isTerminating());
615 >            done.countDown();
616 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
617 >            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
618 >            assertTrue(p.isTerminated());
619 >            assertFalse(p.isTerminating());
620          }
421        assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
422        assertTrue(p1.isTerminated());
621      }
622  
623      /**
624 <     *  isTerminating is not true when running or when terminated
624 >     * isTerminating is not true when running or when terminated
625       */
626      public void testIsTerminating() throws InterruptedException {
627 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
628 <        assertFalse(p1.isTerminating());
629 <        try {
630 <            p1.execute(new SmallRunnable());
631 <            assertFalse(p1.isTerminating());
632 <        } finally {
633 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
634 <        }
635 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
636 <        assertTrue(p1.isTerminated());
637 <        assertFalse(p1.isTerminating());
627 >        final ThreadPoolExecutor p =
628 >            new CustomTPE(1, 1,
629 >                          LONG_DELAY_MS, MILLISECONDS,
630 >                          new ArrayBlockingQueue<Runnable>(10));
631 >        try (PoolCleaner cleaner = cleaner(p)) {
632 >            final CountDownLatch threadStarted = new CountDownLatch(1);
633 >            final CountDownLatch done = new CountDownLatch(1);
634 >            assertFalse(p.isTerminating());
635 >            p.execute(new CheckedRunnable() {
636 >                public void realRun() throws InterruptedException {
637 >                    assertFalse(p.isTerminating());
638 >                    threadStarted.countDown();
639 >                    done.await();
640 >                }});
641 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
642 >            assertFalse(p.isTerminating());
643 >            done.countDown();
644 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
645 >            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
646 >            assertTrue(p.isTerminated());
647 >            assertFalse(p.isTerminating());
648 >        }
649      }
650  
651      /**
652       * getQueue returns the work queue, which contains queued tasks
653       */
654      public void testGetQueue() throws InterruptedException {
655 <        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
656 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q);
657 <        FutureTask[] tasks = new FutureTask[5];
658 <        for (int i = 0; i < 5; i++) {
659 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
660 <            p1.execute(tasks[i]);
661 <        }
662 <        try {
663 <            Thread.sleep(SHORT_DELAY_MS);
664 <            BlockingQueue<Runnable> wq = p1.getQueue();
665 <            assertSame(q, wq);
666 <            assertFalse(wq.contains(tasks[0]));
667 <            assertTrue(wq.contains(tasks[4]));
668 <            for (int i = 1; i < 5; ++i)
669 <                tasks[i].cancel(true);
670 <            p1.shutdownNow();
671 <        } finally {
672 <            joinPool(p1);
655 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
656 >        final ThreadPoolExecutor p =
657 >            new CustomTPE(1, 1,
658 >                          LONG_DELAY_MS, MILLISECONDS,
659 >                          q);
660 >        try (PoolCleaner cleaner = cleaner(p)) {
661 >            final CountDownLatch threadStarted = new CountDownLatch(1);
662 >            final CountDownLatch done = new CountDownLatch(1);
663 >            FutureTask[] tasks = new FutureTask[5];
664 >            for (int i = 0; i < tasks.length; i++) {
665 >                Callable task = new CheckedCallable<Boolean>() {
666 >                    public Boolean realCall() throws InterruptedException {
667 >                        threadStarted.countDown();
668 >                        assertSame(q, p.getQueue());
669 >                        done.await();
670 >                        return Boolean.TRUE;
671 >                    }};
672 >                tasks[i] = new FutureTask(task);
673 >                p.execute(tasks[i]);
674 >            }
675 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
676 >            assertSame(q, p.getQueue());
677 >            assertFalse(q.contains(tasks[0]));
678 >            assertTrue(q.contains(tasks[tasks.length - 1]));
679 >            assertEquals(tasks.length - 1, q.size());
680 >            done.countDown();
681          }
682      }
683  
# Line 469 | Line 686 | public class ThreadPoolExecutorSubclassT
686       */
687      public void testRemove() throws InterruptedException {
688          BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
689 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q);
690 <        FutureTask[] tasks = new FutureTask[5];
691 <        for (int i = 0; i < 5; i++) {
692 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
693 <            p1.execute(tasks[i]);
694 <        }
695 <        try {
696 <            Thread.sleep(SHORT_DELAY_MS);
697 <            assertFalse(p1.remove(tasks[0]));
689 >        final ThreadPoolExecutor p =
690 >            new CustomTPE(1, 1,
691 >                          LONG_DELAY_MS, MILLISECONDS,
692 >                          q);
693 >        try (PoolCleaner cleaner = cleaner(p)) {
694 >            Runnable[] tasks = new Runnable[6];
695 >            final CountDownLatch threadStarted = new CountDownLatch(1);
696 >            final CountDownLatch done = new CountDownLatch(1);
697 >            for (int i = 0; i < tasks.length; i++) {
698 >                tasks[i] = new CheckedRunnable() {
699 >                    public void realRun() throws InterruptedException {
700 >                        threadStarted.countDown();
701 >                        done.await();
702 >                    }};
703 >                p.execute(tasks[i]);
704 >            }
705 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
706 >            assertFalse(p.remove(tasks[0]));
707              assertTrue(q.contains(tasks[4]));
708              assertTrue(q.contains(tasks[3]));
709 <            assertTrue(p1.remove(tasks[4]));
710 <            assertFalse(p1.remove(tasks[4]));
709 >            assertTrue(p.remove(tasks[4]));
710 >            assertFalse(p.remove(tasks[4]));
711              assertFalse(q.contains(tasks[4]));
712              assertTrue(q.contains(tasks[3]));
713 <            assertTrue(p1.remove(tasks[3]));
713 >            assertTrue(p.remove(tasks[3]));
714              assertFalse(q.contains(tasks[3]));
715 <        } finally {
490 <            joinPool(p1);
715 >            done.countDown();
716          }
717      }
718  
719      /**
720 <     *   purge removes cancelled tasks from the queue
720 >     * purge removes cancelled tasks from the queue
721       */
722 <    public void testPurge() {
723 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
724 <        FutureTask[] tasks = new FutureTask[5];
725 <        for (int i = 0; i < 5; i++) {
726 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
727 <            p1.execute(tasks[i]);
722 >    public void testPurge() throws InterruptedException {
723 >        final CountDownLatch threadStarted = new CountDownLatch(1);
724 >        final CountDownLatch done = new CountDownLatch(1);
725 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
726 >        final ThreadPoolExecutor p =
727 >            new CustomTPE(1, 1,
728 >                          LONG_DELAY_MS, MILLISECONDS,
729 >                          q);
730 >        try (PoolCleaner cleaner = cleaner(p, done)) {
731 >            FutureTask[] tasks = new FutureTask[5];
732 >            for (int i = 0; i < tasks.length; i++) {
733 >                Callable task = new CheckedCallable<Boolean>() {
734 >                    public Boolean realCall() throws InterruptedException {
735 >                        threadStarted.countDown();
736 >                        done.await();
737 >                        return Boolean.TRUE;
738 >                    }};
739 >                tasks[i] = new FutureTask(task);
740 >                p.execute(tasks[i]);
741 >            }
742 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
743 >            assertEquals(tasks.length, p.getTaskCount());
744 >            assertEquals(tasks.length - 1, q.size());
745 >            assertEquals(1L, p.getActiveCount());
746 >            assertEquals(0L, p.getCompletedTaskCount());
747 >            tasks[4].cancel(true);
748 >            tasks[3].cancel(false);
749 >            p.purge();
750 >            assertEquals(tasks.length - 3, q.size());
751 >            assertEquals(tasks.length - 2, p.getTaskCount());
752 >            p.purge();         // Nothing to do
753 >            assertEquals(tasks.length - 3, q.size());
754 >            assertEquals(tasks.length - 2, p.getTaskCount());
755          }
504        tasks[4].cancel(true);
505        tasks[3].cancel(true);
506        p1.purge();
507        long count = p1.getTaskCount();
508        assertTrue(count >= 2 && count < 5);
509        joinPool(p1);
756      }
757  
758      /**
759 <     *  shutDownNow returns a list containing tasks that were not run
760 <     */
761 <    public void testShutDownNow() {
762 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
763 <        List l;
764 <        try {
765 <            for (int i = 0; i < 5; i++)
766 <                p1.execute(new MediumPossiblyInterruptedRunnable());
767 <        }
768 <        finally {
759 >     * shutdownNow returns a list containing tasks that were not run,
760 >     * and those tasks are drained from the queue
761 >     */
762 >    public void testShutdownNow() throws InterruptedException {
763 >        final int poolSize = 2;
764 >        final int count = 5;
765 >        final AtomicInteger ran = new AtomicInteger(0);
766 >        final ThreadPoolExecutor p =
767 >            new CustomTPE(poolSize, poolSize,
768 >                          LONG_DELAY_MS, MILLISECONDS,
769 >                          new ArrayBlockingQueue<Runnable>(10));
770 >        final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
771 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
772 >            threadsStarted.countDown();
773              try {
774 <                l = p1.shutdownNow();
775 <            } catch (SecurityException ok) { return; }
776 <
777 <        }
778 <        assertTrue(p1.isShutdown());
779 <        assertTrue(l.size() <= 4);
774 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
775 >            } catch (InterruptedException success) {}
776 >            ran.getAndIncrement();
777 >        }};
778 >        for (int i = 0; i < count; i++)
779 >            p.execute(waiter);
780 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
781 >        assertEquals(poolSize, p.getActiveCount());
782 >        assertEquals(0, p.getCompletedTaskCount());
783 >        final List<Runnable> queuedTasks;
784 >        try {
785 >            queuedTasks = p.shutdownNow();
786 >        } catch (SecurityException ok) {
787 >            return; // Allowed in case test doesn't have privs
788 >        }
789 >        assertTrue(p.isShutdown());
790 >        assertTrue(p.getQueue().isEmpty());
791 >        assertEquals(count - poolSize, queuedTasks.size());
792 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
793 >        assertTrue(p.isTerminated());
794 >        assertEquals(poolSize, ran.get());
795 >        assertEquals(poolSize, p.getCompletedTaskCount());
796      }
797  
798      // Exception Tests
799  
534
800      /**
801       * Constructor throws if corePoolSize argument is less than zero
802       */
803      public void testConstructor1() {
804          try {
805 <            new CustomTPE(-1,1,LONG_DELAY_MS, TimeUnit.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 547 | Line 813 | public class ThreadPoolExecutorSubclassT
813       */
814      public void testConstructor2() {
815          try {
816 <            new CustomTPE(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
816 >            new CustomTPE(1, -1, 1L, SECONDS,
817 >                          new ArrayBlockingQueue<Runnable>(10));
818              shouldThrow();
819          } catch (IllegalArgumentException success) {}
820      }
# Line 557 | Line 824 | public class ThreadPoolExecutorSubclassT
824       */
825      public void testConstructor3() {
826          try {
827 <            new CustomTPE(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
827 >            new CustomTPE(1, 0, 1L, SECONDS,
828 >                          new ArrayBlockingQueue<Runnable>(10));
829              shouldThrow();
830          } catch (IllegalArgumentException success) {}
831      }
# Line 567 | Line 835 | public class ThreadPoolExecutorSubclassT
835       */
836      public void testConstructor4() {
837          try {
838 <            new CustomTPE(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
838 >            new CustomTPE(1, 2, -1L, SECONDS,
839 >                          new ArrayBlockingQueue<Runnable>(10));
840              shouldThrow();
841          } catch (IllegalArgumentException success) {}
842      }
# Line 577 | Line 846 | public class ThreadPoolExecutorSubclassT
846       */
847      public void testConstructor5() {
848          try {
849 <            new CustomTPE(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
849 >            new CustomTPE(2, 1, 1L, SECONDS,
850 >                          new ArrayBlockingQueue<Runnable>(10));
851              shouldThrow();
852          } catch (IllegalArgumentException success) {}
853      }
# Line 587 | Line 857 | public class ThreadPoolExecutorSubclassT
857       */
858      public void testConstructorNullPointerException() {
859          try {
860 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null);
860 >            new CustomTPE(1, 2, 1L, SECONDS, null);
861              shouldThrow();
862          } catch (NullPointerException success) {}
863      }
864  
595
596
865      /**
866       * Constructor throws if corePoolSize argument is less than zero
867       */
868      public void testConstructor6() {
869          try {
870 <            new CustomTPE(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
870 >            new CustomTPE(-1, 1, 1L, SECONDS,
871 >                          new ArrayBlockingQueue<Runnable>(10),
872 >                          new SimpleThreadFactory());
873              shouldThrow();
874          } catch (IllegalArgumentException success) {}
875      }
# Line 609 | Line 879 | public class ThreadPoolExecutorSubclassT
879       */
880      public void testConstructor7() {
881          try {
882 <            new CustomTPE(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
882 >            new CustomTPE(1,-1, 1L, SECONDS,
883 >                          new ArrayBlockingQueue<Runnable>(10),
884 >                          new SimpleThreadFactory());
885              shouldThrow();
886          } catch (IllegalArgumentException success) {}
887      }
# Line 619 | Line 891 | public class ThreadPoolExecutorSubclassT
891       */
892      public void testConstructor8() {
893          try {
894 <            new CustomTPE(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
894 >            new CustomTPE(1, 0, 1L, SECONDS,
895 >                          new ArrayBlockingQueue<Runnable>(10),
896 >                          new SimpleThreadFactory());
897              shouldThrow();
898          } catch (IllegalArgumentException success) {}
899      }
# Line 629 | Line 903 | public class ThreadPoolExecutorSubclassT
903       */
904      public void testConstructor9() {
905          try {
906 <            new CustomTPE(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
906 >            new CustomTPE(1, 2, -1L, SECONDS,
907 >                          new ArrayBlockingQueue<Runnable>(10),
908 >                          new SimpleThreadFactory());
909              shouldThrow();
910          } catch (IllegalArgumentException success) {}
911      }
# Line 639 | Line 915 | public class ThreadPoolExecutorSubclassT
915       */
916      public void testConstructor10() {
917          try {
918 <            new CustomTPE(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
918 >            new CustomTPE(2, 1, 1L, SECONDS,
919 >                          new ArrayBlockingQueue<Runnable>(10),
920 >                          new SimpleThreadFactory());
921              shouldThrow();
922          } catch (IllegalArgumentException success) {}
923      }
# Line 649 | Line 927 | public class ThreadPoolExecutorSubclassT
927       */
928      public void testConstructorNullPointerException2() {
929          try {
930 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory());
930 >            new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory());
931              shouldThrow();
932          } catch (NullPointerException success) {}
933      }
# Line 659 | Line 937 | public class ThreadPoolExecutorSubclassT
937       */
938      public void testConstructorNullPointerException3() {
939          try {
940 <            ThreadFactory f = null;
941 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
940 >            new CustomTPE(1, 2, 1L, SECONDS,
941 >                          new ArrayBlockingQueue<Runnable>(10),
942 >                          (ThreadFactory) null);
943              shouldThrow();
944          } catch (NullPointerException success) {}
945      }
946  
668
947      /**
948       * Constructor throws if corePoolSize argument is less than zero
949       */
950      public void testConstructor11() {
951          try {
952 <            new CustomTPE(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
952 >            new CustomTPE(-1, 1, 1L, SECONDS,
953 >                          new ArrayBlockingQueue<Runnable>(10),
954 >                          new NoOpREHandler());
955              shouldThrow();
956          } catch (IllegalArgumentException success) {}
957      }
# Line 681 | Line 961 | public class ThreadPoolExecutorSubclassT
961       */
962      public void testConstructor12() {
963          try {
964 <            new CustomTPE(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
964 >            new CustomTPE(1, -1, 1L, SECONDS,
965 >                          new ArrayBlockingQueue<Runnable>(10),
966 >                          new NoOpREHandler());
967              shouldThrow();
968          } catch (IllegalArgumentException success) {}
969      }
# Line 691 | Line 973 | public class ThreadPoolExecutorSubclassT
973       */
974      public void testConstructor13() {
975          try {
976 <            new CustomTPE(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
976 >            new CustomTPE(1, 0, 1L, SECONDS,
977 >                          new ArrayBlockingQueue<Runnable>(10),
978 >                          new NoOpREHandler());
979              shouldThrow();
980          } catch (IllegalArgumentException success) {}
981      }
# Line 701 | Line 985 | public class ThreadPoolExecutorSubclassT
985       */
986      public void testConstructor14() {
987          try {
988 <            new CustomTPE(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
988 >            new CustomTPE(1, 2, -1L, SECONDS,
989 >                          new ArrayBlockingQueue<Runnable>(10),
990 >                          new NoOpREHandler());
991              shouldThrow();
992          } catch (IllegalArgumentException success) {}
993      }
# Line 711 | Line 997 | public class ThreadPoolExecutorSubclassT
997       */
998      public void testConstructor15() {
999          try {
1000 <            new CustomTPE(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
1000 >            new CustomTPE(2, 1, 1L, SECONDS,
1001 >                          new ArrayBlockingQueue<Runnable>(10),
1002 >                          new NoOpREHandler());
1003              shouldThrow();
1004          } catch (IllegalArgumentException success) {}
1005      }
# Line 721 | Line 1009 | public class ThreadPoolExecutorSubclassT
1009       */
1010      public void testConstructorNullPointerException4() {
1011          try {
1012 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new NoOpREHandler());
1012 >            new CustomTPE(1, 2, 1L, SECONDS,
1013 >                          null,
1014 >                          new NoOpREHandler());
1015              shouldThrow();
1016          } catch (NullPointerException success) {}
1017      }
# Line 731 | Line 1021 | public class ThreadPoolExecutorSubclassT
1021       */
1022      public void testConstructorNullPointerException5() {
1023          try {
1024 <            RejectedExecutionHandler r = null;
1025 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
1024 >            new CustomTPE(1, 2, 1L, SECONDS,
1025 >                          new ArrayBlockingQueue<Runnable>(10),
1026 >                          (RejectedExecutionHandler) null);
1027              shouldThrow();
1028          } catch (NullPointerException success) {}
1029      }
1030  
740
1031      /**
1032       * Constructor throws if corePoolSize argument is less than zero
1033       */
1034      public void testConstructor16() {
1035          try {
1036 <            new CustomTPE(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1036 >            new CustomTPE(-1, 1, 1L, SECONDS,
1037 >                          new ArrayBlockingQueue<Runnable>(10),
1038 >                          new SimpleThreadFactory(),
1039 >                          new NoOpREHandler());
1040              shouldThrow();
1041          } catch (IllegalArgumentException success) {}
1042      }
# Line 753 | Line 1046 | public class ThreadPoolExecutorSubclassT
1046       */
1047      public void testConstructor17() {
1048          try {
1049 <            new CustomTPE(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1049 >            new CustomTPE(1, -1, 1L, SECONDS,
1050 >                          new ArrayBlockingQueue<Runnable>(10),
1051 >                          new SimpleThreadFactory(),
1052 >                          new NoOpREHandler());
1053              shouldThrow();
1054          } catch (IllegalArgumentException success) {}
1055      }
# Line 763 | Line 1059 | public class ThreadPoolExecutorSubclassT
1059       */
1060      public void testConstructor18() {
1061          try {
1062 <            new CustomTPE(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1062 >            new CustomTPE(1, 0, 1L, SECONDS,
1063 >                          new ArrayBlockingQueue<Runnable>(10),
1064 >                          new SimpleThreadFactory(),
1065 >                          new NoOpREHandler());
1066              shouldThrow();
1067          } catch (IllegalArgumentException success) {}
1068      }
# Line 773 | Line 1072 | public class ThreadPoolExecutorSubclassT
1072       */
1073      public void testConstructor19() {
1074          try {
1075 <            new CustomTPE(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1075 >            new CustomTPE(1, 2, -1L, SECONDS,
1076 >                          new ArrayBlockingQueue<Runnable>(10),
1077 >                          new SimpleThreadFactory(),
1078 >                          new NoOpREHandler());
1079              shouldThrow();
1080          } catch (IllegalArgumentException success) {}
1081      }
# Line 783 | Line 1085 | public class ThreadPoolExecutorSubclassT
1085       */
1086      public void testConstructor20() {
1087          try {
1088 <            new CustomTPE(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1088 >            new CustomTPE(2, 1, 1L, SECONDS,
1089 >                          new ArrayBlockingQueue<Runnable>(10),
1090 >                          new SimpleThreadFactory(),
1091 >                          new NoOpREHandler());
1092              shouldThrow();
1093          } catch (IllegalArgumentException success) {}
1094      }
1095  
1096      /**
1097 <     * Constructor throws if workQueue is set to null
1097 >     * Constructor throws if workQueue is null
1098       */
1099      public void testConstructorNullPointerException6() {
1100          try {
1101 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
1101 >            new CustomTPE(1, 2, 1L, SECONDS,
1102 >                          null,
1103 >                          new SimpleThreadFactory(),
1104 >                          new NoOpREHandler());
1105              shouldThrow();
1106          } catch (NullPointerException success) {}
1107      }
1108  
1109      /**
1110 <     * Constructor throws if handler is set to null
1110 >     * Constructor throws if handler is null
1111       */
1112      public void testConstructorNullPointerException7() {
1113          try {
1114 <            RejectedExecutionHandler r = null;
1115 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
1114 >            new CustomTPE(1, 2, 1L, SECONDS,
1115 >                          new ArrayBlockingQueue<Runnable>(10),
1116 >                          new SimpleThreadFactory(),
1117 >                          (RejectedExecutionHandler) null);
1118              shouldThrow();
1119          } catch (NullPointerException success) {}
1120      }
1121  
1122      /**
1123 <     * Constructor throws if ThreadFactory is set top null
1123 >     * Constructor throws if ThreadFactory is null
1124       */
1125      public void testConstructorNullPointerException8() {
1126          try {
1127 <            ThreadFactory f = null;
1128 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
1127 >            new CustomTPE(1, 2, 1L, SECONDS,
1128 >                          new ArrayBlockingQueue<Runnable>(10),
1129 >                          (ThreadFactory) null,
1130 >                          new NoOpREHandler());
1131              shouldThrow();
1132          } catch (NullPointerException success) {}
1133      }
1134  
823
1135      /**
1136 <     *  execute throws RejectedExecutionException
826 <     *  if saturated.
1136 >     * execute throws RejectedExecutionException if saturated.
1137       */
1138      public void testSaturatedExecute() {
1139 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
1140 <        try {
1141 <
1142 <            for (int i = 0; i < 5; ++i) {
1143 <                p.execute(new MediumRunnable());
1139 >        final ThreadPoolExecutor p =
1140 >            new CustomTPE(1, 1,
1141 >                          LONG_DELAY_MS, MILLISECONDS,
1142 >                          new ArrayBlockingQueue<Runnable>(1));
1143 >        try (PoolCleaner cleaner = cleaner(p)) {
1144 >            final CountDownLatch done = new CountDownLatch(1);
1145 >            Runnable task = new CheckedRunnable() {
1146 >                public void realRun() throws InterruptedException {
1147 >                    done.await();
1148 >                }};
1149 >            for (int i = 0; i < 2; ++i)
1150 >                p.execute(task);
1151 >            for (int i = 0; i < 2; ++i) {
1152 >                try {
1153 >                    p.execute(task);
1154 >                    shouldThrow();
1155 >                } catch (RejectedExecutionException success) {}
1156 >                assertTrue(p.getTaskCount() <= 2);
1157              }
1158 <            shouldThrow();
1159 <        } catch (RejectedExecutionException success) {}
837 <        joinPool(p);
1158 >            done.countDown();
1159 >        }
1160      }
1161  
1162      /**
1163 <     *  executor using CallerRunsPolicy runs task if saturated.
1163 >     * executor using CallerRunsPolicy runs task if saturated.
1164       */
1165      public void testSaturatedExecute2() {
1166 <        RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1167 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1168 <        try {
1169 <
1166 >        final ThreadPoolExecutor p =
1167 >            new CustomTPE(1, 1,
1168 >                          LONG_DELAY_MS, MILLISECONDS,
1169 >                          new ArrayBlockingQueue<Runnable>(1),
1170 >                          new CustomTPE.CallerRunsPolicy());
1171 >        try (PoolCleaner cleaner = cleaner(p)) {
1172 >            final CountDownLatch done = new CountDownLatch(1);
1173 >            Runnable blocker = new CheckedRunnable() {
1174 >                public void realRun() throws InterruptedException {
1175 >                    done.await();
1176 >                }};
1177 >            p.execute(blocker);
1178              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1179 <            for (int i = 0; i < 5; ++i) {
1179 >            for (int i = 0; i < tasks.length; i++)
1180                  tasks[i] = new TrackedNoOpRunnable();
1181 <            }
852 <            TrackedLongRunnable mr = new TrackedLongRunnable();
853 <            p.execute(mr);
854 <            for (int i = 0; i < 5; ++i) {
1181 >            for (int i = 0; i < tasks.length; i++)
1182                  p.execute(tasks[i]);
1183 <            }
857 <            for (int i = 1; i < 5; ++i) {
1183 >            for (int i = 1; i < tasks.length; i++)
1184                  assertTrue(tasks[i].done);
1185 <            }
1186 <            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
861 <        } finally {
862 <            joinPool(p);
1185 >            assertFalse(tasks[0].done); // waiting in queue
1186 >            done.countDown();
1187          }
1188      }
1189  
1190      /**
1191 <     *  executor using DiscardPolicy drops task if saturated.
1191 >     * executor using DiscardPolicy drops task if saturated.
1192       */
1193      public void testSaturatedExecute3() {
1194 <        RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1195 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1196 <        try {
1197 <
1198 <            TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1199 <            for (int i = 0; i < 5; ++i) {
1200 <                tasks[i] = new TrackedNoOpRunnable();
1201 <            }
1202 <            p.execute(new TrackedLongRunnable());
1203 <            for (int i = 0; i < 5; ++i) {
1204 <                p.execute(tasks[i]);
1205 <            }
1206 <            for (int i = 0; i < 5; ++i) {
1194 >        final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1195 >        for (int i = 0; i < tasks.length; ++i)
1196 >            tasks[i] = new TrackedNoOpRunnable();
1197 >        final ThreadPoolExecutor p =
1198 >            new CustomTPE(1, 1,
1199 >                          LONG_DELAY_MS, MILLISECONDS,
1200 >                          new ArrayBlockingQueue<Runnable>(1),
1201 >                          new CustomTPE.DiscardPolicy());
1202 >        try (PoolCleaner cleaner = cleaner(p)) {
1203 >            final CountDownLatch done = new CountDownLatch(1);
1204 >            p.execute(awaiter(done));
1205 >
1206 >            for (TrackedNoOpRunnable task : tasks)
1207 >                p.execute(task);
1208 >            for (int i = 1; i < tasks.length; i++)
1209                  assertFalse(tasks[i].done);
1210 <            }
885 <            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
886 <        } finally {
887 <            joinPool(p);
1210 >            done.countDown();
1211          }
1212 +        for (int i = 1; i < tasks.length; i++)
1213 +            assertFalse(tasks[i].done);
1214 +        assertTrue(tasks[0].done); // was waiting in queue
1215      }
1216  
1217      /**
1218 <     *  executor using DiscardOldestPolicy drops oldest task if saturated.
1218 >     * executor using DiscardOldestPolicy drops oldest task if saturated.
1219       */
1220      public void testSaturatedExecute4() {
1221 <        RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
1222 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1223 <        try {
1224 <            p.execute(new TrackedLongRunnable());
1225 <            TrackedLongRunnable r2 = new TrackedLongRunnable();
1221 >        final CountDownLatch done = new CountDownLatch(1);
1222 >        LatchAwaiter r1 = awaiter(done);
1223 >        LatchAwaiter r2 = awaiter(done);
1224 >        LatchAwaiter r3 = awaiter(done);
1225 >        final ThreadPoolExecutor p =
1226 >            new CustomTPE(1, 1,
1227 >                          LONG_DELAY_MS, MILLISECONDS,
1228 >                          new ArrayBlockingQueue<Runnable>(1),
1229 >                          new CustomTPE.DiscardOldestPolicy());
1230 >        try (PoolCleaner cleaner = cleaner(p)) {
1231 >            assertEquals(LatchAwaiter.NEW, r1.state);
1232 >            assertEquals(LatchAwaiter.NEW, r2.state);
1233 >            assertEquals(LatchAwaiter.NEW, r3.state);
1234 >            p.execute(r1);
1235              p.execute(r2);
1236              assertTrue(p.getQueue().contains(r2));
902            TrackedNoOpRunnable r3 = new TrackedNoOpRunnable();
1237              p.execute(r3);
1238              assertFalse(p.getQueue().contains(r2));
1239              assertTrue(p.getQueue().contains(r3));
1240 <            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
907 <        } finally {
908 <            joinPool(p);
1240 >            done.countDown();
1241          }
1242 +        assertEquals(LatchAwaiter.DONE, r1.state);
1243 +        assertEquals(LatchAwaiter.NEW, r2.state);
1244 +        assertEquals(LatchAwaiter.DONE, r3.state);
1245      }
1246  
1247      /**
1248 <     *  execute throws RejectedExecutionException if shutdown
1248 >     * execute throws RejectedExecutionException if shutdown
1249       */
1250      public void testRejectedExecutionExceptionOnShutdown() {
1251 <        ThreadPoolExecutor tpe =
1252 <            new CustomTPE(1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
1253 <        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1254 <        try {
1255 <            tpe.execute(new NoOpRunnable());
1256 <            shouldThrow();
1257 <        } catch (RejectedExecutionException success) {}
1258 <
1259 <        joinPool(tpe);
1251 >        final ThreadPoolExecutor p =
1252 >            new CustomTPE(1, 1,
1253 >                          LONG_DELAY_MS, MILLISECONDS,
1254 >                          new ArrayBlockingQueue<Runnable>(1));
1255 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1256 >        try (PoolCleaner cleaner = cleaner(p)) {
1257 >            try {
1258 >                p.execute(new NoOpRunnable());
1259 >                shouldThrow();
1260 >            } catch (RejectedExecutionException success) {}
1261 >        }
1262      }
1263  
1264      /**
1265 <     *  execute using CallerRunsPolicy drops task on shutdown
1265 >     * execute using CallerRunsPolicy drops task on shutdown
1266       */
1267      public void testCallerRunsOnShutdown() {
1268 <        RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1269 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1270 <
1268 >        final ThreadPoolExecutor p =
1269 >            new CustomTPE(1, 1,
1270 >                          LONG_DELAY_MS, MILLISECONDS,
1271 >                          new ArrayBlockingQueue<Runnable>(1),
1272 >                          new CustomTPE.CallerRunsPolicy());
1273          try { p.shutdown(); } catch (SecurityException ok) { return; }
1274 <        try {
1274 >        try (PoolCleaner cleaner = cleaner(p)) {
1275              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1276 <            p.execute(r);
1276 >            p.execute(r);
1277              assertFalse(r.done);
939        } finally {
940            joinPool(p);
1278          }
1279      }
1280  
1281      /**
1282 <     *  execute using DiscardPolicy drops task on shutdown
1282 >     * execute using DiscardPolicy drops task on shutdown
1283       */
1284      public void testDiscardOnShutdown() {
1285 <        RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1286 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1287 <
1285 >        final ThreadPoolExecutor p =
1286 >            new CustomTPE(1, 1,
1287 >                          LONG_DELAY_MS, MILLISECONDS,
1288 >                          new ArrayBlockingQueue<Runnable>(1),
1289 >                          new CustomTPE.DiscardPolicy());
1290          try { p.shutdown(); } catch (SecurityException ok) { return; }
1291 <        try {
1291 >        try (PoolCleaner cleaner = cleaner(p)) {
1292              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1293 <            p.execute(r);
1293 >            p.execute(r);
1294              assertFalse(r.done);
956        } finally {
957            joinPool(p);
1295          }
1296      }
1297  
961
1298      /**
1299 <     *  execute using DiscardOldestPolicy drops task on shutdown
1299 >     * execute using DiscardOldestPolicy drops task on shutdown
1300       */
1301      public void testDiscardOldestOnShutdown() {
1302 <        RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
1303 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1302 >        final ThreadPoolExecutor p =
1303 >            new CustomTPE(1, 1,
1304 >                          LONG_DELAY_MS, MILLISECONDS,
1305 >                          new ArrayBlockingQueue<Runnable>(1),
1306 >                          new CustomTPE.DiscardOldestPolicy());
1307  
1308          try { p.shutdown(); } catch (SecurityException ok) { return; }
1309 <        try {
1309 >        try (PoolCleaner cleaner = cleaner(p)) {
1310              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1311 <            p.execute(r);
1311 >            p.execute(r);
1312              assertFalse(r.done);
974        } finally {
975            joinPool(p);
1313          }
1314      }
1315  
979
1316      /**
1317 <     *  execute (null) throws NPE
1317 >     * execute(null) throws NPE
1318       */
1319      public void testExecuteNull() {
1320 <        ThreadPoolExecutor tpe = null;
1321 <        try {
1322 <            tpe = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1323 <            tpe.execute(null);
1324 <            shouldThrow();
1325 <        } catch (NullPointerException success) {}
1326 <
1327 <        joinPool(tpe);
1320 >        final ThreadPoolExecutor p =
1321 >            new CustomTPE(1, 2,
1322 >                          1L, SECONDS,
1323 >                          new ArrayBlockingQueue<Runnable>(10));
1324 >        try (PoolCleaner cleaner = cleaner(p)) {
1325 >            try {
1326 >                p.execute(null);
1327 >                shouldThrow();
1328 >            } catch (NullPointerException success) {}
1329 >        }
1330      }
1331  
1332      /**
1333 <     *  setCorePoolSize of negative value throws IllegalArgumentException
1333 >     * setCorePoolSize of negative value throws IllegalArgumentException
1334       */
1335      public void testCorePoolSizeIllegalArgumentException() {
1336 <        ThreadPoolExecutor tpe = null;
1337 <        try {
1338 <            tpe = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1339 <        } catch (Exception e) {}
1340 <        try {
1341 <            tpe.setCorePoolSize(-1);
1342 <            shouldThrow();
1343 <        } catch (IllegalArgumentException success) {
1344 <        } finally {
1007 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1336 >        final ThreadPoolExecutor p =
1337 >            new CustomTPE(1, 2,
1338 >                          LONG_DELAY_MS, MILLISECONDS,
1339 >                          new ArrayBlockingQueue<Runnable>(10));
1340 >        try (PoolCleaner cleaner = cleaner(p)) {
1341 >            try {
1342 >                p.setCorePoolSize(-1);
1343 >                shouldThrow();
1344 >            } catch (IllegalArgumentException success) {}
1345          }
1009        joinPool(tpe);
1346      }
1347  
1348      /**
1349 <     *  setMaximumPoolSize(int) throws IllegalArgumentException if
1350 <     *  given a value less the core pool size
1349 >     * setMaximumPoolSize(int) throws IllegalArgumentException
1350 >     * if given a value less the core pool size
1351       */
1352      public void testMaximumPoolSizeIllegalArgumentException() {
1353 <        ThreadPoolExecutor tpe = null;
1354 <        try {
1355 <            tpe = new CustomTPE(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1356 <        } catch (Exception e) {}
1357 <        try {
1358 <            tpe.setMaximumPoolSize(1);
1359 <            shouldThrow();
1360 <        } catch (IllegalArgumentException success) {
1361 <        } finally {
1026 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1353 >        final ThreadPoolExecutor p =
1354 >            new CustomTPE(2, 3,
1355 >                          LONG_DELAY_MS, MILLISECONDS,
1356 >                          new ArrayBlockingQueue<Runnable>(10));
1357 >        try (PoolCleaner cleaner = cleaner(p)) {
1358 >            try {
1359 >                p.setMaximumPoolSize(1);
1360 >                shouldThrow();
1361 >            } catch (IllegalArgumentException success) {}
1362          }
1028        joinPool(tpe);
1363      }
1364  
1365      /**
1366 <     *  setMaximumPoolSize throws IllegalArgumentException
1367 <     *  if given a negative value
1366 >     * setMaximumPoolSize throws IllegalArgumentException
1367 >     * if given a negative value
1368       */
1369      public void testMaximumPoolSizeIllegalArgumentException2() {
1370 <        ThreadPoolExecutor tpe = null;
1371 <        try {
1372 <            tpe = new CustomTPE(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1373 <        } catch (Exception e) {}
1374 <        try {
1375 <            tpe.setMaximumPoolSize(-1);
1376 <            shouldThrow();
1377 <        } catch (IllegalArgumentException success) {
1378 <        } finally {
1045 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1370 >        final ThreadPoolExecutor p =
1371 >            new CustomTPE(2, 3,
1372 >                          LONG_DELAY_MS,
1373 >                          MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1374 >        try (PoolCleaner cleaner = cleaner(p)) {
1375 >            try {
1376 >                p.setMaximumPoolSize(-1);
1377 >                shouldThrow();
1378 >            } catch (IllegalArgumentException success) {}
1379          }
1047        joinPool(tpe);
1380      }
1381  
1050
1382      /**
1383 <     *  setKeepAliveTime  throws IllegalArgumentException
1384 <     *  when given a negative value
1383 >     * setKeepAliveTime throws IllegalArgumentException
1384 >     * when given a negative value
1385       */
1386      public void testKeepAliveTimeIllegalArgumentException() {
1387 <        ThreadPoolExecutor tpe = null;
1388 <        try {
1389 <            tpe = new CustomTPE(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1390 <        } catch (Exception e) {}
1391 <
1392 <        try {
1393 <            tpe.setKeepAliveTime(-1,TimeUnit.MILLISECONDS);
1394 <            shouldThrow();
1395 <        } catch (IllegalArgumentException success) {
1065 <        } finally {
1066 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1387 >        final ThreadPoolExecutor p =
1388 >            new CustomTPE(2, 3,
1389 >                          LONG_DELAY_MS, MILLISECONDS,
1390 >                          new ArrayBlockingQueue<Runnable>(10));
1391 >        try (PoolCleaner cleaner = cleaner(p)) {
1392 >            try {
1393 >                p.setKeepAliveTime(-1, MILLISECONDS);
1394 >                shouldThrow();
1395 >            } catch (IllegalArgumentException success) {}
1396          }
1068        joinPool(tpe);
1397      }
1398  
1399      /**
1400       * terminated() is called on termination
1401       */
1402      public void testTerminated() {
1403 <        CustomTPE tpe = new CustomTPE();
1404 <        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1405 <        assertTrue(tpe.terminatedCalled);
1406 <        joinPool(tpe);
1403 >        CustomTPE p = new CustomTPE();
1404 >        try (PoolCleaner cleaner = cleaner(p)) {
1405 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1406 >            assertTrue(p.terminatedCalled());
1407 >            assertTrue(p.isShutdown());
1408 >        }
1409      }
1410  
1411      /**
1412       * beforeExecute and afterExecute are called when executing task
1413       */
1414      public void testBeforeAfter() throws InterruptedException {
1415 <        CustomTPE tpe = new CustomTPE();
1416 <        try {
1417 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1418 <            tpe.execute(r);
1419 <            Thread.sleep(SHORT_DELAY_MS);
1420 <            assertTrue(r.done);
1421 <            assertTrue(tpe.beforeCalled);
1422 <            assertTrue(tpe.afterCalled);
1423 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1424 <        } finally {
1425 <            joinPool(tpe);
1415 >        CustomTPE p = new CustomTPE();
1416 >        try (PoolCleaner cleaner = cleaner(p)) {
1417 >            final CountDownLatch done = new CountDownLatch(1);
1418 >            p.execute(new CheckedRunnable() {
1419 >                public void realRun() {
1420 >                    done.countDown();
1421 >                }});
1422 >            await(p.afterCalled);
1423 >            assertEquals(0, done.getCount());
1424 >            assertTrue(p.afterCalled());
1425 >            assertTrue(p.beforeCalled());
1426          }
1427      }
1428  
# Line 1100 | Line 1430 | public class ThreadPoolExecutorSubclassT
1430       * completed submit of callable returns result
1431       */
1432      public void testSubmitCallable() throws Exception {
1433 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1434 <        try {
1433 >        final ExecutorService e =
1434 >            new CustomTPE(2, 2,
1435 >                          LONG_DELAY_MS, MILLISECONDS,
1436 >                          new ArrayBlockingQueue<Runnable>(10));
1437 >        try (PoolCleaner cleaner = cleaner(e)) {
1438              Future<String> future = e.submit(new StringTask());
1439              String result = future.get();
1440              assertSame(TEST_STRING, result);
1108        } finally {
1109            joinPool(e);
1441          }
1442      }
1443  
# Line 1114 | Line 1445 | public class ThreadPoolExecutorSubclassT
1445       * completed submit of runnable returns successfully
1446       */
1447      public void testSubmitRunnable() throws Exception {
1448 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1449 <        try {
1448 >        final ExecutorService e =
1449 >            new CustomTPE(2, 2,
1450 >                          LONG_DELAY_MS, MILLISECONDS,
1451 >                          new ArrayBlockingQueue<Runnable>(10));
1452 >        try (PoolCleaner cleaner = cleaner(e)) {
1453              Future<?> future = e.submit(new NoOpRunnable());
1454              future.get();
1455              assertTrue(future.isDone());
1122        } finally {
1123            joinPool(e);
1456          }
1457      }
1458  
# Line 1128 | Line 1460 | public class ThreadPoolExecutorSubclassT
1460       * completed submit of (runnable, result) returns result
1461       */
1462      public void testSubmitRunnable2() throws Exception {
1463 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1464 <        try {
1463 >        final ExecutorService e =
1464 >            new CustomTPE(2, 2,
1465 >                          LONG_DELAY_MS, MILLISECONDS,
1466 >                          new ArrayBlockingQueue<Runnable>(10));
1467 >        try (PoolCleaner cleaner = cleaner(e)) {
1468              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1469              String result = future.get();
1470              assertSame(TEST_STRING, result);
1136        } finally {
1137            joinPool(e);
1471          }
1472      }
1473  
1141
1474      /**
1475       * invokeAny(null) throws NPE
1476       */
1477      public void testInvokeAny1() throws Exception {
1478 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1479 <        try {
1480 <            e.invokeAny(null);
1481 <            shouldThrow();
1482 <        } catch (NullPointerException success) {
1483 <        } finally {
1484 <            joinPool(e);
1478 >        final ExecutorService e =
1479 >            new CustomTPE(2, 2,
1480 >                          LONG_DELAY_MS, MILLISECONDS,
1481 >                          new ArrayBlockingQueue<Runnable>(10));
1482 >        try (PoolCleaner cleaner = cleaner(e)) {
1483 >            try {
1484 >                e.invokeAny(null);
1485 >                shouldThrow();
1486 >            } catch (NullPointerException success) {}
1487          }
1488      }
1489  
# Line 1157 | Line 1491 | public class ThreadPoolExecutorSubclassT
1491       * invokeAny(empty collection) throws IAE
1492       */
1493      public void testInvokeAny2() throws Exception {
1494 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1495 <        try {
1496 <            e.invokeAny(new ArrayList<Callable<String>>());
1497 <            shouldThrow();
1498 <        } catch (IllegalArgumentException success) {
1499 <        } finally {
1500 <            joinPool(e);
1494 >        final ExecutorService e =
1495 >            new CustomTPE(2, 2,
1496 >                          LONG_DELAY_MS, MILLISECONDS,
1497 >                          new ArrayBlockingQueue<Runnable>(10));
1498 >        try (PoolCleaner cleaner = cleaner(e)) {
1499 >            try {
1500 >                e.invokeAny(new ArrayList<Callable<String>>());
1501 >                shouldThrow();
1502 >            } catch (IllegalArgumentException success) {}
1503          }
1504      }
1505  
# Line 1171 | Line 1507 | public class ThreadPoolExecutorSubclassT
1507       * invokeAny(c) throws NPE if c has null elements
1508       */
1509      public void testInvokeAny3() throws Exception {
1510 <        final CountDownLatch latch = new CountDownLatch(1);
1511 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1512 <        try {
1513 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1514 <            l.add(new CheckedCallable<String>() {
1515 <                      public String realCall() throws InterruptedException {
1516 <                          latch.await();
1517 <                          return TEST_STRING;
1182 <                      }});
1510 >        CountDownLatch latch = new CountDownLatch(1);
1511 >        final ExecutorService e =
1512 >            new CustomTPE(2, 2,
1513 >                          LONG_DELAY_MS, MILLISECONDS,
1514 >                          new ArrayBlockingQueue<Runnable>(10));
1515 >        try (PoolCleaner cleaner = cleaner(e)) {
1516 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1517 >            l.add(latchAwaitingStringTask(latch));
1518              l.add(null);
1519 <            e.invokeAny(l);
1520 <            shouldThrow();
1521 <        } catch (NullPointerException success) {
1522 <        } finally {
1519 >            try {
1520 >                e.invokeAny(l);
1521 >                shouldThrow();
1522 >            } catch (NullPointerException success) {}
1523              latch.countDown();
1189            joinPool(e);
1524          }
1525      }
1526  
# Line 1194 | Line 1528 | public class ThreadPoolExecutorSubclassT
1528       * invokeAny(c) throws ExecutionException if no task completes
1529       */
1530      public void testInvokeAny4() throws Exception {
1531 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1532 <        try {
1533 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1531 >        final ExecutorService e =
1532 >            new CustomTPE(2, 2,
1533 >                          LONG_DELAY_MS, MILLISECONDS,
1534 >                          new ArrayBlockingQueue<Runnable>(10));
1535 >        try (PoolCleaner cleaner = cleaner(e)) {
1536 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1537              l.add(new NPETask());
1538 <            e.invokeAny(l);
1539 <            shouldThrow();
1540 <        } catch (ExecutionException success) {
1541 <        } finally {
1542 <            joinPool(e);
1538 >            try {
1539 >                e.invokeAny(l);
1540 >                shouldThrow();
1541 >            } catch (ExecutionException success) {
1542 >                assertTrue(success.getCause() instanceof NullPointerException);
1543 >            }
1544          }
1545      }
1546  
# Line 1210 | Line 1548 | public class ThreadPoolExecutorSubclassT
1548       * invokeAny(c) returns result of some task
1549       */
1550      public void testInvokeAny5() throws Exception {
1551 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1552 <        try {
1553 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1551 >        final ExecutorService e =
1552 >            new CustomTPE(2, 2,
1553 >                          LONG_DELAY_MS, MILLISECONDS,
1554 >                          new ArrayBlockingQueue<Runnable>(10));
1555 >        try (PoolCleaner cleaner = cleaner(e)) {
1556 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1557              l.add(new StringTask());
1558              l.add(new StringTask());
1559              String result = e.invokeAny(l);
1560              assertSame(TEST_STRING, result);
1220        } finally {
1221            joinPool(e);
1561          }
1562      }
1563  
# Line 1226 | Line 1565 | public class ThreadPoolExecutorSubclassT
1565       * invokeAll(null) throws NPE
1566       */
1567      public void testInvokeAll1() throws Exception {
1568 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1569 <        try {
1570 <            e.invokeAll(null);
1571 <            shouldThrow();
1572 <        } catch (NullPointerException success) {
1573 <        } finally {
1574 <            joinPool(e);
1568 >        final ExecutorService e =
1569 >            new CustomTPE(2, 2,
1570 >                          LONG_DELAY_MS, MILLISECONDS,
1571 >                          new ArrayBlockingQueue<Runnable>(10));
1572 >        try (PoolCleaner cleaner = cleaner(e)) {
1573 >            try {
1574 >                e.invokeAll(null);
1575 >                shouldThrow();
1576 >            } catch (NullPointerException success) {}
1577          }
1578      }
1579  
# Line 1240 | Line 1581 | public class ThreadPoolExecutorSubclassT
1581       * invokeAll(empty collection) returns empty collection
1582       */
1583      public void testInvokeAll2() throws Exception {
1584 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1585 <        try {
1584 >        final ExecutorService e =
1585 >            new CustomTPE(2, 2,
1586 >                          LONG_DELAY_MS, MILLISECONDS,
1587 >                          new ArrayBlockingQueue<Runnable>(10));
1588 >        try (PoolCleaner cleaner = cleaner(e)) {
1589              List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1590              assertTrue(r.isEmpty());
1247        } finally {
1248            joinPool(e);
1591          }
1592      }
1593  
# Line 1253 | Line 1595 | public class ThreadPoolExecutorSubclassT
1595       * invokeAll(c) throws NPE if c has null elements
1596       */
1597      public void testInvokeAll3() throws Exception {
1598 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1599 <        try {
1600 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1598 >        final ExecutorService e =
1599 >            new CustomTPE(2, 2,
1600 >                          LONG_DELAY_MS, MILLISECONDS,
1601 >                          new ArrayBlockingQueue<Runnable>(10));
1602 >        try (PoolCleaner cleaner = cleaner(e)) {
1603 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1604              l.add(new StringTask());
1605              l.add(null);
1606 <            e.invokeAll(l);
1607 <            shouldThrow();
1608 <        } catch (NullPointerException success) {
1609 <        } finally {
1265 <            joinPool(e);
1606 >            try {
1607 >                e.invokeAll(l);
1608 >                shouldThrow();
1609 >            } catch (NullPointerException success) {}
1610          }
1611      }
1612  
# Line 1270 | Line 1614 | public class ThreadPoolExecutorSubclassT
1614       * get of element of invokeAll(c) throws exception on failed task
1615       */
1616      public void testInvokeAll4() throws Exception {
1617 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1618 <        try {
1619 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1617 >        final ExecutorService e =
1618 >            new CustomTPE(2, 2,
1619 >                          LONG_DELAY_MS, MILLISECONDS,
1620 >                          new ArrayBlockingQueue<Runnable>(10));
1621 >        try (PoolCleaner cleaner = cleaner(e)) {
1622 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1623              l.add(new NPETask());
1624 <            List<Future<String>> result = e.invokeAll(l);
1625 <            assertEquals(1, result.size());
1626 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1627 <                it.next().get();
1628 <            shouldThrow();
1629 <        } catch (ExecutionException success) {
1630 <        } finally {
1631 <            joinPool(e);
1624 >            List<Future<String>> futures = e.invokeAll(l);
1625 >            assertEquals(1, futures.size());
1626 >            try {
1627 >                futures.get(0).get();
1628 >                shouldThrow();
1629 >            } catch (ExecutionException success) {
1630 >                assertTrue(success.getCause() instanceof NullPointerException);
1631 >            }
1632          }
1633      }
1634  
# Line 1289 | Line 1636 | public class ThreadPoolExecutorSubclassT
1636       * invokeAll(c) returns results of all completed tasks
1637       */
1638      public void testInvokeAll5() throws Exception {
1639 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1640 <        try {
1641 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1639 >        final ExecutorService e =
1640 >            new CustomTPE(2, 2,
1641 >                          LONG_DELAY_MS, MILLISECONDS,
1642 >                          new ArrayBlockingQueue<Runnable>(10));
1643 >        try (PoolCleaner cleaner = cleaner(e)) {
1644 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1645              l.add(new StringTask());
1646              l.add(new StringTask());
1647 <            List<Future<String>> result = e.invokeAll(l);
1648 <            assertEquals(2, result.size());
1649 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1650 <                assertSame(TEST_STRING, it.next().get());
1301 <            shouldThrow();
1302 <        } catch (ExecutionException success) {
1303 <        } finally {
1304 <            joinPool(e);
1647 >            List<Future<String>> futures = e.invokeAll(l);
1648 >            assertEquals(2, futures.size());
1649 >            for (Future<String> future : futures)
1650 >                assertSame(TEST_STRING, future.get());
1651          }
1652      }
1653  
1308
1309
1654      /**
1655       * timed invokeAny(null) throws NPE
1656       */
1657      public void testTimedInvokeAny1() throws Exception {
1658 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1659 <        try {
1660 <            e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1661 <            shouldThrow();
1662 <        } catch (NullPointerException success) {
1663 <        } finally {
1664 <            joinPool(e);
1658 >        final ExecutorService e =
1659 >            new CustomTPE(2, 2,
1660 >                          LONG_DELAY_MS, MILLISECONDS,
1661 >                          new ArrayBlockingQueue<Runnable>(10));
1662 >        try (PoolCleaner cleaner = cleaner(e)) {
1663 >            try {
1664 >                e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1665 >                shouldThrow();
1666 >            } catch (NullPointerException success) {}
1667          }
1668      }
1669  
# Line 1325 | Line 1671 | public class ThreadPoolExecutorSubclassT
1671       * timed invokeAny(,,null) throws NPE
1672       */
1673      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1674 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1675 <        try {
1676 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1674 >        final ExecutorService e =
1675 >            new CustomTPE(2, 2,
1676 >                          LONG_DELAY_MS, MILLISECONDS,
1677 >                          new ArrayBlockingQueue<Runnable>(10));
1678 >        try (PoolCleaner cleaner = cleaner(e)) {
1679 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1680              l.add(new StringTask());
1681 <            e.invokeAny(l, MEDIUM_DELAY_MS, null);
1682 <            shouldThrow();
1683 <        } catch (NullPointerException success) {
1684 <        } finally {
1336 <            joinPool(e);
1681 >            try {
1682 >                e.invokeAny(l, MEDIUM_DELAY_MS, null);
1683 >                shouldThrow();
1684 >            } catch (NullPointerException success) {}
1685          }
1686      }
1687  
# Line 1341 | Line 1689 | public class ThreadPoolExecutorSubclassT
1689       * timed invokeAny(empty collection) throws IAE
1690       */
1691      public void testTimedInvokeAny2() throws Exception {
1692 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1693 <        try {
1694 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1695 <            shouldThrow();
1696 <        } catch (IllegalArgumentException success) {
1697 <        } finally {
1698 <            joinPool(e);
1692 >        final ExecutorService e =
1693 >            new CustomTPE(2, 2,
1694 >                          LONG_DELAY_MS, MILLISECONDS,
1695 >                          new ArrayBlockingQueue<Runnable>(10));
1696 >        try (PoolCleaner cleaner = cleaner(e)) {
1697 >            try {
1698 >                e.invokeAny(new ArrayList<Callable<String>>(),
1699 >                            MEDIUM_DELAY_MS, MILLISECONDS);
1700 >                shouldThrow();
1701 >            } catch (IllegalArgumentException success) {}
1702          }
1703      }
1704  
# Line 1355 | Line 1706 | public class ThreadPoolExecutorSubclassT
1706       * timed invokeAny(c) throws NPE if c has null elements
1707       */
1708      public void testTimedInvokeAny3() throws Exception {
1709 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1710 <        try {
1711 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1712 <            l.add(new StringTask());
1709 >        CountDownLatch latch = new CountDownLatch(1);
1710 >        final ExecutorService e =
1711 >            new CustomTPE(2, 2,
1712 >                          LONG_DELAY_MS, MILLISECONDS,
1713 >                          new ArrayBlockingQueue<Runnable>(10));
1714 >        try (PoolCleaner cleaner = cleaner(e)) {
1715 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1716 >            l.add(latchAwaitingStringTask(latch));
1717              l.add(null);
1718 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1719 <            shouldThrow();
1720 <        } catch (NullPointerException success) {
1721 <        } finally {
1722 <            joinPool(e);
1718 >            try {
1719 >                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1720 >                shouldThrow();
1721 >            } catch (NullPointerException success) {}
1722 >            latch.countDown();
1723          }
1724      }
1725  
# Line 1372 | Line 1727 | public class ThreadPoolExecutorSubclassT
1727       * timed invokeAny(c) throws ExecutionException if no task completes
1728       */
1729      public void testTimedInvokeAny4() throws Exception {
1730 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1731 <        try {
1732 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1730 >        final ExecutorService e =
1731 >            new CustomTPE(2, 2,
1732 >                          LONG_DELAY_MS, MILLISECONDS,
1733 >                          new ArrayBlockingQueue<Runnable>(10));
1734 >        try (PoolCleaner cleaner = cleaner(e)) {
1735 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1736              l.add(new NPETask());
1737 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1738 <            shouldThrow();
1739 <        } catch (ExecutionException success) {
1740 <        } finally {
1741 <            joinPool(e);
1737 >            try {
1738 >                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1739 >                shouldThrow();
1740 >            } catch (ExecutionException success) {
1741 >                assertTrue(success.getCause() instanceof NullPointerException);
1742 >            }
1743          }
1744      }
1745  
# Line 1388 | Line 1747 | public class ThreadPoolExecutorSubclassT
1747       * timed invokeAny(c) returns result of some task
1748       */
1749      public void testTimedInvokeAny5() throws Exception {
1750 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1751 <        try {
1752 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1750 >        final ExecutorService e =
1751 >            new CustomTPE(2, 2,
1752 >                          LONG_DELAY_MS, MILLISECONDS,
1753 >                          new ArrayBlockingQueue<Runnable>(10));
1754 >        try (PoolCleaner cleaner = cleaner(e)) {
1755 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1756              l.add(new StringTask());
1757              l.add(new StringTask());
1758 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1758 >            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1759              assertSame(TEST_STRING, result);
1398            shouldThrow();
1399        } catch (ExecutionException success) {
1400        } finally {
1401            joinPool(e);
1760          }
1761      }
1762  
# Line 1406 | Line 1764 | public class ThreadPoolExecutorSubclassT
1764       * timed invokeAll(null) throws NPE
1765       */
1766      public void testTimedInvokeAll1() throws Exception {
1767 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1768 <        try {
1769 <            e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1770 <            shouldThrow();
1771 <        } catch (NullPointerException success) {
1772 <        } finally {
1773 <            joinPool(e);
1767 >        final ExecutorService e =
1768 >            new CustomTPE(2, 2,
1769 >                          LONG_DELAY_MS, MILLISECONDS,
1770 >                          new ArrayBlockingQueue<Runnable>(10));
1771 >        try (PoolCleaner cleaner = cleaner(e)) {
1772 >            try {
1773 >                e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1774 >                shouldThrow();
1775 >            } catch (NullPointerException success) {}
1776          }
1777      }
1778  
# Line 1420 | Line 1780 | public class ThreadPoolExecutorSubclassT
1780       * timed invokeAll(,,null) throws NPE
1781       */
1782      public void testTimedInvokeAllNullTimeUnit() throws Exception {
1783 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1784 <        try {
1785 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1783 >        final ExecutorService e =
1784 >            new CustomTPE(2, 2,
1785 >                          LONG_DELAY_MS, MILLISECONDS,
1786 >                          new ArrayBlockingQueue<Runnable>(10));
1787 >        try (PoolCleaner cleaner = cleaner(e)) {
1788 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1789              l.add(new StringTask());
1790 <            e.invokeAll(l, MEDIUM_DELAY_MS, null);
1791 <            shouldThrow();
1792 <        } catch (NullPointerException success) {
1793 <        } finally {
1431 <            joinPool(e);
1790 >            try {
1791 >                e.invokeAll(l, MEDIUM_DELAY_MS, null);
1792 >                shouldThrow();
1793 >            } catch (NullPointerException success) {}
1794          }
1795      }
1796  
# Line 1436 | Line 1798 | public class ThreadPoolExecutorSubclassT
1798       * timed invokeAll(empty collection) returns empty collection
1799       */
1800      public void testTimedInvokeAll2() throws Exception {
1801 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1802 <        try {
1803 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1801 >        final ExecutorService e =
1802 >            new CustomTPE(2, 2,
1803 >                          LONG_DELAY_MS, MILLISECONDS,
1804 >                          new ArrayBlockingQueue<Runnable>(10));
1805 >        try (PoolCleaner cleaner = cleaner(e)) {
1806 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(),
1807 >                                                 MEDIUM_DELAY_MS, MILLISECONDS);
1808              assertTrue(r.isEmpty());
1443        } finally {
1444            joinPool(e);
1809          }
1810      }
1811  
# Line 1449 | Line 1813 | public class ThreadPoolExecutorSubclassT
1813       * timed invokeAll(c) throws NPE if c has null elements
1814       */
1815      public void testTimedInvokeAll3() throws Exception {
1816 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1817 <        try {
1818 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1816 >        final ExecutorService e =
1817 >            new CustomTPE(2, 2,
1818 >                          LONG_DELAY_MS, MILLISECONDS,
1819 >                          new ArrayBlockingQueue<Runnable>(10));
1820 >        try (PoolCleaner cleaner = cleaner(e)) {
1821 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1822              l.add(new StringTask());
1823              l.add(null);
1824 <            e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1825 <            shouldThrow();
1826 <        } catch (NullPointerException success) {
1827 <        } finally {
1461 <            joinPool(e);
1824 >            try {
1825 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1826 >                shouldThrow();
1827 >            } catch (NullPointerException success) {}
1828          }
1829      }
1830  
# Line 1466 | Line 1832 | public class ThreadPoolExecutorSubclassT
1832       * get of element of invokeAll(c) throws exception on failed task
1833       */
1834      public void testTimedInvokeAll4() throws Exception {
1835 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1836 <        try {
1837 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1835 >        final ExecutorService e =
1836 >            new CustomTPE(2, 2,
1837 >                          LONG_DELAY_MS, MILLISECONDS,
1838 >                          new ArrayBlockingQueue<Runnable>(10));
1839 >        try (PoolCleaner cleaner = cleaner(e)) {
1840 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1841              l.add(new NPETask());
1842 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1843 <            assertEquals(1, result.size());
1844 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1845 <                it.next().get();
1846 <            shouldThrow();
1847 <        } catch (ExecutionException success) {
1848 <        } finally {
1849 <            joinPool(e);
1842 >            List<Future<String>> futures =
1843 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1844 >            assertEquals(1, futures.size());
1845 >            try {
1846 >                futures.get(0).get();
1847 >                shouldThrow();
1848 >            } catch (ExecutionException success) {
1849 >                assertTrue(success.getCause() instanceof NullPointerException);
1850 >            }
1851          }
1852      }
1853  
# Line 1485 | Line 1855 | public class ThreadPoolExecutorSubclassT
1855       * timed invokeAll(c) returns results of all completed tasks
1856       */
1857      public void testTimedInvokeAll5() throws Exception {
1858 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1859 <        try {
1860 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1858 >        final ExecutorService e =
1859 >            new CustomTPE(2, 2,
1860 >                          LONG_DELAY_MS, MILLISECONDS,
1861 >                          new ArrayBlockingQueue<Runnable>(10));
1862 >        try (PoolCleaner cleaner = cleaner(e)) {
1863 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1864              l.add(new StringTask());
1865              l.add(new StringTask());
1866 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1867 <            assertEquals(2, result.size());
1868 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1869 <                assertSame(TEST_STRING, it.next().get());
1870 <            shouldThrow();
1498 <        } catch (ExecutionException success) {
1499 <        } finally {
1500 <            joinPool(e);
1866 >            List<Future<String>> futures =
1867 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1868 >            assertEquals(2, futures.size());
1869 >            for (Future<String> future : futures)
1870 >                assertSame(TEST_STRING, future.get());
1871          }
1872      }
1873  
# Line 1505 | Line 1875 | public class ThreadPoolExecutorSubclassT
1875       * timed invokeAll(c) cancels tasks not completed by timeout
1876       */
1877      public void testTimedInvokeAll6() throws Exception {
1878 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1879 <        try {
1880 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1881 <            l.add(new StringTask());
1882 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1883 <            l.add(new StringTask());
1884 <            List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
1885 <            assertEquals(3, result.size());
1886 <            Iterator<Future<String>> it = result.iterator();
1887 <            Future<String> f1 = it.next();
1888 <            Future<String> f2 = it.next();
1889 <            Future<String> f3 = it.next();
1890 <            assertTrue(f1.isDone());
1891 <            assertTrue(f2.isDone());
1892 <            assertTrue(f3.isDone());
1893 <            assertFalse(f1.isCancelled());
1894 <            assertTrue(f2.isCancelled());
1895 <        } finally {
1896 <            joinPool(e);
1878 >        final ExecutorService e =
1879 >            new CustomTPE(2, 2,
1880 >                          LONG_DELAY_MS, MILLISECONDS,
1881 >                          new ArrayBlockingQueue<Runnable>(10));
1882 >        try (PoolCleaner cleaner = cleaner(e)) {
1883 >            for (long timeout = timeoutMillis();;) {
1884 >                List<Callable<String>> tasks = new ArrayList<>();
1885 >                tasks.add(new StringTask("0"));
1886 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1887 >                tasks.add(new StringTask("2"));
1888 >                long startTime = System.nanoTime();
1889 >                List<Future<String>> futures =
1890 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1891 >                assertEquals(tasks.size(), futures.size());
1892 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1893 >                for (Future future : futures)
1894 >                    assertTrue(future.isDone());
1895 >                assertTrue(futures.get(1).isCancelled());
1896 >                try {
1897 >                    assertEquals("0", futures.get(0).get());
1898 >                    assertEquals("2", futures.get(2).get());
1899 >                    break;
1900 >                } catch (CancellationException retryWithLongerTimeout) {
1901 >                    timeout *= 2;
1902 >                    if (timeout >= LONG_DELAY_MS / 2)
1903 >                        fail("expected exactly one task to be cancelled");
1904 >                }
1905 >            }
1906          }
1907      }
1908  
# Line 1532 | Line 1911 | public class ThreadPoolExecutorSubclassT
1911       * thread factory fails to create more
1912       */
1913      public void testFailingThreadFactory() throws InterruptedException {
1914 <        ExecutorService e = new CustomTPE(100, 100, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory());
1915 <        try {
1916 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1917 <            for (int k = 0; k < 100; ++k) {
1918 <                e.execute(new NoOpRunnable());
1919 <            }
1920 <            Thread.sleep(LONG_DELAY_MS);
1921 <        } finally {
1922 <            joinPool(e);
1914 >        final ExecutorService e =
1915 >            new CustomTPE(100, 100,
1916 >                          LONG_DELAY_MS, MILLISECONDS,
1917 >                          new LinkedBlockingQueue<Runnable>(),
1918 >                          new FailingThreadFactory());
1919 >        try (PoolCleaner cleaner = cleaner(e)) {
1920 >            final int TASKS = 100;
1921 >            final CountDownLatch done = new CountDownLatch(TASKS);
1922 >            for (int k = 0; k < TASKS; ++k)
1923 >                e.execute(new CheckedRunnable() {
1924 >                    public void realRun() {
1925 >                        done.countDown();
1926 >                    }});
1927 >            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1928          }
1929      }
1930  
# Line 1548 | Line 1932 | public class ThreadPoolExecutorSubclassT
1932       * allowsCoreThreadTimeOut is by default false.
1933       */
1934      public void testAllowsCoreThreadTimeOut() {
1935 <        ThreadPoolExecutor tpe = new CustomTPE(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1936 <        assertFalse(tpe.allowsCoreThreadTimeOut());
1937 <        joinPool(tpe);
1935 >        final ThreadPoolExecutor p =
1936 >            new CustomTPE(2, 2,
1937 >                          1000, MILLISECONDS,
1938 >                          new ArrayBlockingQueue<Runnable>(10));
1939 >        try (PoolCleaner cleaner = cleaner(p)) {
1940 >            assertFalse(p.allowsCoreThreadTimeOut());
1941 >        }
1942      }
1943  
1944      /**
1945       * allowCoreThreadTimeOut(true) causes idle threads to time out
1946       */
1947 <    public void testAllowCoreThreadTimeOut_true() throws InterruptedException {
1948 <        ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1949 <        tpe.allowCoreThreadTimeOut(true);
1950 <        tpe.execute(new NoOpRunnable());
1951 <        try {
1952 <            Thread.sleep(MEDIUM_DELAY_MS);
1953 <            assertEquals(0, tpe.getPoolSize());
1954 <        } finally {
1955 <            joinPool(tpe);
1947 >    public void testAllowCoreThreadTimeOut_true() throws Exception {
1948 >        long keepAliveTime = timeoutMillis();
1949 >        final ThreadPoolExecutor p =
1950 >            new CustomTPE(2, 10,
1951 >                          keepAliveTime, MILLISECONDS,
1952 >                          new ArrayBlockingQueue<Runnable>(10));
1953 >        try (PoolCleaner cleaner = cleaner(p)) {
1954 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1955 >            p.allowCoreThreadTimeOut(true);
1956 >            p.execute(new CheckedRunnable() {
1957 >                public void realRun() {
1958 >                    threadStarted.countDown();
1959 >                    assertEquals(1, p.getPoolSize());
1960 >                }});
1961 >            await(threadStarted);
1962 >            delay(keepAliveTime);
1963 >            long startTime = System.nanoTime();
1964 >            while (p.getPoolSize() > 0
1965 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1966 >                Thread.yield();
1967 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1968 >            assertEquals(0, p.getPoolSize());
1969          }
1970      }
1971  
1972      /**
1973       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1974       */
1975 <    public void testAllowCoreThreadTimeOut_false() throws InterruptedException {
1976 <        ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1977 <        tpe.allowCoreThreadTimeOut(false);
1978 <        tpe.execute(new NoOpRunnable());
1979 <        try {
1980 <            Thread.sleep(MEDIUM_DELAY_MS);
1981 <            assertTrue(tpe.getPoolSize() >= 1);
1982 <        } finally {
1983 <            joinPool(tpe);
1975 >    public void testAllowCoreThreadTimeOut_false() throws Exception {
1976 >        long keepAliveTime = timeoutMillis();
1977 >        final ThreadPoolExecutor p =
1978 >            new CustomTPE(2, 10,
1979 >                          keepAliveTime, MILLISECONDS,
1980 >                          new ArrayBlockingQueue<Runnable>(10));
1981 >        try (PoolCleaner cleaner = cleaner(p)) {
1982 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1983 >            p.allowCoreThreadTimeOut(false);
1984 >            p.execute(new CheckedRunnable() {
1985 >                public void realRun() throws InterruptedException {
1986 >                    threadStarted.countDown();
1987 >                    assertTrue(p.getPoolSize() >= 1);
1988 >                }});
1989 >            delay(2 * keepAliveTime);
1990 >            assertTrue(p.getPoolSize() >= 1);
1991 >        }
1992 >    }
1993 >
1994 >    /**
1995 >     * get(cancelled task) throws CancellationException
1996 >     * (in part, a test of CustomTPE itself)
1997 >     */
1998 >    public void testGet_cancelled() throws Exception {
1999 >        final ExecutorService e =
2000 >            new CustomTPE(1, 1,
2001 >                          LONG_DELAY_MS, MILLISECONDS,
2002 >                          new LinkedBlockingQueue<Runnable>());
2003 >        try (PoolCleaner cleaner = cleaner(e)) {
2004 >            final CountDownLatch blockerStarted = new CountDownLatch(1);
2005 >            final CountDownLatch done = new CountDownLatch(1);
2006 >            final List<Future<?>> futures = new ArrayList<>();
2007 >            for (int i = 0; i < 2; i++) {
2008 >                Runnable r = new CheckedRunnable() { public void realRun()
2009 >                                                         throws Throwable {
2010 >                    blockerStarted.countDown();
2011 >                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
2012 >                }};
2013 >                futures.add(e.submit(r));
2014 >            }
2015 >            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
2016 >            for (Future<?> future : futures) future.cancel(false);
2017 >            for (Future<?> future : futures) {
2018 >                try {
2019 >                    future.get();
2020 >                    shouldThrow();
2021 >                } catch (CancellationException success) {}
2022 >                try {
2023 >                    future.get(LONG_DELAY_MS, MILLISECONDS);
2024 >                    shouldThrow();
2025 >                } catch (CancellationException success) {}
2026 >                assertTrue(future.isCancelled());
2027 >                assertTrue(future.isDone());
2028 >            }
2029 >            done.countDown();
2030          }
2031      }
2032  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines