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.20 by jsr166, Sat Oct 9 22:27:16 2010 UTC vs.
Revision 1.99 by jsr166, Mon May 29 22:44:27 2017 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.*;
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 < import java.util.concurrent.locks.*;
10 > import static java.util.concurrent.TimeUnit.SECONDS;
11  
12 < import junit.framework.*;
13 < import java.util.*;
12 > import java.util.ArrayList;
13 > import java.util.Collection;
14 > import java.util.Collections;
15 > import java.util.List;
16 > import java.util.concurrent.ArrayBlockingQueue;
17 > import java.util.concurrent.BlockingQueue;
18 > import java.util.concurrent.Callable;
19 > import java.util.concurrent.CancellationException;
20 > import java.util.concurrent.CountDownLatch;
21 > import java.util.concurrent.ExecutionException;
22 > import java.util.concurrent.ExecutorService;
23 > import java.util.concurrent.Future;
24 > import java.util.concurrent.FutureTask;
25 > import java.util.concurrent.LinkedBlockingQueue;
26 > import java.util.concurrent.RejectedExecutionException;
27 > import java.util.concurrent.RejectedExecutionHandler;
28 > import java.util.concurrent.RunnableFuture;
29 > import java.util.concurrent.SynchronousQueue;
30 > import java.util.concurrent.ThreadFactory;
31 > import java.util.concurrent.ThreadPoolExecutor;
32 > import java.util.concurrent.TimeoutException;
33 > import java.util.concurrent.TimeUnit;
34 > import java.util.concurrent.atomic.AtomicInteger;
35 > import java.util.concurrent.locks.Condition;
36 > import java.util.concurrent.locks.ReentrantLock;
37 >
38 > import junit.framework.Test;
39 > import junit.framework.TestSuite;
40  
41   public class ThreadPoolExecutorSubclassTest extends JSR166TestCase {
42      public static void main(String[] args) {
43 <        junit.textui.TestRunner.run(suite());
43 >        main(suite(), args);
44      }
45      public static Test suite() {
46          return new TestSuite(ThreadPoolExecutorSubclassTest.class);
# Line 37 | Line 62 | public class ThreadPoolExecutorSubclassT
62          CustomTask(final Runnable r, final V res) {
63              if (r == null) throw new NullPointerException();
64              callable = new Callable<V>() {
65 <            public V call() throws Exception { r.run(); return res; }};
65 >                public V call() throws Exception { r.run(); return res; }};
66          }
67          public boolean isDone() {
68              lock.lock(); try { return done; } finally { lock.unlock() ; }
# Line 60 | Line 85 | public class ThreadPoolExecutorSubclassT
85              finally { lock.unlock() ; }
86          }
87          public void run() {
63            boolean runme;
88              lock.lock();
89              try {
90 <                runme = !done;
91 <                if (!runme)
92 <                    thread = Thread.currentThread();
90 >                if (done)
91 >                    return;
92 >                thread = Thread.currentThread();
93              }
94              finally { lock.unlock() ; }
71            if (!runme) return;
95              V v = null;
96              Exception e = null;
97              try {
# Line 79 | Line 102 | public class ThreadPoolExecutorSubclassT
102              }
103              lock.lock();
104              try {
105 <                result = v;
106 <                exception = e;
107 <                done = true;
108 <                thread = null;
109 <                cond.signalAll();
105 >                if (!done) {
106 >                    result = v;
107 >                    exception = e;
108 >                    done = true;
109 >                    thread = null;
110 >                    cond.signalAll();
111 >                }
112              }
113              finally { lock.unlock(); }
114          }
# Line 92 | Line 117 | public class ThreadPoolExecutorSubclassT
117              try {
118                  while (!done)
119                      cond.await();
120 +                if (cancelled)
121 +                    throw new CancellationException();
122                  if (exception != null)
123                      throw new ExecutionException(exception);
124                  return result;
# Line 103 | Line 130 | public class ThreadPoolExecutorSubclassT
130              long nanos = unit.toNanos(timeout);
131              lock.lock();
132              try {
133 <                for (;;) {
134 <                    if (done) break;
108 <                    if (nanos < 0)
133 >                while (!done) {
134 >                    if (nanos <= 0L)
135                          throw new TimeoutException();
136                      nanos = cond.awaitNanos(nanos);
137                  }
138 +                if (cancelled)
139 +                    throw new CancellationException();
140                  if (exception != null)
141                      throw new ExecutionException(exception);
142                  return result;
# Line 117 | Line 145 | public class ThreadPoolExecutorSubclassT
145          }
146      }
147  
120
148      static class CustomTPE extends ThreadPoolExecutor {
149          protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
150              return new CustomTask<V>(c);
# Line 164 | Line 191 | public class ThreadPoolExecutorSubclassT
191                workQueue, threadFactory, handler);
192          }
193  
194 <        volatile boolean beforeCalled = false;
195 <        volatile boolean afterCalled = false;
196 <        volatile boolean terminatedCalled = false;
194 >        final CountDownLatch beforeCalled = new CountDownLatch(1);
195 >        final CountDownLatch afterCalled = new CountDownLatch(1);
196 >        final CountDownLatch terminatedCalled = new CountDownLatch(1);
197 >
198          public CustomTPE() {
199              super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
200          }
201          protected void beforeExecute(Thread t, Runnable r) {
202 <            beforeCalled = true;
202 >            beforeCalled.countDown();
203          }
204          protected void afterExecute(Runnable r, Throwable t) {
205 <            afterCalled = true;
205 >            afterCalled.countDown();
206          }
207          protected void terminated() {
208 <            terminatedCalled = true;
208 >            terminatedCalled.countDown();
209          }
210  
211 +        public boolean beforeCalled() {
212 +            return beforeCalled.getCount() == 0;
213 +        }
214 +        public boolean afterCalled() {
215 +            return afterCalled.getCount() == 0;
216 +        }
217 +        public boolean terminatedCalled() {
218 +            return terminatedCalled.getCount() == 0;
219 +        }
220      }
221  
222      static class FailingThreadFactory implements ThreadFactory {
# Line 190 | Line 227 | public class ThreadPoolExecutorSubclassT
227          }
228      }
229  
193
230      /**
231       * execute successfully executes a runnable
232       */
233      public void testExecute() throws InterruptedException {
234 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
235 <        try {
236 <            p1.execute(new ShortRunnable());
237 <            Thread.sleep(SMALL_DELAY_MS);
238 <        } finally {
239 <            joinPool(p1);
234 >        final ThreadPoolExecutor p =
235 >            new CustomTPE(1, 1,
236 >                          2 * LONG_DELAY_MS, MILLISECONDS,
237 >                          new ArrayBlockingQueue<Runnable>(10));
238 >        try (PoolCleaner cleaner = cleaner(p)) {
239 >            final CountDownLatch done = new CountDownLatch(1);
240 >            final Runnable task = new CheckedRunnable() {
241 >                public void realRun() { done.countDown(); }};
242 >            p.execute(task);
243 >            await(done);
244          }
245      }
246  
# Line 209 | Line 249 | public class ThreadPoolExecutorSubclassT
249       * thread becomes active
250       */
251      public void testGetActiveCount() throws InterruptedException {
252 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
253 <        assertEquals(0, p2.getActiveCount());
254 <        p2.execute(new MediumRunnable());
255 <        Thread.sleep(SHORT_DELAY_MS);
256 <        assertEquals(1, p2.getActiveCount());
257 <        joinPool(p2);
252 >        final CountDownLatch done = new CountDownLatch(1);
253 >        final ThreadPoolExecutor p =
254 >            new CustomTPE(2, 2,
255 >                          LONG_DELAY_MS, MILLISECONDS,
256 >                          new ArrayBlockingQueue<Runnable>(10));
257 >        try (PoolCleaner cleaner = cleaner(p, done)) {
258 >            final CountDownLatch threadStarted = new CountDownLatch(1);
259 >            assertEquals(0, p.getActiveCount());
260 >            p.execute(new CheckedRunnable() {
261 >                public void realRun() throws InterruptedException {
262 >                    threadStarted.countDown();
263 >                    assertEquals(1, p.getActiveCount());
264 >                    await(done);
265 >                }});
266 >            await(threadStarted);
267 >            assertEquals(1, p.getActiveCount());
268 >        }
269      }
270  
271      /**
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, 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
299       */
300      public void testPrestartAllCoreThreads() {
301 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, 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      /**
# Line 250 | Line 321 | public class ThreadPoolExecutorSubclassT
321       * when tasks complete
322       */
323      public void testGetCompletedTaskCount() throws InterruptedException {
324 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, 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 >                    await(threadProceed);
338 >                    threadDone.countDown();
339 >                }});
340 >            await(threadStarted);
341 >            assertEquals(0, p.getCompletedTaskCount());
342 >            threadProceed.countDown();
343 >            await(threadDone);
344 >            long startTime = System.nanoTime();
345 >            while (p.getCompletedTaskCount() != 1) {
346 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
347 >                    fail("timed out");
348 >                Thread.yield();
349 >            }
350 >        }
351      }
352  
353      /**
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, 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
368       */
369      public void testGetKeepAliveTime() {
370 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, 1000, 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  
280
379      /**
380       * getThreadFactory returns factory in constructor if not set
381       */
382      public void testGetThreadFactory() {
383 <        ThreadFactory tf = new SimpleThreadFactory();
384 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler());
385 <        assertSame(tf, p.getThreadFactory());
386 <        joinPool(p);
383 >        final ThreadFactory threadFactory = new SimpleThreadFactory();
384 >        final ThreadPoolExecutor p =
385 >            new CustomTPE(1, 2,
386 >                          LONG_DELAY_MS, MILLISECONDS,
387 >                          new ArrayBlockingQueue<Runnable>(10),
388 >                          threadFactory,
389 >                          new NoOpREHandler());
390 >        try (PoolCleaner cleaner = cleaner(p)) {
391 >            assertSame(threadFactory, p.getThreadFactory());
392 >        }
393      }
394  
395      /**
396       * setThreadFactory sets the thread factory returned by getThreadFactory
397       */
398      public void testSetThreadFactory() {
399 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
400 <        ThreadFactory tf = new SimpleThreadFactory();
401 <        p.setThreadFactory(tf);
402 <        assertSame(tf, p.getThreadFactory());
403 <        joinPool(p);
399 >        final ThreadPoolExecutor p =
400 >            new CustomTPE(1, 2,
401 >                          LONG_DELAY_MS, MILLISECONDS,
402 >                          new ArrayBlockingQueue<Runnable>(10));
403 >        try (PoolCleaner cleaner = cleaner(p)) {
404 >            ThreadFactory threadFactory = new SimpleThreadFactory();
405 >            p.setThreadFactory(threadFactory);
406 >            assertSame(threadFactory, p.getThreadFactory());
407 >        }
408      }
409  
302
410      /**
411       * setThreadFactory(null) throws NPE
412       */
413      public void testSetThreadFactoryNull() {
414 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
415 <        try {
416 <            p.setThreadFactory(null);
417 <            shouldThrow();
418 <        } catch (NullPointerException success) {
419 <        } finally {
420 <            joinPool(p);
414 >        final ThreadPoolExecutor p =
415 >            new CustomTPE(1, 2,
416 >                          LONG_DELAY_MS, MILLISECONDS,
417 >                          new ArrayBlockingQueue<Runnable>(10));
418 >        try (PoolCleaner cleaner = cleaner(p)) {
419 >            try {
420 >                p.setThreadFactory(null);
421 >                shouldThrow();
422 >            } catch (NullPointerException success) {}
423          }
424      }
425  
# Line 318 | Line 427 | public class ThreadPoolExecutorSubclassT
427       * getRejectedExecutionHandler returns handler in constructor if not set
428       */
429      public void testGetRejectedExecutionHandler() {
430 <        RejectedExecutionHandler h = new NoOpREHandler();
431 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h);
432 <        assertSame(h, p.getRejectedExecutionHandler());
433 <        joinPool(p);
430 >        final RejectedExecutionHandler handler = new NoOpREHandler();
431 >        final ThreadPoolExecutor p =
432 >            new CustomTPE(1, 2,
433 >                          LONG_DELAY_MS, MILLISECONDS,
434 >                          new ArrayBlockingQueue<Runnable>(10),
435 >                          handler);
436 >        try (PoolCleaner cleaner = cleaner(p)) {
437 >            assertSame(handler, p.getRejectedExecutionHandler());
438 >        }
439      }
440  
441      /**
# Line 329 | Line 443 | public class ThreadPoolExecutorSubclassT
443       * getRejectedExecutionHandler
444       */
445      public void testSetRejectedExecutionHandler() {
446 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
447 <        RejectedExecutionHandler h = new NoOpREHandler();
448 <        p.setRejectedExecutionHandler(h);
449 <        assertSame(h, p.getRejectedExecutionHandler());
450 <        joinPool(p);
446 >        final ThreadPoolExecutor p =
447 >            new CustomTPE(1, 2,
448 >                          LONG_DELAY_MS, MILLISECONDS,
449 >                          new ArrayBlockingQueue<Runnable>(10));
450 >        try (PoolCleaner cleaner = cleaner(p)) {
451 >            RejectedExecutionHandler handler = new NoOpREHandler();
452 >            p.setRejectedExecutionHandler(handler);
453 >            assertSame(handler, p.getRejectedExecutionHandler());
454 >        }
455      }
456  
339
457      /**
458       * setRejectedExecutionHandler(null) throws NPE
459       */
460      public void testSetRejectedExecutionHandlerNull() {
461 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
462 <        try {
463 <            p.setRejectedExecutionHandler(null);
464 <            shouldThrow();
465 <        } catch (NullPointerException success) {
466 <        } finally {
467 <            joinPool(p);
461 >        final ThreadPoolExecutor p =
462 >            new CustomTPE(1, 2,
463 >                          LONG_DELAY_MS, MILLISECONDS,
464 >                          new ArrayBlockingQueue<Runnable>(10));
465 >        try (PoolCleaner cleaner = cleaner(p)) {
466 >            try {
467 >                p.setRejectedExecutionHandler(null);
468 >                shouldThrow();
469 >            } catch (NullPointerException success) {}
470          }
471      }
472  
354
473      /**
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, 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 >                        await(done);
492 >                        assertEquals(THREADS, p.getLargestPoolSize());
493 >                    }});
494 >            await(threadsStarted);
495 >            assertEquals(THREADS, p.getLargestPoolSize());
496 >        }
497 >        assertEquals(THREADS, p.getLargestPoolSize());
498      }
499  
500      /**
# Line 371 | Line 502 | public class ThreadPoolExecutorSubclassT
502       * otherwise set
503       */
504      public void testGetMaximumPoolSize() {
505 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, 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, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
524 <        assertEquals(0, p1.getPoolSize());
525 <        p1.execute(new MediumRunnable());
526 <        assertEquals(1, p1.getPoolSize());
527 <        joinPool(p1);
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 >                    await(done);
536 >                }});
537 >            await(threadStarted);
538 >            assertEquals(1, p.getPoolSize());
539 >        }
540      }
541  
542      /**
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, 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 >                    await(done);
560 >                }});
561 >            await(threadStarted);
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 >                        await(done);
571 >                    }});
572 >            }
573 >            assertEquals(1 + TASKS, p.getTaskCount());
574 >            assertEquals(0, p.getCompletedTaskCount());
575 >        }
576 >        assertEquals(1 + TASKS, p.getTaskCount());
577 >        assertEquals(1 + TASKS, p.getCompletedTaskCount());
578      }
579  
580      /**
581 <     * isShutDown is false before shutdown, true after
581 >     * isShutdown is false before shutdown, true after
582       */
583      public void testIsShutdown() {
584 <
585 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, 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  
415
595      /**
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, 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 >                    await(done);
612 >                }});
613 >            await(threadStarted);
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          }
427        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
428        assertTrue(p1.isTerminated());
621      }
622  
623      /**
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, 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, 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 >                    await(done);
640 >                }});
641 >            await(threadStarted);
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, 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 CountDownLatch done = new CountDownLatch(1);
656 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
657 >        final ThreadPoolExecutor p =
658 >            new CustomTPE(1, 1,
659 >                          LONG_DELAY_MS, MILLISECONDS,
660 >                          q);
661 >        try (PoolCleaner cleaner = cleaner(p, done)) {
662 >            final CountDownLatch threadStarted = new CountDownLatch(1);
663 >            FutureTask[] tasks = new FutureTask[5];
664 >            for (int i = 0; i < tasks.length; i++) {
665 >                Callable<Boolean> task = new CheckedCallable<Boolean>() {
666 >                    public Boolean realCall() throws InterruptedException {
667 >                        threadStarted.countDown();
668 >                        assertSame(q, p.getQueue());
669 >                        await(done);
670 >                        return Boolean.TRUE;
671 >                    }};
672 >                tasks[i] = new FutureTask(task);
673 >                p.execute(tasks[i]);
674 >            }
675 >            await(threadStarted);
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          }
681      }
682  
# Line 474 | Line 684 | public class ThreadPoolExecutorSubclassT
684       * remove(task) removes queued task, and fails to remove active task
685       */
686      public void testRemove() throws InterruptedException {
687 <        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
688 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, q);
689 <        FutureTask[] tasks = new FutureTask[5];
690 <        for (int i = 0; i < 5; i++) {
691 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
692 <            p1.execute(tasks[i]);
693 <        }
694 <        try {
695 <            Thread.sleep(SHORT_DELAY_MS);
696 <            assertFalse(p1.remove(tasks[0]));
687 >        final CountDownLatch done = new CountDownLatch(1);
688 >        BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
689 >        final ThreadPoolExecutor p =
690 >            new CustomTPE(1, 1,
691 >                          LONG_DELAY_MS, MILLISECONDS,
692 >                          q);
693 >        try (PoolCleaner cleaner = cleaner(p, done)) {
694 >            Runnable[] tasks = new Runnable[6];
695 >            final CountDownLatch threadStarted = new CountDownLatch(1);
696 >            for (int i = 0; i < tasks.length; i++) {
697 >                tasks[i] = new CheckedRunnable() {
698 >                    public void realRun() throws InterruptedException {
699 >                        threadStarted.countDown();
700 >                        await(done);
701 >                    }};
702 >                p.execute(tasks[i]);
703 >            }
704 >            await(threadStarted);
705 >            assertFalse(p.remove(tasks[0]));
706              assertTrue(q.contains(tasks[4]));
707              assertTrue(q.contains(tasks[3]));
708 <            assertTrue(p1.remove(tasks[4]));
709 <            assertFalse(p1.remove(tasks[4]));
708 >            assertTrue(p.remove(tasks[4]));
709 >            assertFalse(p.remove(tasks[4]));
710              assertFalse(q.contains(tasks[4]));
711              assertTrue(q.contains(tasks[3]));
712 <            assertTrue(p1.remove(tasks[3]));
712 >            assertTrue(p.remove(tasks[3]));
713              assertFalse(q.contains(tasks[3]));
495        } finally {
496            joinPool(p1);
714          }
715      }
716  
717      /**
718       * purge removes cancelled tasks from the queue
719       */
720 <    public void testPurge() {
721 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
722 <        FutureTask[] tasks = new FutureTask[5];
723 <        for (int i = 0; i < 5; i++) {
724 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
725 <            p1.execute(tasks[i]);
720 >    public void testPurge() throws InterruptedException {
721 >        final CountDownLatch threadStarted = new CountDownLatch(1);
722 >        final CountDownLatch done = new CountDownLatch(1);
723 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
724 >        final ThreadPoolExecutor p =
725 >            new CustomTPE(1, 1,
726 >                          LONG_DELAY_MS, MILLISECONDS,
727 >                          q);
728 >        try (PoolCleaner cleaner = cleaner(p, done)) {
729 >            FutureTask[] tasks = new FutureTask[5];
730 >            for (int i = 0; i < tasks.length; i++) {
731 >                Callable<Boolean> task = new CheckedCallable<Boolean>() {
732 >                    public Boolean realCall() throws InterruptedException {
733 >                        threadStarted.countDown();
734 >                        await(done);
735 >                        return Boolean.TRUE;
736 >                    }};
737 >                tasks[i] = new FutureTask(task);
738 >                p.execute(tasks[i]);
739 >            }
740 >            await(threadStarted);
741 >            assertEquals(tasks.length, p.getTaskCount());
742 >            assertEquals(tasks.length - 1, q.size());
743 >            assertEquals(1L, p.getActiveCount());
744 >            assertEquals(0L, p.getCompletedTaskCount());
745 >            tasks[4].cancel(true);
746 >            tasks[3].cancel(false);
747 >            p.purge();
748 >            assertEquals(tasks.length - 3, q.size());
749 >            assertEquals(tasks.length - 2, p.getTaskCount());
750 >            p.purge();         // Nothing to do
751 >            assertEquals(tasks.length - 3, q.size());
752 >            assertEquals(tasks.length - 2, p.getTaskCount());
753          }
510        tasks[4].cancel(true);
511        tasks[3].cancel(true);
512        p1.purge();
513        long count = p1.getTaskCount();
514        assertTrue(count >= 2 && count < 5);
515        joinPool(p1);
754      }
755  
756      /**
757 <     * shutDownNow returns a list containing tasks that were not run
758 <     */
759 <    public void testShutDownNow() {
760 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
761 <        List l;
762 <        try {
763 <            for (int i = 0; i < 5; i++)
764 <                p1.execute(new MediumPossiblyInterruptedRunnable());
765 <        }
766 <        finally {
757 >     * shutdownNow returns a list containing tasks that were not run,
758 >     * and those tasks are drained from the queue
759 >     */
760 >    public void testShutdownNow() throws InterruptedException {
761 >        final int poolSize = 2;
762 >        final int count = 5;
763 >        final AtomicInteger ran = new AtomicInteger(0);
764 >        final ThreadPoolExecutor p =
765 >            new CustomTPE(poolSize, poolSize,
766 >                          LONG_DELAY_MS, MILLISECONDS,
767 >                          new ArrayBlockingQueue<Runnable>(10));
768 >        final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
769 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
770 >            threadsStarted.countDown();
771              try {
772 <                l = p1.shutdownNow();
773 <            } catch (SecurityException ok) { return; }
774 <        }
775 <        assertTrue(p1.isShutdown());
776 <        assertTrue(l.size() <= 4);
772 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
773 >            } catch (InterruptedException success) {}
774 >            ran.getAndIncrement();
775 >        }};
776 >        for (int i = 0; i < count; i++)
777 >            p.execute(waiter);
778 >        await(threadsStarted);
779 >        assertEquals(poolSize, p.getActiveCount());
780 >        assertEquals(0, p.getCompletedTaskCount());
781 >        final List<Runnable> queuedTasks;
782 >        try {
783 >            queuedTasks = p.shutdownNow();
784 >        } catch (SecurityException ok) {
785 >            return; // Allowed in case test doesn't have privs
786 >        }
787 >        assertTrue(p.isShutdown());
788 >        assertTrue(p.getQueue().isEmpty());
789 >        assertEquals(count - poolSize, queuedTasks.size());
790 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
791 >        assertTrue(p.isTerminated());
792 >        assertEquals(poolSize, ran.get());
793 >        assertEquals(poolSize, p.getCompletedTaskCount());
794      }
795  
796      // Exception Tests
797  
539
798      /**
799       * Constructor throws if corePoolSize argument is less than zero
800       */
801      public void testConstructor1() {
802          try {
803 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
803 >            new CustomTPE(-1, 1, 1L, SECONDS,
804 >                          new ArrayBlockingQueue<Runnable>(10));
805              shouldThrow();
806          } catch (IllegalArgumentException success) {}
807      }
# Line 552 | Line 811 | public class ThreadPoolExecutorSubclassT
811       */
812      public void testConstructor2() {
813          try {
814 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
814 >            new CustomTPE(1, -1, 1L, SECONDS,
815 >                          new ArrayBlockingQueue<Runnable>(10));
816              shouldThrow();
817          } catch (IllegalArgumentException success) {}
818      }
# Line 562 | Line 822 | public class ThreadPoolExecutorSubclassT
822       */
823      public void testConstructor3() {
824          try {
825 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
825 >            new CustomTPE(1, 0, 1L, SECONDS,
826 >                          new ArrayBlockingQueue<Runnable>(10));
827              shouldThrow();
828          } catch (IllegalArgumentException success) {}
829      }
# Line 572 | Line 833 | public class ThreadPoolExecutorSubclassT
833       */
834      public void testConstructor4() {
835          try {
836 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
836 >            new CustomTPE(1, 2, -1L, SECONDS,
837 >                          new ArrayBlockingQueue<Runnable>(10));
838              shouldThrow();
839          } catch (IllegalArgumentException success) {}
840      }
# Line 582 | Line 844 | public class ThreadPoolExecutorSubclassT
844       */
845      public void testConstructor5() {
846          try {
847 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
847 >            new CustomTPE(2, 1, 1L, SECONDS,
848 >                          new ArrayBlockingQueue<Runnable>(10));
849              shouldThrow();
850          } catch (IllegalArgumentException success) {}
851      }
# Line 592 | Line 855 | public class ThreadPoolExecutorSubclassT
855       */
856      public void testConstructorNullPointerException() {
857          try {
858 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null);
858 >            new CustomTPE(1, 2, 1L, SECONDS, null);
859              shouldThrow();
860          } catch (NullPointerException success) {}
861      }
862  
600
601
863      /**
864       * Constructor throws if corePoolSize argument is less than zero
865       */
866      public void testConstructor6() {
867          try {
868 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
868 >            new CustomTPE(-1, 1, 1L, SECONDS,
869 >                          new ArrayBlockingQueue<Runnable>(10),
870 >                          new SimpleThreadFactory());
871              shouldThrow();
872          } catch (IllegalArgumentException success) {}
873      }
# Line 614 | Line 877 | public class ThreadPoolExecutorSubclassT
877       */
878      public void testConstructor7() {
879          try {
880 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
880 >            new CustomTPE(1,-1, 1L, SECONDS,
881 >                          new ArrayBlockingQueue<Runnable>(10),
882 >                          new SimpleThreadFactory());
883              shouldThrow();
884          } catch (IllegalArgumentException success) {}
885      }
# Line 624 | Line 889 | public class ThreadPoolExecutorSubclassT
889       */
890      public void testConstructor8() {
891          try {
892 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
892 >            new CustomTPE(1, 0, 1L, SECONDS,
893 >                          new ArrayBlockingQueue<Runnable>(10),
894 >                          new SimpleThreadFactory());
895              shouldThrow();
896          } catch (IllegalArgumentException success) {}
897      }
# Line 634 | Line 901 | public class ThreadPoolExecutorSubclassT
901       */
902      public void testConstructor9() {
903          try {
904 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
904 >            new CustomTPE(1, 2, -1L, SECONDS,
905 >                          new ArrayBlockingQueue<Runnable>(10),
906 >                          new SimpleThreadFactory());
907              shouldThrow();
908          } catch (IllegalArgumentException success) {}
909      }
# Line 644 | Line 913 | public class ThreadPoolExecutorSubclassT
913       */
914      public void testConstructor10() {
915          try {
916 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
916 >            new CustomTPE(2, 1, 1L, SECONDS,
917 >                          new ArrayBlockingQueue<Runnable>(10),
918 >                          new SimpleThreadFactory());
919              shouldThrow();
920          } catch (IllegalArgumentException success) {}
921      }
# Line 654 | Line 925 | public class ThreadPoolExecutorSubclassT
925       */
926      public void testConstructorNullPointerException2() {
927          try {
928 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
928 >            new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory());
929              shouldThrow();
930          } catch (NullPointerException success) {}
931      }
# Line 664 | Line 935 | public class ThreadPoolExecutorSubclassT
935       */
936      public void testConstructorNullPointerException3() {
937          try {
938 <            ThreadFactory f = null;
939 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
938 >            new CustomTPE(1, 2, 1L, SECONDS,
939 >                          new ArrayBlockingQueue<Runnable>(10),
940 >                          (ThreadFactory) null);
941              shouldThrow();
942          } catch (NullPointerException success) {}
943      }
944  
673
945      /**
946       * Constructor throws if corePoolSize argument is less than zero
947       */
948      public void testConstructor11() {
949          try {
950 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
950 >            new CustomTPE(-1, 1, 1L, SECONDS,
951 >                          new ArrayBlockingQueue<Runnable>(10),
952 >                          new NoOpREHandler());
953              shouldThrow();
954          } catch (IllegalArgumentException success) {}
955      }
# Line 686 | Line 959 | public class ThreadPoolExecutorSubclassT
959       */
960      public void testConstructor12() {
961          try {
962 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
962 >            new CustomTPE(1, -1, 1L, SECONDS,
963 >                          new ArrayBlockingQueue<Runnable>(10),
964 >                          new NoOpREHandler());
965              shouldThrow();
966          } catch (IllegalArgumentException success) {}
967      }
# Line 696 | Line 971 | public class ThreadPoolExecutorSubclassT
971       */
972      public void testConstructor13() {
973          try {
974 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
974 >            new CustomTPE(1, 0, 1L, SECONDS,
975 >                          new ArrayBlockingQueue<Runnable>(10),
976 >                          new NoOpREHandler());
977              shouldThrow();
978          } catch (IllegalArgumentException success) {}
979      }
# Line 706 | Line 983 | public class ThreadPoolExecutorSubclassT
983       */
984      public void testConstructor14() {
985          try {
986 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
986 >            new CustomTPE(1, 2, -1L, SECONDS,
987 >                          new ArrayBlockingQueue<Runnable>(10),
988 >                          new NoOpREHandler());
989              shouldThrow();
990          } catch (IllegalArgumentException success) {}
991      }
# Line 716 | Line 995 | public class ThreadPoolExecutorSubclassT
995       */
996      public void testConstructor15() {
997          try {
998 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
998 >            new CustomTPE(2, 1, 1L, SECONDS,
999 >                          new ArrayBlockingQueue<Runnable>(10),
1000 >                          new NoOpREHandler());
1001              shouldThrow();
1002          } catch (IllegalArgumentException success) {}
1003      }
# Line 726 | Line 1007 | public class ThreadPoolExecutorSubclassT
1007       */
1008      public void testConstructorNullPointerException4() {
1009          try {
1010 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
1010 >            new CustomTPE(1, 2, 1L, SECONDS,
1011 >                          null,
1012 >                          new NoOpREHandler());
1013              shouldThrow();
1014          } catch (NullPointerException success) {}
1015      }
# Line 736 | Line 1019 | public class ThreadPoolExecutorSubclassT
1019       */
1020      public void testConstructorNullPointerException5() {
1021          try {
1022 <            RejectedExecutionHandler r = null;
1023 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
1022 >            new CustomTPE(1, 2, 1L, SECONDS,
1023 >                          new ArrayBlockingQueue<Runnable>(10),
1024 >                          (RejectedExecutionHandler) null);
1025              shouldThrow();
1026          } catch (NullPointerException success) {}
1027      }
1028  
745
1029      /**
1030       * Constructor throws if corePoolSize argument is less than zero
1031       */
1032      public void testConstructor16() {
1033          try {
1034 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1034 >            new CustomTPE(-1, 1, 1L, SECONDS,
1035 >                          new ArrayBlockingQueue<Runnable>(10),
1036 >                          new SimpleThreadFactory(),
1037 >                          new NoOpREHandler());
1038              shouldThrow();
1039          } catch (IllegalArgumentException success) {}
1040      }
# Line 758 | Line 1044 | public class ThreadPoolExecutorSubclassT
1044       */
1045      public void testConstructor17() {
1046          try {
1047 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1047 >            new CustomTPE(1, -1, 1L, SECONDS,
1048 >                          new ArrayBlockingQueue<Runnable>(10),
1049 >                          new SimpleThreadFactory(),
1050 >                          new NoOpREHandler());
1051              shouldThrow();
1052          } catch (IllegalArgumentException success) {}
1053      }
# Line 768 | Line 1057 | public class ThreadPoolExecutorSubclassT
1057       */
1058      public void testConstructor18() {
1059          try {
1060 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1060 >            new CustomTPE(1, 0, 1L, SECONDS,
1061 >                          new ArrayBlockingQueue<Runnable>(10),
1062 >                          new SimpleThreadFactory(),
1063 >                          new NoOpREHandler());
1064              shouldThrow();
1065          } catch (IllegalArgumentException success) {}
1066      }
# Line 778 | Line 1070 | public class ThreadPoolExecutorSubclassT
1070       */
1071      public void testConstructor19() {
1072          try {
1073 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1073 >            new CustomTPE(1, 2, -1L, SECONDS,
1074 >                          new ArrayBlockingQueue<Runnable>(10),
1075 >                          new SimpleThreadFactory(),
1076 >                          new NoOpREHandler());
1077              shouldThrow();
1078          } catch (IllegalArgumentException success) {}
1079      }
# Line 788 | Line 1083 | public class ThreadPoolExecutorSubclassT
1083       */
1084      public void testConstructor20() {
1085          try {
1086 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1086 >            new CustomTPE(2, 1, 1L, SECONDS,
1087 >                          new ArrayBlockingQueue<Runnable>(10),
1088 >                          new SimpleThreadFactory(),
1089 >                          new NoOpREHandler());
1090              shouldThrow();
1091          } catch (IllegalArgumentException success) {}
1092      }
# Line 798 | Line 1096 | public class ThreadPoolExecutorSubclassT
1096       */
1097      public void testConstructorNullPointerException6() {
1098          try {
1099 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
1099 >            new CustomTPE(1, 2, 1L, SECONDS,
1100 >                          null,
1101 >                          new SimpleThreadFactory(),
1102 >                          new NoOpREHandler());
1103              shouldThrow();
1104          } catch (NullPointerException success) {}
1105      }
# Line 808 | Line 1109 | public class ThreadPoolExecutorSubclassT
1109       */
1110      public void testConstructorNullPointerException7() {
1111          try {
1112 <            RejectedExecutionHandler r = null;
1113 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
1112 >            new CustomTPE(1, 2, 1L, SECONDS,
1113 >                          new ArrayBlockingQueue<Runnable>(10),
1114 >                          new SimpleThreadFactory(),
1115 >                          (RejectedExecutionHandler) null);
1116              shouldThrow();
1117          } catch (NullPointerException success) {}
1118      }
# Line 819 | Line 1122 | public class ThreadPoolExecutorSubclassT
1122       */
1123      public void testConstructorNullPointerException8() {
1124          try {
1125 <            ThreadFactory f = null;
1126 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
1125 >            new CustomTPE(1, 2, 1L, SECONDS,
1126 >                          new ArrayBlockingQueue<Runnable>(10),
1127 >                          (ThreadFactory) null,
1128 >                          new NoOpREHandler());
1129              shouldThrow();
1130          } catch (NullPointerException success) {}
1131      }
1132  
828
1133      /**
1134       * execute throws RejectedExecutionException if saturated.
1135       */
1136      public void testSaturatedExecute() {
1137 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
1138 <        try {
1139 <
1140 <            for (int i = 0; i < 5; ++i) {
1141 <                p.execute(new MediumRunnable());
1137 >        final CountDownLatch done = new CountDownLatch(1);
1138 >        final ThreadPoolExecutor p =
1139 >            new CustomTPE(1, 1,
1140 >                          LONG_DELAY_MS, MILLISECONDS,
1141 >                          new ArrayBlockingQueue<Runnable>(1));
1142 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1143 >            Runnable task = new CheckedRunnable() {
1144 >                public void realRun() throws InterruptedException {
1145 >                    await(done);
1146 >                }};
1147 >            for (int i = 0; i < 2; ++i)
1148 >                p.execute(task);
1149 >            for (int i = 0; i < 2; ++i) {
1150 >                try {
1151 >                    p.execute(task);
1152 >                    shouldThrow();
1153 >                } catch (RejectedExecutionException success) {}
1154 >                assertTrue(p.getTaskCount() <= 2);
1155              }
1156 <            shouldThrow();
840 <        } catch (RejectedExecutionException success) {}
841 <        joinPool(p);
1156 >        }
1157      }
1158  
1159      /**
1160       * executor using CallerRunsPolicy runs task if saturated.
1161       */
1162      public void testSaturatedExecute2() {
1163 <        RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1164 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1165 <        try {
1166 <
1163 >        final CountDownLatch done = new CountDownLatch(1);
1164 >        final ThreadPoolExecutor p =
1165 >            new CustomTPE(1, 1,
1166 >                          LONG_DELAY_MS, MILLISECONDS,
1167 >                          new ArrayBlockingQueue<Runnable>(1),
1168 >                          new CustomTPE.CallerRunsPolicy());
1169 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1170 >            Runnable blocker = new CheckedRunnable() {
1171 >                public void realRun() throws InterruptedException {
1172 >                    await(done);
1173 >                }};
1174 >            p.execute(blocker);
1175              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1176 <            for (int i = 0; i < 5; ++i) {
1176 >            for (int i = 0; i < tasks.length; i++)
1177                  tasks[i] = new TrackedNoOpRunnable();
1178 <            }
856 <            TrackedLongRunnable mr = new TrackedLongRunnable();
857 <            p.execute(mr);
858 <            for (int i = 0; i < 5; ++i) {
1178 >            for (int i = 0; i < tasks.length; i++)
1179                  p.execute(tasks[i]);
1180 <            }
861 <            for (int i = 1; i < 5; ++i) {
1180 >            for (int i = 1; i < tasks.length; i++)
1181                  assertTrue(tasks[i].done);
1182 <            }
864 <            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
865 <        } finally {
866 <            joinPool(p);
1182 >            assertFalse(tasks[0].done); // waiting in queue
1183          }
1184      }
1185  
# Line 871 | Line 1187 | public class ThreadPoolExecutorSubclassT
1187       * executor using DiscardPolicy drops task if saturated.
1188       */
1189      public void testSaturatedExecute3() {
1190 <        RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1191 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1192 <        try {
1193 <
1194 <            TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1195 <            for (int i = 0; i < 5; ++i) {
1196 <                tasks[i] = new TrackedNoOpRunnable();
1197 <            }
1198 <            p.execute(new TrackedLongRunnable());
1199 <            for (int i = 0; i < 5; ++i) {
1200 <                p.execute(tasks[i]);
1201 <            }
1202 <            for (int i = 0; i < 5; ++i) {
1190 >        final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1191 >        for (int i = 0; i < tasks.length; ++i)
1192 >            tasks[i] = new TrackedNoOpRunnable();
1193 >        final CountDownLatch done = new CountDownLatch(1);
1194 >        final ThreadPoolExecutor p =
1195 >            new CustomTPE(1, 1,
1196 >                          LONG_DELAY_MS, MILLISECONDS,
1197 >                          new ArrayBlockingQueue<Runnable>(1),
1198 >                          new CustomTPE.DiscardPolicy());
1199 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1200 >            p.execute(awaiter(done));
1201 >
1202 >            for (TrackedNoOpRunnable task : tasks)
1203 >                p.execute(task);
1204 >            for (int i = 1; i < tasks.length; i++)
1205                  assertFalse(tasks[i].done);
888            }
889            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
890        } finally {
891            joinPool(p);
1206          }
1207 +        for (int i = 1; i < tasks.length; i++)
1208 +            assertFalse(tasks[i].done);
1209 +        assertTrue(tasks[0].done); // was waiting in queue
1210      }
1211  
1212      /**
1213       * executor using DiscardOldestPolicy drops oldest task if saturated.
1214       */
1215      public void testSaturatedExecute4() {
1216 <        RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
1217 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1218 <        try {
1219 <            p.execute(new TrackedLongRunnable());
1220 <            TrackedLongRunnable r2 = new TrackedLongRunnable();
1216 >        final CountDownLatch done = new CountDownLatch(1);
1217 >        LatchAwaiter r1 = awaiter(done);
1218 >        LatchAwaiter r2 = awaiter(done);
1219 >        LatchAwaiter r3 = awaiter(done);
1220 >        final ThreadPoolExecutor p =
1221 >            new CustomTPE(1, 1,
1222 >                          LONG_DELAY_MS, MILLISECONDS,
1223 >                          new ArrayBlockingQueue<Runnable>(1),
1224 >                          new CustomTPE.DiscardOldestPolicy());
1225 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1226 >            assertEquals(LatchAwaiter.NEW, r1.state);
1227 >            assertEquals(LatchAwaiter.NEW, r2.state);
1228 >            assertEquals(LatchAwaiter.NEW, r3.state);
1229 >            p.execute(r1);
1230              p.execute(r2);
1231              assertTrue(p.getQueue().contains(r2));
906            TrackedNoOpRunnable r3 = new TrackedNoOpRunnable();
1232              p.execute(r3);
1233              assertFalse(p.getQueue().contains(r2));
1234              assertTrue(p.getQueue().contains(r3));
910            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
911        } finally {
912            joinPool(p);
1235          }
1236 +        assertEquals(LatchAwaiter.DONE, r1.state);
1237 +        assertEquals(LatchAwaiter.NEW, r2.state);
1238 +        assertEquals(LatchAwaiter.DONE, r3.state);
1239      }
1240  
1241      /**
1242       * execute throws RejectedExecutionException if shutdown
1243       */
1244      public void testRejectedExecutionExceptionOnShutdown() {
1245 <        ThreadPoolExecutor tpe =
1246 <            new CustomTPE(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
1247 <        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1248 <        try {
1249 <            tpe.execute(new NoOpRunnable());
1250 <            shouldThrow();
1251 <        } catch (RejectedExecutionException success) {}
1252 <
1253 <        joinPool(tpe);
1245 >        final ThreadPoolExecutor p =
1246 >            new CustomTPE(1, 1,
1247 >                          LONG_DELAY_MS, MILLISECONDS,
1248 >                          new ArrayBlockingQueue<Runnable>(1));
1249 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1250 >        try (PoolCleaner cleaner = cleaner(p)) {
1251 >            try {
1252 >                p.execute(new NoOpRunnable());
1253 >                shouldThrow();
1254 >            } catch (RejectedExecutionException success) {}
1255 >        }
1256      }
1257  
1258      /**
1259       * execute using CallerRunsPolicy drops task on shutdown
1260       */
1261      public void testCallerRunsOnShutdown() {
1262 <        RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1263 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1264 <
1262 >        final ThreadPoolExecutor p =
1263 >            new CustomTPE(1, 1,
1264 >                          LONG_DELAY_MS, MILLISECONDS,
1265 >                          new ArrayBlockingQueue<Runnable>(1),
1266 >                          new CustomTPE.CallerRunsPolicy());
1267          try { p.shutdown(); } catch (SecurityException ok) { return; }
1268 <        try {
1268 >        try (PoolCleaner cleaner = cleaner(p)) {
1269              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1270              p.execute(r);
1271              assertFalse(r.done);
943        } finally {
944            joinPool(p);
1272          }
1273      }
1274  
# Line 949 | Line 1276 | public class ThreadPoolExecutorSubclassT
1276       * execute using DiscardPolicy drops task on shutdown
1277       */
1278      public void testDiscardOnShutdown() {
1279 <        RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1280 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1281 <
1279 >        final ThreadPoolExecutor p =
1280 >            new CustomTPE(1, 1,
1281 >                          LONG_DELAY_MS, MILLISECONDS,
1282 >                          new ArrayBlockingQueue<Runnable>(1),
1283 >                          new CustomTPE.DiscardPolicy());
1284          try { p.shutdown(); } catch (SecurityException ok) { return; }
1285 <        try {
1285 >        try (PoolCleaner cleaner = cleaner(p)) {
1286              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1287              p.execute(r);
1288              assertFalse(r.done);
960        } finally {
961            joinPool(p);
1289          }
1290      }
1291  
965
1292      /**
1293       * execute using DiscardOldestPolicy drops task on shutdown
1294       */
1295      public void testDiscardOldestOnShutdown() {
1296 <        RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
1297 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1296 >        final ThreadPoolExecutor p =
1297 >            new CustomTPE(1, 1,
1298 >                          LONG_DELAY_MS, MILLISECONDS,
1299 >                          new ArrayBlockingQueue<Runnable>(1),
1300 >                          new CustomTPE.DiscardOldestPolicy());
1301  
1302          try { p.shutdown(); } catch (SecurityException ok) { return; }
1303 <        try {
1303 >        try (PoolCleaner cleaner = cleaner(p)) {
1304              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1305              p.execute(r);
1306              assertFalse(r.done);
978        } finally {
979            joinPool(p);
1307          }
1308      }
1309  
983
1310      /**
1311       * execute(null) throws NPE
1312       */
1313      public void testExecuteNull() {
1314 <        ThreadPoolExecutor tpe = null;
1315 <        try {
1316 <            tpe = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1317 <            tpe.execute(null);
1318 <            shouldThrow();
1319 <        } catch (NullPointerException success) {}
1320 <
1321 <        joinPool(tpe);
1314 >        final ThreadPoolExecutor p =
1315 >            new CustomTPE(1, 2,
1316 >                          1L, SECONDS,
1317 >                          new ArrayBlockingQueue<Runnable>(10));
1318 >        try (PoolCleaner cleaner = cleaner(p)) {
1319 >            try {
1320 >                p.execute(null);
1321 >                shouldThrow();
1322 >            } catch (NullPointerException success) {}
1323 >        }
1324      }
1325  
1326      /**
1327       * setCorePoolSize of negative value throws IllegalArgumentException
1328       */
1329      public void testCorePoolSizeIllegalArgumentException() {
1330 <        ThreadPoolExecutor tpe =
1331 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1332 <        try {
1333 <            tpe.setCorePoolSize(-1);
1334 <            shouldThrow();
1335 <        } catch (IllegalArgumentException success) {
1336 <        } finally {
1337 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1330 >        final ThreadPoolExecutor p =
1331 >            new CustomTPE(1, 2,
1332 >                          LONG_DELAY_MS, MILLISECONDS,
1333 >                          new ArrayBlockingQueue<Runnable>(10));
1334 >        try (PoolCleaner cleaner = cleaner(p)) {
1335 >            try {
1336 >                p.setCorePoolSize(-1);
1337 >                shouldThrow();
1338 >            } catch (IllegalArgumentException success) {}
1339          }
1011        joinPool(tpe);
1340      }
1341  
1342      /**
# Line 1016 | Line 1344 | public class ThreadPoolExecutorSubclassT
1344       * if given a value less the core pool size
1345       */
1346      public void testMaximumPoolSizeIllegalArgumentException() {
1347 <        ThreadPoolExecutor tpe =
1348 <            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1349 <        try {
1350 <            tpe.setMaximumPoolSize(1);
1351 <            shouldThrow();
1352 <        } catch (IllegalArgumentException success) {
1353 <        } finally {
1354 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1347 >        final ThreadPoolExecutor p =
1348 >            new CustomTPE(2, 3,
1349 >                          LONG_DELAY_MS, MILLISECONDS,
1350 >                          new ArrayBlockingQueue<Runnable>(10));
1351 >        try (PoolCleaner cleaner = cleaner(p)) {
1352 >            try {
1353 >                p.setMaximumPoolSize(1);
1354 >                shouldThrow();
1355 >            } catch (IllegalArgumentException success) {}
1356          }
1028        joinPool(tpe);
1357      }
1358  
1359      /**
# Line 1033 | Line 1361 | public class ThreadPoolExecutorSubclassT
1361       * if given a negative value
1362       */
1363      public void testMaximumPoolSizeIllegalArgumentException2() {
1364 <        ThreadPoolExecutor tpe =
1365 <            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1366 <        try {
1367 <            tpe.setMaximumPoolSize(-1);
1368 <            shouldThrow();
1369 <        } catch (IllegalArgumentException success) {
1370 <        } finally {
1371 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1364 >        final ThreadPoolExecutor p =
1365 >            new CustomTPE(2, 3,
1366 >                          LONG_DELAY_MS,
1367 >                          MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1368 >        try (PoolCleaner cleaner = cleaner(p)) {
1369 >            try {
1370 >                p.setMaximumPoolSize(-1);
1371 >                shouldThrow();
1372 >            } catch (IllegalArgumentException success) {}
1373          }
1045        joinPool(tpe);
1374      }
1375  
1048
1376      /**
1377       * setKeepAliveTime throws IllegalArgumentException
1378       * when given a negative value
1379       */
1380      public void testKeepAliveTimeIllegalArgumentException() {
1381 <        ThreadPoolExecutor tpe =
1382 <            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1383 <
1384 <        try {
1385 <            tpe.setKeepAliveTime(-1,MILLISECONDS);
1386 <            shouldThrow();
1387 <        } catch (IllegalArgumentException success) {
1388 <        } finally {
1389 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1381 >        final ThreadPoolExecutor p =
1382 >            new CustomTPE(2, 3,
1383 >                          LONG_DELAY_MS, MILLISECONDS,
1384 >                          new ArrayBlockingQueue<Runnable>(10));
1385 >        try (PoolCleaner cleaner = cleaner(p)) {
1386 >            try {
1387 >                p.setKeepAliveTime(-1, MILLISECONDS);
1388 >                shouldThrow();
1389 >            } catch (IllegalArgumentException success) {}
1390          }
1064        joinPool(tpe);
1391      }
1392  
1393      /**
1394       * terminated() is called on termination
1395       */
1396      public void testTerminated() {
1397 <        CustomTPE tpe = new CustomTPE();
1398 <        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1399 <        assertTrue(tpe.terminatedCalled);
1400 <        joinPool(tpe);
1397 >        CustomTPE p = new CustomTPE();
1398 >        try (PoolCleaner cleaner = cleaner(p)) {
1399 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1400 >            assertTrue(p.terminatedCalled());
1401 >            assertTrue(p.isShutdown());
1402 >        }
1403      }
1404  
1405      /**
1406       * beforeExecute and afterExecute are called when executing task
1407       */
1408      public void testBeforeAfter() throws InterruptedException {
1409 <        CustomTPE tpe = new CustomTPE();
1410 <        try {
1411 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1412 <            tpe.execute(r);
1413 <            Thread.sleep(SHORT_DELAY_MS);
1414 <            assertTrue(r.done);
1415 <            assertTrue(tpe.beforeCalled);
1416 <            assertTrue(tpe.afterCalled);
1417 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1418 <        } finally {
1419 <            joinPool(tpe);
1409 >        CustomTPE p = new CustomTPE();
1410 >        try (PoolCleaner cleaner = cleaner(p)) {
1411 >            final CountDownLatch done = new CountDownLatch(1);
1412 >            p.execute(new CheckedRunnable() {
1413 >                public void realRun() {
1414 >                    done.countDown();
1415 >                }});
1416 >            await(p.afterCalled);
1417 >            assertEquals(0, done.getCount());
1418 >            assertTrue(p.afterCalled());
1419 >            assertTrue(p.beforeCalled());
1420          }
1421      }
1422  
# Line 1096 | Line 1424 | public class ThreadPoolExecutorSubclassT
1424       * completed submit of callable returns result
1425       */
1426      public void testSubmitCallable() throws Exception {
1427 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1428 <        try {
1427 >        final ExecutorService e =
1428 >            new CustomTPE(2, 2,
1429 >                          LONG_DELAY_MS, MILLISECONDS,
1430 >                          new ArrayBlockingQueue<Runnable>(10));
1431 >        try (PoolCleaner cleaner = cleaner(e)) {
1432              Future<String> future = e.submit(new StringTask());
1433              String result = future.get();
1434              assertSame(TEST_STRING, result);
1104        } finally {
1105            joinPool(e);
1435          }
1436      }
1437  
# Line 1110 | Line 1439 | public class ThreadPoolExecutorSubclassT
1439       * completed submit of runnable returns successfully
1440       */
1441      public void testSubmitRunnable() throws Exception {
1442 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1443 <        try {
1442 >        final ExecutorService e =
1443 >            new CustomTPE(2, 2,
1444 >                          LONG_DELAY_MS, MILLISECONDS,
1445 >                          new ArrayBlockingQueue<Runnable>(10));
1446 >        try (PoolCleaner cleaner = cleaner(e)) {
1447              Future<?> future = e.submit(new NoOpRunnable());
1448              future.get();
1449              assertTrue(future.isDone());
1118        } finally {
1119            joinPool(e);
1450          }
1451      }
1452  
# Line 1124 | Line 1454 | public class ThreadPoolExecutorSubclassT
1454       * completed submit of (runnable, result) returns result
1455       */
1456      public void testSubmitRunnable2() throws Exception {
1457 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1458 <        try {
1457 >        final ExecutorService e =
1458 >            new CustomTPE(2, 2,
1459 >                          LONG_DELAY_MS, MILLISECONDS,
1460 >                          new ArrayBlockingQueue<Runnable>(10));
1461 >        try (PoolCleaner cleaner = cleaner(e)) {
1462              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1463              String result = future.get();
1464              assertSame(TEST_STRING, result);
1132        } finally {
1133            joinPool(e);
1465          }
1466      }
1467  
1137
1468      /**
1469 <     * invokeAny(null) throws NPE
1469 >     * invokeAny(null) throws NullPointerException
1470       */
1471      public void testInvokeAny1() throws Exception {
1472 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1473 <        try {
1474 <            e.invokeAny(null);
1475 <            shouldThrow();
1476 <        } catch (NullPointerException success) {
1477 <        } finally {
1478 <            joinPool(e);
1472 >        final ExecutorService e =
1473 >            new CustomTPE(2, 2,
1474 >                          LONG_DELAY_MS, MILLISECONDS,
1475 >                          new ArrayBlockingQueue<Runnable>(10));
1476 >        try (PoolCleaner cleaner = cleaner(e)) {
1477 >            try {
1478 >                e.invokeAny(null);
1479 >                shouldThrow();
1480 >            } catch (NullPointerException success) {}
1481          }
1482      }
1483  
1484      /**
1485 <     * invokeAny(empty collection) throws IAE
1485 >     * invokeAny(empty collection) throws IllegalArgumentException
1486       */
1487      public void testInvokeAny2() throws Exception {
1488 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1489 <        try {
1490 <            e.invokeAny(new ArrayList<Callable<String>>());
1491 <            shouldThrow();
1492 <        } catch (IllegalArgumentException success) {
1493 <        } finally {
1494 <            joinPool(e);
1488 >        final ExecutorService e =
1489 >            new CustomTPE(2, 2,
1490 >                          LONG_DELAY_MS, MILLISECONDS,
1491 >                          new ArrayBlockingQueue<Runnable>(10));
1492 >        try (PoolCleaner cleaner = cleaner(e)) {
1493 >            try {
1494 >                e.invokeAny(new ArrayList<Callable<String>>());
1495 >                shouldThrow();
1496 >            } catch (IllegalArgumentException success) {}
1497          }
1498      }
1499  
# Line 1168 | Line 1502 | public class ThreadPoolExecutorSubclassT
1502       */
1503      public void testInvokeAny3() throws Exception {
1504          CountDownLatch latch = new CountDownLatch(1);
1505 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1506 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1507 <        l.add(latchAwaitingStringTask(latch));
1508 <        l.add(null);
1509 <        try {
1510 <            e.invokeAny(l);
1511 <            shouldThrow();
1512 <        } catch (NullPointerException success) {
1513 <        } finally {
1505 >        final ExecutorService e =
1506 >            new CustomTPE(2, 2,
1507 >                          LONG_DELAY_MS, MILLISECONDS,
1508 >                          new ArrayBlockingQueue<Runnable>(10));
1509 >        try (PoolCleaner cleaner = cleaner(e)) {
1510 >            List<Callable<String>> l = new ArrayList<>();
1511 >            l.add(latchAwaitingStringTask(latch));
1512 >            l.add(null);
1513 >            try {
1514 >                e.invokeAny(l);
1515 >                shouldThrow();
1516 >            } catch (NullPointerException success) {}
1517              latch.countDown();
1181            joinPool(e);
1518          }
1519      }
1520  
# Line 1186 | Line 1522 | public class ThreadPoolExecutorSubclassT
1522       * invokeAny(c) throws ExecutionException if no task completes
1523       */
1524      public void testInvokeAny4() throws Exception {
1525 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1526 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1527 <        l.add(new NPETask());
1528 <        try {
1529 <            e.invokeAny(l);
1530 <            shouldThrow();
1531 <        } catch (ExecutionException success) {
1532 <            assertTrue(success.getCause() instanceof NullPointerException);
1533 <        } finally {
1534 <            joinPool(e);
1525 >        final ExecutorService e =
1526 >            new CustomTPE(2, 2,
1527 >                          LONG_DELAY_MS, MILLISECONDS,
1528 >                          new ArrayBlockingQueue<Runnable>(10));
1529 >        try (PoolCleaner cleaner = cleaner(e)) {
1530 >            List<Callable<String>> l = new ArrayList<>();
1531 >            l.add(new NPETask());
1532 >            try {
1533 >                e.invokeAny(l);
1534 >                shouldThrow();
1535 >            } catch (ExecutionException success) {
1536 >                assertTrue(success.getCause() instanceof NullPointerException);
1537 >            }
1538          }
1539      }
1540  
# Line 1203 | Line 1542 | public class ThreadPoolExecutorSubclassT
1542       * invokeAny(c) returns result of some task
1543       */
1544      public void testInvokeAny5() throws Exception {
1545 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1546 <        try {
1547 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1545 >        final ExecutorService e =
1546 >            new CustomTPE(2, 2,
1547 >                          LONG_DELAY_MS, MILLISECONDS,
1548 >                          new ArrayBlockingQueue<Runnable>(10));
1549 >        try (PoolCleaner cleaner = cleaner(e)) {
1550 >            List<Callable<String>> l = new ArrayList<>();
1551              l.add(new StringTask());
1552              l.add(new StringTask());
1553              String result = e.invokeAny(l);
1554              assertSame(TEST_STRING, result);
1213        } finally {
1214            joinPool(e);
1555          }
1556      }
1557  
# Line 1219 | Line 1559 | public class ThreadPoolExecutorSubclassT
1559       * invokeAll(null) throws NPE
1560       */
1561      public void testInvokeAll1() throws Exception {
1562 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1563 <        try {
1564 <            e.invokeAll(null);
1565 <            shouldThrow();
1566 <        } catch (NullPointerException success) {
1567 <        } finally {
1568 <            joinPool(e);
1562 >        final ExecutorService e =
1563 >            new CustomTPE(2, 2,
1564 >                          LONG_DELAY_MS, MILLISECONDS,
1565 >                          new ArrayBlockingQueue<Runnable>(10));
1566 >        try (PoolCleaner cleaner = cleaner(e)) {
1567 >            try {
1568 >                e.invokeAll(null);
1569 >                shouldThrow();
1570 >            } catch (NullPointerException success) {}
1571          }
1572      }
1573  
1574      /**
1575 <     * invokeAll(empty collection) returns empty collection
1575 >     * invokeAll(empty collection) returns empty list
1576       */
1577      public void testInvokeAll2() throws Exception {
1578 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1579 <        try {
1580 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1578 >        final ExecutorService e =
1579 >            new CustomTPE(2, 2,
1580 >                          LONG_DELAY_MS, MILLISECONDS,
1581 >                          new ArrayBlockingQueue<Runnable>(10));
1582 >        final Collection<Callable<String>> emptyCollection
1583 >            = Collections.emptyList();
1584 >        try (PoolCleaner cleaner = cleaner(e)) {
1585 >            List<Future<String>> r = e.invokeAll(emptyCollection);
1586              assertTrue(r.isEmpty());
1240        } finally {
1241            joinPool(e);
1587          }
1588      }
1589  
# Line 1246 | Line 1591 | public class ThreadPoolExecutorSubclassT
1591       * invokeAll(c) throws NPE if c has null elements
1592       */
1593      public void testInvokeAll3() throws Exception {
1594 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1595 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1596 <        l.add(new StringTask());
1597 <        l.add(null);
1598 <        try {
1599 <            e.invokeAll(l);
1600 <            shouldThrow();
1601 <        } catch (NullPointerException success) {
1602 <        } finally {
1603 <            joinPool(e);
1594 >        final ExecutorService e =
1595 >            new CustomTPE(2, 2,
1596 >                          LONG_DELAY_MS, MILLISECONDS,
1597 >                          new ArrayBlockingQueue<Runnable>(10));
1598 >        try (PoolCleaner cleaner = cleaner(e)) {
1599 >            List<Callable<String>> l = new ArrayList<>();
1600 >            l.add(new StringTask());
1601 >            l.add(null);
1602 >            try {
1603 >                e.invokeAll(l);
1604 >                shouldThrow();
1605 >            } catch (NullPointerException success) {}
1606          }
1607      }
1608  
# Line 1263 | Line 1610 | public class ThreadPoolExecutorSubclassT
1610       * get of element of invokeAll(c) throws exception on failed task
1611       */
1612      public void testInvokeAll4() throws Exception {
1613 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1614 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1615 <        l.add(new NPETask());
1616 <        List<Future<String>> futures = e.invokeAll(l);
1617 <        assertEquals(1, futures.size());
1618 <        try {
1619 <            futures.get(0).get();
1620 <            shouldThrow();
1621 <        } catch (ExecutionException success) {
1622 <            assertTrue(success.getCause() instanceof NullPointerException);
1623 <        } finally {
1624 <            joinPool(e);
1613 >        final ExecutorService e =
1614 >            new CustomTPE(2, 2,
1615 >                          LONG_DELAY_MS, MILLISECONDS,
1616 >                          new ArrayBlockingQueue<Runnable>(10));
1617 >        try (PoolCleaner cleaner = cleaner(e)) {
1618 >            List<Callable<String>> l = new ArrayList<>();
1619 >            l.add(new NPETask());
1620 >            List<Future<String>> futures = e.invokeAll(l);
1621 >            assertEquals(1, futures.size());
1622 >            try {
1623 >                futures.get(0).get();
1624 >                shouldThrow();
1625 >            } catch (ExecutionException success) {
1626 >                assertTrue(success.getCause() instanceof NullPointerException);
1627 >            }
1628          }
1629      }
1630  
# Line 1282 | Line 1632 | public class ThreadPoolExecutorSubclassT
1632       * invokeAll(c) returns results of all completed tasks
1633       */
1634      public void testInvokeAll5() throws Exception {
1635 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1636 <        try {
1637 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1635 >        final ExecutorService e =
1636 >            new CustomTPE(2, 2,
1637 >                          LONG_DELAY_MS, MILLISECONDS,
1638 >                          new ArrayBlockingQueue<Runnable>(10));
1639 >        try (PoolCleaner cleaner = cleaner(e)) {
1640 >            List<Callable<String>> l = new ArrayList<>();
1641              l.add(new StringTask());
1642              l.add(new StringTask());
1643              List<Future<String>> futures = e.invokeAll(l);
1644              assertEquals(2, futures.size());
1645              for (Future<String> future : futures)
1646                  assertSame(TEST_STRING, future.get());
1294        } finally {
1295            joinPool(e);
1647          }
1648      }
1649  
1299
1300
1650      /**
1651       * timed invokeAny(null) throws NPE
1652       */
1653      public void testTimedInvokeAny1() throws Exception {
1654 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1655 <        try {
1656 <            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1657 <            shouldThrow();
1658 <        } catch (NullPointerException success) {
1659 <        } finally {
1660 <            joinPool(e);
1654 >        final ExecutorService e =
1655 >            new CustomTPE(2, 2,
1656 >                          LONG_DELAY_MS, MILLISECONDS,
1657 >                          new ArrayBlockingQueue<Runnable>(10));
1658 >        try (PoolCleaner cleaner = cleaner(e)) {
1659 >            try {
1660 >                e.invokeAny(null, randomTimeout(), randomTimeUnit());
1661 >                shouldThrow();
1662 >            } catch (NullPointerException success) {}
1663          }
1664      }
1665  
# Line 1316 | Line 1667 | public class ThreadPoolExecutorSubclassT
1667       * timed invokeAny(,,null) throws NPE
1668       */
1669      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1670 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1671 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1672 <        l.add(new StringTask());
1673 <        try {
1674 <            e.invokeAny(l, MEDIUM_DELAY_MS, null);
1675 <            shouldThrow();
1676 <        } catch (NullPointerException success) {
1677 <        } finally {
1678 <            joinPool(e);
1670 >        final ExecutorService e =
1671 >            new CustomTPE(2, 2,
1672 >                          LONG_DELAY_MS, MILLISECONDS,
1673 >                          new ArrayBlockingQueue<Runnable>(10));
1674 >        try (PoolCleaner cleaner = cleaner(e)) {
1675 >            List<Callable<String>> l = new ArrayList<>();
1676 >            l.add(new StringTask());
1677 >            try {
1678 >                e.invokeAny(l, randomTimeout(), null);
1679 >                shouldThrow();
1680 >            } catch (NullPointerException success) {}
1681          }
1682      }
1683  
1684      /**
1685 <     * timed invokeAny(empty collection) throws IAE
1685 >     * timed invokeAny(empty collection) throws IllegalArgumentException
1686       */
1687      public void testTimedInvokeAny2() throws Exception {
1688 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1689 <        try {
1690 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1691 <            shouldThrow();
1692 <        } catch (IllegalArgumentException success) {
1693 <        } finally {
1694 <            joinPool(e);
1688 >        final ExecutorService e =
1689 >            new CustomTPE(2, 2,
1690 >                          LONG_DELAY_MS, MILLISECONDS,
1691 >                          new ArrayBlockingQueue<Runnable>(10));
1692 >        final Collection<Callable<String>> emptyCollection
1693 >            = Collections.emptyList();
1694 >        try (PoolCleaner cleaner = cleaner(e)) {
1695 >            try {
1696 >                e.invokeAny(emptyCollection, randomTimeout(), randomTimeUnit());
1697 >                shouldThrow();
1698 >            } catch (IllegalArgumentException success) {}
1699          }
1700      }
1701  
# Line 1347 | Line 1704 | public class ThreadPoolExecutorSubclassT
1704       */
1705      public void testTimedInvokeAny3() throws Exception {
1706          CountDownLatch latch = new CountDownLatch(1);
1707 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1708 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1709 <        l.add(latchAwaitingStringTask(latch));
1710 <        l.add(null);
1711 <        try {
1712 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1713 <            shouldThrow();
1714 <        } catch (NullPointerException success) {
1715 <        } finally {
1707 >        final ExecutorService e =
1708 >            new CustomTPE(2, 2,
1709 >                          LONG_DELAY_MS, MILLISECONDS,
1710 >                          new ArrayBlockingQueue<Runnable>(10));
1711 >        try (PoolCleaner cleaner = cleaner(e)) {
1712 >            List<Callable<String>> l = new ArrayList<>();
1713 >            l.add(latchAwaitingStringTask(latch));
1714 >            l.add(null);
1715 >            try {
1716 >                e.invokeAny(l, randomTimeout(), MILLISECONDS);
1717 >                shouldThrow();
1718 >            } catch (NullPointerException success) {}
1719              latch.countDown();
1360            joinPool(e);
1720          }
1721      }
1722  
# Line 1365 | Line 1724 | public class ThreadPoolExecutorSubclassT
1724       * timed invokeAny(c) throws ExecutionException if no task completes
1725       */
1726      public void testTimedInvokeAny4() throws Exception {
1727 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1728 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1729 <        l.add(new NPETask());
1730 <        try {
1731 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1732 <            shouldThrow();
1733 <        } catch (ExecutionException success) {
1734 <            assertTrue(success.getCause() instanceof NullPointerException);
1735 <        } finally {
1736 <            joinPool(e);
1727 >        final ExecutorService e =
1728 >            new CustomTPE(2, 2,
1729 >                          LONG_DELAY_MS, MILLISECONDS,
1730 >                          new ArrayBlockingQueue<Runnable>(10));
1731 >        try (PoolCleaner cleaner = cleaner(e)) {
1732 >            long startTime = System.nanoTime();
1733 >            List<Callable<String>> l = new ArrayList<>();
1734 >            l.add(new NPETask());
1735 >            try {
1736 >                e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1737 >                shouldThrow();
1738 >            } catch (ExecutionException success) {
1739 >                assertTrue(success.getCause() instanceof NullPointerException);
1740 >            }
1741 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1742          }
1743      }
1744  
# Line 1382 | Line 1746 | public class ThreadPoolExecutorSubclassT
1746       * timed invokeAny(c) returns result of some task
1747       */
1748      public void testTimedInvokeAny5() throws Exception {
1749 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1750 <        try {
1751 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1749 >        final ExecutorService e =
1750 >            new CustomTPE(2, 2,
1751 >                          LONG_DELAY_MS, MILLISECONDS,
1752 >                          new ArrayBlockingQueue<Runnable>(10));
1753 >        try (PoolCleaner cleaner = cleaner(e)) {
1754 >            long startTime = System.nanoTime();
1755 >            List<Callable<String>> l = new ArrayList<>();
1756              l.add(new StringTask());
1757              l.add(new StringTask());
1758 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1758 >            String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1759              assertSame(TEST_STRING, result);
1760 <        } finally {
1393 <            joinPool(e);
1760 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1761          }
1762      }
1763  
# Line 1398 | Line 1765 | public class ThreadPoolExecutorSubclassT
1765       * timed invokeAll(null) throws NPE
1766       */
1767      public void testTimedInvokeAll1() throws Exception {
1768 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1769 <        try {
1770 <            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1771 <            shouldThrow();
1772 <        } catch (NullPointerException success) {
1773 <        } finally {
1774 <            joinPool(e);
1768 >        final ExecutorService e =
1769 >            new CustomTPE(2, 2,
1770 >                          LONG_DELAY_MS, MILLISECONDS,
1771 >                          new ArrayBlockingQueue<Runnable>(10));
1772 >        try (PoolCleaner cleaner = cleaner(e)) {
1773 >            try {
1774 >                e.invokeAll(null, randomTimeout(), randomTimeUnit());
1775 >                shouldThrow();
1776 >            } catch (NullPointerException success) {}
1777          }
1778      }
1779  
# Line 1412 | Line 1781 | public class ThreadPoolExecutorSubclassT
1781       * timed invokeAll(,,null) throws NPE
1782       */
1783      public void testTimedInvokeAllNullTimeUnit() throws Exception {
1784 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1785 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1786 <        l.add(new StringTask());
1787 <        try {
1788 <            e.invokeAll(l, MEDIUM_DELAY_MS, null);
1789 <            shouldThrow();
1790 <        } catch (NullPointerException success) {
1791 <        } finally {
1792 <            joinPool(e);
1784 >        final ExecutorService e =
1785 >            new CustomTPE(2, 2,
1786 >                          LONG_DELAY_MS, MILLISECONDS,
1787 >                          new ArrayBlockingQueue<Runnable>(10));
1788 >        try (PoolCleaner cleaner = cleaner(e)) {
1789 >            List<Callable<String>> l = new ArrayList<>();
1790 >            l.add(new StringTask());
1791 >            try {
1792 >                e.invokeAll(l, randomTimeout(), null);
1793 >                shouldThrow();
1794 >            } catch (NullPointerException success) {}
1795          }
1796      }
1797  
1798      /**
1799 <     * timed invokeAll(empty collection) returns empty collection
1799 >     * timed invokeAll(empty collection) returns empty list
1800       */
1801      public void testTimedInvokeAll2() throws Exception {
1802 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1803 <        try {
1804 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1802 >        final ExecutorService e =
1803 >            new CustomTPE(2, 2,
1804 >                          LONG_DELAY_MS, MILLISECONDS,
1805 >                          new ArrayBlockingQueue<Runnable>(10));
1806 >        final Collection<Callable<String>> emptyCollection
1807 >            = Collections.emptyList();
1808 >        try (PoolCleaner cleaner = cleaner(e)) {
1809 >            List<Future<String>> r =
1810 >                e.invokeAll(emptyCollection, randomTimeout(), randomTimeUnit());
1811              assertTrue(r.isEmpty());
1435        } finally {
1436            joinPool(e);
1812          }
1813      }
1814  
# Line 1441 | Line 1816 | public class ThreadPoolExecutorSubclassT
1816       * timed invokeAll(c) throws NPE if c has null elements
1817       */
1818      public void testTimedInvokeAll3() throws Exception {
1819 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1820 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1821 <        l.add(new StringTask());
1822 <        l.add(null);
1823 <        try {
1824 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1825 <            shouldThrow();
1826 <        } catch (NullPointerException success) {
1827 <        } finally {
1828 <            joinPool(e);
1819 >        final ExecutorService e =
1820 >            new CustomTPE(2, 2,
1821 >                          LONG_DELAY_MS, MILLISECONDS,
1822 >                          new ArrayBlockingQueue<Runnable>(10));
1823 >        try (PoolCleaner cleaner = cleaner(e)) {
1824 >            List<Callable<String>> l = new ArrayList<>();
1825 >            l.add(new StringTask());
1826 >            l.add(null);
1827 >            try {
1828 >                e.invokeAll(l, randomTimeout(), randomTimeUnit());
1829 >                shouldThrow();
1830 >            } catch (NullPointerException success) {}
1831          }
1832      }
1833  
# Line 1458 | Line 1835 | public class ThreadPoolExecutorSubclassT
1835       * get of element of invokeAll(c) throws exception on failed task
1836       */
1837      public void testTimedInvokeAll4() throws Exception {
1838 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1839 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1840 <        l.add(new NPETask());
1841 <        List<Future<String>> futures =
1842 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1843 <        assertEquals(1, futures.size());
1844 <        try {
1845 <            futures.get(0).get();
1846 <            shouldThrow();
1847 <        } catch (ExecutionException success) {
1848 <            assertTrue(success.getCause() instanceof NullPointerException);
1849 <        } finally {
1850 <            joinPool(e);
1838 >        final ExecutorService e =
1839 >            new CustomTPE(2, 2,
1840 >                          LONG_DELAY_MS, MILLISECONDS,
1841 >                          new ArrayBlockingQueue<Runnable>(10));
1842 >        try (PoolCleaner cleaner = cleaner(e)) {
1843 >            List<Callable<String>> l = new ArrayList<>();
1844 >            l.add(new NPETask());
1845 >            List<Future<String>> futures =
1846 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1847 >            assertEquals(1, futures.size());
1848 >            try {
1849 >                futures.get(0).get();
1850 >                shouldThrow();
1851 >            } catch (ExecutionException success) {
1852 >                assertTrue(success.getCause() instanceof NullPointerException);
1853 >            }
1854          }
1855      }
1856  
# Line 1478 | Line 1858 | public class ThreadPoolExecutorSubclassT
1858       * timed invokeAll(c) returns results of all completed tasks
1859       */
1860      public void testTimedInvokeAll5() throws Exception {
1861 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1862 <        try {
1863 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1861 >        final ExecutorService e =
1862 >            new CustomTPE(2, 2,
1863 >                          LONG_DELAY_MS, MILLISECONDS,
1864 >                          new ArrayBlockingQueue<Runnable>(10));
1865 >        try (PoolCleaner cleaner = cleaner(e)) {
1866 >            List<Callable<String>> l = new ArrayList<>();
1867              l.add(new StringTask());
1868              l.add(new StringTask());
1869              List<Future<String>> futures =
1870 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1870 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1871              assertEquals(2, futures.size());
1872              for (Future<String> future : futures)
1873                  assertSame(TEST_STRING, future.get());
1491        } finally {
1492            joinPool(e);
1874          }
1875      }
1876  
# Line 1497 | Line 1878 | public class ThreadPoolExecutorSubclassT
1878       * timed invokeAll(c) cancels tasks not completed by timeout
1879       */
1880      public void testTimedInvokeAll6() throws Exception {
1881 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1882 <        try {
1883 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1884 <            l.add(new StringTask());
1885 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1886 <            l.add(new StringTask());
1887 <            List<Future<String>> futures =
1888 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1889 <            assertEquals(3, futures.size());
1890 <            Iterator<Future<String>> it = futures.iterator();
1891 <            Future<String> f1 = it.next();
1892 <            Future<String> f2 = it.next();
1893 <            Future<String> f3 = it.next();
1894 <            assertTrue(f1.isDone());
1895 <            assertTrue(f2.isDone());
1896 <            assertTrue(f3.isDone());
1897 <            assertFalse(f1.isCancelled());
1898 <            assertTrue(f2.isCancelled());
1899 <        } finally {
1900 <            joinPool(e);
1881 >        for (long timeout = timeoutMillis();;) {
1882 >            final CountDownLatch done = new CountDownLatch(1);
1883 >            final Callable<String> waiter = new CheckedCallable<String>() {
1884 >                public String realCall() {
1885 >                    try { done.await(LONG_DELAY_MS, MILLISECONDS); }
1886 >                    catch (InterruptedException ok) {}
1887 >                    return "1"; }};
1888 >            final ExecutorService p =
1889 >                new CustomTPE(2, 2,
1890 >                              LONG_DELAY_MS, MILLISECONDS,
1891 >                              new ArrayBlockingQueue<Runnable>(10));
1892 >            try (PoolCleaner cleaner = cleaner(p, done)) {
1893 >                List<Callable<String>> tasks = new ArrayList<>();
1894 >                tasks.add(new StringTask("0"));
1895 >                tasks.add(waiter);
1896 >                tasks.add(new StringTask("2"));
1897 >                long startTime = System.nanoTime();
1898 >                List<Future<String>> futures =
1899 >                    p.invokeAll(tasks, timeout, MILLISECONDS);
1900 >                assertEquals(tasks.size(), futures.size());
1901 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1902 >                for (Future future : futures)
1903 >                    assertTrue(future.isDone());
1904 >                assertTrue(futures.get(1).isCancelled());
1905 >                try {
1906 >                    assertEquals("0", futures.get(0).get());
1907 >                    assertEquals("2", futures.get(2).get());
1908 >                    break;
1909 >                } catch (CancellationException retryWithLongerTimeout) {
1910 >                    timeout *= 2;
1911 >                    if (timeout >= LONG_DELAY_MS / 2)
1912 >                        fail("expected exactly one task to be cancelled");
1913 >                }
1914 >            }
1915          }
1916      }
1917  
# Line 1525 | Line 1920 | public class ThreadPoolExecutorSubclassT
1920       * thread factory fails to create more
1921       */
1922      public void testFailingThreadFactory() throws InterruptedException {
1923 <        ExecutorService e = new CustomTPE(100, 100, LONG_DELAY_MS, MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory());
1924 <        try {
1925 <            for (int k = 0; k < 100; ++k) {
1926 <                e.execute(new NoOpRunnable());
1927 <            }
1928 <            Thread.sleep(LONG_DELAY_MS);
1929 <        } finally {
1930 <            joinPool(e);
1923 >        final ExecutorService e =
1924 >            new CustomTPE(100, 100,
1925 >                          LONG_DELAY_MS, MILLISECONDS,
1926 >                          new LinkedBlockingQueue<Runnable>(),
1927 >                          new FailingThreadFactory());
1928 >        try (PoolCleaner cleaner = cleaner(e)) {
1929 >            final int TASKS = 100;
1930 >            final CountDownLatch done = new CountDownLatch(TASKS);
1931 >            for (int k = 0; k < TASKS; ++k)
1932 >                e.execute(new CheckedRunnable() {
1933 >                    public void realRun() {
1934 >                        done.countDown();
1935 >                    }});
1936 >            await(done);
1937          }
1938      }
1939  
# Line 1540 | Line 1941 | public class ThreadPoolExecutorSubclassT
1941       * allowsCoreThreadTimeOut is by default false.
1942       */
1943      public void testAllowsCoreThreadTimeOut() {
1944 <        ThreadPoolExecutor tpe = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1945 <        assertFalse(tpe.allowsCoreThreadTimeOut());
1946 <        joinPool(tpe);
1944 >        final ThreadPoolExecutor p =
1945 >            new CustomTPE(2, 2,
1946 >                          1000, MILLISECONDS,
1947 >                          new ArrayBlockingQueue<Runnable>(10));
1948 >        try (PoolCleaner cleaner = cleaner(p)) {
1949 >            assertFalse(p.allowsCoreThreadTimeOut());
1950 >        }
1951      }
1952  
1953      /**
1954       * allowCoreThreadTimeOut(true) causes idle threads to time out
1955       */
1956 <    public void testAllowCoreThreadTimeOut_true() throws InterruptedException {
1957 <        ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1958 <        tpe.allowCoreThreadTimeOut(true);
1959 <        tpe.execute(new NoOpRunnable());
1960 <        try {
1961 <            Thread.sleep(MEDIUM_DELAY_MS);
1962 <            assertEquals(0, tpe.getPoolSize());
1963 <        } finally {
1964 <            joinPool(tpe);
1956 >    public void testAllowCoreThreadTimeOut_true() throws Exception {
1957 >        long keepAliveTime = timeoutMillis();
1958 >        final ThreadPoolExecutor p =
1959 >            new CustomTPE(2, 10,
1960 >                          keepAliveTime, MILLISECONDS,
1961 >                          new ArrayBlockingQueue<Runnable>(10));
1962 >        try (PoolCleaner cleaner = cleaner(p)) {
1963 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1964 >            p.allowCoreThreadTimeOut(true);
1965 >            p.execute(new CheckedRunnable() {
1966 >                public void realRun() {
1967 >                    threadStarted.countDown();
1968 >                    assertEquals(1, p.getPoolSize());
1969 >                }});
1970 >            await(threadStarted);
1971 >            delay(keepAliveTime);
1972 >            long startTime = System.nanoTime();
1973 >            while (p.getPoolSize() > 0
1974 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1975 >                Thread.yield();
1976 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1977 >            assertEquals(0, p.getPoolSize());
1978          }
1979      }
1980  
1981      /**
1982       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1983       */
1984 <    public void testAllowCoreThreadTimeOut_false() throws InterruptedException {
1985 <        ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1986 <        tpe.allowCoreThreadTimeOut(false);
1987 <        tpe.execute(new NoOpRunnable());
1988 <        try {
1989 <            Thread.sleep(MEDIUM_DELAY_MS);
1990 <            assertTrue(tpe.getPoolSize() >= 1);
1991 <        } finally {
1992 <            joinPool(tpe);
1984 >    public void testAllowCoreThreadTimeOut_false() throws Exception {
1985 >        long keepAliveTime = timeoutMillis();
1986 >        final ThreadPoolExecutor p =
1987 >            new CustomTPE(2, 10,
1988 >                          keepAliveTime, MILLISECONDS,
1989 >                          new ArrayBlockingQueue<Runnable>(10));
1990 >        try (PoolCleaner cleaner = cleaner(p)) {
1991 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1992 >            p.allowCoreThreadTimeOut(false);
1993 >            p.execute(new CheckedRunnable() {
1994 >                public void realRun() throws InterruptedException {
1995 >                    threadStarted.countDown();
1996 >                    assertTrue(p.getPoolSize() >= 1);
1997 >                }});
1998 >            delay(2 * keepAliveTime);
1999 >            assertTrue(p.getPoolSize() >= 1);
2000 >        }
2001 >    }
2002 >
2003 >    /**
2004 >     * get(cancelled task) throws CancellationException
2005 >     * (in part, a test of CustomTPE itself)
2006 >     */
2007 >    public void testGet_cancelled() throws Exception {
2008 >        final CountDownLatch done = new CountDownLatch(1);
2009 >        final ExecutorService e =
2010 >            new CustomTPE(1, 1,
2011 >                          LONG_DELAY_MS, MILLISECONDS,
2012 >                          new LinkedBlockingQueue<Runnable>());
2013 >        try (PoolCleaner cleaner = cleaner(e, done)) {
2014 >            final CountDownLatch blockerStarted = new CountDownLatch(1);
2015 >            final List<Future<?>> futures = new ArrayList<>();
2016 >            for (int i = 0; i < 2; i++) {
2017 >                Runnable r = new CheckedRunnable() { public void realRun()
2018 >                                                         throws Throwable {
2019 >                    blockerStarted.countDown();
2020 >                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
2021 >                }};
2022 >                futures.add(e.submit(r));
2023 >            }
2024 >            await(blockerStarted);
2025 >            for (Future<?> future : futures) future.cancel(false);
2026 >            for (Future<?> future : futures) {
2027 >                try {
2028 >                    future.get();
2029 >                    shouldThrow();
2030 >                } catch (CancellationException success) {}
2031 >                try {
2032 >                    future.get(LONG_DELAY_MS, MILLISECONDS);
2033 >                    shouldThrow();
2034 >                } catch (CancellationException success) {}
2035 >                assertTrue(future.isCancelled());
2036 >                assertTrue(future.isDone());
2037 >            }
2038          }
2039      }
2040  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines