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.18 by jsr166, Wed Aug 25 00:07:03 2010 UTC vs.
Revision 1.57 by jsr166, Sun Oct 4 01:50:30 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
230 >     * execute successfully executes a runnable
231       */
232      public void testExecute() throws InterruptedException {
233 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
234 <        try {
235 <            p1.execute(new ShortRunnable());
236 <            Thread.sleep(SMALL_DELAY_MS);
237 <        } finally {
238 <            joinPool(p1);
233 >        final ThreadPoolExecutor p =
234 >            new CustomTPE(1, 1,
235 >                          2 * LONG_DELAY_MS, MILLISECONDS,
236 >                          new ArrayBlockingQueue<Runnable>(10));
237 >        try (PoolCleaner cleaner = cleaner(p)) {
238 >            final CountDownLatch done = new CountDownLatch(1);
239 >            final Runnable task = new CheckedRunnable() {
240 >                public void realRun() { done.countDown(); }};
241 >            p.execute(task);
242 >            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
243          }
244      }
245  
246      /**
247 <     *  getActiveCount increases but doesn't overestimate, when a
248 <     *  thread becomes active
247 >     * getActiveCount increases but doesn't overestimate, when a
248 >     * thread becomes active
249       */
250      public void testGetActiveCount() throws InterruptedException {
251 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
252 <        assertEquals(0, p2.getActiveCount());
253 <        p2.execute(new MediumRunnable());
254 <        Thread.sleep(SHORT_DELAY_MS);
255 <        assertEquals(1, p2.getActiveCount());
256 <        joinPool(p2);
251 >        final ThreadPoolExecutor p =
252 >            new CustomTPE(2, 2,
253 >                          LONG_DELAY_MS, MILLISECONDS,
254 >                          new ArrayBlockingQueue<Runnable>(10));
255 >        final CountDownLatch threadStarted = new CountDownLatch(1);
256 >        final CountDownLatch done = new CountDownLatch(1);
257 >        try (PoolCleaner cleaner = cleaner(p)) {
258 >            assertEquals(0, p.getActiveCount());
259 >            p.execute(new CheckedRunnable() {
260 >                public void realRun() throws InterruptedException {
261 >                    threadStarted.countDown();
262 >                    assertEquals(1, p.getActiveCount());
263 >                    done.await();
264 >                }});
265 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
266 >            assertEquals(1, p.getActiveCount());
267 >            done.countDown();
268 >        }
269      }
270  
271      /**
272 <     *  prestartCoreThread starts a thread if under corePoolSize, else doesn't
272 >     * prestartCoreThread starts a thread if under corePoolSize, else doesn't
273       */
274      public void testPrestartCoreThread() {
275 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
276 <        assertEquals(0, p2.getPoolSize());
277 <        assertTrue(p2.prestartCoreThread());
278 <        assertEquals(1, p2.getPoolSize());
279 <        assertTrue(p2.prestartCoreThread());
280 <        assertEquals(2, p2.getPoolSize());
281 <        assertFalse(p2.prestartCoreThread());
282 <        assertEquals(2, p2.getPoolSize());
283 <        joinPool(p2);
275 >        ThreadPoolExecutor p =
276 >            new CustomTPE(2, 6,
277 >                          LONG_DELAY_MS, MILLISECONDS,
278 >                          new ArrayBlockingQueue<Runnable>(10));
279 >        try (PoolCleaner cleaner = cleaner(p)) {
280 >            assertEquals(0, p.getPoolSize());
281 >            assertTrue(p.prestartCoreThread());
282 >            assertEquals(1, p.getPoolSize());
283 >            assertTrue(p.prestartCoreThread());
284 >            assertEquals(2, p.getPoolSize());
285 >            assertFalse(p.prestartCoreThread());
286 >            assertEquals(2, p.getPoolSize());
287 >            p.setCorePoolSize(4);
288 >            assertTrue(p.prestartCoreThread());
289 >            assertEquals(3, p.getPoolSize());
290 >            assertTrue(p.prestartCoreThread());
291 >            assertEquals(4, p.getPoolSize());
292 >            assertFalse(p.prestartCoreThread());
293 >            assertEquals(4, p.getPoolSize());
294 >        }
295      }
296  
297      /**
298 <     *  prestartAllCoreThreads starts all corePoolSize threads
298 >     * prestartAllCoreThreads starts all corePoolSize threads
299       */
300      public void testPrestartAllCoreThreads() {
301 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
302 <        assertEquals(0, p2.getPoolSize());
303 <        p2.prestartAllCoreThreads();
304 <        assertEquals(2, p2.getPoolSize());
305 <        p2.prestartAllCoreThreads();
306 <        assertEquals(2, p2.getPoolSize());
307 <        joinPool(p2);
301 >        ThreadPoolExecutor p =
302 >            new CustomTPE(2, 6,
303 >                          LONG_DELAY_MS, MILLISECONDS,
304 >                          new ArrayBlockingQueue<Runnable>(10));
305 >        try (PoolCleaner cleaner = cleaner(p)) {
306 >            assertEquals(0, p.getPoolSize());
307 >            p.prestartAllCoreThreads();
308 >            assertEquals(2, p.getPoolSize());
309 >            p.prestartAllCoreThreads();
310 >            assertEquals(2, p.getPoolSize());
311 >            p.setCorePoolSize(4);
312 >            p.prestartAllCoreThreads();
313 >            assertEquals(4, p.getPoolSize());
314 >            p.prestartAllCoreThreads();
315 >            assertEquals(4, p.getPoolSize());
316 >        }
317      }
318  
319      /**
320 <     *   getCompletedTaskCount increases, but doesn't overestimate,
321 <     *   when tasks complete
320 >     * getCompletedTaskCount increases, but doesn't overestimate,
321 >     * when tasks complete
322       */
323      public void testGetCompletedTaskCount() throws InterruptedException {
324 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
325 <        assertEquals(0, p2.getCompletedTaskCount());
326 <        p2.execute(new ShortRunnable());
327 <        Thread.sleep(SMALL_DELAY_MS);
328 <        assertEquals(1, p2.getCompletedTaskCount());
329 <        try { p2.shutdown(); } catch (SecurityException ok) { return; }
330 <        joinPool(p2);
324 >        final ThreadPoolExecutor p =
325 >            new CustomTPE(2, 2,
326 >                          LONG_DELAY_MS, MILLISECONDS,
327 >                          new ArrayBlockingQueue<Runnable>(10));
328 >        try (PoolCleaner cleaner = cleaner(p)) {
329 >            final CountDownLatch threadStarted = new CountDownLatch(1);
330 >            final CountDownLatch threadProceed = new CountDownLatch(1);
331 >            final CountDownLatch threadDone = new CountDownLatch(1);
332 >            assertEquals(0, p.getCompletedTaskCount());
333 >            p.execute(new CheckedRunnable() {
334 >                public void realRun() throws InterruptedException {
335 >                    threadStarted.countDown();
336 >                    assertEquals(0, p.getCompletedTaskCount());
337 >                    threadProceed.await();
338 >                    threadDone.countDown();
339 >                }});
340 >            await(threadStarted);
341 >            assertEquals(0, p.getCompletedTaskCount());
342 >            threadProceed.countDown();
343 >            threadDone.await();
344 >            long startTime = System.nanoTime();
345 >            while (p.getCompletedTaskCount() != 1) {
346 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
347 >                    fail("timed out");
348 >                Thread.yield();
349 >            }
350 >        }
351      }
352  
353      /**
354 <     *   getCorePoolSize returns size given in constructor if not otherwise set
354 >     * getCorePoolSize returns size given in constructor if not otherwise set
355       */
356      public void testGetCorePoolSize() {
357 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
358 <        assertEquals(1, p1.getCorePoolSize());
359 <        joinPool(p1);
357 >        ThreadPoolExecutor p =
358 >            new CustomTPE(1, 1,
359 >                          LONG_DELAY_MS, MILLISECONDS,
360 >                          new ArrayBlockingQueue<Runnable>(10));
361 >        try (PoolCleaner cleaner = cleaner(p)) {
362 >            assertEquals(1, p.getCorePoolSize());
363 >        }
364      }
365  
366      /**
367 <     *   getKeepAliveTime returns value given in constructor if not otherwise set
367 >     * getKeepAliveTime returns value given in constructor if not otherwise set
368       */
369      public void testGetKeepAliveTime() {
370 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
371 <        assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS));
372 <        joinPool(p2);
370 >        ThreadPoolExecutor p =
371 >            new CustomTPE(2, 2,
372 >                          1000, MILLISECONDS,
373 >                          new ArrayBlockingQueue<Runnable>(10));
374 >        try (PoolCleaner cleaner = cleaner(p)) {
375 >            assertEquals(1, p.getKeepAliveTime(SECONDS));
376 >        }
377      }
378  
280
379      /**
380       * getThreadFactory returns factory in constructor if not set
381       */
382      public void testGetThreadFactory() {
383 <        ThreadFactory tf = new SimpleThreadFactory();
384 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler());
385 <        assertSame(tf, p.getThreadFactory());
386 <        joinPool(p);
383 >        ThreadFactory threadFactory = new SimpleThreadFactory();
384 >        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 >        ThreadPoolExecutor p =
400 >            new CustomTPE(1, 2,
401 >                          LONG_DELAY_MS, MILLISECONDS,
402 >                          new ArrayBlockingQueue<Runnable>(10));
403 >        try (PoolCleaner cleaner = cleaner(p)) {
404 >            ThreadFactory threadFactory = new SimpleThreadFactory();
405 >            p.setThreadFactory(threadFactory);
406 >            assertSame(threadFactory, p.getThreadFactory());
407 >        }
408      }
409  
302
410      /**
411       * setThreadFactory(null) throws NPE
412       */
# Line 336 | Line 443 | public class ThreadPoolExecutorSubclassT
443          joinPool(p);
444      }
445  
339
446      /**
447       * setRejectedExecutionHandler(null) throws NPE
448       */
# Line 351 | Line 457 | public class ThreadPoolExecutorSubclassT
457          }
458      }
459  
354
460      /**
461 <     *   getLargestPoolSize increases, but doesn't overestimate, when
462 <     *   multiple threads active
461 >     * getLargestPoolSize increases, but doesn't overestimate, when
462 >     * multiple threads active
463       */
464      public void testGetLargestPoolSize() throws InterruptedException {
465 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
466 <        assertEquals(0, p2.getLargestPoolSize());
467 <        p2.execute(new MediumRunnable());
468 <        p2.execute(new MediumRunnable());
469 <        Thread.sleep(SHORT_DELAY_MS);
470 <        assertEquals(2, p2.getLargestPoolSize());
471 <        joinPool(p2);
465 >        final int THREADS = 3;
466 >        final ThreadPoolExecutor p =
467 >            new CustomTPE(THREADS, THREADS,
468 >                          LONG_DELAY_MS, MILLISECONDS,
469 >                          new ArrayBlockingQueue<Runnable>(10));
470 >        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
471 >        final CountDownLatch done = new CountDownLatch(1);
472 >        try {
473 >            assertEquals(0, p.getLargestPoolSize());
474 >            for (int i = 0; i < THREADS; i++)
475 >                p.execute(new CheckedRunnable() {
476 >                    public void realRun() throws InterruptedException {
477 >                        threadsStarted.countDown();
478 >                        done.await();
479 >                        assertEquals(THREADS, p.getLargestPoolSize());
480 >                    }});
481 >            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
482 >            assertEquals(THREADS, p.getLargestPoolSize());
483 >        } finally {
484 >            done.countDown();
485 >            joinPool(p);
486 >            assertEquals(THREADS, p.getLargestPoolSize());
487 >        }
488      }
489  
490      /**
491 <     *   getMaximumPoolSize returns value given in constructor if not
492 <     *   otherwise set
491 >     * getMaximumPoolSize returns value given in constructor if not
492 >     * otherwise set
493       */
494      public void testGetMaximumPoolSize() {
495 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
496 <        assertEquals(2, p2.getMaximumPoolSize());
497 <        joinPool(p2);
495 >        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
496 >        assertEquals(2, p.getMaximumPoolSize());
497 >        joinPool(p);
498      }
499  
500      /**
501 <     *   getPoolSize increases, but doesn't overestimate, when threads
502 <     *   become active
501 >     * getPoolSize increases, but doesn't overestimate, when threads
502 >     * become active
503       */
504 <    public void testGetPoolSize() {
505 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
506 <        assertEquals(0, p1.getPoolSize());
507 <        p1.execute(new MediumRunnable());
508 <        assertEquals(1, p1.getPoolSize());
509 <        joinPool(p1);
504 >    public void testGetPoolSize() throws InterruptedException {
505 >        final ThreadPoolExecutor p =
506 >            new CustomTPE(1, 1,
507 >                          LONG_DELAY_MS, MILLISECONDS,
508 >                          new ArrayBlockingQueue<Runnable>(10));
509 >        final CountDownLatch threadStarted = new CountDownLatch(1);
510 >        final CountDownLatch done = new CountDownLatch(1);
511 >        try {
512 >            assertEquals(0, p.getPoolSize());
513 >            p.execute(new CheckedRunnable() {
514 >                public void realRun() throws InterruptedException {
515 >                    threadStarted.countDown();
516 >                    assertEquals(1, p.getPoolSize());
517 >                    done.await();
518 >                }});
519 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
520 >            assertEquals(1, p.getPoolSize());
521 >        } finally {
522 >            done.countDown();
523 >            joinPool(p);
524 >        }
525      }
526  
527      /**
528 <     *  getTaskCount increases, but doesn't overestimate, when tasks submitted
528 >     * getTaskCount increases, but doesn't overestimate, when tasks submitted
529       */
530      public void testGetTaskCount() throws InterruptedException {
531 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
532 <        assertEquals(0, p1.getTaskCount());
533 <        p1.execute(new MediumRunnable());
534 <        Thread.sleep(SHORT_DELAY_MS);
535 <        assertEquals(1, p1.getTaskCount());
536 <        joinPool(p1);
531 >        final ThreadPoolExecutor p =
532 >            new CustomTPE(1, 1,
533 >                          LONG_DELAY_MS, MILLISECONDS,
534 >                          new ArrayBlockingQueue<Runnable>(10));
535 >        final CountDownLatch threadStarted = new CountDownLatch(1);
536 >        final CountDownLatch done = new CountDownLatch(1);
537 >        try {
538 >            assertEquals(0, p.getTaskCount());
539 >            p.execute(new CheckedRunnable() {
540 >                public void realRun() throws InterruptedException {
541 >                    threadStarted.countDown();
542 >                    assertEquals(1, p.getTaskCount());
543 >                    done.await();
544 >                }});
545 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
546 >            assertEquals(1, p.getTaskCount());
547 >        } finally {
548 >            done.countDown();
549 >            joinPool(p);
550 >        }
551      }
552  
553      /**
554 <     *   isShutDown is false before shutdown, true after
554 >     * isShutdown is false before shutdown, true after
555       */
556      public void testIsShutdown() {
557  
558 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
559 <        assertFalse(p1.isShutdown());
560 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
561 <        assertTrue(p1.isShutdown());
562 <        joinPool(p1);
558 >        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
559 >        assertFalse(p.isShutdown());
560 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
561 >        assertTrue(p.isShutdown());
562 >        joinPool(p);
563      }
564  
415
565      /**
566 <     *  isTerminated is false before termination, true after
566 >     * isTerminated is false before termination, true after
567       */
568      public void testIsTerminated() throws InterruptedException {
569 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
570 <        assertFalse(p1.isTerminated());
571 <        try {
572 <            p1.execute(new MediumRunnable());
573 <        } finally {
574 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
575 <        }
576 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
577 <        assertTrue(p1.isTerminated());
569 >        final ThreadPoolExecutor p =
570 >            new CustomTPE(1, 1,
571 >                          LONG_DELAY_MS, MILLISECONDS,
572 >                          new ArrayBlockingQueue<Runnable>(10));
573 >        final CountDownLatch threadStarted = new CountDownLatch(1);
574 >        final CountDownLatch done = new CountDownLatch(1);
575 >        try {
576 >            assertFalse(p.isTerminating());
577 >            p.execute(new CheckedRunnable() {
578 >                public void realRun() throws InterruptedException {
579 >                    assertFalse(p.isTerminating());
580 >                    threadStarted.countDown();
581 >                    done.await();
582 >                }});
583 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
584 >            assertFalse(p.isTerminating());
585 >            done.countDown();
586 >        } finally {
587 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
588 >        }
589 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
590 >        assertTrue(p.isTerminated());
591 >        assertFalse(p.isTerminating());
592      }
593  
594      /**
595 <     *  isTerminating is not true when running or when terminated
595 >     * isTerminating is not true when running or when terminated
596       */
597      public void testIsTerminating() throws InterruptedException {
598 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
599 <        assertFalse(p1.isTerminating());
600 <        try {
601 <            p1.execute(new SmallRunnable());
602 <            assertFalse(p1.isTerminating());
603 <        } finally {
604 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
605 <        }
606 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
607 <        assertTrue(p1.isTerminated());
608 <        assertFalse(p1.isTerminating());
598 >        final ThreadPoolExecutor p =
599 >            new CustomTPE(1, 1,
600 >                          LONG_DELAY_MS, MILLISECONDS,
601 >                          new ArrayBlockingQueue<Runnable>(10));
602 >        final CountDownLatch threadStarted = new CountDownLatch(1);
603 >        final CountDownLatch done = new CountDownLatch(1);
604 >        try {
605 >            assertFalse(p.isTerminating());
606 >            p.execute(new CheckedRunnable() {
607 >                public void realRun() throws InterruptedException {
608 >                    assertFalse(p.isTerminating());
609 >                    threadStarted.countDown();
610 >                    done.await();
611 >                }});
612 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
613 >            assertFalse(p.isTerminating());
614 >            done.countDown();
615 >        } finally {
616 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
617 >        }
618 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
619 >        assertTrue(p.isTerminated());
620 >        assertFalse(p.isTerminating());
621      }
622  
623      /**
624       * getQueue returns the work queue, which contains queued tasks
625       */
626      public void testGetQueue() throws InterruptedException {
627 <        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
628 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, q);
629 <        FutureTask[] tasks = new FutureTask[5];
630 <        for (int i = 0; i < 5; i++) {
631 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
632 <            p1.execute(tasks[i]);
633 <        }
634 <        try {
635 <            Thread.sleep(SHORT_DELAY_MS);
636 <            BlockingQueue<Runnable> wq = p1.getQueue();
637 <            assertSame(q, wq);
638 <            assertFalse(wq.contains(tasks[0]));
639 <            assertTrue(wq.contains(tasks[4]));
640 <            for (int i = 1; i < 5; ++i)
641 <                tasks[i].cancel(true);
642 <            p1.shutdownNow();
627 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
628 >        final ThreadPoolExecutor p =
629 >            new CustomTPE(1, 1,
630 >                          LONG_DELAY_MS, MILLISECONDS,
631 >                          q);
632 >        final CountDownLatch threadStarted = new CountDownLatch(1);
633 >        final CountDownLatch done = new CountDownLatch(1);
634 >        try {
635 >            FutureTask[] tasks = new FutureTask[5];
636 >            for (int i = 0; i < tasks.length; i++) {
637 >                Callable task = new CheckedCallable<Boolean>() {
638 >                    public Boolean realCall() throws InterruptedException {
639 >                        threadStarted.countDown();
640 >                        assertSame(q, p.getQueue());
641 >                        done.await();
642 >                        return Boolean.TRUE;
643 >                    }};
644 >                tasks[i] = new FutureTask(task);
645 >                p.execute(tasks[i]);
646 >            }
647 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
648 >            assertSame(q, p.getQueue());
649 >            assertFalse(q.contains(tasks[0]));
650 >            assertTrue(q.contains(tasks[tasks.length - 1]));
651 >            assertEquals(tasks.length - 1, q.size());
652          } finally {
653 <            joinPool(p1);
653 >            done.countDown();
654 >            joinPool(p);
655          }
656      }
657  
# Line 475 | Line 660 | public class ThreadPoolExecutorSubclassT
660       */
661      public void testRemove() throws InterruptedException {
662          BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
663 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, q);
664 <        FutureTask[] tasks = new FutureTask[5];
665 <        for (int i = 0; i < 5; i++) {
666 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
667 <            p1.execute(tasks[i]);
668 <        }
669 <        try {
670 <            Thread.sleep(SHORT_DELAY_MS);
671 <            assertFalse(p1.remove(tasks[0]));
663 >        final ThreadPoolExecutor p =
664 >            new CustomTPE(1, 1,
665 >                          LONG_DELAY_MS, MILLISECONDS,
666 >                          q);
667 >        Runnable[] tasks = new Runnable[6];
668 >        final CountDownLatch threadStarted = new CountDownLatch(1);
669 >        final CountDownLatch done = new CountDownLatch(1);
670 >        try {
671 >            for (int i = 0; i < tasks.length; i++) {
672 >                tasks[i] = new CheckedRunnable() {
673 >                        public void realRun() throws InterruptedException {
674 >                            threadStarted.countDown();
675 >                            done.await();
676 >                        }};
677 >                p.execute(tasks[i]);
678 >            }
679 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
680 >            assertFalse(p.remove(tasks[0]));
681              assertTrue(q.contains(tasks[4]));
682              assertTrue(q.contains(tasks[3]));
683 <            assertTrue(p1.remove(tasks[4]));
684 <            assertFalse(p1.remove(tasks[4]));
683 >            assertTrue(p.remove(tasks[4]));
684 >            assertFalse(p.remove(tasks[4]));
685              assertFalse(q.contains(tasks[4]));
686              assertTrue(q.contains(tasks[3]));
687 <            assertTrue(p1.remove(tasks[3]));
687 >            assertTrue(p.remove(tasks[3]));
688              assertFalse(q.contains(tasks[3]));
689          } finally {
690 <            joinPool(p1);
690 >            done.countDown();
691 >            joinPool(p);
692          }
693      }
694  
695      /**
696 <     *   purge removes cancelled tasks from the queue
696 >     * purge removes cancelled tasks from the queue
697       */
698 <    public void testPurge() {
699 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
698 >    public void testPurge() throws InterruptedException {
699 >        final CountDownLatch threadStarted = new CountDownLatch(1);
700 >        final CountDownLatch done = new CountDownLatch(1);
701 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
702 >        final ThreadPoolExecutor p =
703 >            new CustomTPE(1, 1,
704 >                          LONG_DELAY_MS, MILLISECONDS,
705 >                          q);
706          FutureTask[] tasks = new FutureTask[5];
707 <        for (int i = 0; i < 5; i++) {
708 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
709 <            p1.execute(tasks[i]);
707 >        try {
708 >            for (int i = 0; i < tasks.length; i++) {
709 >                Callable task = new CheckedCallable<Boolean>() {
710 >                    public Boolean realCall() throws InterruptedException {
711 >                        threadStarted.countDown();
712 >                        done.await();
713 >                        return Boolean.TRUE;
714 >                    }};
715 >                tasks[i] = new FutureTask(task);
716 >                p.execute(tasks[i]);
717 >            }
718 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
719 >            assertEquals(tasks.length, p.getTaskCount());
720 >            assertEquals(tasks.length - 1, q.size());
721 >            assertEquals(1L, p.getActiveCount());
722 >            assertEquals(0L, p.getCompletedTaskCount());
723 >            tasks[4].cancel(true);
724 >            tasks[3].cancel(false);
725 >            p.purge();
726 >            assertEquals(tasks.length - 3, q.size());
727 >            assertEquals(tasks.length - 2, p.getTaskCount());
728 >            p.purge();         // Nothing to do
729 >            assertEquals(tasks.length - 3, q.size());
730 >            assertEquals(tasks.length - 2, p.getTaskCount());
731 >        } finally {
732 >            done.countDown();
733 >            joinPool(p);
734          }
510        tasks[4].cancel(true);
511        tasks[3].cancel(true);
512        p1.purge();
513        long count = p1.getTaskCount();
514        assertTrue(count >= 2 && count < 5);
515        joinPool(p1);
735      }
736  
737      /**
738 <     *  shutDownNow returns a list containing tasks that were not run
738 >     * shutdownNow returns a list containing tasks that were not run,
739 >     * and those tasks are drained from the queue
740       */
741 <    public void testShutDownNow() {
742 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
743 <        List l;
744 <        try {
745 <            for (int i = 0; i < 5; i++)
746 <                p1.execute(new MediumPossiblyInterruptedRunnable());
747 <        }
748 <        finally {
741 >    public void testShutdownNow() throws InterruptedException {
742 >        final int poolSize = 2;
743 >        final int count = 5;
744 >        final AtomicInteger ran = new AtomicInteger(0);
745 >        ThreadPoolExecutor p =
746 >            new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS,
747 >                          new ArrayBlockingQueue<Runnable>(10));
748 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
749 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
750 >            threadsStarted.countDown();
751              try {
752 <                l = p1.shutdownNow();
753 <            } catch (SecurityException ok) { return; }
754 <        }
755 <        assertTrue(p1.isShutdown());
756 <        assertTrue(l.size() <= 4);
752 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
753 >            } catch (InterruptedException success) {}
754 >            ran.getAndIncrement();
755 >        }};
756 >        for (int i = 0; i < count; i++)
757 >            p.execute(waiter);
758 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
759 >        assertEquals(poolSize, p.getActiveCount());
760 >        assertEquals(0, p.getCompletedTaskCount());
761 >        final List<Runnable> queuedTasks;
762 >        try {
763 >            queuedTasks = p.shutdownNow();
764 >        } catch (SecurityException ok) {
765 >            return; // Allowed in case test doesn't have privs
766 >        }
767 >        assertTrue(p.isShutdown());
768 >        assertTrue(p.getQueue().isEmpty());
769 >        assertEquals(count - poolSize, queuedTasks.size());
770 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
771 >        assertTrue(p.isTerminated());
772 >        assertEquals(poolSize, ran.get());
773 >        assertEquals(poolSize, p.getCompletedTaskCount());
774      }
775  
776      // Exception Tests
777  
539
778      /**
779       * Constructor throws if corePoolSize argument is less than zero
780       */
781      public void testConstructor1() {
782          try {
783 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
783 >            new CustomTPE(-1, 1, 1L, SECONDS,
784 >                          new ArrayBlockingQueue<Runnable>(10));
785              shouldThrow();
786          } catch (IllegalArgumentException success) {}
787      }
# Line 552 | Line 791 | public class ThreadPoolExecutorSubclassT
791       */
792      public void testConstructor2() {
793          try {
794 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
794 >            new CustomTPE(1, -1, 1L, SECONDS,
795 >                          new ArrayBlockingQueue<Runnable>(10));
796              shouldThrow();
797          } catch (IllegalArgumentException success) {}
798      }
# Line 562 | Line 802 | public class ThreadPoolExecutorSubclassT
802       */
803      public void testConstructor3() {
804          try {
805 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
805 >            new CustomTPE(1, 0, 1L, SECONDS,
806 >                          new ArrayBlockingQueue<Runnable>(10));
807              shouldThrow();
808          } catch (IllegalArgumentException success) {}
809      }
# Line 572 | Line 813 | public class ThreadPoolExecutorSubclassT
813       */
814      public void testConstructor4() {
815          try {
816 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
816 >            new CustomTPE(1, 2, -1L, SECONDS,
817 >                          new ArrayBlockingQueue<Runnable>(10));
818              shouldThrow();
819          } catch (IllegalArgumentException success) {}
820      }
# Line 582 | Line 824 | public class ThreadPoolExecutorSubclassT
824       */
825      public void testConstructor5() {
826          try {
827 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
827 >            new CustomTPE(2, 1, 1L, SECONDS,
828 >                          new ArrayBlockingQueue<Runnable>(10));
829              shouldThrow();
830          } catch (IllegalArgumentException success) {}
831      }
# Line 592 | Line 835 | public class ThreadPoolExecutorSubclassT
835       */
836      public void testConstructorNullPointerException() {
837          try {
838 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null);
838 >            new CustomTPE(1, 2, 1L, SECONDS, null);
839              shouldThrow();
840          } catch (NullPointerException success) {}
841      }
842  
600
601
843      /**
844       * Constructor throws if corePoolSize argument is less than zero
845       */
846      public void testConstructor6() {
847          try {
848 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
848 >            new CustomTPE(-1, 1, 1L, SECONDS,
849 >                          new ArrayBlockingQueue<Runnable>(10),
850 >                          new SimpleThreadFactory());
851              shouldThrow();
852          } catch (IllegalArgumentException success) {}
853      }
# Line 614 | Line 857 | public class ThreadPoolExecutorSubclassT
857       */
858      public void testConstructor7() {
859          try {
860 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
860 >            new CustomTPE(1,-1, 1L, SECONDS,
861 >                          new ArrayBlockingQueue<Runnable>(10),
862 >                          new SimpleThreadFactory());
863              shouldThrow();
864          } catch (IllegalArgumentException success) {}
865      }
# Line 624 | Line 869 | public class ThreadPoolExecutorSubclassT
869       */
870      public void testConstructor8() {
871          try {
872 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
872 >            new CustomTPE(1, 0, 1L, SECONDS,
873 >                          new ArrayBlockingQueue<Runnable>(10),
874 >                          new SimpleThreadFactory());
875              shouldThrow();
876          } catch (IllegalArgumentException success) {}
877      }
# Line 634 | Line 881 | public class ThreadPoolExecutorSubclassT
881       */
882      public void testConstructor9() {
883          try {
884 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
884 >            new CustomTPE(1, 2, -1L, SECONDS,
885 >                          new ArrayBlockingQueue<Runnable>(10),
886 >                          new SimpleThreadFactory());
887              shouldThrow();
888          } catch (IllegalArgumentException success) {}
889      }
# Line 644 | Line 893 | public class ThreadPoolExecutorSubclassT
893       */
894      public void testConstructor10() {
895          try {
896 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
896 >            new CustomTPE(2, 1, 1L, SECONDS,
897 >                          new ArrayBlockingQueue<Runnable>(10),
898 >                          new SimpleThreadFactory());
899              shouldThrow();
900          } catch (IllegalArgumentException success) {}
901      }
# Line 654 | Line 905 | public class ThreadPoolExecutorSubclassT
905       */
906      public void testConstructorNullPointerException2() {
907          try {
908 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
908 >            new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory());
909              shouldThrow();
910          } catch (NullPointerException success) {}
911      }
# Line 664 | Line 915 | public class ThreadPoolExecutorSubclassT
915       */
916      public void testConstructorNullPointerException3() {
917          try {
918 <            ThreadFactory f = null;
919 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
918 >            new CustomTPE(1, 2, 1L, SECONDS,
919 >                          new ArrayBlockingQueue<Runnable>(10),
920 >                          (ThreadFactory) null);
921              shouldThrow();
922          } catch (NullPointerException success) {}
923      }
924  
673
925      /**
926       * Constructor throws if corePoolSize argument is less than zero
927       */
928      public void testConstructor11() {
929          try {
930 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
930 >            new CustomTPE(-1, 1, 1L, SECONDS,
931 >                          new ArrayBlockingQueue<Runnable>(10),
932 >                          new NoOpREHandler());
933              shouldThrow();
934          } catch (IllegalArgumentException success) {}
935      }
# Line 686 | Line 939 | public class ThreadPoolExecutorSubclassT
939       */
940      public void testConstructor12() {
941          try {
942 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
942 >            new CustomTPE(1, -1, 1L, SECONDS,
943 >                          new ArrayBlockingQueue<Runnable>(10),
944 >                          new NoOpREHandler());
945              shouldThrow();
946          } catch (IllegalArgumentException success) {}
947      }
# Line 696 | Line 951 | public class ThreadPoolExecutorSubclassT
951       */
952      public void testConstructor13() {
953          try {
954 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
954 >            new CustomTPE(1, 0, 1L, SECONDS,
955 >                          new ArrayBlockingQueue<Runnable>(10),
956 >                          new NoOpREHandler());
957              shouldThrow();
958          } catch (IllegalArgumentException success) {}
959      }
# Line 706 | Line 963 | public class ThreadPoolExecutorSubclassT
963       */
964      public void testConstructor14() {
965          try {
966 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
966 >            new CustomTPE(1, 2, -1L, SECONDS,
967 >                          new ArrayBlockingQueue<Runnable>(10),
968 >                          new NoOpREHandler());
969              shouldThrow();
970          } catch (IllegalArgumentException success) {}
971      }
# Line 716 | Line 975 | public class ThreadPoolExecutorSubclassT
975       */
976      public void testConstructor15() {
977          try {
978 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
978 >            new CustomTPE(2, 1, 1L, SECONDS,
979 >                          new ArrayBlockingQueue<Runnable>(10),
980 >                          new NoOpREHandler());
981              shouldThrow();
982          } catch (IllegalArgumentException success) {}
983      }
# Line 726 | Line 987 | public class ThreadPoolExecutorSubclassT
987       */
988      public void testConstructorNullPointerException4() {
989          try {
990 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
990 >            new CustomTPE(1, 2, 1L, SECONDS,
991 >                          null,
992 >                          new NoOpREHandler());
993              shouldThrow();
994          } catch (NullPointerException success) {}
995      }
# Line 736 | Line 999 | public class ThreadPoolExecutorSubclassT
999       */
1000      public void testConstructorNullPointerException5() {
1001          try {
1002 <            RejectedExecutionHandler r = null;
1003 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
1002 >            new CustomTPE(1, 2, 1L, SECONDS,
1003 >                          new ArrayBlockingQueue<Runnable>(10),
1004 >                          (RejectedExecutionHandler) null);
1005              shouldThrow();
1006          } catch (NullPointerException success) {}
1007      }
1008  
745
1009      /**
1010       * Constructor throws if corePoolSize argument is less than zero
1011       */
1012      public void testConstructor16() {
1013          try {
1014 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1014 >            new CustomTPE(-1, 1, 1L, SECONDS,
1015 >                          new ArrayBlockingQueue<Runnable>(10),
1016 >                          new SimpleThreadFactory(),
1017 >                          new NoOpREHandler());
1018              shouldThrow();
1019          } catch (IllegalArgumentException success) {}
1020      }
# Line 758 | Line 1024 | public class ThreadPoolExecutorSubclassT
1024       */
1025      public void testConstructor17() {
1026          try {
1027 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1027 >            new CustomTPE(1, -1, 1L, SECONDS,
1028 >                          new ArrayBlockingQueue<Runnable>(10),
1029 >                          new SimpleThreadFactory(),
1030 >                          new NoOpREHandler());
1031              shouldThrow();
1032          } catch (IllegalArgumentException success) {}
1033      }
# Line 768 | Line 1037 | public class ThreadPoolExecutorSubclassT
1037       */
1038      public void testConstructor18() {
1039          try {
1040 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1040 >            new CustomTPE(1, 0, 1L, SECONDS,
1041 >                          new ArrayBlockingQueue<Runnable>(10),
1042 >                          new SimpleThreadFactory(),
1043 >                          new NoOpREHandler());
1044              shouldThrow();
1045          } catch (IllegalArgumentException success) {}
1046      }
# Line 778 | Line 1050 | public class ThreadPoolExecutorSubclassT
1050       */
1051      public void testConstructor19() {
1052          try {
1053 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1053 >            new CustomTPE(1, 2, -1L, SECONDS,
1054 >                          new ArrayBlockingQueue<Runnable>(10),
1055 >                          new SimpleThreadFactory(),
1056 >                          new NoOpREHandler());
1057              shouldThrow();
1058          } catch (IllegalArgumentException success) {}
1059      }
# Line 788 | Line 1063 | public class ThreadPoolExecutorSubclassT
1063       */
1064      public void testConstructor20() {
1065          try {
1066 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1066 >            new CustomTPE(2, 1, 1L, SECONDS,
1067 >                          new ArrayBlockingQueue<Runnable>(10),
1068 >                          new SimpleThreadFactory(),
1069 >                          new NoOpREHandler());
1070              shouldThrow();
1071          } catch (IllegalArgumentException success) {}
1072      }
1073  
1074      /**
1075 <     * Constructor throws if workQueue is set to null
1075 >     * Constructor throws if workQueue is null
1076       */
1077      public void testConstructorNullPointerException6() {
1078          try {
1079 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
1079 >            new CustomTPE(1, 2, 1L, SECONDS,
1080 >                          null,
1081 >                          new SimpleThreadFactory(),
1082 >                          new NoOpREHandler());
1083              shouldThrow();
1084          } catch (NullPointerException success) {}
1085      }
1086  
1087      /**
1088 <     * Constructor throws if handler is set to null
1088 >     * Constructor throws if handler is null
1089       */
1090      public void testConstructorNullPointerException7() {
1091          try {
1092 <            RejectedExecutionHandler r = null;
1093 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
1092 >            new CustomTPE(1, 2, 1L, SECONDS,
1093 >                          new ArrayBlockingQueue<Runnable>(10),
1094 >                          new SimpleThreadFactory(),
1095 >                          (RejectedExecutionHandler) null);
1096              shouldThrow();
1097          } catch (NullPointerException success) {}
1098      }
1099  
1100      /**
1101 <     * Constructor throws if ThreadFactory is set top null
1101 >     * Constructor throws if ThreadFactory is null
1102       */
1103      public void testConstructorNullPointerException8() {
1104          try {
1105 <            ThreadFactory f = null;
1106 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
1105 >            new CustomTPE(1, 2, 1L, SECONDS,
1106 >                          new ArrayBlockingQueue<Runnable>(10),
1107 >                          (ThreadFactory) null,
1108 >                          new NoOpREHandler());
1109              shouldThrow();
1110          } catch (NullPointerException success) {}
1111      }
1112  
828
1113      /**
1114 <     *  execute throws RejectedExecutionException
831 <     *  if saturated.
1114 >     * execute throws RejectedExecutionException if saturated.
1115       */
1116      public void testSaturatedExecute() {
1117 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
1118 <        try {
1119 <
1120 <            for (int i = 0; i < 5; ++i) {
1121 <                p.execute(new MediumRunnable());
1117 >        ThreadPoolExecutor p =
1118 >            new CustomTPE(1, 1,
1119 >                          LONG_DELAY_MS, MILLISECONDS,
1120 >                          new ArrayBlockingQueue<Runnable>(1));
1121 >        final CountDownLatch done = new CountDownLatch(1);
1122 >        try {
1123 >            Runnable task = new CheckedRunnable() {
1124 >                public void realRun() throws InterruptedException {
1125 >                    done.await();
1126 >                }};
1127 >            for (int i = 0; i < 2; ++i)
1128 >                p.execute(task);
1129 >            for (int i = 0; i < 2; ++i) {
1130 >                try {
1131 >                    p.execute(task);
1132 >                    shouldThrow();
1133 >                } catch (RejectedExecutionException success) {}
1134 >                assertTrue(p.getTaskCount() <= 2);
1135              }
1136 <            shouldThrow();
1137 <        } catch (RejectedExecutionException success) {}
1138 <        joinPool(p);
1136 >        } finally {
1137 >            done.countDown();
1138 >            joinPool(p);
1139 >        }
1140      }
1141  
1142      /**
1143 <     *  executor using CallerRunsPolicy runs task if saturated.
1143 >     * executor using CallerRunsPolicy runs task if saturated.
1144       */
1145      public void testSaturatedExecute2() {
1146          RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1147 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1147 >        ThreadPoolExecutor p = new CustomTPE(1, 1,
1148 >                                             LONG_DELAY_MS, MILLISECONDS,
1149 >                                             new ArrayBlockingQueue<Runnable>(1),
1150 >                                             h);
1151          try {
852
1152              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1153 <            for (int i = 0; i < 5; ++i) {
1153 >            for (int i = 0; i < tasks.length; ++i)
1154                  tasks[i] = new TrackedNoOpRunnable();
856            }
1155              TrackedLongRunnable mr = new TrackedLongRunnable();
1156              p.execute(mr);
1157 <            for (int i = 0; i < 5; ++i) {
1157 >            for (int i = 0; i < tasks.length; ++i)
1158                  p.execute(tasks[i]);
1159 <            }
862 <            for (int i = 1; i < 5; ++i) {
1159 >            for (int i = 1; i < tasks.length; ++i)
1160                  assertTrue(tasks[i].done);
864            }
1161              try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1162          } finally {
1163              joinPool(p);
# Line 869 | Line 1165 | public class ThreadPoolExecutorSubclassT
1165      }
1166  
1167      /**
1168 <     *  executor using DiscardPolicy drops task if saturated.
1168 >     * executor using DiscardPolicy drops task if saturated.
1169       */
1170      public void testSaturatedExecute3() {
1171          RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1172 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1172 >        ThreadPoolExecutor p =
1173 >            new CustomTPE(1, 1,
1174 >                          LONG_DELAY_MS, MILLISECONDS,
1175 >                          new ArrayBlockingQueue<Runnable>(1),
1176 >                          h);
1177          try {
878
1178              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1179 <            for (int i = 0; i < 5; ++i) {
1179 >            for (int i = 0; i < tasks.length; ++i)
1180                  tasks[i] = new TrackedNoOpRunnable();
882            }
1181              p.execute(new TrackedLongRunnable());
1182 <            for (int i = 0; i < 5; ++i) {
1183 <                p.execute(tasks[i]);
1184 <            }
1185 <            for (int i = 0; i < 5; ++i) {
888 <                assertFalse(tasks[i].done);
889 <            }
1182 >            for (TrackedNoOpRunnable task : tasks)
1183 >                p.execute(task);
1184 >            for (TrackedNoOpRunnable task : tasks)
1185 >                assertFalse(task.done);
1186              try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1187          } finally {
1188              joinPool(p);
# Line 894 | Line 1190 | public class ThreadPoolExecutorSubclassT
1190      }
1191  
1192      /**
1193 <     *  executor using DiscardOldestPolicy drops oldest task if saturated.
1193 >     * executor using DiscardOldestPolicy drops oldest task if saturated.
1194       */
1195      public void testSaturatedExecute4() {
1196          RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
# Line 915 | Line 1211 | public class ThreadPoolExecutorSubclassT
1211      }
1212  
1213      /**
1214 <     *  execute throws RejectedExecutionException if shutdown
1214 >     * execute throws RejectedExecutionException if shutdown
1215       */
1216      public void testRejectedExecutionExceptionOnShutdown() {
1217 <        ThreadPoolExecutor tpe =
1217 >        ThreadPoolExecutor p =
1218              new CustomTPE(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
1219 <        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1219 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1220          try {
1221 <            tpe.execute(new NoOpRunnable());
1221 >            p.execute(new NoOpRunnable());
1222              shouldThrow();
1223          } catch (RejectedExecutionException success) {}
1224  
1225 <        joinPool(tpe);
1225 >        joinPool(p);
1226      }
1227  
1228      /**
1229 <     *  execute using CallerRunsPolicy drops task on shutdown
1229 >     * execute using CallerRunsPolicy drops task on shutdown
1230       */
1231      public void testCallerRunsOnShutdown() {
1232          RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
# Line 947 | Line 1243 | public class ThreadPoolExecutorSubclassT
1243      }
1244  
1245      /**
1246 <     *  execute using DiscardPolicy drops task on shutdown
1246 >     * execute using DiscardPolicy drops task on shutdown
1247       */
1248      public void testDiscardOnShutdown() {
1249          RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
# Line 963 | Line 1259 | public class ThreadPoolExecutorSubclassT
1259          }
1260      }
1261  
966
1262      /**
1263 <     *  execute using DiscardOldestPolicy drops task on shutdown
1263 >     * execute using DiscardOldestPolicy drops task on shutdown
1264       */
1265      public void testDiscardOldestOnShutdown() {
1266          RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
# Line 981 | Line 1276 | public class ThreadPoolExecutorSubclassT
1276          }
1277      }
1278  
984
1279      /**
1280       * execute(null) throws NPE
1281       */
1282      public void testExecuteNull() {
1283 <        ThreadPoolExecutor tpe = null;
1283 >        ThreadPoolExecutor p =
1284 >            new CustomTPE(1, 2, 1L, SECONDS,
1285 >                          new ArrayBlockingQueue<Runnable>(10));
1286          try {
1287 <            tpe = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
992 <            tpe.execute(null);
1287 >            p.execute(null);
1288              shouldThrow();
1289          } catch (NullPointerException success) {}
1290  
1291 <        joinPool(tpe);
1291 >        joinPool(p);
1292      }
1293  
1294      /**
1295 <     *  setCorePoolSize of negative value throws IllegalArgumentException
1295 >     * setCorePoolSize of negative value throws IllegalArgumentException
1296       */
1297      public void testCorePoolSizeIllegalArgumentException() {
1298 <        ThreadPoolExecutor tpe =
1298 >        ThreadPoolExecutor p =
1299              new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1300          try {
1301 <            tpe.setCorePoolSize(-1);
1301 >            p.setCorePoolSize(-1);
1302              shouldThrow();
1303          } catch (IllegalArgumentException success) {
1304          } finally {
1305 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1305 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1306          }
1307 <        joinPool(tpe);
1307 >        joinPool(p);
1308      }
1309  
1310      /**
1311 <     *  setMaximumPoolSize(int) throws IllegalArgumentException if
1312 <     *  given a value less the core pool size
1311 >     * setMaximumPoolSize(int) throws IllegalArgumentException
1312 >     * if given a value less the core pool size
1313       */
1314      public void testMaximumPoolSizeIllegalArgumentException() {
1315 <        ThreadPoolExecutor tpe =
1315 >        ThreadPoolExecutor p =
1316              new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1317          try {
1318 <            tpe.setMaximumPoolSize(1);
1318 >            p.setMaximumPoolSize(1);
1319              shouldThrow();
1320          } catch (IllegalArgumentException success) {
1321          } finally {
1322 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1322 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1323          }
1324 <        joinPool(tpe);
1324 >        joinPool(p);
1325      }
1326  
1327      /**
1328 <     *  setMaximumPoolSize throws IllegalArgumentException
1329 <     *  if given a negative value
1328 >     * setMaximumPoolSize throws IllegalArgumentException
1329 >     * if given a negative value
1330       */
1331      public void testMaximumPoolSizeIllegalArgumentException2() {
1332 <        ThreadPoolExecutor tpe =
1332 >        ThreadPoolExecutor p =
1333              new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1334          try {
1335 <            tpe.setMaximumPoolSize(-1);
1335 >            p.setMaximumPoolSize(-1);
1336              shouldThrow();
1337          } catch (IllegalArgumentException success) {
1338          } finally {
1339 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1339 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1340          }
1341 <        joinPool(tpe);
1341 >        joinPool(p);
1342      }
1343  
1049
1344      /**
1345 <     *  setKeepAliveTime  throws IllegalArgumentException
1346 <     *  when given a negative value
1345 >     * setKeepAliveTime throws IllegalArgumentException
1346 >     * when given a negative value
1347       */
1348      public void testKeepAliveTimeIllegalArgumentException() {
1349 <        ThreadPoolExecutor tpe =
1349 >        ThreadPoolExecutor p =
1350              new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1351  
1352          try {
1353 <            tpe.setKeepAliveTime(-1,MILLISECONDS);
1353 >            p.setKeepAliveTime(-1,MILLISECONDS);
1354              shouldThrow();
1355          } catch (IllegalArgumentException success) {
1356          } finally {
1357 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1357 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1358          }
1359 <        joinPool(tpe);
1359 >        joinPool(p);
1360      }
1361  
1362      /**
1363       * terminated() is called on termination
1364       */
1365      public void testTerminated() {
1366 <        CustomTPE tpe = new CustomTPE();
1367 <        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1368 <        assertTrue(tpe.terminatedCalled);
1369 <        joinPool(tpe);
1366 >        CustomTPE p = new CustomTPE();
1367 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1368 >        assertTrue(p.terminatedCalled());
1369 >        joinPool(p);
1370      }
1371  
1372      /**
1373       * beforeExecute and afterExecute are called when executing task
1374       */
1375      public void testBeforeAfter() throws InterruptedException {
1376 <        CustomTPE tpe = new CustomTPE();
1376 >        CustomTPE p = new CustomTPE();
1377          try {
1378 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1379 <            tpe.execute(r);
1380 <            Thread.sleep(SHORT_DELAY_MS);
1381 <            assertTrue(r.done);
1382 <            assertTrue(tpe.beforeCalled);
1383 <            assertTrue(tpe.afterCalled);
1384 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1378 >            final CountDownLatch done = new CountDownLatch(1);
1379 >            p.execute(new CheckedRunnable() {
1380 >                public void realRun() {
1381 >                    done.countDown();
1382 >                }});
1383 >            await(p.afterCalled);
1384 >            assertEquals(0, done.getCount());
1385 >            assertTrue(p.afterCalled());
1386 >            assertTrue(p.beforeCalled());
1387 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1388          } finally {
1389 <            joinPool(tpe);
1389 >            joinPool(p);
1390          }
1391      }
1392  
# Line 1135 | Line 1432 | public class ThreadPoolExecutorSubclassT
1432          }
1433      }
1434  
1138
1435      /**
1436       * invokeAny(null) throws NPE
1437       */
# Line 1297 | Line 1593 | public class ThreadPoolExecutorSubclassT
1593          }
1594      }
1595  
1300
1301
1596      /**
1597       * timed invokeAny(null) throws NPE
1598       */
# Line 1485 | Line 1779 | public class ThreadPoolExecutorSubclassT
1779              l.add(new StringTask());
1780              l.add(new StringTask());
1781              List<Future<String>> futures =
1782 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1782 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1783              assertEquals(2, futures.size());
1784              for (Future<String> future : futures)
1785                  assertSame(TEST_STRING, future.get());
# Line 1500 | Line 1794 | public class ThreadPoolExecutorSubclassT
1794      public void testTimedInvokeAll6() throws Exception {
1795          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1796          try {
1797 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1798 <            l.add(new StringTask());
1799 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1800 <            l.add(new StringTask());
1801 <            List<Future<String>> futures =
1802 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1803 <            assertEquals(3, futures.size());
1804 <            Iterator<Future<String>> it = futures.iterator();
1805 <            Future<String> f1 = it.next();
1806 <            Future<String> f2 = it.next();
1807 <            Future<String> f3 = it.next();
1808 <            assertTrue(f1.isDone());
1809 <            assertTrue(f2.isDone());
1810 <            assertTrue(f3.isDone());
1811 <            assertFalse(f1.isCancelled());
1812 <            assertTrue(f2.isCancelled());
1797 >            for (long timeout = timeoutMillis();;) {
1798 >                List<Callable<String>> tasks = new ArrayList<>();
1799 >                tasks.add(new StringTask("0"));
1800 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1801 >                tasks.add(new StringTask("2"));
1802 >                long startTime = System.nanoTime();
1803 >                List<Future<String>> futures =
1804 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1805 >                assertEquals(tasks.size(), futures.size());
1806 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1807 >                for (Future future : futures)
1808 >                    assertTrue(future.isDone());
1809 >                assertTrue(futures.get(1).isCancelled());
1810 >                try {
1811 >                    assertEquals("0", futures.get(0).get());
1812 >                    assertEquals("2", futures.get(2).get());
1813 >                    break;
1814 >                } catch (CancellationException retryWithLongerTimeout) {
1815 >                    timeout *= 2;
1816 >                    if (timeout >= LONG_DELAY_MS / 2)
1817 >                        fail("expected exactly one task to be cancelled");
1818 >                }
1819 >            }
1820          } finally {
1821              joinPool(e);
1822          }
# Line 1526 | Line 1827 | public class ThreadPoolExecutorSubclassT
1827       * thread factory fails to create more
1828       */
1829      public void testFailingThreadFactory() throws InterruptedException {
1830 <        ExecutorService e = new CustomTPE(100, 100, LONG_DELAY_MS, MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory());
1831 <        try {
1832 <            for (int k = 0; k < 100; ++k) {
1833 <                e.execute(new NoOpRunnable());
1834 <            }
1835 <            Thread.sleep(LONG_DELAY_MS);
1830 >        final ExecutorService e =
1831 >            new CustomTPE(100, 100,
1832 >                          LONG_DELAY_MS, MILLISECONDS,
1833 >                          new LinkedBlockingQueue<Runnable>(),
1834 >                          new FailingThreadFactory());
1835 >        try {
1836 >            final int TASKS = 100;
1837 >            final CountDownLatch done = new CountDownLatch(TASKS);
1838 >            for (int k = 0; k < TASKS; ++k)
1839 >                e.execute(new CheckedRunnable() {
1840 >                    public void realRun() {
1841 >                        done.countDown();
1842 >                    }});
1843 >            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1844          } finally {
1845              joinPool(e);
1846          }
# Line 1541 | Line 1850 | public class ThreadPoolExecutorSubclassT
1850       * allowsCoreThreadTimeOut is by default false.
1851       */
1852      public void testAllowsCoreThreadTimeOut() {
1853 <        ThreadPoolExecutor tpe = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1854 <        assertFalse(tpe.allowsCoreThreadTimeOut());
1855 <        joinPool(tpe);
1853 >        ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1854 >        assertFalse(p.allowsCoreThreadTimeOut());
1855 >        joinPool(p);
1856      }
1857  
1858      /**
1859       * allowCoreThreadTimeOut(true) causes idle threads to time out
1860       */
1861 <    public void testAllowCoreThreadTimeOut_true() throws InterruptedException {
1862 <        ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1863 <        tpe.allowCoreThreadTimeOut(true);
1864 <        tpe.execute(new NoOpRunnable());
1865 <        try {
1866 <            Thread.sleep(MEDIUM_DELAY_MS);
1867 <            assertEquals(0, tpe.getPoolSize());
1861 >    public void testAllowCoreThreadTimeOut_true() throws Exception {
1862 >        long keepAliveTime = timeoutMillis();
1863 >        final ThreadPoolExecutor p =
1864 >            new CustomTPE(2, 10,
1865 >                          keepAliveTime, MILLISECONDS,
1866 >                          new ArrayBlockingQueue<Runnable>(10));
1867 >        final CountDownLatch threadStarted = new CountDownLatch(1);
1868 >        try {
1869 >            p.allowCoreThreadTimeOut(true);
1870 >            p.execute(new CheckedRunnable() {
1871 >                public void realRun() {
1872 >                    threadStarted.countDown();
1873 >                    assertEquals(1, p.getPoolSize());
1874 >                }});
1875 >            await(threadStarted);
1876 >            delay(keepAliveTime);
1877 >            long startTime = System.nanoTime();
1878 >            while (p.getPoolSize() > 0
1879 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1880 >                Thread.yield();
1881 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1882 >            assertEquals(0, p.getPoolSize());
1883          } finally {
1884 <            joinPool(tpe);
1884 >            joinPool(p);
1885          }
1886      }
1887  
1888      /**
1889       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1890       */
1891 <    public void testAllowCoreThreadTimeOut_false() throws InterruptedException {
1892 <        ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1893 <        tpe.allowCoreThreadTimeOut(false);
1894 <        tpe.execute(new NoOpRunnable());
1895 <        try {
1896 <            Thread.sleep(MEDIUM_DELAY_MS);
1897 <            assertTrue(tpe.getPoolSize() >= 1);
1891 >    public void testAllowCoreThreadTimeOut_false() throws Exception {
1892 >        long keepAliveTime = timeoutMillis();
1893 >        final ThreadPoolExecutor p =
1894 >            new CustomTPE(2, 10,
1895 >                          keepAliveTime, MILLISECONDS,
1896 >                          new ArrayBlockingQueue<Runnable>(10));
1897 >        final CountDownLatch threadStarted = new CountDownLatch(1);
1898 >        try {
1899 >            p.allowCoreThreadTimeOut(false);
1900 >            p.execute(new CheckedRunnable() {
1901 >                public void realRun() throws InterruptedException {
1902 >                    threadStarted.countDown();
1903 >                    assertTrue(p.getPoolSize() >= 1);
1904 >                }});
1905 >            delay(2 * keepAliveTime);
1906 >            assertTrue(p.getPoolSize() >= 1);
1907 >        } finally {
1908 >            joinPool(p);
1909 >        }
1910 >    }
1911 >
1912 >    /**
1913 >     * get(cancelled task) throws CancellationException
1914 >     * (in part, a test of CustomTPE itself)
1915 >     */
1916 >    public void testGet_cancelled() throws Exception {
1917 >        final ExecutorService e =
1918 >            new CustomTPE(1, 1,
1919 >                          LONG_DELAY_MS, MILLISECONDS,
1920 >                          new LinkedBlockingQueue<Runnable>());
1921 >        try {
1922 >            final CountDownLatch blockerStarted = new CountDownLatch(1);
1923 >            final CountDownLatch done = new CountDownLatch(1);
1924 >            final List<Future<?>> futures = new ArrayList<>();
1925 >            for (int i = 0; i < 2; i++) {
1926 >                Runnable r = new CheckedRunnable() { public void realRun()
1927 >                                                         throws Throwable {
1928 >                    blockerStarted.countDown();
1929 >                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
1930 >                }};
1931 >                futures.add(e.submit(r));
1932 >            }
1933 >            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
1934 >            for (Future<?> future : futures) future.cancel(false);
1935 >            for (Future<?> future : futures) {
1936 >                try {
1937 >                    future.get();
1938 >                    shouldThrow();
1939 >                } catch (CancellationException success) {}
1940 >                try {
1941 >                    future.get(LONG_DELAY_MS, MILLISECONDS);
1942 >                    shouldThrow();
1943 >                } catch (CancellationException success) {}
1944 >                assertTrue(future.isCancelled());
1945 >                assertTrue(future.isDone());
1946 >            }
1947 >            done.countDown();
1948          } finally {
1949 <            joinPool(tpe);
1949 >            joinPool(e);
1950          }
1951      }
1952  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines