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

Comparing jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java (file contents):
Revision 1.21 by jsr166, Mon Oct 11 07:21:32 2010 UTC vs.
Revision 1.67 by jsr166, Sun Oct 4 02:24:49 2015 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
8  
9 import java.util.concurrent.*;
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 < import java.util.concurrent.locks.*;
10 > import static java.util.concurrent.TimeUnit.SECONDS;
11  
12 < import junit.framework.*;
13 < import java.util.*;
12 > import java.util.ArrayList;
13 > import java.util.List;
14 > import java.util.concurrent.ArrayBlockingQueue;
15 > import java.util.concurrent.BlockingQueue;
16 > import java.util.concurrent.Callable;
17 > import java.util.concurrent.CancellationException;
18 > import java.util.concurrent.CountDownLatch;
19 > import java.util.concurrent.ExecutionException;
20 > import java.util.concurrent.Executors;
21 > import java.util.concurrent.ExecutorService;
22 > import java.util.concurrent.Future;
23 > import java.util.concurrent.FutureTask;
24 > import java.util.concurrent.LinkedBlockingQueue;
25 > import java.util.concurrent.RejectedExecutionException;
26 > import java.util.concurrent.RejectedExecutionHandler;
27 > import java.util.concurrent.RunnableFuture;
28 > import java.util.concurrent.SynchronousQueue;
29 > import java.util.concurrent.ThreadFactory;
30 > import java.util.concurrent.ThreadPoolExecutor;
31 > import java.util.concurrent.TimeoutException;
32 > import java.util.concurrent.TimeUnit;
33 > import java.util.concurrent.atomic.AtomicInteger;
34 > import java.util.concurrent.locks.Condition;
35 > import java.util.concurrent.locks.ReentrantLock;
36 >
37 > import junit.framework.Test;
38 > import junit.framework.TestSuite;
39  
40   public class ThreadPoolExecutorSubclassTest extends JSR166TestCase {
41      public static void main(String[] args) {
42 <        junit.textui.TestRunner.run(suite());
42 >        main(suite(), args);
43      }
44      public static Test suite() {
45          return new TestSuite(ThreadPoolExecutorSubclassTest.class);
# Line 37 | Line 61 | public class ThreadPoolExecutorSubclassT
61          CustomTask(final Runnable r, final V res) {
62              if (r == null) throw new NullPointerException();
63              callable = new Callable<V>() {
64 <            public V call() throws Exception { r.run(); return res; }};
64 >                public V call() throws Exception { r.run(); return res; }};
65          }
66          public boolean isDone() {
67              lock.lock(); try { return done; } finally { lock.unlock() ; }
# Line 60 | Line 84 | public class ThreadPoolExecutorSubclassT
84              finally { lock.unlock() ; }
85          }
86          public void run() {
63            boolean runme;
87              lock.lock();
88              try {
89 <                runme = !done;
90 <                if (!runme)
91 <                    thread = Thread.currentThread();
89 >                if (done)
90 >                    return;
91 >                thread = Thread.currentThread();
92              }
93              finally { lock.unlock() ; }
71            if (!runme) return;
94              V v = null;
95              Exception e = null;
96              try {
# Line 79 | Line 101 | public class ThreadPoolExecutorSubclassT
101              }
102              lock.lock();
103              try {
104 <                result = v;
105 <                exception = e;
106 <                done = true;
107 <                thread = null;
108 <                cond.signalAll();
104 >                if (!done) {
105 >                    result = v;
106 >                    exception = e;
107 >                    done = true;
108 >                    thread = null;
109 >                    cond.signalAll();
110 >                }
111              }
112              finally { lock.unlock(); }
113          }
# Line 92 | Line 116 | public class ThreadPoolExecutorSubclassT
116              try {
117                  while (!done)
118                      cond.await();
119 +                if (cancelled)
120 +                    throw new CancellationException();
121                  if (exception != null)
122                      throw new ExecutionException(exception);
123                  return result;
# Line 103 | Line 129 | public class ThreadPoolExecutorSubclassT
129              long nanos = unit.toNanos(timeout);
130              lock.lock();
131              try {
132 <                for (;;) {
133 <                    if (done) break;
108 <                    if (nanos < 0)
132 >                while (!done) {
133 >                    if (nanos <= 0L)
134                          throw new TimeoutException();
135                      nanos = cond.awaitNanos(nanos);
136                  }
137 +                if (cancelled)
138 +                    throw new CancellationException();
139                  if (exception != null)
140                      throw new ExecutionException(exception);
141                  return result;
# Line 117 | Line 144 | public class ThreadPoolExecutorSubclassT
144          }
145      }
146  
120
147      static class CustomTPE extends ThreadPoolExecutor {
148          protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
149              return new CustomTask<V>(c);
# Line 164 | Line 190 | public class ThreadPoolExecutorSubclassT
190                workQueue, threadFactory, handler);
191          }
192  
193 <        volatile boolean beforeCalled = false;
194 <        volatile boolean afterCalled = false;
195 <        volatile boolean terminatedCalled = false;
193 >        final CountDownLatch beforeCalled = new CountDownLatch(1);
194 >        final CountDownLatch afterCalled = new CountDownLatch(1);
195 >        final CountDownLatch terminatedCalled = new CountDownLatch(1);
196 >
197          public CustomTPE() {
198              super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
199          }
200          protected void beforeExecute(Thread t, Runnable r) {
201 <            beforeCalled = true;
201 >            beforeCalled.countDown();
202          }
203          protected void afterExecute(Runnable r, Throwable t) {
204 <            afterCalled = true;
204 >            afterCalled.countDown();
205          }
206          protected void terminated() {
207 <            terminatedCalled = true;
207 >            terminatedCalled.countDown();
208          }
209  
210 +        public boolean beforeCalled() {
211 +            return beforeCalled.getCount() == 0;
212 +        }
213 +        public boolean afterCalled() {
214 +            return afterCalled.getCount() == 0;
215 +        }
216 +        public boolean terminatedCalled() {
217 +            return terminatedCalled.getCount() == 0;
218 +        }
219      }
220  
221      static class FailingThreadFactory implements ThreadFactory {
# Line 190 | Line 226 | public class ThreadPoolExecutorSubclassT
226          }
227      }
228  
193
229      /**
230       * execute successfully executes a runnable
231       */
232      public void testExecute() throws InterruptedException {
233          final ThreadPoolExecutor p =
234              new CustomTPE(1, 1,
235 <                          LONG_DELAY_MS, MILLISECONDS,
235 >                          2 * LONG_DELAY_MS, MILLISECONDS,
236                            new ArrayBlockingQueue<Runnable>(10));
237 <        final CountDownLatch done = new CountDownLatch(1);
238 <        final Runnable task = new CheckedRunnable() {
239 <            public void realRun() {
240 <                done.countDown();
206 <            }};
207 <        try {
237 >        try (PoolCleaner cleaner = cleaner(p)) {
238 >            final CountDownLatch done = new CountDownLatch(1);
239 >            final Runnable task = new CheckedRunnable() {
240 >                public void realRun() { done.countDown(); }};
241              p.execute(task);
242 <            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
210 <        } finally {
211 <            joinPool(p);
242 >            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
243          }
244      }
245  
# Line 221 | Line 252 | public class ThreadPoolExecutorSubclassT
252              new CustomTPE(2, 2,
253                            LONG_DELAY_MS, MILLISECONDS,
254                            new ArrayBlockingQueue<Runnable>(10));
255 <        final CountDownLatch threadStarted = new CountDownLatch(1);
256 <        final CountDownLatch done = new CountDownLatch(1);
257 <        try {
255 >        try (PoolCleaner cleaner = cleaner(p)) {
256 >            final CountDownLatch threadStarted = new CountDownLatch(1);
257 >            final CountDownLatch done = new CountDownLatch(1);
258              assertEquals(0, p.getActiveCount());
259              p.execute(new CheckedRunnable() {
260                  public void realRun() throws InterruptedException {
# Line 231 | Line 262 | public class ThreadPoolExecutorSubclassT
262                      assertEquals(1, p.getActiveCount());
263                      done.await();
264                  }});
265 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
265 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
266              assertEquals(1, p.getActiveCount());
236        } finally {
267              done.countDown();
238            joinPool(p);
268          }
269      }
270  
# Line 243 | Line 272 | public class ThreadPoolExecutorSubclassT
272       * prestartCoreThread starts a thread if under corePoolSize, else doesn't
273       */
274      public void testPrestartCoreThread() {
275 <        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
276 <        assertEquals(0, p.getPoolSize());
277 <        assertTrue(p.prestartCoreThread());
278 <        assertEquals(1, p.getPoolSize());
279 <        assertTrue(p.prestartCoreThread());
280 <        assertEquals(2, p.getPoolSize());
281 <        assertFalse(p.prestartCoreThread());
282 <        assertEquals(2, p.getPoolSize());
283 <        joinPool(p);
275 >        ThreadPoolExecutor p =
276 >            new CustomTPE(2, 6,
277 >                          LONG_DELAY_MS, MILLISECONDS,
278 >                          new ArrayBlockingQueue<Runnable>(10));
279 >        try (PoolCleaner cleaner = cleaner(p)) {
280 >            assertEquals(0, p.getPoolSize());
281 >            assertTrue(p.prestartCoreThread());
282 >            assertEquals(1, p.getPoolSize());
283 >            assertTrue(p.prestartCoreThread());
284 >            assertEquals(2, p.getPoolSize());
285 >            assertFalse(p.prestartCoreThread());
286 >            assertEquals(2, p.getPoolSize());
287 >            p.setCorePoolSize(4);
288 >            assertTrue(p.prestartCoreThread());
289 >            assertEquals(3, p.getPoolSize());
290 >            assertTrue(p.prestartCoreThread());
291 >            assertEquals(4, p.getPoolSize());
292 >            assertFalse(p.prestartCoreThread());
293 >            assertEquals(4, p.getPoolSize());
294 >        }
295      }
296  
297      /**
298       * prestartAllCoreThreads starts all corePoolSize threads
299       */
300      public void testPrestartAllCoreThreads() {
301 <        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
302 <        assertEquals(0, p.getPoolSize());
303 <        p.prestartAllCoreThreads();
304 <        assertEquals(2, p.getPoolSize());
305 <        p.prestartAllCoreThreads();
306 <        assertEquals(2, p.getPoolSize());
307 <        joinPool(p);
301 >        ThreadPoolExecutor p =
302 >            new CustomTPE(2, 6,
303 >                          LONG_DELAY_MS, MILLISECONDS,
304 >                          new ArrayBlockingQueue<Runnable>(10));
305 >        try (PoolCleaner cleaner = cleaner(p)) {
306 >            assertEquals(0, p.getPoolSize());
307 >            p.prestartAllCoreThreads();
308 >            assertEquals(2, p.getPoolSize());
309 >            p.prestartAllCoreThreads();
310 >            assertEquals(2, p.getPoolSize());
311 >            p.setCorePoolSize(4);
312 >            p.prestartAllCoreThreads();
313 >            assertEquals(4, p.getPoolSize());
314 >            p.prestartAllCoreThreads();
315 >            assertEquals(4, p.getPoolSize());
316 >        }
317      }
318  
319      /**
# Line 276 | Line 325 | public class ThreadPoolExecutorSubclassT
325              new CustomTPE(2, 2,
326                            LONG_DELAY_MS, MILLISECONDS,
327                            new ArrayBlockingQueue<Runnable>(10));
328 <        final CountDownLatch threadStarted = new CountDownLatch(1);
329 <        final CountDownLatch threadProceed = new CountDownLatch(1);
330 <        final CountDownLatch threadDone = new CountDownLatch(1);
331 <        try {
328 >        try (PoolCleaner cleaner = cleaner(p)) {
329 >            final CountDownLatch threadStarted = new CountDownLatch(1);
330 >            final CountDownLatch threadProceed = new CountDownLatch(1);
331 >            final CountDownLatch threadDone = new CountDownLatch(1);
332              assertEquals(0, p.getCompletedTaskCount());
333              p.execute(new CheckedRunnable() {
334                  public void realRun() throws InterruptedException {
# Line 288 | Line 337 | public class ThreadPoolExecutorSubclassT
337                      threadProceed.await();
338                      threadDone.countDown();
339                  }});
340 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
340 >            await(threadStarted);
341              assertEquals(0, p.getCompletedTaskCount());
342              threadProceed.countDown();
343              threadDone.await();
344 <            Thread.sleep(SHORT_DELAY_MS);
345 <            assertEquals(1, p.getCompletedTaskCount());
346 <        } finally {
347 <            joinPool(p);
344 >            long startTime = System.nanoTime();
345 >            while (p.getCompletedTaskCount() != 1) {
346 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
347 >                    fail("timed out");
348 >                Thread.yield();
349 >            }
350          }
351      }
352  
# Line 303 | Line 354 | public class ThreadPoolExecutorSubclassT
354       * getCorePoolSize returns size given in constructor if not otherwise set
355       */
356      public void testGetCorePoolSize() {
357 <        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
358 <        assertEquals(1, p.getCorePoolSize());
359 <        joinPool(p);
357 >        final ThreadPoolExecutor p =
358 >            new CustomTPE(1, 1,
359 >                          LONG_DELAY_MS, MILLISECONDS,
360 >                          new ArrayBlockingQueue<Runnable>(10));
361 >        try (PoolCleaner cleaner = cleaner(p)) {
362 >            assertEquals(1, p.getCorePoolSize());
363 >        }
364      }
365  
366      /**
367       * getKeepAliveTime returns value given in constructor if not otherwise set
368       */
369      public void testGetKeepAliveTime() {
370 <        ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
371 <        assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS));
372 <        joinPool(p);
370 >        final ThreadPoolExecutor p =
371 >            new CustomTPE(2, 2,
372 >                          1000, MILLISECONDS,
373 >                          new ArrayBlockingQueue<Runnable>(10));
374 >        try (PoolCleaner cleaner = cleaner(p)) {
375 >            assertEquals(1, p.getKeepAliveTime(SECONDS));
376 >        }
377      }
378  
320
379      /**
380       * getThreadFactory returns factory in constructor if not set
381       */
382      public void testGetThreadFactory() {
383 <        ThreadFactory tf = new SimpleThreadFactory();
384 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler());
385 <        assertSame(tf, p.getThreadFactory());
386 <        joinPool(p);
383 >        final ThreadFactory threadFactory = new SimpleThreadFactory();
384 >        final ThreadPoolExecutor p =
385 >            new CustomTPE(1, 2,
386 >                          LONG_DELAY_MS, MILLISECONDS,
387 >                          new ArrayBlockingQueue<Runnable>(10),
388 >                          threadFactory,
389 >                          new NoOpREHandler());
390 >        try (PoolCleaner cleaner = cleaner(p)) {
391 >            assertSame(threadFactory, p.getThreadFactory());
392 >        }
393      }
394  
395      /**
396       * setThreadFactory sets the thread factory returned by getThreadFactory
397       */
398      public void testSetThreadFactory() {
399 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
400 <        ThreadFactory tf = new SimpleThreadFactory();
401 <        p.setThreadFactory(tf);
402 <        assertSame(tf, p.getThreadFactory());
403 <        joinPool(p);
399 >        final ThreadPoolExecutor p =
400 >            new CustomTPE(1, 2,
401 >                          LONG_DELAY_MS, MILLISECONDS,
402 >                          new ArrayBlockingQueue<Runnable>(10));
403 >        try (PoolCleaner cleaner = cleaner(p)) {
404 >            ThreadFactory threadFactory = new SimpleThreadFactory();
405 >            p.setThreadFactory(threadFactory);
406 >            assertSame(threadFactory, p.getThreadFactory());
407 >        }
408      }
409  
342
410      /**
411       * setThreadFactory(null) throws NPE
412       */
413      public void testSetThreadFactoryNull() {
414 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
415 <        try {
416 <            p.setThreadFactory(null);
417 <            shouldThrow();
418 <        } catch (NullPointerException success) {
419 <        } finally {
420 <            joinPool(p);
414 >        final ThreadPoolExecutor p =
415 >            new CustomTPE(1, 2,
416 >                          LONG_DELAY_MS, MILLISECONDS,
417 >                          new ArrayBlockingQueue<Runnable>(10));
418 >        try (PoolCleaner cleaner = cleaner(p)) {
419 >            try {
420 >                p.setThreadFactory(null);
421 >                shouldThrow();
422 >            } catch (NullPointerException success) {}
423          }
424      }
425  
# Line 358 | Line 427 | public class ThreadPoolExecutorSubclassT
427       * getRejectedExecutionHandler returns handler in constructor if not set
428       */
429      public void testGetRejectedExecutionHandler() {
430 <        RejectedExecutionHandler h = new NoOpREHandler();
431 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h);
432 <        assertSame(h, p.getRejectedExecutionHandler());
433 <        joinPool(p);
430 >        final RejectedExecutionHandler handler = new NoOpREHandler();
431 >        final ThreadPoolExecutor p =
432 >            new CustomTPE(1, 2,
433 >                          LONG_DELAY_MS, MILLISECONDS,
434 >                          new ArrayBlockingQueue<Runnable>(10),
435 >                          handler);
436 >        try (PoolCleaner cleaner = cleaner(p)) {
437 >            assertSame(handler, p.getRejectedExecutionHandler());
438 >        }
439      }
440  
441      /**
# Line 369 | Line 443 | public class ThreadPoolExecutorSubclassT
443       * getRejectedExecutionHandler
444       */
445      public void testSetRejectedExecutionHandler() {
446 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
447 <        RejectedExecutionHandler h = new NoOpREHandler();
448 <        p.setRejectedExecutionHandler(h);
449 <        assertSame(h, p.getRejectedExecutionHandler());
450 <        joinPool(p);
446 >        final ThreadPoolExecutor p =
447 >            new CustomTPE(1, 2,
448 >                          LONG_DELAY_MS, MILLISECONDS,
449 >                          new ArrayBlockingQueue<Runnable>(10));
450 >        try (PoolCleaner cleaner = cleaner(p)) {
451 >            RejectedExecutionHandler handler = new NoOpREHandler();
452 >            p.setRejectedExecutionHandler(handler);
453 >            assertSame(handler, p.getRejectedExecutionHandler());
454 >        }
455      }
456  
379
457      /**
458       * setRejectedExecutionHandler(null) throws NPE
459       */
460      public void testSetRejectedExecutionHandlerNull() {
461 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
462 <        try {
463 <            p.setRejectedExecutionHandler(null);
464 <            shouldThrow();
465 <        } catch (NullPointerException success) {
466 <        } finally {
467 <            joinPool(p);
461 >        final ThreadPoolExecutor p =
462 >            new CustomTPE(1, 2,
463 >                          LONG_DELAY_MS, MILLISECONDS,
464 >                          new ArrayBlockingQueue<Runnable>(10));
465 >        try (PoolCleaner cleaner = cleaner(p)) {
466 >            try {
467 >                p.setRejectedExecutionHandler(null);
468 >                shouldThrow();
469 >            } catch (NullPointerException success) {}
470          }
471      }
472  
394
473      /**
474       * getLargestPoolSize increases, but doesn't overestimate, when
475       * multiple threads active
# Line 402 | Line 480 | public class ThreadPoolExecutorSubclassT
480              new CustomTPE(THREADS, THREADS,
481                            LONG_DELAY_MS, MILLISECONDS,
482                            new ArrayBlockingQueue<Runnable>(10));
483 <        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
484 <        final CountDownLatch done = new CountDownLatch(1);
485 <        try {
483 >        try (PoolCleaner cleaner = cleaner(p)) {
484 >            final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
485 >            final CountDownLatch done = new CountDownLatch(1);
486              assertEquals(0, p.getLargestPoolSize());
487              for (int i = 0; i < THREADS; i++)
488                  p.execute(new CheckedRunnable() {
# Line 413 | Line 491 | public class ThreadPoolExecutorSubclassT
491                          done.await();
492                          assertEquals(THREADS, p.getLargestPoolSize());
493                      }});
494 <            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
417 <            assertEquals(THREADS, p.getLargestPoolSize());
418 <        } finally {
419 <            done.countDown();
420 <            joinPool(p);
494 >            assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
495              assertEquals(THREADS, p.getLargestPoolSize());
496 +            done.countDown();   // release pool
497          }
498 +        assertEquals(THREADS, p.getLargestPoolSize());
499      }
500  
501      /**
# Line 427 | Line 503 | public class ThreadPoolExecutorSubclassT
503       * otherwise set
504       */
505      public void testGetMaximumPoolSize() {
506 <        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
507 <        assertEquals(2, p.getMaximumPoolSize());
508 <        joinPool(p);
506 >        final ThreadPoolExecutor p =
507 >            new CustomTPE(2, 3,
508 >                          LONG_DELAY_MS, MILLISECONDS,
509 >                          new ArrayBlockingQueue<Runnable>(10));
510 >        try (PoolCleaner cleaner = cleaner(p)) {
511 >            assertEquals(3, p.getMaximumPoolSize());
512 >            p.setMaximumPoolSize(5);
513 >            assertEquals(5, p.getMaximumPoolSize());
514 >            p.setMaximumPoolSize(4);
515 >            assertEquals(4, p.getMaximumPoolSize());
516 >        }
517      }
518  
519      /**
# Line 441 | Line 525 | public class ThreadPoolExecutorSubclassT
525              new CustomTPE(1, 1,
526                            LONG_DELAY_MS, MILLISECONDS,
527                            new ArrayBlockingQueue<Runnable>(10));
528 <        final CountDownLatch threadStarted = new CountDownLatch(1);
529 <        final CountDownLatch done = new CountDownLatch(1);
530 <        try {
528 >        try (PoolCleaner cleaner = cleaner(p)) {
529 >            final CountDownLatch threadStarted = new CountDownLatch(1);
530 >            final CountDownLatch done = new CountDownLatch(1);
531              assertEquals(0, p.getPoolSize());
532              p.execute(new CheckedRunnable() {
533                  public void realRun() throws InterruptedException {
# Line 451 | Line 535 | public class ThreadPoolExecutorSubclassT
535                      assertEquals(1, p.getPoolSize());
536                      done.await();
537                  }});
538 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
538 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
539              assertEquals(1, p.getPoolSize());
540 <        } finally {
457 <            done.countDown();
458 <            joinPool(p);
540 >            done.countDown();   // release pool
541          }
542      }
543  
# Line 467 | Line 549 | public class ThreadPoolExecutorSubclassT
549              new CustomTPE(1, 1,
550                            LONG_DELAY_MS, MILLISECONDS,
551                            new ArrayBlockingQueue<Runnable>(10));
552 <        final CountDownLatch threadStarted = new CountDownLatch(1);
553 <        final CountDownLatch done = new CountDownLatch(1);
554 <        try {
552 >        try (PoolCleaner cleaner = cleaner(p)) {
553 >            final CountDownLatch threadStarted = new CountDownLatch(1);
554 >            final CountDownLatch done = new CountDownLatch(1);
555              assertEquals(0, p.getTaskCount());
556              p.execute(new CheckedRunnable() {
557                  public void realRun() throws InterruptedException {
# Line 477 | Line 559 | public class ThreadPoolExecutorSubclassT
559                      assertEquals(1, p.getTaskCount());
560                      done.await();
561                  }});
562 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
562 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
563              assertEquals(1, p.getTaskCount());
482        } finally {
564              done.countDown();
484            joinPool(p);
565          }
566      }
567  
568      /**
569 <     * isShutDown is false before shutdown, true after
569 >     * isShutdown is false before shutdown, true after
570       */
571      public void testIsShutdown() {
572  
# Line 497 | Line 577 | public class ThreadPoolExecutorSubclassT
577          joinPool(p);
578      }
579  
500
580      /**
581       * isTerminated is false before termination, true after
582       */
# Line 512 | Line 591 | public class ThreadPoolExecutorSubclassT
591              assertFalse(p.isTerminating());
592              p.execute(new CheckedRunnable() {
593                  public void realRun() throws InterruptedException {
515                    threadStarted.countDown();
594                      assertFalse(p.isTerminating());
595 +                    threadStarted.countDown();
596                      done.await();
597                  }});
598 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
598 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
599              assertFalse(p.isTerminating());
600              done.countDown();
601          } finally {
# Line 541 | Line 620 | public class ThreadPoolExecutorSubclassT
620              assertFalse(p.isTerminating());
621              p.execute(new CheckedRunnable() {
622                  public void realRun() throws InterruptedException {
544                    threadStarted.countDown();
623                      assertFalse(p.isTerminating());
624 +                    threadStarted.countDown();
625                      done.await();
626                  }});
627 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
627 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
628              assertFalse(p.isTerminating());
629              done.countDown();
630          } finally {
# Line 580 | Line 659 | public class ThreadPoolExecutorSubclassT
659                  tasks[i] = new FutureTask(task);
660                  p.execute(tasks[i]);
661              }
662 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
662 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
663              assertSame(q, p.getQueue());
664              assertFalse(q.contains(tasks[0]));
665              assertTrue(q.contains(tasks[tasks.length - 1]));
# Line 612 | Line 691 | public class ThreadPoolExecutorSubclassT
691                          }};
692                  p.execute(tasks[i]);
693              }
694 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
694 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
695              assertFalse(p.remove(tasks[0]));
696              assertTrue(q.contains(tasks[4]));
697              assertTrue(q.contains(tasks[3]));
# Line 651 | Line 730 | public class ThreadPoolExecutorSubclassT
730                  tasks[i] = new FutureTask(task);
731                  p.execute(tasks[i]);
732              }
733 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
733 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
734              assertEquals(tasks.length, p.getTaskCount());
735              assertEquals(tasks.length - 1, q.size());
736              assertEquals(1L, p.getActiveCount());
# Line 671 | Line 750 | public class ThreadPoolExecutorSubclassT
750      }
751  
752      /**
753 <     * shutDownNow returns a list containing tasks that were not run
753 >     * shutdownNow returns a list containing tasks that were not run,
754 >     * and those tasks are drained from the queue
755       */
756 <    public void testShutDownNow() {
757 <        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
758 <        List l;
759 <        try {
760 <            for (int i = 0; i < 5; i++)
761 <                p.execute(new MediumPossiblyInterruptedRunnable());
762 <        }
763 <        finally {
756 >    public void testShutdownNow() throws InterruptedException {
757 >        final int poolSize = 2;
758 >        final int count = 5;
759 >        final AtomicInteger ran = new AtomicInteger(0);
760 >        ThreadPoolExecutor p =
761 >            new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS,
762 >                          new ArrayBlockingQueue<Runnable>(10));
763 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
764 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
765 >            threadsStarted.countDown();
766              try {
767 <                l = p.shutdownNow();
768 <            } catch (SecurityException ok) { return; }
767 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
768 >            } catch (InterruptedException success) {}
769 >            ran.getAndIncrement();
770 >        }};
771 >        for (int i = 0; i < count; i++)
772 >            p.execute(waiter);
773 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
774 >        assertEquals(poolSize, p.getActiveCount());
775 >        assertEquals(0, p.getCompletedTaskCount());
776 >        final List<Runnable> queuedTasks;
777 >        try {
778 >            queuedTasks = p.shutdownNow();
779 >        } catch (SecurityException ok) {
780 >            return; // Allowed in case test doesn't have privs
781          }
782          assertTrue(p.isShutdown());
783 <        assertTrue(l.size() <= 4);
783 >        assertTrue(p.getQueue().isEmpty());
784 >        assertEquals(count - poolSize, queuedTasks.size());
785 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
786 >        assertTrue(p.isTerminated());
787 >        assertEquals(poolSize, ran.get());
788 >        assertEquals(poolSize, p.getCompletedTaskCount());
789      }
790  
791      // Exception Tests
792  
694
793      /**
794       * Constructor throws if corePoolSize argument is less than zero
795       */
796      public void testConstructor1() {
797          try {
798 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
798 >            new CustomTPE(-1, 1, 1L, SECONDS,
799 >                          new ArrayBlockingQueue<Runnable>(10));
800              shouldThrow();
801          } catch (IllegalArgumentException success) {}
802      }
# Line 707 | Line 806 | public class ThreadPoolExecutorSubclassT
806       */
807      public void testConstructor2() {
808          try {
809 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
809 >            new CustomTPE(1, -1, 1L, SECONDS,
810 >                          new ArrayBlockingQueue<Runnable>(10));
811              shouldThrow();
812          } catch (IllegalArgumentException success) {}
813      }
# Line 717 | Line 817 | public class ThreadPoolExecutorSubclassT
817       */
818      public void testConstructor3() {
819          try {
820 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
820 >            new CustomTPE(1, 0, 1L, SECONDS,
821 >                          new ArrayBlockingQueue<Runnable>(10));
822              shouldThrow();
823          } catch (IllegalArgumentException success) {}
824      }
# Line 727 | Line 828 | public class ThreadPoolExecutorSubclassT
828       */
829      public void testConstructor4() {
830          try {
831 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
831 >            new CustomTPE(1, 2, -1L, SECONDS,
832 >                          new ArrayBlockingQueue<Runnable>(10));
833              shouldThrow();
834          } catch (IllegalArgumentException success) {}
835      }
# Line 737 | Line 839 | public class ThreadPoolExecutorSubclassT
839       */
840      public void testConstructor5() {
841          try {
842 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
842 >            new CustomTPE(2, 1, 1L, SECONDS,
843 >                          new ArrayBlockingQueue<Runnable>(10));
844              shouldThrow();
845          } catch (IllegalArgumentException success) {}
846      }
# Line 747 | Line 850 | public class ThreadPoolExecutorSubclassT
850       */
851      public void testConstructorNullPointerException() {
852          try {
853 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null);
853 >            new CustomTPE(1, 2, 1L, SECONDS, null);
854              shouldThrow();
855          } catch (NullPointerException success) {}
856      }
857  
755
756
858      /**
859       * Constructor throws if corePoolSize argument is less than zero
860       */
861      public void testConstructor6() {
862          try {
863 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
863 >            new CustomTPE(-1, 1, 1L, SECONDS,
864 >                          new ArrayBlockingQueue<Runnable>(10),
865 >                          new SimpleThreadFactory());
866              shouldThrow();
867          } catch (IllegalArgumentException success) {}
868      }
# Line 769 | Line 872 | public class ThreadPoolExecutorSubclassT
872       */
873      public void testConstructor7() {
874          try {
875 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
875 >            new CustomTPE(1,-1, 1L, SECONDS,
876 >                          new ArrayBlockingQueue<Runnable>(10),
877 >                          new SimpleThreadFactory());
878              shouldThrow();
879          } catch (IllegalArgumentException success) {}
880      }
# Line 779 | Line 884 | public class ThreadPoolExecutorSubclassT
884       */
885      public void testConstructor8() {
886          try {
887 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
887 >            new CustomTPE(1, 0, 1L, SECONDS,
888 >                          new ArrayBlockingQueue<Runnable>(10),
889 >                          new SimpleThreadFactory());
890              shouldThrow();
891          } catch (IllegalArgumentException success) {}
892      }
# Line 789 | Line 896 | public class ThreadPoolExecutorSubclassT
896       */
897      public void testConstructor9() {
898          try {
899 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
899 >            new CustomTPE(1, 2, -1L, SECONDS,
900 >                          new ArrayBlockingQueue<Runnable>(10),
901 >                          new SimpleThreadFactory());
902              shouldThrow();
903          } catch (IllegalArgumentException success) {}
904      }
# Line 799 | Line 908 | public class ThreadPoolExecutorSubclassT
908       */
909      public void testConstructor10() {
910          try {
911 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
911 >            new CustomTPE(2, 1, 1L, SECONDS,
912 >                          new ArrayBlockingQueue<Runnable>(10),
913 >                          new SimpleThreadFactory());
914              shouldThrow();
915          } catch (IllegalArgumentException success) {}
916      }
# Line 809 | Line 920 | public class ThreadPoolExecutorSubclassT
920       */
921      public void testConstructorNullPointerException2() {
922          try {
923 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
923 >            new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory());
924              shouldThrow();
925          } catch (NullPointerException success) {}
926      }
# Line 819 | Line 930 | public class ThreadPoolExecutorSubclassT
930       */
931      public void testConstructorNullPointerException3() {
932          try {
933 <            ThreadFactory f = null;
934 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
933 >            new CustomTPE(1, 2, 1L, SECONDS,
934 >                          new ArrayBlockingQueue<Runnable>(10),
935 >                          (ThreadFactory) null);
936              shouldThrow();
937          } catch (NullPointerException success) {}
938      }
939  
828
940      /**
941       * Constructor throws if corePoolSize argument is less than zero
942       */
943      public void testConstructor11() {
944          try {
945 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
945 >            new CustomTPE(-1, 1, 1L, SECONDS,
946 >                          new ArrayBlockingQueue<Runnable>(10),
947 >                          new NoOpREHandler());
948              shouldThrow();
949          } catch (IllegalArgumentException success) {}
950      }
# Line 841 | Line 954 | public class ThreadPoolExecutorSubclassT
954       */
955      public void testConstructor12() {
956          try {
957 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
957 >            new CustomTPE(1, -1, 1L, SECONDS,
958 >                          new ArrayBlockingQueue<Runnable>(10),
959 >                          new NoOpREHandler());
960              shouldThrow();
961          } catch (IllegalArgumentException success) {}
962      }
# Line 851 | Line 966 | public class ThreadPoolExecutorSubclassT
966       */
967      public void testConstructor13() {
968          try {
969 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
969 >            new CustomTPE(1, 0, 1L, SECONDS,
970 >                          new ArrayBlockingQueue<Runnable>(10),
971 >                          new NoOpREHandler());
972              shouldThrow();
973          } catch (IllegalArgumentException success) {}
974      }
# Line 861 | Line 978 | public class ThreadPoolExecutorSubclassT
978       */
979      public void testConstructor14() {
980          try {
981 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
981 >            new CustomTPE(1, 2, -1L, SECONDS,
982 >                          new ArrayBlockingQueue<Runnable>(10),
983 >                          new NoOpREHandler());
984              shouldThrow();
985          } catch (IllegalArgumentException success) {}
986      }
# Line 871 | Line 990 | public class ThreadPoolExecutorSubclassT
990       */
991      public void testConstructor15() {
992          try {
993 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
993 >            new CustomTPE(2, 1, 1L, SECONDS,
994 >                          new ArrayBlockingQueue<Runnable>(10),
995 >                          new NoOpREHandler());
996              shouldThrow();
997          } catch (IllegalArgumentException success) {}
998      }
# Line 881 | Line 1002 | public class ThreadPoolExecutorSubclassT
1002       */
1003      public void testConstructorNullPointerException4() {
1004          try {
1005 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
1005 >            new CustomTPE(1, 2, 1L, SECONDS,
1006 >                          null,
1007 >                          new NoOpREHandler());
1008              shouldThrow();
1009          } catch (NullPointerException success) {}
1010      }
# Line 891 | Line 1014 | public class ThreadPoolExecutorSubclassT
1014       */
1015      public void testConstructorNullPointerException5() {
1016          try {
1017 <            RejectedExecutionHandler r = null;
1018 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
1017 >            new CustomTPE(1, 2, 1L, SECONDS,
1018 >                          new ArrayBlockingQueue<Runnable>(10),
1019 >                          (RejectedExecutionHandler) null);
1020              shouldThrow();
1021          } catch (NullPointerException success) {}
1022      }
1023  
900
1024      /**
1025       * Constructor throws if corePoolSize argument is less than zero
1026       */
1027      public void testConstructor16() {
1028          try {
1029 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1029 >            new CustomTPE(-1, 1, 1L, SECONDS,
1030 >                          new ArrayBlockingQueue<Runnable>(10),
1031 >                          new SimpleThreadFactory(),
1032 >                          new NoOpREHandler());
1033              shouldThrow();
1034          } catch (IllegalArgumentException success) {}
1035      }
# Line 913 | Line 1039 | public class ThreadPoolExecutorSubclassT
1039       */
1040      public void testConstructor17() {
1041          try {
1042 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1042 >            new CustomTPE(1, -1, 1L, SECONDS,
1043 >                          new ArrayBlockingQueue<Runnable>(10),
1044 >                          new SimpleThreadFactory(),
1045 >                          new NoOpREHandler());
1046              shouldThrow();
1047          } catch (IllegalArgumentException success) {}
1048      }
# Line 923 | Line 1052 | public class ThreadPoolExecutorSubclassT
1052       */
1053      public void testConstructor18() {
1054          try {
1055 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1055 >            new CustomTPE(1, 0, 1L, SECONDS,
1056 >                          new ArrayBlockingQueue<Runnable>(10),
1057 >                          new SimpleThreadFactory(),
1058 >                          new NoOpREHandler());
1059              shouldThrow();
1060          } catch (IllegalArgumentException success) {}
1061      }
# Line 933 | Line 1065 | public class ThreadPoolExecutorSubclassT
1065       */
1066      public void testConstructor19() {
1067          try {
1068 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1068 >            new CustomTPE(1, 2, -1L, SECONDS,
1069 >                          new ArrayBlockingQueue<Runnable>(10),
1070 >                          new SimpleThreadFactory(),
1071 >                          new NoOpREHandler());
1072              shouldThrow();
1073          } catch (IllegalArgumentException success) {}
1074      }
# Line 943 | Line 1078 | public class ThreadPoolExecutorSubclassT
1078       */
1079      public void testConstructor20() {
1080          try {
1081 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1081 >            new CustomTPE(2, 1, 1L, SECONDS,
1082 >                          new ArrayBlockingQueue<Runnable>(10),
1083 >                          new SimpleThreadFactory(),
1084 >                          new NoOpREHandler());
1085              shouldThrow();
1086          } catch (IllegalArgumentException success) {}
1087      }
# Line 953 | Line 1091 | public class ThreadPoolExecutorSubclassT
1091       */
1092      public void testConstructorNullPointerException6() {
1093          try {
1094 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
1094 >            new CustomTPE(1, 2, 1L, SECONDS,
1095 >                          null,
1096 >                          new SimpleThreadFactory(),
1097 >                          new NoOpREHandler());
1098              shouldThrow();
1099          } catch (NullPointerException success) {}
1100      }
# Line 963 | Line 1104 | public class ThreadPoolExecutorSubclassT
1104       */
1105      public void testConstructorNullPointerException7() {
1106          try {
1107 <            RejectedExecutionHandler r = null;
1108 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
1107 >            new CustomTPE(1, 2, 1L, SECONDS,
1108 >                          new ArrayBlockingQueue<Runnable>(10),
1109 >                          new SimpleThreadFactory(),
1110 >                          (RejectedExecutionHandler) null);
1111              shouldThrow();
1112          } catch (NullPointerException success) {}
1113      }
# Line 974 | Line 1117 | public class ThreadPoolExecutorSubclassT
1117       */
1118      public void testConstructorNullPointerException8() {
1119          try {
1120 <            new CustomTPE(1, 2,
978 <                          LONG_DELAY_MS, MILLISECONDS,
1120 >            new CustomTPE(1, 2, 1L, SECONDS,
1121                            new ArrayBlockingQueue<Runnable>(10),
1122                            (ThreadFactory) null,
1123                            new NoOpREHandler());
# Line 983 | Line 1125 | public class ThreadPoolExecutorSubclassT
1125          } catch (NullPointerException success) {}
1126      }
1127  
986
1128      /**
1129       * execute throws RejectedExecutionException if saturated.
1130       */
# Line 1133 | Line 1274 | public class ThreadPoolExecutorSubclassT
1274          }
1275      }
1276  
1136
1277      /**
1278       * execute using DiscardOldestPolicy drops task on shutdown
1279       */
# Line 1151 | Line 1291 | public class ThreadPoolExecutorSubclassT
1291          }
1292      }
1293  
1154
1294      /**
1295       * execute(null) throws NPE
1296       */
1297      public void testExecuteNull() {
1298 <        ThreadPoolExecutor p = null;
1298 >        ThreadPoolExecutor p =
1299 >            new CustomTPE(1, 2, 1L, SECONDS,
1300 >                          new ArrayBlockingQueue<Runnable>(10));
1301          try {
1161            p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1302              p.execute(null);
1303              shouldThrow();
1304          } catch (NullPointerException success) {}
# Line 1216 | Line 1356 | public class ThreadPoolExecutorSubclassT
1356          joinPool(p);
1357      }
1358  
1219
1359      /**
1360       * setKeepAliveTime throws IllegalArgumentException
1361       * when given a negative value
# Line 1241 | Line 1380 | public class ThreadPoolExecutorSubclassT
1380      public void testTerminated() {
1381          CustomTPE p = new CustomTPE();
1382          try { p.shutdown(); } catch (SecurityException ok) { return; }
1383 <        assertTrue(p.terminatedCalled);
1383 >        assertTrue(p.terminatedCalled());
1384          joinPool(p);
1385      }
1386  
# Line 1251 | Line 1390 | public class ThreadPoolExecutorSubclassT
1390      public void testBeforeAfter() throws InterruptedException {
1391          CustomTPE p = new CustomTPE();
1392          try {
1393 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1394 <            p.execute(r);
1395 <            Thread.sleep(SHORT_DELAY_MS);
1396 <            assertTrue(r.done);
1397 <            assertTrue(p.beforeCalled);
1398 <            assertTrue(p.afterCalled);
1393 >            final CountDownLatch done = new CountDownLatch(1);
1394 >            p.execute(new CheckedRunnable() {
1395 >                public void realRun() {
1396 >                    done.countDown();
1397 >                }});
1398 >            await(p.afterCalled);
1399 >            assertEquals(0, done.getCount());
1400 >            assertTrue(p.afterCalled());
1401 >            assertTrue(p.beforeCalled());
1402              try { p.shutdown(); } catch (SecurityException ok) { return; }
1403          } finally {
1404              joinPool(p);
# Line 1305 | Line 1447 | public class ThreadPoolExecutorSubclassT
1447          }
1448      }
1449  
1308
1450      /**
1451       * invokeAny(null) throws NPE
1452       */
# Line 1467 | Line 1608 | public class ThreadPoolExecutorSubclassT
1608          }
1609      }
1610  
1470
1471
1611      /**
1612       * timed invokeAny(null) throws NPE
1613       */
# Line 1655 | Line 1794 | public class ThreadPoolExecutorSubclassT
1794              l.add(new StringTask());
1795              l.add(new StringTask());
1796              List<Future<String>> futures =
1797 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1797 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1798              assertEquals(2, futures.size());
1799              for (Future<String> future : futures)
1800                  assertSame(TEST_STRING, future.get());
# Line 1670 | Line 1809 | public class ThreadPoolExecutorSubclassT
1809      public void testTimedInvokeAll6() throws Exception {
1810          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1811          try {
1812 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1813 <            l.add(new StringTask());
1814 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1815 <            l.add(new StringTask());
1816 <            List<Future<String>> futures =
1817 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1818 <            assertEquals(3, futures.size());
1819 <            Iterator<Future<String>> it = futures.iterator();
1820 <            Future<String> f1 = it.next();
1821 <            Future<String> f2 = it.next();
1822 <            Future<String> f3 = it.next();
1823 <            assertTrue(f1.isDone());
1824 <            assertTrue(f2.isDone());
1825 <            assertTrue(f3.isDone());
1826 <            assertFalse(f1.isCancelled());
1827 <            assertTrue(f2.isCancelled());
1812 >            for (long timeout = timeoutMillis();;) {
1813 >                List<Callable<String>> tasks = new ArrayList<>();
1814 >                tasks.add(new StringTask("0"));
1815 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1816 >                tasks.add(new StringTask("2"));
1817 >                long startTime = System.nanoTime();
1818 >                List<Future<String>> futures =
1819 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1820 >                assertEquals(tasks.size(), futures.size());
1821 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1822 >                for (Future future : futures)
1823 >                    assertTrue(future.isDone());
1824 >                assertTrue(futures.get(1).isCancelled());
1825 >                try {
1826 >                    assertEquals("0", futures.get(0).get());
1827 >                    assertEquals("2", futures.get(2).get());
1828 >                    break;
1829 >                } catch (CancellationException retryWithLongerTimeout) {
1830 >                    timeout *= 2;
1831 >                    if (timeout >= LONG_DELAY_MS / 2)
1832 >                        fail("expected exactly one task to be cancelled");
1833 >                }
1834 >            }
1835          } finally {
1836              joinPool(e);
1837          }
# Line 1728 | Line 1874 | public class ThreadPoolExecutorSubclassT
1874       * allowCoreThreadTimeOut(true) causes idle threads to time out
1875       */
1876      public void testAllowCoreThreadTimeOut_true() throws Exception {
1877 +        long keepAliveTime = timeoutMillis();
1878          final ThreadPoolExecutor p =
1879              new CustomTPE(2, 10,
1880 <                          SHORT_DELAY_MS, MILLISECONDS,
1880 >                          keepAliveTime, MILLISECONDS,
1881                            new ArrayBlockingQueue<Runnable>(10));
1882          final CountDownLatch threadStarted = new CountDownLatch(1);
1883          try {
1884              p.allowCoreThreadTimeOut(true);
1885              p.execute(new CheckedRunnable() {
1886 <                public void realRun() throws InterruptedException {
1886 >                public void realRun() {
1887                      threadStarted.countDown();
1888                      assertEquals(1, p.getPoolSize());
1889                  }});
1890 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
1891 <            for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) {
1892 <                if (p.getPoolSize() == 0)
1893 <                    break;
1894 <                Thread.sleep(10);
1895 <            }
1890 >            await(threadStarted);
1891 >            delay(keepAliveTime);
1892 >            long startTime = System.nanoTime();
1893 >            while (p.getPoolSize() > 0
1894 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1895 >                Thread.yield();
1896 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1897              assertEquals(0, p.getPoolSize());
1898          } finally {
1899              joinPool(p);
# Line 1756 | Line 1904 | public class ThreadPoolExecutorSubclassT
1904       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1905       */
1906      public void testAllowCoreThreadTimeOut_false() throws Exception {
1907 +        long keepAliveTime = timeoutMillis();
1908          final ThreadPoolExecutor p =
1909              new CustomTPE(2, 10,
1910 <                          SHORT_DELAY_MS, MILLISECONDS,
1910 >                          keepAliveTime, MILLISECONDS,
1911                            new ArrayBlockingQueue<Runnable>(10));
1912          final CountDownLatch threadStarted = new CountDownLatch(1);
1913          try {
# Line 1768 | Line 1917 | public class ThreadPoolExecutorSubclassT
1917                      threadStarted.countDown();
1918                      assertTrue(p.getPoolSize() >= 1);
1919                  }});
1920 <            Thread.sleep(SMALL_DELAY_MS);
1920 >            delay(2 * keepAliveTime);
1921              assertTrue(p.getPoolSize() >= 1);
1922          } finally {
1923              joinPool(p);
1924          }
1925      }
1926  
1927 +    /**
1928 +     * get(cancelled task) throws CancellationException
1929 +     * (in part, a test of CustomTPE itself)
1930 +     */
1931 +    public void testGet_cancelled() throws Exception {
1932 +        final ExecutorService e =
1933 +            new CustomTPE(1, 1,
1934 +                          LONG_DELAY_MS, MILLISECONDS,
1935 +                          new LinkedBlockingQueue<Runnable>());
1936 +        try {
1937 +            final CountDownLatch blockerStarted = new CountDownLatch(1);
1938 +            final CountDownLatch done = new CountDownLatch(1);
1939 +            final List<Future<?>> futures = new ArrayList<>();
1940 +            for (int i = 0; i < 2; i++) {
1941 +                Runnable r = new CheckedRunnable() { public void realRun()
1942 +                                                         throws Throwable {
1943 +                    blockerStarted.countDown();
1944 +                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
1945 +                }};
1946 +                futures.add(e.submit(r));
1947 +            }
1948 +            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
1949 +            for (Future<?> future : futures) future.cancel(false);
1950 +            for (Future<?> future : futures) {
1951 +                try {
1952 +                    future.get();
1953 +                    shouldThrow();
1954 +                } catch (CancellationException success) {}
1955 +                try {
1956 +                    future.get(LONG_DELAY_MS, MILLISECONDS);
1957 +                    shouldThrow();
1958 +                } catch (CancellationException success) {}
1959 +                assertTrue(future.isCancelled());
1960 +                assertTrue(future.isDone());
1961 +            }
1962 +            done.countDown();
1963 +        } finally {
1964 +            joinPool(e);
1965 +        }
1966 +    }
1967 +
1968   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines