ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ThreadPoolExecutorTest.java (file contents):
Revision 1.46 by jsr166, Sun May 29 13:55:36 2011 UTC vs.
Revision 1.115 by jsr166, Mon Mar 20 00:21:54 2017 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 import java.util.concurrent.*;
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 < import java.util.concurrent.atomic.*;
11 < import junit.framework.*;
12 < import java.util.*;
10 > import static java.util.concurrent.TimeUnit.NANOSECONDS;
11 > import static java.util.concurrent.TimeUnit.SECONDS;
12 >
13 > import java.util.ArrayList;
14 > import java.util.List;
15 > import java.util.concurrent.ArrayBlockingQueue;
16 > import java.util.concurrent.BlockingQueue;
17 > import java.util.concurrent.Callable;
18 > import java.util.concurrent.CancellationException;
19 > import java.util.concurrent.CountDownLatch;
20 > import java.util.concurrent.ExecutionException;
21 > import java.util.concurrent.ExecutorService;
22 > import java.util.concurrent.Future;
23 > import java.util.concurrent.FutureTask;
24 > import java.util.concurrent.LinkedBlockingQueue;
25 > import java.util.concurrent.RejectedExecutionException;
26 > import java.util.concurrent.RejectedExecutionHandler;
27 > import java.util.concurrent.SynchronousQueue;
28 > import java.util.concurrent.ThreadFactory;
29 > import java.util.concurrent.ThreadPoolExecutor;
30 > import java.util.concurrent.atomic.AtomicInteger;
31 >
32 > import junit.framework.Test;
33 > import junit.framework.TestSuite;
34  
35   public class ThreadPoolExecutorTest extends JSR166TestCase {
36      public static void main(String[] args) {
37 <        junit.textui.TestRunner.run(suite());
37 >        main(suite(), args);
38      }
39      public static Test suite() {
40          return new TestSuite(ThreadPoolExecutorTest.class);
# Line 65 | Line 85 | public class ThreadPoolExecutorTest exte
85              new ThreadPoolExecutor(1, 1,
86                                     LONG_DELAY_MS, MILLISECONDS,
87                                     new ArrayBlockingQueue<Runnable>(10));
88 <        final CountDownLatch done = new CountDownLatch(1);
89 <        final Runnable task = new CheckedRunnable() {
90 <            public void realRun() {
91 <                done.countDown();
72 <            }};
73 <        try {
88 >        try (PoolCleaner cleaner = cleaner(p)) {
89 >            final CountDownLatch done = new CountDownLatch(1);
90 >            final Runnable task = new CheckedRunnable() {
91 >                public void realRun() { done.countDown(); }};
92              p.execute(task);
93 <            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
76 <        } finally {
77 <            joinPool(p);
93 >            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
94          }
95      }
96  
# Line 83 | Line 99 | public class ThreadPoolExecutorTest exte
99       * thread becomes active
100       */
101      public void testGetActiveCount() throws InterruptedException {
102 +        final CountDownLatch done = new CountDownLatch(1);
103          final ThreadPoolExecutor p =
104              new ThreadPoolExecutor(2, 2,
105                                     LONG_DELAY_MS, MILLISECONDS,
106                                     new ArrayBlockingQueue<Runnable>(10));
107 <        final CountDownLatch threadStarted = new CountDownLatch(1);
108 <        final CountDownLatch done = new CountDownLatch(1);
92 <        try {
107 >        try (PoolCleaner cleaner = cleaner(p, done)) {
108 >            final CountDownLatch threadStarted = new CountDownLatch(1);
109              assertEquals(0, p.getActiveCount());
110              p.execute(new CheckedRunnable() {
111                  public void realRun() throws InterruptedException {
112                      threadStarted.countDown();
113                      assertEquals(1, p.getActiveCount());
114 <                    done.await();
114 >                    await(done);
115                  }});
116 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
116 >            await(threadStarted);
117              assertEquals(1, p.getActiveCount());
102        } finally {
103            done.countDown();
104            joinPool(p);
118          }
119      }
120  
# Line 110 | Line 123 | public class ThreadPoolExecutorTest exte
123       */
124      public void testPrestartCoreThread() {
125          final ThreadPoolExecutor p =
126 <            new ThreadPoolExecutor(2, 2,
126 >            new ThreadPoolExecutor(2, 6,
127                                     LONG_DELAY_MS, MILLISECONDS,
128                                     new ArrayBlockingQueue<Runnable>(10));
129 <        assertEquals(0, p.getPoolSize());
130 <        assertTrue(p.prestartCoreThread());
131 <        assertEquals(1, p.getPoolSize());
132 <        assertTrue(p.prestartCoreThread());
133 <        assertEquals(2, p.getPoolSize());
134 <        assertFalse(p.prestartCoreThread());
135 <        assertEquals(2, p.getPoolSize());
136 <        joinPool(p);
129 >        try (PoolCleaner cleaner = cleaner(p)) {
130 >            assertEquals(0, p.getPoolSize());
131 >            assertTrue(p.prestartCoreThread());
132 >            assertEquals(1, p.getPoolSize());
133 >            assertTrue(p.prestartCoreThread());
134 >            assertEquals(2, p.getPoolSize());
135 >            assertFalse(p.prestartCoreThread());
136 >            assertEquals(2, p.getPoolSize());
137 >            p.setCorePoolSize(4);
138 >            assertTrue(p.prestartCoreThread());
139 >            assertEquals(3, p.getPoolSize());
140 >            assertTrue(p.prestartCoreThread());
141 >            assertEquals(4, p.getPoolSize());
142 >            assertFalse(p.prestartCoreThread());
143 >            assertEquals(4, p.getPoolSize());
144 >        }
145      }
146  
147      /**
# Line 128 | Line 149 | public class ThreadPoolExecutorTest exte
149       */
150      public void testPrestartAllCoreThreads() {
151          final ThreadPoolExecutor p =
152 <            new ThreadPoolExecutor(2, 2,
152 >            new ThreadPoolExecutor(2, 6,
153                                     LONG_DELAY_MS, MILLISECONDS,
154                                     new ArrayBlockingQueue<Runnable>(10));
155 <        assertEquals(0, p.getPoolSize());
156 <        p.prestartAllCoreThreads();
157 <        assertEquals(2, p.getPoolSize());
158 <        p.prestartAllCoreThreads();
159 <        assertEquals(2, p.getPoolSize());
160 <        joinPool(p);
155 >        try (PoolCleaner cleaner = cleaner(p)) {
156 >            assertEquals(0, p.getPoolSize());
157 >            p.prestartAllCoreThreads();
158 >            assertEquals(2, p.getPoolSize());
159 >            p.prestartAllCoreThreads();
160 >            assertEquals(2, p.getPoolSize());
161 >            p.setCorePoolSize(4);
162 >            p.prestartAllCoreThreads();
163 >            assertEquals(4, p.getPoolSize());
164 >            p.prestartAllCoreThreads();
165 >            assertEquals(4, p.getPoolSize());
166 >        }
167      }
168  
169      /**
# Line 148 | Line 175 | public class ThreadPoolExecutorTest exte
175              new ThreadPoolExecutor(2, 2,
176                                     LONG_DELAY_MS, MILLISECONDS,
177                                     new ArrayBlockingQueue<Runnable>(10));
178 <        final CountDownLatch threadStarted = new CountDownLatch(1);
179 <        final CountDownLatch threadProceed = new CountDownLatch(1);
180 <        final CountDownLatch threadDone = new CountDownLatch(1);
181 <        try {
178 >        try (PoolCleaner cleaner = cleaner(p)) {
179 >            final CountDownLatch threadStarted = new CountDownLatch(1);
180 >            final CountDownLatch threadProceed = new CountDownLatch(1);
181 >            final CountDownLatch threadDone = new CountDownLatch(1);
182              assertEquals(0, p.getCompletedTaskCount());
183              p.execute(new CheckedRunnable() {
184                  public void realRun() throws InterruptedException {
# Line 170 | Line 197 | public class ThreadPoolExecutorTest exte
197                      fail("timed out");
198                  Thread.yield();
199              }
173        } finally {
174            joinPool(p);
200          }
201      }
202  
# Line 183 | Line 208 | public class ThreadPoolExecutorTest exte
208              new ThreadPoolExecutor(1, 1,
209                                     LONG_DELAY_MS, MILLISECONDS,
210                                     new ArrayBlockingQueue<Runnable>(10));
211 <        assertEquals(1, p.getCorePoolSize());
212 <        joinPool(p);
211 >        try (PoolCleaner cleaner = cleaner(p)) {
212 >            assertEquals(1, p.getCorePoolSize());
213 >        }
214      }
215  
216      /**
# Line 195 | Line 221 | public class ThreadPoolExecutorTest exte
221              new ThreadPoolExecutor(2, 2,
222                                     1000, MILLISECONDS,
223                                     new ArrayBlockingQueue<Runnable>(10));
224 <        assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS));
225 <        joinPool(p);
224 >        try (PoolCleaner cleaner = cleaner(p)) {
225 >            assertEquals(1, p.getKeepAliveTime(SECONDS));
226 >        }
227      }
228  
229      /**
230       * getThreadFactory returns factory in constructor if not set
231       */
232      public void testGetThreadFactory() {
233 <        ThreadFactory tf = new SimpleThreadFactory();
233 >        ThreadFactory threadFactory = new SimpleThreadFactory();
234          final ThreadPoolExecutor p =
235              new ThreadPoolExecutor(1, 2,
236                                     LONG_DELAY_MS, MILLISECONDS,
237                                     new ArrayBlockingQueue<Runnable>(10),
238 <                                   tf,
238 >                                   threadFactory,
239                                     new NoOpREHandler());
240 <        assertSame(tf, p.getThreadFactory());
241 <        joinPool(p);
240 >        try (PoolCleaner cleaner = cleaner(p)) {
241 >            assertSame(threadFactory, p.getThreadFactory());
242 >        }
243      }
244  
245      /**
# Line 222 | Line 250 | public class ThreadPoolExecutorTest exte
250              new ThreadPoolExecutor(1, 2,
251                                     LONG_DELAY_MS, MILLISECONDS,
252                                     new ArrayBlockingQueue<Runnable>(10));
253 <        ThreadFactory tf = new SimpleThreadFactory();
254 <        p.setThreadFactory(tf);
255 <        assertSame(tf, p.getThreadFactory());
256 <        joinPool(p);
253 >        try (PoolCleaner cleaner = cleaner(p)) {
254 >            ThreadFactory threadFactory = new SimpleThreadFactory();
255 >            p.setThreadFactory(threadFactory);
256 >            assertSame(threadFactory, p.getThreadFactory());
257 >        }
258      }
259  
260      /**
# Line 236 | Line 265 | public class ThreadPoolExecutorTest exte
265              new ThreadPoolExecutor(1, 2,
266                                     LONG_DELAY_MS, MILLISECONDS,
267                                     new ArrayBlockingQueue<Runnable>(10));
268 <        try {
269 <            p.setThreadFactory(null);
270 <            shouldThrow();
271 <        } catch (NullPointerException success) {
272 <        } finally {
244 <            joinPool(p);
268 >        try (PoolCleaner cleaner = cleaner(p)) {
269 >            try {
270 >                p.setThreadFactory(null);
271 >                shouldThrow();
272 >            } catch (NullPointerException success) {}
273          }
274      }
275  
# Line 249 | Line 277 | public class ThreadPoolExecutorTest exte
277       * getRejectedExecutionHandler returns handler in constructor if not set
278       */
279      public void testGetRejectedExecutionHandler() {
280 <        final RejectedExecutionHandler h = new NoOpREHandler();
280 >        final RejectedExecutionHandler handler = new NoOpREHandler();
281          final ThreadPoolExecutor p =
282              new ThreadPoolExecutor(1, 2,
283                                     LONG_DELAY_MS, MILLISECONDS,
284                                     new ArrayBlockingQueue<Runnable>(10),
285 <                                   h);
286 <        assertSame(h, p.getRejectedExecutionHandler());
287 <        joinPool(p);
285 >                                   handler);
286 >        try (PoolCleaner cleaner = cleaner(p)) {
287 >            assertSame(handler, p.getRejectedExecutionHandler());
288 >        }
289      }
290  
291      /**
# Line 268 | Line 297 | public class ThreadPoolExecutorTest exte
297              new ThreadPoolExecutor(1, 2,
298                                     LONG_DELAY_MS, MILLISECONDS,
299                                     new ArrayBlockingQueue<Runnable>(10));
300 <        RejectedExecutionHandler h = new NoOpREHandler();
301 <        p.setRejectedExecutionHandler(h);
302 <        assertSame(h, p.getRejectedExecutionHandler());
303 <        joinPool(p);
300 >        try (PoolCleaner cleaner = cleaner(p)) {
301 >            RejectedExecutionHandler handler = new NoOpREHandler();
302 >            p.setRejectedExecutionHandler(handler);
303 >            assertSame(handler, p.getRejectedExecutionHandler());
304 >        }
305      }
306  
307      /**
# Line 282 | Line 312 | public class ThreadPoolExecutorTest exte
312              new ThreadPoolExecutor(1, 2,
313                                     LONG_DELAY_MS, MILLISECONDS,
314                                     new ArrayBlockingQueue<Runnable>(10));
315 <        try {
316 <            p.setRejectedExecutionHandler(null);
317 <            shouldThrow();
318 <        } catch (NullPointerException success) {
319 <        } finally {
290 <            joinPool(p);
315 >        try (PoolCleaner cleaner = cleaner(p)) {
316 >            try {
317 >                p.setRejectedExecutionHandler(null);
318 >                shouldThrow();
319 >            } catch (NullPointerException success) {}
320          }
321      }
322  
# Line 297 | Line 326 | public class ThreadPoolExecutorTest exte
326       */
327      public void testGetLargestPoolSize() throws InterruptedException {
328          final int THREADS = 3;
329 +        final CountDownLatch done = new CountDownLatch(1);
330          final ThreadPoolExecutor p =
331              new ThreadPoolExecutor(THREADS, THREADS,
332                                     LONG_DELAY_MS, MILLISECONDS,
333                                     new ArrayBlockingQueue<Runnable>(10));
334 <        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
305 <        final CountDownLatch done = new CountDownLatch(1);
306 <        try {
334 >        try (PoolCleaner cleaner = cleaner(p, done)) {
335              assertEquals(0, p.getLargestPoolSize());
336 +            final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
337              for (int i = 0; i < THREADS; i++)
338                  p.execute(new CheckedRunnable() {
339                      public void realRun() throws InterruptedException {
340                          threadsStarted.countDown();
341 <                        done.await();
341 >                        await(done);
342                          assertEquals(THREADS, p.getLargestPoolSize());
343                      }});
344 <            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
316 <            assertEquals(THREADS, p.getLargestPoolSize());
317 <        } finally {
318 <            done.countDown();
319 <            joinPool(p);
344 >            await(threadsStarted);
345              assertEquals(THREADS, p.getLargestPoolSize());
346          }
347 +        assertEquals(THREADS, p.getLargestPoolSize());
348      }
349  
350      /**
# Line 330 | Line 356 | public class ThreadPoolExecutorTest exte
356              new ThreadPoolExecutor(2, 3,
357                                     LONG_DELAY_MS, MILLISECONDS,
358                                     new ArrayBlockingQueue<Runnable>(10));
359 <        assertEquals(3, p.getMaximumPoolSize());
360 <        joinPool(p);
359 >        try (PoolCleaner cleaner = cleaner(p)) {
360 >            assertEquals(3, p.getMaximumPoolSize());
361 >            p.setMaximumPoolSize(5);
362 >            assertEquals(5, p.getMaximumPoolSize());
363 >            p.setMaximumPoolSize(4);
364 >            assertEquals(4, p.getMaximumPoolSize());
365 >        }
366      }
367  
368      /**
# Line 339 | Line 370 | public class ThreadPoolExecutorTest exte
370       * become active
371       */
372      public void testGetPoolSize() throws InterruptedException {
373 +        final CountDownLatch done = new CountDownLatch(1);
374          final ThreadPoolExecutor p =
375              new ThreadPoolExecutor(1, 1,
376                                     LONG_DELAY_MS, MILLISECONDS,
377                                     new ArrayBlockingQueue<Runnable>(10));
378 <        final CountDownLatch threadStarted = new CountDownLatch(1);
347 <        final CountDownLatch done = new CountDownLatch(1);
348 <        try {
378 >        try (PoolCleaner cleaner = cleaner(p, done)) {
379              assertEquals(0, p.getPoolSize());
380 +            final CountDownLatch threadStarted = new CountDownLatch(1);
381              p.execute(new CheckedRunnable() {
382                  public void realRun() throws InterruptedException {
383                      threadStarted.countDown();
384                      assertEquals(1, p.getPoolSize());
385 <                    done.await();
385 >                    await(done);
386                  }});
387 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
387 >            await(threadStarted);
388              assertEquals(1, p.getPoolSize());
358        } finally {
359            done.countDown();
360            joinPool(p);
389          }
390      }
391  
# Line 365 | Line 393 | public class ThreadPoolExecutorTest exte
393       * getTaskCount increases, but doesn't overestimate, when tasks submitted
394       */
395      public void testGetTaskCount() throws InterruptedException {
396 +        final int TASKS = 3;
397 +        final CountDownLatch done = new CountDownLatch(1);
398          final ThreadPoolExecutor p =
399              new ThreadPoolExecutor(1, 1,
400                                     LONG_DELAY_MS, MILLISECONDS,
401                                     new ArrayBlockingQueue<Runnable>(10));
402 <        final CountDownLatch threadStarted = new CountDownLatch(1);
403 <        final CountDownLatch done = new CountDownLatch(1);
374 <        try {
402 >        try (PoolCleaner cleaner = cleaner(p, done)) {
403 >            final CountDownLatch threadStarted = new CountDownLatch(1);
404              assertEquals(0, p.getTaskCount());
405 +            assertEquals(0, p.getCompletedTaskCount());
406              p.execute(new CheckedRunnable() {
407                  public void realRun() throws InterruptedException {
408                      threadStarted.countDown();
409 <                    assertEquals(1, p.getTaskCount());
380 <                    done.await();
409 >                    await(done);
410                  }});
411 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
411 >            await(threadStarted);
412              assertEquals(1, p.getTaskCount());
413 <        } finally {
414 <            done.countDown();
415 <            joinPool(p);
413 >            assertEquals(0, p.getCompletedTaskCount());
414 >            for (int i = 0; i < TASKS; i++) {
415 >                assertEquals(1 + i, p.getTaskCount());
416 >                p.execute(new CheckedRunnable() {
417 >                    public void realRun() throws InterruptedException {
418 >                        threadStarted.countDown();
419 >                        assertEquals(1 + TASKS, p.getTaskCount());
420 >                        await(done);
421 >                    }});
422 >            }
423 >            assertEquals(1 + TASKS, p.getTaskCount());
424 >            assertEquals(0, p.getCompletedTaskCount());
425          }
426 +        assertEquals(1 + TASKS, p.getTaskCount());
427 +        assertEquals(1 + TASKS, p.getCompletedTaskCount());
428      }
429  
430      /**
# Line 395 | Line 435 | public class ThreadPoolExecutorTest exte
435              new ThreadPoolExecutor(1, 1,
436                                     LONG_DELAY_MS, MILLISECONDS,
437                                     new ArrayBlockingQueue<Runnable>(10));
438 <        assertFalse(p.isShutdown());
439 <        try { p.shutdown(); } catch (SecurityException ok) { return; }
440 <        assertTrue(p.isShutdown());
441 <        joinPool(p);
438 >        try (PoolCleaner cleaner = cleaner(p)) {
439 >            assertFalse(p.isShutdown());
440 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
441 >            assertTrue(p.isShutdown());
442 >        }
443 >    }
444 >
445 >    /**
446 >     * awaitTermination on a non-shutdown pool times out
447 >     */
448 >    public void testAwaitTermination_timesOut() throws InterruptedException {
449 >        final ThreadPoolExecutor p =
450 >            new ThreadPoolExecutor(1, 1,
451 >                                   LONG_DELAY_MS, MILLISECONDS,
452 >                                   new ArrayBlockingQueue<Runnable>(10));
453 >        try (PoolCleaner cleaner = cleaner(p)) {
454 >            assertFalse(p.isTerminated());
455 >            assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS));
456 >            assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
457 >            assertFalse(p.awaitTermination(-1L, NANOSECONDS));
458 >            assertFalse(p.awaitTermination(-1L, MILLISECONDS));
459 >            assertFalse(p.awaitTermination(0L, NANOSECONDS));
460 >            assertFalse(p.awaitTermination(0L, MILLISECONDS));
461 >            long timeoutNanos = 999999L;
462 >            long startTime = System.nanoTime();
463 >            assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
464 >            assertTrue(System.nanoTime() - startTime >= timeoutNanos);
465 >            assertFalse(p.isTerminated());
466 >            startTime = System.nanoTime();
467 >            long timeoutMillis = timeoutMillis();
468 >            assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS));
469 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
470 >            assertFalse(p.isTerminated());
471 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
472 >            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
473 >            assertTrue(p.isTerminated());
474 >        }
475      }
476  
477      /**
# Line 409 | Line 482 | public class ThreadPoolExecutorTest exte
482              new ThreadPoolExecutor(1, 1,
483                                     LONG_DELAY_MS, MILLISECONDS,
484                                     new ArrayBlockingQueue<Runnable>(10));
485 <        final CountDownLatch threadStarted = new CountDownLatch(1);
486 <        final CountDownLatch done = new CountDownLatch(1);
487 <        assertFalse(p.isTerminated());
488 <        try {
485 >        try (PoolCleaner cleaner = cleaner(p)) {
486 >            final CountDownLatch threadStarted = new CountDownLatch(1);
487 >            final CountDownLatch done = new CountDownLatch(1);
488 >            assertFalse(p.isTerminating());
489              p.execute(new CheckedRunnable() {
490                  public void realRun() throws InterruptedException {
491 <                    assertFalse(p.isTerminated());
491 >                    assertFalse(p.isTerminating());
492                      threadStarted.countDown();
493 <                    done.await();
493 >                    await(done);
494                  }});
495 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
495 >            await(threadStarted);
496              assertFalse(p.isTerminating());
497              done.countDown();
425        } finally {
498              try { p.shutdown(); } catch (SecurityException ok) { return; }
499 +            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
500 +            assertTrue(p.isTerminated());
501 +            assertFalse(p.isTerminating());
502          }
428        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
429        assertTrue(p.isTerminated());
503      }
504  
505      /**
# Line 437 | Line 510 | public class ThreadPoolExecutorTest exte
510              new ThreadPoolExecutor(1, 1,
511                                     LONG_DELAY_MS, MILLISECONDS,
512                                     new ArrayBlockingQueue<Runnable>(10));
513 <        final CountDownLatch threadStarted = new CountDownLatch(1);
514 <        final CountDownLatch done = new CountDownLatch(1);
515 <        try {
513 >        try (PoolCleaner cleaner = cleaner(p)) {
514 >            final CountDownLatch threadStarted = new CountDownLatch(1);
515 >            final CountDownLatch done = new CountDownLatch(1);
516              assertFalse(p.isTerminating());
517              p.execute(new CheckedRunnable() {
518                  public void realRun() throws InterruptedException {
519                      assertFalse(p.isTerminating());
520                      threadStarted.countDown();
521 <                    done.await();
521 >                    await(done);
522                  }});
523 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
523 >            await(threadStarted);
524              assertFalse(p.isTerminating());
525              done.countDown();
453        } finally {
526              try { p.shutdown(); } catch (SecurityException ok) { return; }
527 +            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
528 +            assertTrue(p.isTerminated());
529 +            assertFalse(p.isTerminating());
530          }
456        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
457        assertTrue(p.isTerminated());
458        assertFalse(p.isTerminating());
531      }
532  
533      /**
534       * getQueue returns the work queue, which contains queued tasks
535       */
536      public void testGetQueue() throws InterruptedException {
537 <        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
537 >        final CountDownLatch done = new CountDownLatch(1);
538 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
539          final ThreadPoolExecutor p =
540              new ThreadPoolExecutor(1, 1,
541                                     LONG_DELAY_MS, MILLISECONDS,
542                                     q);
543 <        final CountDownLatch threadStarted = new CountDownLatch(1);
544 <        final CountDownLatch done = new CountDownLatch(1);
472 <        try {
543 >        try (PoolCleaner cleaner = cleaner(p, done)) {
544 >            final CountDownLatch threadStarted = new CountDownLatch(1);
545              FutureTask[] tasks = new FutureTask[5];
546              for (int i = 0; i < tasks.length; i++) {
547                  Callable task = new CheckedCallable<Boolean>() {
548                      public Boolean realCall() throws InterruptedException {
549                          threadStarted.countDown();
550                          assertSame(q, p.getQueue());
551 <                        done.await();
551 >                        await(done);
552                          return Boolean.TRUE;
553                      }};
554                  tasks[i] = new FutureTask(task);
555                  p.execute(tasks[i]);
556              }
557 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
557 >            await(threadStarted);
558              assertSame(q, p.getQueue());
559              assertFalse(q.contains(tasks[0]));
560              assertTrue(q.contains(tasks[tasks.length - 1]));
561              assertEquals(tasks.length - 1, q.size());
490        } finally {
491            done.countDown();
492            joinPool(p);
562          }
563      }
564  
# Line 497 | Line 566 | public class ThreadPoolExecutorTest exte
566       * remove(task) removes queued task, and fails to remove active task
567       */
568      public void testRemove() throws InterruptedException {
569 <        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
569 >        final CountDownLatch done = new CountDownLatch(1);
570 >        BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
571          final ThreadPoolExecutor p =
572              new ThreadPoolExecutor(1, 1,
573                                     LONG_DELAY_MS, MILLISECONDS,
574                                     q);
575 <        Runnable[] tasks = new Runnable[5];
576 <        final CountDownLatch threadStarted = new CountDownLatch(1);
577 <        final CountDownLatch done = new CountDownLatch(1);
508 <        try {
575 >        try (PoolCleaner cleaner = cleaner(p, done)) {
576 >            Runnable[] tasks = new Runnable[6];
577 >            final CountDownLatch threadStarted = new CountDownLatch(1);
578              for (int i = 0; i < tasks.length; i++) {
579                  tasks[i] = new CheckedRunnable() {
580                      public void realRun() throws InterruptedException {
581                          threadStarted.countDown();
582 <                        done.await();
582 >                        await(done);
583                      }};
584                  p.execute(tasks[i]);
585              }
586 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
586 >            await(threadStarted);
587              assertFalse(p.remove(tasks[0]));
588              assertTrue(q.contains(tasks[4]));
589              assertTrue(q.contains(tasks[3]));
# Line 524 | Line 593 | public class ThreadPoolExecutorTest exte
593              assertTrue(q.contains(tasks[3]));
594              assertTrue(p.remove(tasks[3]));
595              assertFalse(q.contains(tasks[3]));
527        } finally {
528            done.countDown();
529            joinPool(p);
596          }
597      }
598  
# Line 536 | Line 602 | public class ThreadPoolExecutorTest exte
602      public void testPurge() throws InterruptedException {
603          final CountDownLatch threadStarted = new CountDownLatch(1);
604          final CountDownLatch done = new CountDownLatch(1);
605 <        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
605 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
606          final ThreadPoolExecutor p =
607              new ThreadPoolExecutor(1, 1,
608                                     LONG_DELAY_MS, MILLISECONDS,
609                                     q);
610 <        FutureTask[] tasks = new FutureTask[5];
611 <        try {
610 >        try (PoolCleaner cleaner = cleaner(p, done)) {
611 >            FutureTask[] tasks = new FutureTask[5];
612              for (int i = 0; i < tasks.length; i++) {
613                  Callable task = new CheckedCallable<Boolean>() {
614                      public Boolean realCall() throws InterruptedException {
615                          threadStarted.countDown();
616 <                        done.await();
616 >                        await(done);
617                          return Boolean.TRUE;
618                      }};
619                  tasks[i] = new FutureTask(task);
620                  p.execute(tasks[i]);
621              }
622 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
622 >            await(threadStarted);
623              assertEquals(tasks.length, p.getTaskCount());
624              assertEquals(tasks.length - 1, q.size());
625              assertEquals(1L, p.getActiveCount());
# Line 566 | Line 632 | public class ThreadPoolExecutorTest exte
632              p.purge();         // Nothing to do
633              assertEquals(tasks.length - 3, q.size());
634              assertEquals(tasks.length - 2, p.getTaskCount());
569        } finally {
570            done.countDown();
571            joinPool(p);
635          }
636      }
637  
638      /**
639 <     * shutdownNow returns a list containing tasks that were not run
639 >     * shutdownNow returns a list containing tasks that were not run,
640 >     * and those tasks are drained from the queue
641       */
642 <    public void testShutdownNow() {
642 >    public void testShutdownNow() throws InterruptedException {
643 >        final int poolSize = 2;
644 >        final int count = 5;
645 >        final AtomicInteger ran = new AtomicInteger(0);
646          final ThreadPoolExecutor p =
647 <            new ThreadPoolExecutor(1, 1,
647 >            new ThreadPoolExecutor(poolSize, poolSize,
648                                     LONG_DELAY_MS, MILLISECONDS,
649                                     new ArrayBlockingQueue<Runnable>(10));
650 <        List l;
651 <        try {
652 <            for (int i = 0; i < 5; i++)
586 <                p.execute(new MediumPossiblyInterruptedRunnable());
587 <        }
588 <        finally {
650 >        final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
651 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
652 >            threadsStarted.countDown();
653              try {
654 <                l = p.shutdownNow();
655 <            } catch (SecurityException ok) { return; }
654 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
655 >            } catch (InterruptedException success) {}
656 >            ran.getAndIncrement();
657 >        }};
658 >        for (int i = 0; i < count; i++)
659 >            p.execute(waiter);
660 >        await(threadsStarted);
661 >        assertEquals(poolSize, p.getActiveCount());
662 >        assertEquals(0, p.getCompletedTaskCount());
663 >        final List<Runnable> queuedTasks;
664 >        try {
665 >            queuedTasks = p.shutdownNow();
666 >        } catch (SecurityException ok) {
667 >            return; // Allowed in case test doesn't have privs
668          }
669          assertTrue(p.isShutdown());
670 <        assertTrue(l.size() <= 4);
670 >        assertTrue(p.getQueue().isEmpty());
671 >        assertEquals(count - poolSize, queuedTasks.size());
672 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
673 >        assertTrue(p.isTerminated());
674 >        assertEquals(poolSize, ran.get());
675 >        assertEquals(poolSize, p.getCompletedTaskCount());
676      }
677  
678      // Exception Tests
# Line 601 | Line 682 | public class ThreadPoolExecutorTest exte
682       */
683      public void testConstructor1() {
684          try {
685 <            new ThreadPoolExecutor(-1, 1,
605 <                                   LONG_DELAY_MS, MILLISECONDS,
685 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
686                                     new ArrayBlockingQueue<Runnable>(10));
687              shouldThrow();
688          } catch (IllegalArgumentException success) {}
# Line 613 | Line 693 | public class ThreadPoolExecutorTest exte
693       */
694      public void testConstructor2() {
695          try {
696 <            new ThreadPoolExecutor(1, -1,
617 <                                   LONG_DELAY_MS, MILLISECONDS,
696 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
697                                     new ArrayBlockingQueue<Runnable>(10));
698              shouldThrow();
699          } catch (IllegalArgumentException success) {}
# Line 625 | Line 704 | public class ThreadPoolExecutorTest exte
704       */
705      public void testConstructor3() {
706          try {
707 <            new ThreadPoolExecutor(1, 0,
629 <                                   LONG_DELAY_MS, MILLISECONDS,
707 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
708                                     new ArrayBlockingQueue<Runnable>(10));
709              shouldThrow();
710          } catch (IllegalArgumentException success) {}
# Line 637 | Line 715 | public class ThreadPoolExecutorTest exte
715       */
716      public void testConstructor4() {
717          try {
718 <            new ThreadPoolExecutor(1, 2,
641 <                                   -1L, MILLISECONDS,
718 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
719                                     new ArrayBlockingQueue<Runnable>(10));
720              shouldThrow();
721          } catch (IllegalArgumentException success) {}
# Line 649 | Line 726 | public class ThreadPoolExecutorTest exte
726       */
727      public void testConstructor5() {
728          try {
729 <            new ThreadPoolExecutor(2, 1,
653 <                                   LONG_DELAY_MS, MILLISECONDS,
729 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
730                                     new ArrayBlockingQueue<Runnable>(10));
731              shouldThrow();
732          } catch (IllegalArgumentException success) {}
# Line 661 | Line 737 | public class ThreadPoolExecutorTest exte
737       */
738      public void testConstructorNullPointerException() {
739          try {
740 <            new ThreadPoolExecutor(1, 2,
665 <                                   LONG_DELAY_MS, MILLISECONDS,
740 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
741                                     (BlockingQueue) null);
742              shouldThrow();
743          } catch (NullPointerException success) {}
# Line 673 | Line 748 | public class ThreadPoolExecutorTest exte
748       */
749      public void testConstructor6() {
750          try {
751 <            new ThreadPoolExecutor(-1, 1,
677 <                                   LONG_DELAY_MS, MILLISECONDS,
751 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
752                                     new ArrayBlockingQueue<Runnable>(10),
753                                     new SimpleThreadFactory());
754              shouldThrow();
# Line 686 | Line 760 | public class ThreadPoolExecutorTest exte
760       */
761      public void testConstructor7() {
762          try {
763 <            new ThreadPoolExecutor(1, -1,
690 <                                   LONG_DELAY_MS, MILLISECONDS,
763 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
764                                     new ArrayBlockingQueue<Runnable>(10),
765                                     new SimpleThreadFactory());
766              shouldThrow();
# Line 699 | Line 772 | public class ThreadPoolExecutorTest exte
772       */
773      public void testConstructor8() {
774          try {
775 <            new ThreadPoolExecutor(1, 0,
703 <                                   LONG_DELAY_MS, MILLISECONDS,
775 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
776                                     new ArrayBlockingQueue<Runnable>(10),
777                                     new SimpleThreadFactory());
778              shouldThrow();
# Line 712 | Line 784 | public class ThreadPoolExecutorTest exte
784       */
785      public void testConstructor9() {
786          try {
787 <            new ThreadPoolExecutor(1, 2,
716 <                                   -1L, MILLISECONDS,
787 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
788                                     new ArrayBlockingQueue<Runnable>(10),
789                                     new SimpleThreadFactory());
790              shouldThrow();
# Line 725 | Line 796 | public class ThreadPoolExecutorTest exte
796       */
797      public void testConstructor10() {
798          try {
799 <            new ThreadPoolExecutor(2, 1,
729 <                                   LONG_DELAY_MS, MILLISECONDS,
799 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
800                                     new ArrayBlockingQueue<Runnable>(10),
801                                     new SimpleThreadFactory());
802              shouldThrow();
# Line 738 | Line 808 | public class ThreadPoolExecutorTest exte
808       */
809      public void testConstructorNullPointerException2() {
810          try {
811 <            new ThreadPoolExecutor(1, 2,
742 <                                   LONG_DELAY_MS, MILLISECONDS,
811 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
812                                     (BlockingQueue) null,
813                                     new SimpleThreadFactory());
814              shouldThrow();
# Line 751 | Line 820 | public class ThreadPoolExecutorTest exte
820       */
821      public void testConstructorNullPointerException3() {
822          try {
823 <            new ThreadPoolExecutor(1, 2,
755 <                                   LONG_DELAY_MS, MILLISECONDS,
823 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
824                                     new ArrayBlockingQueue<Runnable>(10),
825                                     (ThreadFactory) null);
826              shouldThrow();
# Line 764 | Line 832 | public class ThreadPoolExecutorTest exte
832       */
833      public void testConstructor11() {
834          try {
835 <            new ThreadPoolExecutor(-1, 1,
768 <                                   LONG_DELAY_MS, MILLISECONDS,
835 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
836                                     new ArrayBlockingQueue<Runnable>(10),
837                                     new NoOpREHandler());
838              shouldThrow();
# Line 777 | Line 844 | public class ThreadPoolExecutorTest exte
844       */
845      public void testConstructor12() {
846          try {
847 <            new ThreadPoolExecutor(1, -1,
781 <                                   LONG_DELAY_MS, MILLISECONDS,
847 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
848                                     new ArrayBlockingQueue<Runnable>(10),
849                                     new NoOpREHandler());
850              shouldThrow();
# Line 790 | Line 856 | public class ThreadPoolExecutorTest exte
856       */
857      public void testConstructor13() {
858          try {
859 <            new ThreadPoolExecutor(1, 0,
794 <                                   LONG_DELAY_MS, MILLISECONDS,
859 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
860                                     new ArrayBlockingQueue<Runnable>(10),
861                                     new NoOpREHandler());
862              shouldThrow();
# Line 803 | Line 868 | public class ThreadPoolExecutorTest exte
868       */
869      public void testConstructor14() {
870          try {
871 <            new ThreadPoolExecutor(1, 2,
807 <                                   -1L, MILLISECONDS,
871 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
872                                     new ArrayBlockingQueue<Runnable>(10),
873                                     new NoOpREHandler());
874              shouldThrow();
# Line 816 | Line 880 | public class ThreadPoolExecutorTest exte
880       */
881      public void testConstructor15() {
882          try {
883 <            new ThreadPoolExecutor(2, 1,
820 <                                   LONG_DELAY_MS, MILLISECONDS,
883 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
884                                     new ArrayBlockingQueue<Runnable>(10),
885                                     new NoOpREHandler());
886              shouldThrow();
# Line 829 | Line 892 | public class ThreadPoolExecutorTest exte
892       */
893      public void testConstructorNullPointerException4() {
894          try {
895 <            new ThreadPoolExecutor(1, 2,
833 <                                   LONG_DELAY_MS, MILLISECONDS,
895 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
896                                     (BlockingQueue) null,
897                                     new NoOpREHandler());
898              shouldThrow();
# Line 842 | Line 904 | public class ThreadPoolExecutorTest exte
904       */
905      public void testConstructorNullPointerException5() {
906          try {
907 <            new ThreadPoolExecutor(1, 2,
846 <                                   LONG_DELAY_MS, MILLISECONDS,
907 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
908                                     new ArrayBlockingQueue<Runnable>(10),
909                                     (RejectedExecutionHandler) null);
910              shouldThrow();
# Line 855 | Line 916 | public class ThreadPoolExecutorTest exte
916       */
917      public void testConstructor16() {
918          try {
919 <            new ThreadPoolExecutor(-1, 1,
859 <                                   LONG_DELAY_MS, MILLISECONDS,
919 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
920                                     new ArrayBlockingQueue<Runnable>(10),
921                                     new SimpleThreadFactory(),
922                                     new NoOpREHandler());
# Line 869 | Line 929 | public class ThreadPoolExecutorTest exte
929       */
930      public void testConstructor17() {
931          try {
932 <            new ThreadPoolExecutor(1, -1,
873 <                                   LONG_DELAY_MS, MILLISECONDS,
932 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
933                                     new ArrayBlockingQueue<Runnable>(10),
934                                     new SimpleThreadFactory(),
935                                     new NoOpREHandler());
# Line 883 | Line 942 | public class ThreadPoolExecutorTest exte
942       */
943      public void testConstructor18() {
944          try {
945 <            new ThreadPoolExecutor(1, 0,
887 <                                   LONG_DELAY_MS, MILLISECONDS,
945 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
946                                     new ArrayBlockingQueue<Runnable>(10),
947                                     new SimpleThreadFactory(),
948                                     new NoOpREHandler());
# Line 897 | Line 955 | public class ThreadPoolExecutorTest exte
955       */
956      public void testConstructor19() {
957          try {
958 <            new ThreadPoolExecutor(1, 2,
901 <                                   -1L, MILLISECONDS,
958 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
959                                     new ArrayBlockingQueue<Runnable>(10),
960                                     new SimpleThreadFactory(),
961                                     new NoOpREHandler());
# Line 911 | Line 968 | public class ThreadPoolExecutorTest exte
968       */
969      public void testConstructor20() {
970          try {
971 <            new ThreadPoolExecutor(2, 1,
915 <                                   LONG_DELAY_MS, MILLISECONDS,
971 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
972                                     new ArrayBlockingQueue<Runnable>(10),
973                                     new SimpleThreadFactory(),
974                                     new NoOpREHandler());
# Line 925 | Line 981 | public class ThreadPoolExecutorTest exte
981       */
982      public void testConstructorNullPointerException6() {
983          try {
984 <            new ThreadPoolExecutor(1, 2,
929 <                                   LONG_DELAY_MS, MILLISECONDS,
984 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
985                                     (BlockingQueue) null,
986                                     new SimpleThreadFactory(),
987                                     new NoOpREHandler());
# Line 939 | Line 994 | public class ThreadPoolExecutorTest exte
994       */
995      public void testConstructorNullPointerException7() {
996          try {
997 <            new ThreadPoolExecutor(1, 2,
943 <                                   LONG_DELAY_MS, MILLISECONDS,
997 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
998                                     new ArrayBlockingQueue<Runnable>(10),
999                                     new SimpleThreadFactory(),
1000                                     (RejectedExecutionHandler) null);
# Line 953 | Line 1007 | public class ThreadPoolExecutorTest exte
1007       */
1008      public void testConstructorNullPointerException8() {
1009          try {
1010 <            new ThreadPoolExecutor(1, 2,
957 <                                   LONG_DELAY_MS, MILLISECONDS,
1010 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1011                                     new ArrayBlockingQueue<Runnable>(10),
1012                                     (ThreadFactory) null,
1013                                     new NoOpREHandler());
# Line 966 | Line 1019 | public class ThreadPoolExecutorTest exte
1019       * get of submitted callable throws InterruptedException if interrupted
1020       */
1021      public void testInterruptedSubmit() throws InterruptedException {
1022 +        final CountDownLatch done = new CountDownLatch(1);
1023          final ThreadPoolExecutor p =
1024              new ThreadPoolExecutor(1, 1,
1025 <                                   60, TimeUnit.SECONDS,
1025 >                                   60, SECONDS,
1026                                     new ArrayBlockingQueue<Runnable>(10));
1027  
1028 <        final CountDownLatch threadStarted = new CountDownLatch(1);
1029 <        final CountDownLatch done = new CountDownLatch(1);
976 <        try {
1028 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1029 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1030              Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1031                  public void realRun() throws Exception {
1032                      Callable task = new CheckedCallable<Boolean>() {
1033                          public Boolean realCall() throws InterruptedException {
1034                              threadStarted.countDown();
1035 <                            done.await();
1035 >                            await(done);
1036                              return Boolean.TRUE;
1037                          }};
1038                      p.submit(task).get();
1039                  }});
1040  
1041 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
1041 >            await(threadStarted);
1042              t.interrupt();
1043 <            awaitTermination(t, MEDIUM_DELAY_MS);
991 <        } finally {
992 <            done.countDown();
993 <            joinPool(p);
1043 >            awaitTermination(t);
1044          }
1045      }
1046  
# Line 998 | Line 1048 | public class ThreadPoolExecutorTest exte
1048       * execute throws RejectedExecutionException if saturated.
1049       */
1050      public void testSaturatedExecute() {
1051 <        ThreadPoolExecutor p =
1051 >        final CountDownLatch done = new CountDownLatch(1);
1052 >        final ThreadPoolExecutor p =
1053              new ThreadPoolExecutor(1, 1,
1054                                     LONG_DELAY_MS, MILLISECONDS,
1055                                     new ArrayBlockingQueue<Runnable>(1));
1056 <        final CountDownLatch done = new CountDownLatch(1);
1006 <        try {
1056 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1057              Runnable task = new CheckedRunnable() {
1058                  public void realRun() throws InterruptedException {
1059 <                    done.await();
1059 >                    await(done);
1060                  }};
1061              for (int i = 0; i < 2; ++i)
1062                  p.execute(task);
# Line 1017 | Line 1067 | public class ThreadPoolExecutorTest exte
1067                  } catch (RejectedExecutionException success) {}
1068                  assertTrue(p.getTaskCount() <= 2);
1069              }
1020        } finally {
1021            done.countDown();
1022            joinPool(p);
1070          }
1071      }
1072  
# Line 1027 | Line 1074 | public class ThreadPoolExecutorTest exte
1074       * submit(runnable) throws RejectedExecutionException if saturated.
1075       */
1076      public void testSaturatedSubmitRunnable() {
1077 <        ThreadPoolExecutor p =
1077 >        final CountDownLatch done = new CountDownLatch(1);
1078 >        final ThreadPoolExecutor p =
1079              new ThreadPoolExecutor(1, 1,
1080                                     LONG_DELAY_MS, MILLISECONDS,
1081                                     new ArrayBlockingQueue<Runnable>(1));
1082 <        final CountDownLatch done = new CountDownLatch(1);
1035 <        try {
1082 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1083              Runnable task = new CheckedRunnable() {
1084                  public void realRun() throws InterruptedException {
1085 <                    done.await();
1085 >                    await(done);
1086                  }};
1087              for (int i = 0; i < 2; ++i)
1088                  p.submit(task);
# Line 1046 | Line 1093 | public class ThreadPoolExecutorTest exte
1093                  } catch (RejectedExecutionException success) {}
1094                  assertTrue(p.getTaskCount() <= 2);
1095              }
1049        } finally {
1050            done.countDown();
1051            joinPool(p);
1096          }
1097      }
1098  
# Line 1056 | Line 1100 | public class ThreadPoolExecutorTest exte
1100       * submit(callable) throws RejectedExecutionException if saturated.
1101       */
1102      public void testSaturatedSubmitCallable() {
1103 <        ThreadPoolExecutor p =
1103 >        final CountDownLatch done = new CountDownLatch(1);
1104 >        final ThreadPoolExecutor p =
1105              new ThreadPoolExecutor(1, 1,
1106                                     LONG_DELAY_MS, MILLISECONDS,
1107                                     new ArrayBlockingQueue<Runnable>(1));
1108 <        final CountDownLatch done = new CountDownLatch(1);
1064 <        try {
1108 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1109              Runnable task = new CheckedRunnable() {
1110                  public void realRun() throws InterruptedException {
1111 <                    done.await();
1111 >                    await(done);
1112                  }};
1113              for (int i = 0; i < 2; ++i)
1114 <                p.submit(Executors.callable(task));
1114 >                p.execute(task);
1115              for (int i = 0; i < 2; ++i) {
1116                  try {
1117                      p.execute(task);
# Line 1075 | Line 1119 | public class ThreadPoolExecutorTest exte
1119                  } catch (RejectedExecutionException success) {}
1120                  assertTrue(p.getTaskCount() <= 2);
1121              }
1078        } finally {
1079            done.countDown();
1080            joinPool(p);
1122          }
1123      }
1124  
# Line 1085 | Line 1126 | public class ThreadPoolExecutorTest exte
1126       * executor using CallerRunsPolicy runs task if saturated.
1127       */
1128      public void testSaturatedExecute2() {
1088        RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
1129          final ThreadPoolExecutor p =
1130              new ThreadPoolExecutor(1, 1,
1131                                     LONG_DELAY_MS,
1132                                     MILLISECONDS,
1133                                     new ArrayBlockingQueue<Runnable>(1),
1134 <                                   h);
1135 <        try {
1134 >                                   new ThreadPoolExecutor.CallerRunsPolicy());
1135 >        try (PoolCleaner cleaner = cleaner(p)) {
1136 >            final CountDownLatch done = new CountDownLatch(1);
1137 >            Runnable blocker = new CheckedRunnable() {
1138 >                public void realRun() throws InterruptedException {
1139 >                    await(done);
1140 >                }};
1141 >            p.execute(blocker);
1142              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1143 <            for (int i = 0; i < tasks.length; ++i)
1143 >            for (int i = 0; i < tasks.length; i++)
1144                  tasks[i] = new TrackedNoOpRunnable();
1145 <            TrackedLongRunnable mr = new TrackedLongRunnable();
1100 <            p.execute(mr);
1101 <            for (int i = 0; i < tasks.length; ++i)
1145 >            for (int i = 0; i < tasks.length; i++)
1146                  p.execute(tasks[i]);
1147 <            for (int i = 1; i < tasks.length; ++i)
1147 >            for (int i = 1; i < tasks.length; i++)
1148                  assertTrue(tasks[i].done);
1149 <            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1150 <        } finally {
1107 <            joinPool(p);
1149 >            assertFalse(tasks[0].done); // waiting in queue
1150 >            done.countDown();
1151          }
1152      }
1153  
# Line 1112 | Line 1155 | public class ThreadPoolExecutorTest exte
1155       * executor using DiscardPolicy drops task if saturated.
1156       */
1157      public void testSaturatedExecute3() {
1158 <        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
1158 >        final CountDownLatch done = new CountDownLatch(1);
1159 >        final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1160 >        for (int i = 0; i < tasks.length; ++i)
1161 >            tasks[i] = new TrackedNoOpRunnable();
1162          final ThreadPoolExecutor p =
1163              new ThreadPoolExecutor(1, 1,
1164 <                                   LONG_DELAY_MS, MILLISECONDS,
1165 <                                   new ArrayBlockingQueue<Runnable>(1),
1166 <                                   h);
1167 <        try {
1168 <            TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1169 <            for (int i = 0; i < tasks.length; ++i)
1124 <                tasks[i] = new TrackedNoOpRunnable();
1125 <            p.execute(new TrackedLongRunnable());
1164 >                          LONG_DELAY_MS, MILLISECONDS,
1165 >                          new ArrayBlockingQueue<Runnable>(1),
1166 >                          new ThreadPoolExecutor.DiscardPolicy());
1167 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1168 >            p.execute(awaiter(done));
1169 >
1170              for (TrackedNoOpRunnable task : tasks)
1171                  p.execute(task);
1172 <            for (TrackedNoOpRunnable task : tasks)
1173 <                assertFalse(task.done);
1130 <            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1131 <        } finally {
1132 <            joinPool(p);
1172 >            for (int i = 1; i < tasks.length; i++)
1173 >                assertFalse(tasks[i].done);
1174          }
1175 +        for (int i = 1; i < tasks.length; i++)
1176 +            assertFalse(tasks[i].done);
1177 +        assertTrue(tasks[0].done); // was waiting in queue
1178      }
1179  
1180      /**
1181       * executor using DiscardOldestPolicy drops oldest task if saturated.
1182       */
1183      public void testSaturatedExecute4() {
1184 <        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
1184 >        final CountDownLatch done = new CountDownLatch(1);
1185 >        LatchAwaiter r1 = awaiter(done);
1186 >        LatchAwaiter r2 = awaiter(done);
1187 >        LatchAwaiter r3 = awaiter(done);
1188          final ThreadPoolExecutor p =
1189              new ThreadPoolExecutor(1, 1,
1190                                     LONG_DELAY_MS, MILLISECONDS,
1191                                     new ArrayBlockingQueue<Runnable>(1),
1192 <                                   h);
1193 <        try {
1194 <            p.execute(new TrackedLongRunnable());
1195 <            TrackedLongRunnable r2 = new TrackedLongRunnable();
1192 >                                   new ThreadPoolExecutor.DiscardOldestPolicy());
1193 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1194 >            assertEquals(LatchAwaiter.NEW, r1.state);
1195 >            assertEquals(LatchAwaiter.NEW, r2.state);
1196 >            assertEquals(LatchAwaiter.NEW, r3.state);
1197 >            p.execute(r1);
1198              p.execute(r2);
1199              assertTrue(p.getQueue().contains(r2));
1151            TrackedNoOpRunnable r3 = new TrackedNoOpRunnable();
1200              p.execute(r3);
1201              assertFalse(p.getQueue().contains(r2));
1202              assertTrue(p.getQueue().contains(r3));
1155            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1156        } finally {
1157            joinPool(p);
1203          }
1204 +        assertEquals(LatchAwaiter.DONE, r1.state);
1205 +        assertEquals(LatchAwaiter.NEW, r2.state);
1206 +        assertEquals(LatchAwaiter.DONE, r3.state);
1207      }
1208  
1209      /**
1210       * execute throws RejectedExecutionException if shutdown
1211       */
1212      public void testRejectedExecutionExceptionOnShutdown() {
1213 <        ThreadPoolExecutor p =
1213 >        final ThreadPoolExecutor p =
1214              new ThreadPoolExecutor(1, 1,
1215                                     LONG_DELAY_MS, MILLISECONDS,
1216                                     new ArrayBlockingQueue<Runnable>(1));
1217          try { p.shutdown(); } catch (SecurityException ok) { return; }
1218 <        try {
1219 <            p.execute(new NoOpRunnable());
1220 <            shouldThrow();
1221 <        } catch (RejectedExecutionException success) {}
1222 <
1223 <        joinPool(p);
1218 >        try (PoolCleaner cleaner = cleaner(p)) {
1219 >            try {
1220 >                p.execute(new NoOpRunnable());
1221 >                shouldThrow();
1222 >            } catch (RejectedExecutionException success) {}
1223 >        }
1224      }
1225  
1226      /**
# Line 1186 | Line 1234 | public class ThreadPoolExecutorTest exte
1234                                     new ArrayBlockingQueue<Runnable>(1), h);
1235  
1236          try { p.shutdown(); } catch (SecurityException ok) { return; }
1237 <        try {
1237 >        try (PoolCleaner cleaner = cleaner(p)) {
1238              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1239              p.execute(r);
1240              assertFalse(r.done);
1193        } finally {
1194            joinPool(p);
1241          }
1242      }
1243  
# Line 1199 | Line 1245 | public class ThreadPoolExecutorTest exte
1245       * execute using DiscardPolicy drops task on shutdown
1246       */
1247      public void testDiscardOnShutdown() {
1248 <        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
1203 <        ThreadPoolExecutor p =
1248 >        final ThreadPoolExecutor p =
1249              new ThreadPoolExecutor(1, 1,
1250                                     LONG_DELAY_MS, MILLISECONDS,
1251                                     new ArrayBlockingQueue<Runnable>(1),
1252 <                                   h);
1252 >                                   new ThreadPoolExecutor.DiscardPolicy());
1253  
1254          try { p.shutdown(); } catch (SecurityException ok) { return; }
1255 <        try {
1255 >        try (PoolCleaner cleaner = cleaner(p)) {
1256              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1257              p.execute(r);
1258              assertFalse(r.done);
1214        } finally {
1215            joinPool(p);
1259          }
1260      }
1261  
# Line 1220 | Line 1263 | public class ThreadPoolExecutorTest exte
1263       * execute using DiscardOldestPolicy drops task on shutdown
1264       */
1265      public void testDiscardOldestOnShutdown() {
1266 <        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
1224 <        ThreadPoolExecutor p =
1266 >        final ThreadPoolExecutor p =
1267              new ThreadPoolExecutor(1, 1,
1268                                     LONG_DELAY_MS, MILLISECONDS,
1269                                     new ArrayBlockingQueue<Runnable>(1),
1270 <                                   h);
1270 >                                   new ThreadPoolExecutor.DiscardOldestPolicy());
1271  
1272          try { p.shutdown(); } catch (SecurityException ok) { return; }
1273 <        try {
1273 >        try (PoolCleaner cleaner = cleaner(p)) {
1274              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1275              p.execute(r);
1276              assertFalse(r.done);
1235        } finally {
1236            joinPool(p);
1277          }
1278      }
1279  
# Line 1241 | Line 1281 | public class ThreadPoolExecutorTest exte
1281       * execute(null) throws NPE
1282       */
1283      public void testExecuteNull() {
1284 <        ThreadPoolExecutor p =
1284 >        final ThreadPoolExecutor p =
1285              new ThreadPoolExecutor(1, 2,
1286 <                                   LONG_DELAY_MS, MILLISECONDS,
1286 >                                   1L, SECONDS,
1287                                     new ArrayBlockingQueue<Runnable>(10));
1288 <        try {
1289 <            p.execute(null);
1290 <            shouldThrow();
1291 <        } catch (NullPointerException success) {}
1292 <
1293 <        joinPool(p);
1288 >        try (PoolCleaner cleaner = cleaner(p)) {
1289 >            try {
1290 >                p.execute(null);
1291 >                shouldThrow();
1292 >            } catch (NullPointerException success) {}
1293 >        }
1294      }
1295  
1296      /**
1297       * setCorePoolSize of negative value throws IllegalArgumentException
1298       */
1299      public void testCorePoolSizeIllegalArgumentException() {
1300 <        ThreadPoolExecutor p =
1300 >        final ThreadPoolExecutor p =
1301              new ThreadPoolExecutor(1, 2,
1302                                     LONG_DELAY_MS, MILLISECONDS,
1303                                     new ArrayBlockingQueue<Runnable>(10));
1304 <        try {
1305 <            p.setCorePoolSize(-1);
1306 <            shouldThrow();
1307 <        } catch (IllegalArgumentException success) {
1308 <        } finally {
1269 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1304 >        try (PoolCleaner cleaner = cleaner(p)) {
1305 >            try {
1306 >                p.setCorePoolSize(-1);
1307 >                shouldThrow();
1308 >            } catch (IllegalArgumentException success) {}
1309          }
1271        joinPool(p);
1310      }
1311  
1312      /**
# Line 1276 | Line 1314 | public class ThreadPoolExecutorTest exte
1314       * given a value less the core pool size
1315       */
1316      public void testMaximumPoolSizeIllegalArgumentException() {
1317 <        ThreadPoolExecutor p =
1317 >        final ThreadPoolExecutor p =
1318              new ThreadPoolExecutor(2, 3,
1319                                     LONG_DELAY_MS, MILLISECONDS,
1320                                     new ArrayBlockingQueue<Runnable>(10));
1321 <        try {
1322 <            p.setMaximumPoolSize(1);
1323 <            shouldThrow();
1324 <        } catch (IllegalArgumentException success) {
1325 <        } finally {
1288 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1321 >        try (PoolCleaner cleaner = cleaner(p)) {
1322 >            try {
1323 >                p.setMaximumPoolSize(1);
1324 >                shouldThrow();
1325 >            } catch (IllegalArgumentException success) {}
1326          }
1290        joinPool(p);
1327      }
1328  
1329      /**
# Line 1295 | Line 1331 | public class ThreadPoolExecutorTest exte
1331       * if given a negative value
1332       */
1333      public void testMaximumPoolSizeIllegalArgumentException2() {
1334 <        ThreadPoolExecutor p =
1334 >        final ThreadPoolExecutor p =
1335              new ThreadPoolExecutor(2, 3,
1336                                     LONG_DELAY_MS, MILLISECONDS,
1337                                     new ArrayBlockingQueue<Runnable>(10));
1338 <        try {
1339 <            p.setMaximumPoolSize(-1);
1340 <            shouldThrow();
1341 <        } catch (IllegalArgumentException success) {
1342 <        } finally {
1343 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1338 >        try (PoolCleaner cleaner = cleaner(p)) {
1339 >            try {
1340 >                p.setMaximumPoolSize(-1);
1341 >                shouldThrow();
1342 >            } catch (IllegalArgumentException success) {}
1343 >        }
1344 >    }
1345 >
1346 >    /**
1347 >     * Configuration changes that allow core pool size greater than
1348 >     * max pool size result in IllegalArgumentException.
1349 >     */
1350 >    public void testPoolSizeInvariants() {
1351 >        final ThreadPoolExecutor p =
1352 >            new ThreadPoolExecutor(1, 1,
1353 >                                   LONG_DELAY_MS, MILLISECONDS,
1354 >                                   new ArrayBlockingQueue<Runnable>(10));
1355 >        try (PoolCleaner cleaner = cleaner(p)) {
1356 >            for (int s = 1; s < 5; s++) {
1357 >                p.setMaximumPoolSize(s);
1358 >                p.setCorePoolSize(s);
1359 >                try {
1360 >                    p.setMaximumPoolSize(s - 1);
1361 >                    shouldThrow();
1362 >                } catch (IllegalArgumentException success) {}
1363 >                assertEquals(s, p.getCorePoolSize());
1364 >                assertEquals(s, p.getMaximumPoolSize());
1365 >                try {
1366 >                    p.setCorePoolSize(s + 1);
1367 >                    shouldThrow();
1368 >                } catch (IllegalArgumentException success) {}
1369 >                assertEquals(s, p.getCorePoolSize());
1370 >                assertEquals(s, p.getMaximumPoolSize());
1371 >            }
1372          }
1309        joinPool(p);
1373      }
1374  
1375      /**
# Line 1314 | Line 1377 | public class ThreadPoolExecutorTest exte
1377       * when given a negative value
1378       */
1379      public void testKeepAliveTimeIllegalArgumentException() {
1380 <        ThreadPoolExecutor p =
1380 >        final ThreadPoolExecutor p =
1381              new ThreadPoolExecutor(2, 3,
1382                                     LONG_DELAY_MS, MILLISECONDS,
1383                                     new ArrayBlockingQueue<Runnable>(10));
1384 <        try {
1385 <            p.setKeepAliveTime(-1,MILLISECONDS);
1386 <            shouldThrow();
1387 <        } catch (IllegalArgumentException success) {
1388 <        } finally {
1326 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1384 >        try (PoolCleaner cleaner = cleaner(p)) {
1385 >            try {
1386 >                p.setKeepAliveTime(-1, MILLISECONDS);
1387 >                shouldThrow();
1388 >            } catch (IllegalArgumentException success) {}
1389          }
1328        joinPool(p);
1390      }
1391  
1392      /**
# Line 1333 | Line 1394 | public class ThreadPoolExecutorTest exte
1394       */
1395      public void testTerminated() {
1396          ExtendedTPE p = new ExtendedTPE();
1397 <        try { p.shutdown(); } catch (SecurityException ok) { return; }
1398 <        assertTrue(p.terminatedCalled());
1399 <        joinPool(p);
1397 >        try (PoolCleaner cleaner = cleaner(p)) {
1398 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1399 >            assertTrue(p.terminatedCalled());
1400 >            assertTrue(p.isShutdown());
1401 >        }
1402      }
1403  
1404      /**
# Line 1343 | Line 1406 | public class ThreadPoolExecutorTest exte
1406       */
1407      public void testBeforeAfter() throws InterruptedException {
1408          ExtendedTPE p = new ExtendedTPE();
1409 <        try {
1409 >        try (PoolCleaner cleaner = cleaner(p)) {
1410              final CountDownLatch done = new CountDownLatch(1);
1411 <            final CheckedRunnable task = new CheckedRunnable() {
1411 >            p.execute(new CheckedRunnable() {
1412                  public void realRun() {
1413                      done.countDown();
1414 <                }};
1352 <            p.execute(task);
1414 >                }});
1415              await(p.afterCalled);
1416              assertEquals(0, done.getCount());
1417              assertTrue(p.afterCalled());
1418              assertTrue(p.beforeCalled());
1357            try { p.shutdown(); } catch (SecurityException ok) { return; }
1358        } finally {
1359            joinPool(p);
1419          }
1420      }
1421  
# Line 1364 | Line 1423 | public class ThreadPoolExecutorTest exte
1423       * completed submit of callable returns result
1424       */
1425      public void testSubmitCallable() throws Exception {
1426 <        ExecutorService e =
1426 >        final ExecutorService e =
1427              new ThreadPoolExecutor(2, 2,
1428                                     LONG_DELAY_MS, MILLISECONDS,
1429                                     new ArrayBlockingQueue<Runnable>(10));
1430 <        try {
1430 >        try (PoolCleaner cleaner = cleaner(e)) {
1431              Future<String> future = e.submit(new StringTask());
1432              String result = future.get();
1433              assertSame(TEST_STRING, result);
1375        } finally {
1376            joinPool(e);
1434          }
1435      }
1436  
# Line 1381 | Line 1438 | public class ThreadPoolExecutorTest exte
1438       * completed submit of runnable returns successfully
1439       */
1440      public void testSubmitRunnable() throws Exception {
1441 <        ExecutorService e =
1441 >        final ExecutorService e =
1442              new ThreadPoolExecutor(2, 2,
1443                                     LONG_DELAY_MS, MILLISECONDS,
1444                                     new ArrayBlockingQueue<Runnable>(10));
1445 <        try {
1445 >        try (PoolCleaner cleaner = cleaner(e)) {
1446              Future<?> future = e.submit(new NoOpRunnable());
1447              future.get();
1448              assertTrue(future.isDone());
1392        } finally {
1393            joinPool(e);
1449          }
1450      }
1451  
# Line 1398 | Line 1453 | public class ThreadPoolExecutorTest exte
1453       * completed submit of (runnable, result) returns result
1454       */
1455      public void testSubmitRunnable2() throws Exception {
1456 <        ExecutorService e =
1456 >        final ExecutorService e =
1457              new ThreadPoolExecutor(2, 2,
1458                                     LONG_DELAY_MS, MILLISECONDS,
1459                                     new ArrayBlockingQueue<Runnable>(10));
1460 <        try {
1460 >        try (PoolCleaner cleaner = cleaner(e)) {
1461              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1462              String result = future.get();
1463              assertSame(TEST_STRING, result);
1409        } finally {
1410            joinPool(e);
1464          }
1465      }
1466  
# Line 1415 | Line 1468 | public class ThreadPoolExecutorTest exte
1468       * invokeAny(null) throws NPE
1469       */
1470      public void testInvokeAny1() throws Exception {
1471 <        ExecutorService e =
1471 >        final ExecutorService e =
1472              new ThreadPoolExecutor(2, 2,
1473                                     LONG_DELAY_MS, MILLISECONDS,
1474                                     new ArrayBlockingQueue<Runnable>(10));
1475 <        try {
1476 <            e.invokeAny(null);
1477 <            shouldThrow();
1478 <        } catch (NullPointerException success) {
1479 <        } finally {
1427 <            joinPool(e);
1475 >        try (PoolCleaner cleaner = cleaner(e)) {
1476 >            try {
1477 >                e.invokeAny(null);
1478 >                shouldThrow();
1479 >            } catch (NullPointerException success) {}
1480          }
1481      }
1482  
# Line 1432 | Line 1484 | public class ThreadPoolExecutorTest exte
1484       * invokeAny(empty collection) throws IAE
1485       */
1486      public void testInvokeAny2() throws Exception {
1487 <        ExecutorService e =
1487 >        final ExecutorService e =
1488              new ThreadPoolExecutor(2, 2,
1489                                     LONG_DELAY_MS, MILLISECONDS,
1490                                     new ArrayBlockingQueue<Runnable>(10));
1491 <        try {
1492 <            e.invokeAny(new ArrayList<Callable<String>>());
1493 <            shouldThrow();
1494 <        } catch (IllegalArgumentException success) {
1495 <        } finally {
1444 <            joinPool(e);
1491 >        try (PoolCleaner cleaner = cleaner(e)) {
1492 >            try {
1493 >                e.invokeAny(new ArrayList<Callable<String>>());
1494 >                shouldThrow();
1495 >            } catch (IllegalArgumentException success) {}
1496          }
1497      }
1498  
# Line 1454 | Line 1505 | public class ThreadPoolExecutorTest exte
1505              new ThreadPoolExecutor(2, 2,
1506                                     LONG_DELAY_MS, MILLISECONDS,
1507                                     new ArrayBlockingQueue<Runnable>(10));
1508 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1509 <        l.add(latchAwaitingStringTask(latch));
1510 <        l.add(null);
1511 <        try {
1512 <            e.invokeAny(l);
1513 <            shouldThrow();
1514 <        } catch (NullPointerException success) {
1515 <        } finally {
1508 >        try (PoolCleaner cleaner = cleaner(e)) {
1509 >            List<Callable<String>> l = new ArrayList<>();
1510 >            l.add(latchAwaitingStringTask(latch));
1511 >            l.add(null);
1512 >            try {
1513 >                e.invokeAny(l);
1514 >                shouldThrow();
1515 >            } catch (NullPointerException success) {}
1516              latch.countDown();
1466            joinPool(e);
1517          }
1518      }
1519  
# Line 1471 | Line 1521 | public class ThreadPoolExecutorTest exte
1521       * invokeAny(c) throws ExecutionException if no task completes
1522       */
1523      public void testInvokeAny4() throws Exception {
1524 <        ExecutorService e =
1524 >        final ExecutorService e =
1525              new ThreadPoolExecutor(2, 2,
1526                                     LONG_DELAY_MS, MILLISECONDS,
1527                                     new ArrayBlockingQueue<Runnable>(10));
1528 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1529 <        l.add(new NPETask());
1530 <        try {
1531 <            e.invokeAny(l);
1532 <            shouldThrow();
1533 <        } catch (ExecutionException success) {
1534 <            assertTrue(success.getCause() instanceof NullPointerException);
1535 <        } finally {
1536 <            joinPool(e);
1528 >        try (PoolCleaner cleaner = cleaner(e)) {
1529 >            List<Callable<String>> l = new ArrayList<>();
1530 >            l.add(new NPETask());
1531 >            try {
1532 >                e.invokeAny(l);
1533 >                shouldThrow();
1534 >            } catch (ExecutionException success) {
1535 >                assertTrue(success.getCause() instanceof NullPointerException);
1536 >            }
1537          }
1538      }
1539  
# Line 1491 | Line 1541 | public class ThreadPoolExecutorTest exte
1541       * invokeAny(c) returns result of some task
1542       */
1543      public void testInvokeAny5() throws Exception {
1544 <        ExecutorService e =
1544 >        final ExecutorService e =
1545              new ThreadPoolExecutor(2, 2,
1546                                     LONG_DELAY_MS, MILLISECONDS,
1547                                     new ArrayBlockingQueue<Runnable>(10));
1548 <        try {
1549 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1548 >        try (PoolCleaner cleaner = cleaner(e)) {
1549 >            List<Callable<String>> l = new ArrayList<>();
1550              l.add(new StringTask());
1551              l.add(new StringTask());
1552              String result = e.invokeAny(l);
1553              assertSame(TEST_STRING, result);
1504        } finally {
1505            joinPool(e);
1554          }
1555      }
1556  
# Line 1510 | Line 1558 | public class ThreadPoolExecutorTest exte
1558       * invokeAll(null) throws NPE
1559       */
1560      public void testInvokeAll1() throws Exception {
1561 <        ExecutorService e =
1561 >        final ExecutorService e =
1562              new ThreadPoolExecutor(2, 2,
1563                                     LONG_DELAY_MS, MILLISECONDS,
1564                                     new ArrayBlockingQueue<Runnable>(10));
1565 <        try {
1566 <            e.invokeAll(null);
1567 <            shouldThrow();
1568 <        } catch (NullPointerException success) {
1569 <        } finally {
1522 <            joinPool(e);
1565 >        try (PoolCleaner cleaner = cleaner(e)) {
1566 >            try {
1567 >                e.invokeAll(null);
1568 >                shouldThrow();
1569 >            } catch (NullPointerException success) {}
1570          }
1571      }
1572  
# Line 1527 | Line 1574 | public class ThreadPoolExecutorTest exte
1574       * invokeAll(empty collection) returns empty collection
1575       */
1576      public void testInvokeAll2() throws InterruptedException {
1577 <        ExecutorService e =
1577 >        final ExecutorService e =
1578              new ThreadPoolExecutor(2, 2,
1579                                     LONG_DELAY_MS, MILLISECONDS,
1580                                     new ArrayBlockingQueue<Runnable>(10));
1581 <        try {
1581 >        try (PoolCleaner cleaner = cleaner(e)) {
1582              List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1583              assertTrue(r.isEmpty());
1537        } finally {
1538            joinPool(e);
1584          }
1585      }
1586  
# Line 1543 | Line 1588 | public class ThreadPoolExecutorTest exte
1588       * invokeAll(c) throws NPE if c has null elements
1589       */
1590      public void testInvokeAll3() throws Exception {
1591 <        ExecutorService e =
1591 >        final ExecutorService e =
1592              new ThreadPoolExecutor(2, 2,
1593                                     LONG_DELAY_MS, MILLISECONDS,
1594                                     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 {
1558 <            joinPool(e);
1595 >        try (PoolCleaner cleaner = cleaner(e)) {
1596 >            List<Callable<String>> l = new ArrayList<>();
1597 >            l.add(new StringTask());
1598 >            l.add(null);
1599 >            try {
1600 >                e.invokeAll(l);
1601 >                shouldThrow();
1602 >            } catch (NullPointerException success) {}
1603          }
1604      }
1605  
# Line 1563 | Line 1607 | public class ThreadPoolExecutorTest exte
1607       * get of element of invokeAll(c) throws exception on failed task
1608       */
1609      public void testInvokeAll4() throws Exception {
1610 <        ExecutorService e =
1610 >        final ExecutorService e =
1611              new ThreadPoolExecutor(2, 2,
1612                                     LONG_DELAY_MS, MILLISECONDS,
1613                                     new ArrayBlockingQueue<Runnable>(10));
1614 <        try {
1615 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1614 >        try (PoolCleaner cleaner = cleaner(e)) {
1615 >            List<Callable<String>> l = new ArrayList<>();
1616              l.add(new NPETask());
1617              List<Future<String>> futures = e.invokeAll(l);
1618              assertEquals(1, futures.size());
# Line 1578 | Line 1622 | public class ThreadPoolExecutorTest exte
1622              } catch (ExecutionException success) {
1623                  assertTrue(success.getCause() instanceof NullPointerException);
1624              }
1581        } finally {
1582            joinPool(e);
1625          }
1626      }
1627  
# Line 1587 | Line 1629 | public class ThreadPoolExecutorTest exte
1629       * invokeAll(c) returns results of all completed tasks
1630       */
1631      public void testInvokeAll5() throws Exception {
1632 <        ExecutorService e =
1632 >        final ExecutorService e =
1633              new ThreadPoolExecutor(2, 2,
1634                                     LONG_DELAY_MS, MILLISECONDS,
1635                                     new ArrayBlockingQueue<Runnable>(10));
1636 <        try {
1637 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1636 >        try (PoolCleaner cleaner = cleaner(e)) {
1637 >            List<Callable<String>> l = new ArrayList<>();
1638              l.add(new StringTask());
1639              l.add(new StringTask());
1640              List<Future<String>> futures = e.invokeAll(l);
1641              assertEquals(2, futures.size());
1642              for (Future<String> future : futures)
1643                  assertSame(TEST_STRING, future.get());
1602        } finally {
1603            joinPool(e);
1644          }
1645      }
1646  
# Line 1608 | Line 1648 | public class ThreadPoolExecutorTest exte
1648       * timed invokeAny(null) throws NPE
1649       */
1650      public void testTimedInvokeAny1() throws Exception {
1651 <        ExecutorService e =
1651 >        final ExecutorService e =
1652              new ThreadPoolExecutor(2, 2,
1653                                     LONG_DELAY_MS, MILLISECONDS,
1654                                     new ArrayBlockingQueue<Runnable>(10));
1655 <        try {
1656 <            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1657 <            shouldThrow();
1658 <        } catch (NullPointerException success) {
1659 <        } finally {
1620 <            joinPool(e);
1655 >        try (PoolCleaner cleaner = cleaner(e)) {
1656 >            try {
1657 >                e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1658 >                shouldThrow();
1659 >            } catch (NullPointerException success) {}
1660          }
1661      }
1662  
# Line 1625 | Line 1664 | public class ThreadPoolExecutorTest exte
1664       * timed invokeAny(,,null) throws NPE
1665       */
1666      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1667 <        ExecutorService e =
1667 >        final ExecutorService e =
1668              new ThreadPoolExecutor(2, 2,
1669                                     LONG_DELAY_MS, MILLISECONDS,
1670                                     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 {
1639 <            joinPool(e);
1671 >        try (PoolCleaner cleaner = cleaner(e)) {
1672 >            List<Callable<String>> l = new ArrayList<>();
1673 >            l.add(new StringTask());
1674 >            try {
1675 >                e.invokeAny(l, MEDIUM_DELAY_MS, null);
1676 >                shouldThrow();
1677 >            } catch (NullPointerException success) {}
1678          }
1679      }
1680  
# Line 1644 | Line 1682 | public class ThreadPoolExecutorTest exte
1682       * timed invokeAny(empty collection) throws IAE
1683       */
1684      public void testTimedInvokeAny2() throws Exception {
1685 <        ExecutorService e =
1685 >        final ExecutorService e =
1686              new ThreadPoolExecutor(2, 2,
1687                                     LONG_DELAY_MS, MILLISECONDS,
1688                                     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);
1689 >        try (PoolCleaner cleaner = cleaner(e)) {
1690 >            try {
1691 >                e.invokeAny(new ArrayList<Callable<String>>(),
1692 >                            MEDIUM_DELAY_MS, MILLISECONDS);
1693 >                shouldThrow();
1694 >            } catch (IllegalArgumentException success) {}
1695          }
1696      }
1697  
# Line 1666 | Line 1704 | public class ThreadPoolExecutorTest exte
1704              new ThreadPoolExecutor(2, 2,
1705                                     LONG_DELAY_MS, MILLISECONDS,
1706                                     new ArrayBlockingQueue<Runnable>(10));
1707 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1708 <        l.add(latchAwaitingStringTask(latch));
1709 <        l.add(null);
1710 <        try {
1711 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1712 <            shouldThrow();
1713 <        } catch (NullPointerException success) {
1714 <        } finally {
1707 >        try (PoolCleaner cleaner = cleaner(e)) {
1708 >            List<Callable<String>> l = new ArrayList<>();
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              latch.countDown();
1678            joinPool(e);
1716          }
1717      }
1718  
# Line 1683 | Line 1720 | public class ThreadPoolExecutorTest exte
1720       * timed invokeAny(c) throws ExecutionException if no task completes
1721       */
1722      public void testTimedInvokeAny4() throws Exception {
1723 <        ExecutorService e =
1723 >        final ExecutorService e =
1724              new ThreadPoolExecutor(2, 2,
1725                                     LONG_DELAY_MS, MILLISECONDS,
1726                                     new ArrayBlockingQueue<Runnable>(10));
1727 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1728 <        l.add(new NPETask());
1729 <        try {
1730 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1731 <            shouldThrow();
1732 <        } catch (ExecutionException success) {
1733 <            assertTrue(success.getCause() instanceof NullPointerException);
1734 <        } finally {
1735 <            joinPool(e);
1727 >        try (PoolCleaner cleaner = cleaner(e)) {
1728 >            long startTime = System.nanoTime();
1729 >            List<Callable<String>> l = new ArrayList<>();
1730 >            l.add(new NPETask());
1731 >            try {
1732 >                e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1733 >                shouldThrow();
1734 >            } catch (ExecutionException success) {
1735 >                assertTrue(success.getCause() instanceof NullPointerException);
1736 >            }
1737 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1738          }
1739      }
1740  
# Line 1703 | Line 1742 | public class ThreadPoolExecutorTest exte
1742       * timed invokeAny(c) returns result of some task
1743       */
1744      public void testTimedInvokeAny5() throws Exception {
1745 <        ExecutorService e =
1745 >        final ExecutorService e =
1746              new ThreadPoolExecutor(2, 2,
1747                                     LONG_DELAY_MS, MILLISECONDS,
1748                                     new ArrayBlockingQueue<Runnable>(10));
1749 <        try {
1750 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1749 >        try (PoolCleaner cleaner = cleaner(e)) {
1750 >            long startTime = System.nanoTime();
1751 >            List<Callable<String>> l = new ArrayList<>();
1752              l.add(new StringTask());
1753              l.add(new StringTask());
1754 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1754 >            String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1755              assertSame(TEST_STRING, result);
1756 <        } finally {
1717 <            joinPool(e);
1756 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1757          }
1758      }
1759  
# Line 1722 | Line 1761 | public class ThreadPoolExecutorTest exte
1761       * timed invokeAll(null) throws NPE
1762       */
1763      public void testTimedInvokeAll1() throws Exception {
1764 <        ExecutorService e =
1764 >        final ExecutorService e =
1765              new ThreadPoolExecutor(2, 2,
1766                                     LONG_DELAY_MS, MILLISECONDS,
1767                                     new ArrayBlockingQueue<Runnable>(10));
1768 <        try {
1769 <            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1770 <            shouldThrow();
1771 <        } catch (NullPointerException success) {
1772 <        } finally {
1734 <            joinPool(e);
1768 >        try (PoolCleaner cleaner = cleaner(e)) {
1769 >            try {
1770 >                e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1771 >                shouldThrow();
1772 >            } catch (NullPointerException success) {}
1773          }
1774      }
1775  
# Line 1739 | Line 1777 | public class ThreadPoolExecutorTest exte
1777       * timed invokeAll(,,null) throws NPE
1778       */
1779      public void testTimedInvokeAllNullTimeUnit() throws Exception {
1780 <        ExecutorService e =
1780 >        final ExecutorService e =
1781              new ThreadPoolExecutor(2, 2,
1782                                     LONG_DELAY_MS, MILLISECONDS,
1783                                     new ArrayBlockingQueue<Runnable>(10));
1784 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1785 <        l.add(new StringTask());
1786 <        try {
1787 <            e.invokeAll(l, MEDIUM_DELAY_MS, null);
1788 <            shouldThrow();
1789 <        } catch (NullPointerException success) {
1790 <        } finally {
1753 <            joinPool(e);
1784 >        try (PoolCleaner cleaner = cleaner(e)) {
1785 >            List<Callable<String>> l = new ArrayList<>();
1786 >            l.add(new StringTask());
1787 >            try {
1788 >                e.invokeAll(l, MEDIUM_DELAY_MS, null);
1789 >                shouldThrow();
1790 >            } catch (NullPointerException success) {}
1791          }
1792      }
1793  
# Line 1758 | Line 1795 | public class ThreadPoolExecutorTest exte
1795       * timed invokeAll(empty collection) returns empty collection
1796       */
1797      public void testTimedInvokeAll2() throws InterruptedException {
1798 <        ExecutorService e =
1798 >        final ExecutorService e =
1799              new ThreadPoolExecutor(2, 2,
1800                                     LONG_DELAY_MS, MILLISECONDS,
1801                                     new ArrayBlockingQueue<Runnable>(10));
1802 <        try {
1803 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1802 >        try (PoolCleaner cleaner = cleaner(e)) {
1803 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(),
1804 >                                                 MEDIUM_DELAY_MS, MILLISECONDS);
1805              assertTrue(r.isEmpty());
1768        } finally {
1769            joinPool(e);
1806          }
1807      }
1808  
# Line 1774 | Line 1810 | public class ThreadPoolExecutorTest exte
1810       * timed invokeAll(c) throws NPE if c has null elements
1811       */
1812      public void testTimedInvokeAll3() throws Exception {
1813 <        ExecutorService e =
1813 >        final ExecutorService e =
1814              new ThreadPoolExecutor(2, 2,
1815                                     LONG_DELAY_MS, MILLISECONDS,
1816                                     new ArrayBlockingQueue<Runnable>(10));
1817 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1818 <        l.add(new StringTask());
1819 <        l.add(null);
1820 <        try {
1821 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1822 <            shouldThrow();
1823 <        } catch (NullPointerException success) {
1824 <        } finally {
1789 <            joinPool(e);
1817 >        try (PoolCleaner cleaner = cleaner(e)) {
1818 >            List<Callable<String>> l = new ArrayList<>();
1819 >            l.add(new StringTask());
1820 >            l.add(null);
1821 >            try {
1822 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1823 >                shouldThrow();
1824 >            } catch (NullPointerException success) {}
1825          }
1826      }
1827  
# Line 1794 | Line 1829 | public class ThreadPoolExecutorTest exte
1829       * get of element of invokeAll(c) throws exception on failed task
1830       */
1831      public void testTimedInvokeAll4() throws Exception {
1832 <        ExecutorService e =
1832 >        final ExecutorService e =
1833              new ThreadPoolExecutor(2, 2,
1834                                     LONG_DELAY_MS, MILLISECONDS,
1835                                     new ArrayBlockingQueue<Runnable>(10));
1836 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1837 <        l.add(new NPETask());
1838 <        List<Future<String>> futures =
1839 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1840 <        assertEquals(1, futures.size());
1841 <        try {
1842 <            futures.get(0).get();
1843 <            shouldThrow();
1844 <        } catch (ExecutionException success) {
1845 <            assertTrue(success.getCause() instanceof NullPointerException);
1846 <        } finally {
1847 <            joinPool(e);
1836 >        try (PoolCleaner cleaner = cleaner(e)) {
1837 >            List<Callable<String>> l = new ArrayList<>();
1838 >            l.add(new NPETask());
1839 >            List<Future<String>> futures =
1840 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1841 >            assertEquals(1, futures.size());
1842 >            try {
1843 >                futures.get(0).get();
1844 >                shouldThrow();
1845 >            } catch (ExecutionException success) {
1846 >                assertTrue(success.getCause() instanceof NullPointerException);
1847 >            }
1848          }
1849      }
1850  
# Line 1817 | Line 1852 | public class ThreadPoolExecutorTest exte
1852       * timed invokeAll(c) returns results of all completed tasks
1853       */
1854      public void testTimedInvokeAll5() throws Exception {
1855 <        ExecutorService e =
1855 >        final ExecutorService e =
1856              new ThreadPoolExecutor(2, 2,
1857                                     LONG_DELAY_MS, MILLISECONDS,
1858                                     new ArrayBlockingQueue<Runnable>(10));
1859 <        try {
1860 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1859 >        try (PoolCleaner cleaner = cleaner(e)) {
1860 >            List<Callable<String>> l = new ArrayList<>();
1861              l.add(new StringTask());
1862              l.add(new StringTask());
1863              List<Future<String>> futures =
1864 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1864 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1865              assertEquals(2, futures.size());
1866              for (Future<String> future : futures)
1867                  assertSame(TEST_STRING, future.get());
1833        } finally {
1834            joinPool(e);
1868          }
1869      }
1870  
# Line 1839 | Line 1872 | public class ThreadPoolExecutorTest exte
1872       * timed invokeAll(c) cancels tasks not completed by timeout
1873       */
1874      public void testTimedInvokeAll6() throws Exception {
1875 <        ExecutorService e =
1876 <            new ThreadPoolExecutor(2, 2,
1877 <                                   LONG_DELAY_MS, MILLISECONDS,
1878 <                                   new ArrayBlockingQueue<Runnable>(10));
1879 <        try {
1880 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1881 <            l.add(new StringTask());
1882 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1883 <            l.add(new StringTask());
1884 <            List<Future<String>> futures =
1885 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1886 <            assertEquals(l.size(), futures.size());
1887 <            for (Future future : futures)
1888 <                assertTrue(future.isDone());
1889 <            assertFalse(futures.get(0).isCancelled());
1890 <            assertTrue(futures.get(1).isCancelled());
1891 <        } finally {
1892 <            joinPool(e);
1875 >        for (long timeout = timeoutMillis();;) {
1876 >            final CountDownLatch done = new CountDownLatch(1);
1877 >            final Callable<String> waiter = new CheckedCallable<String>() {
1878 >                public String realCall() {
1879 >                    try { done.await(LONG_DELAY_MS, MILLISECONDS); }
1880 >                    catch (InterruptedException ok) {}
1881 >                    return "1"; }};
1882 >            final ExecutorService p =
1883 >                new ThreadPoolExecutor(2, 2,
1884 >                                       LONG_DELAY_MS, MILLISECONDS,
1885 >                                       new ArrayBlockingQueue<Runnable>(10));
1886 >            try (PoolCleaner cleaner = cleaner(p, done)) {
1887 >                List<Callable<String>> tasks = new ArrayList<>();
1888 >                tasks.add(new StringTask("0"));
1889 >                tasks.add(waiter);
1890 >                tasks.add(new StringTask("2"));
1891 >                long startTime = System.nanoTime();
1892 >                List<Future<String>> futures =
1893 >                    p.invokeAll(tasks, timeout, MILLISECONDS);
1894 >                assertEquals(tasks.size(), futures.size());
1895 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1896 >                for (Future future : futures)
1897 >                    assertTrue(future.isDone());
1898 >                assertTrue(futures.get(1).isCancelled());
1899 >                try {
1900 >                    assertEquals("0", futures.get(0).get());
1901 >                    assertEquals("2", futures.get(2).get());
1902 >                    break;
1903 >                } catch (CancellationException retryWithLongerTimeout) {
1904 >                    timeout *= 2;
1905 >                    if (timeout >= LONG_DELAY_MS / 2)
1906 >                        fail("expected exactly one task to be cancelled");
1907 >                }
1908 >            }
1909          }
1910      }
1911  
# Line 1870 | Line 1919 | public class ThreadPoolExecutorTest exte
1919                                     LONG_DELAY_MS, MILLISECONDS,
1920                                     new LinkedBlockingQueue<Runnable>(),
1921                                     new FailingThreadFactory());
1922 <        try {
1922 >        try (PoolCleaner cleaner = cleaner(e)) {
1923              final int TASKS = 100;
1924              final CountDownLatch done = new CountDownLatch(TASKS);
1925              for (int k = 0; k < TASKS; ++k)
# Line 1879 | Line 1928 | public class ThreadPoolExecutorTest exte
1928                          done.countDown();
1929                      }});
1930              assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1882        } finally {
1883            joinPool(e);
1931          }
1932      }
1933  
# Line 1892 | Line 1939 | public class ThreadPoolExecutorTest exte
1939              new ThreadPoolExecutor(2, 2,
1940                                     1000, MILLISECONDS,
1941                                     new ArrayBlockingQueue<Runnable>(10));
1942 <        assertFalse(p.allowsCoreThreadTimeOut());
1943 <        joinPool(p);
1942 >        try (PoolCleaner cleaner = cleaner(p)) {
1943 >            assertFalse(p.allowsCoreThreadTimeOut());
1944 >        }
1945      }
1946  
1947      /**
1948       * allowCoreThreadTimeOut(true) causes idle threads to time out
1949       */
1950      public void testAllowCoreThreadTimeOut_true() throws Exception {
1951 <        long coreThreadTimeOut = SHORT_DELAY_MS;
1951 >        long keepAliveTime = timeoutMillis();
1952          final ThreadPoolExecutor p =
1953              new ThreadPoolExecutor(2, 10,
1954 <                                   coreThreadTimeOut, MILLISECONDS,
1954 >                                   keepAliveTime, MILLISECONDS,
1955                                     new ArrayBlockingQueue<Runnable>(10));
1956 <        final CountDownLatch threadStarted = new CountDownLatch(1);
1957 <        try {
1956 >        try (PoolCleaner cleaner = cleaner(p)) {
1957 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1958              p.allowCoreThreadTimeOut(true);
1959              p.execute(new CheckedRunnable() {
1960                  public void realRun() {
# Line 1914 | Line 1962 | public class ThreadPoolExecutorTest exte
1962                      assertEquals(1, p.getPoolSize());
1963                  }});
1964              await(threadStarted);
1965 <            delay(coreThreadTimeOut);
1965 >            delay(keepAliveTime);
1966              long startTime = System.nanoTime();
1967              while (p.getPoolSize() > 0
1968                     && millisElapsedSince(startTime) < LONG_DELAY_MS)
1969                  Thread.yield();
1970              assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1971              assertEquals(0, p.getPoolSize());
1924        } finally {
1925            joinPool(p);
1972          }
1973      }
1974  
# Line 1930 | Line 1976 | public class ThreadPoolExecutorTest exte
1976       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1977       */
1978      public void testAllowCoreThreadTimeOut_false() throws Exception {
1979 <        long coreThreadTimeOut = SHORT_DELAY_MS;
1979 >        long keepAliveTime = timeoutMillis();
1980          final ThreadPoolExecutor p =
1981              new ThreadPoolExecutor(2, 10,
1982 <                                   coreThreadTimeOut, MILLISECONDS,
1982 >                                   keepAliveTime, MILLISECONDS,
1983                                     new ArrayBlockingQueue<Runnable>(10));
1984 <        final CountDownLatch threadStarted = new CountDownLatch(1);
1985 <        try {
1984 >        try (PoolCleaner cleaner = cleaner(p)) {
1985 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1986              p.allowCoreThreadTimeOut(false);
1987              p.execute(new CheckedRunnable() {
1988                  public void realRun() throws InterruptedException {
1989                      threadStarted.countDown();
1990                      assertTrue(p.getPoolSize() >= 1);
1991                  }});
1992 <            delay(2 * coreThreadTimeOut);
1992 >            delay(2 * keepAliveTime);
1993              assertTrue(p.getPoolSize() >= 1);
1948        } finally {
1949            joinPool(p);
1994          }
1995      }
1996  
# Line 1962 | Line 2006 | public class ThreadPoolExecutorTest exte
2006                  done.countDown();
2007              }};
2008          final ThreadPoolExecutor p =
2009 <            new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS,
2009 >            new ThreadPoolExecutor(1, 30,
2010 >                                   60, SECONDS,
2011                                     new ArrayBlockingQueue(30));
2012 <        try {
2012 >        try (PoolCleaner cleaner = cleaner(p)) {
2013              for (int i = 0; i < nTasks; ++i) {
2014                  for (;;) {
2015                      try {
# Line 1976 | Line 2021 | public class ThreadPoolExecutorTest exte
2021              }
2022              // enough time to run all tasks
2023              assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS));
2024 <        } finally {
2025 <            joinPool(p);
2024 >        }
2025 >    }
2026 >
2027 >    /**
2028 >     * get(cancelled task) throws CancellationException
2029 >     */
2030 >    public void testGet_cancelled() throws Exception {
2031 >        final CountDownLatch done = new CountDownLatch(1);
2032 >        final ExecutorService e =
2033 >            new ThreadPoolExecutor(1, 1,
2034 >                                   LONG_DELAY_MS, MILLISECONDS,
2035 >                                   new LinkedBlockingQueue<Runnable>());
2036 >        try (PoolCleaner cleaner = cleaner(e, done)) {
2037 >            final CountDownLatch blockerStarted = new CountDownLatch(1);
2038 >            final List<Future<?>> futures = new ArrayList<>();
2039 >            for (int i = 0; i < 2; i++) {
2040 >                Runnable r = new CheckedRunnable() { public void realRun()
2041 >                                                         throws Throwable {
2042 >                    blockerStarted.countDown();
2043 >                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
2044 >                }};
2045 >                futures.add(e.submit(r));
2046 >            }
2047 >            await(blockerStarted);
2048 >            for (Future<?> future : futures) future.cancel(false);
2049 >            for (Future<?> future : futures) {
2050 >                try {
2051 >                    future.get();
2052 >                    shouldThrow();
2053 >                } catch (CancellationException success) {}
2054 >                try {
2055 >                    future.get(LONG_DELAY_MS, MILLISECONDS);
2056 >                    shouldThrow();
2057 >                } catch (CancellationException success) {}
2058 >                assertTrue(future.isCancelled());
2059 >                assertTrue(future.isDone());
2060 >            }
2061          }
2062      }
2063  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines