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.14 by jsr166, Sat Nov 21 20:13:20 2009 UTC vs.
Revision 1.53 by jsr166, Sun Oct 4 01:23:41 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 >        final CountDownLatch threadStarted = new CountDownLatch(1);
329 >        final CountDownLatch threadProceed = new CountDownLatch(1);
330 >        final CountDownLatch threadDone = new CountDownLatch(1);
331 >        try {
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 >        } finally {
351 >            joinPool(p);
352 >        }
353      }
354  
355      /**
356 <     *   getCorePoolSize returns size given in constructor if not otherwise set
356 >     * getCorePoolSize returns size given in constructor if not otherwise set
357       */
358      public void testGetCorePoolSize() {
359 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
360 <        assertEquals(1, p1.getCorePoolSize());
361 <        joinPool(p1);
359 >        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
360 >        assertEquals(1, p.getCorePoolSize());
361 >        joinPool(p);
362      }
363  
364      /**
365 <     *   getKeepAliveTime returns value given in constructor if not otherwise set
365 >     * getKeepAliveTime returns value given in constructor if not otherwise set
366       */
367      public void testGetKeepAliveTime() {
368 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
369 <        assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS));
370 <        joinPool(p2);
368 >        ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
369 >        assertEquals(1, p.getKeepAliveTime(SECONDS));
370 >        joinPool(p);
371      }
372  
280
373      /**
374       * getThreadFactory returns factory in constructor if not set
375       */
# Line 299 | Line 391 | public class ThreadPoolExecutorSubclassT
391          joinPool(p);
392      }
393  
302
394      /**
395       * setThreadFactory(null) throws NPE
396       */
# Line 336 | Line 427 | public class ThreadPoolExecutorSubclassT
427          joinPool(p);
428      }
429  
339
430      /**
431       * setRejectedExecutionHandler(null) throws NPE
432       */
# Line 351 | Line 441 | public class ThreadPoolExecutorSubclassT
441          }
442      }
443  
354
444      /**
445 <     *   getLargestPoolSize increases, but doesn't overestimate, when
446 <     *   multiple threads active
445 >     * getLargestPoolSize increases, but doesn't overestimate, when
446 >     * multiple threads active
447       */
448      public void testGetLargestPoolSize() throws InterruptedException {
449 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
450 <        assertEquals(0, p2.getLargestPoolSize());
451 <        p2.execute(new MediumRunnable());
452 <        p2.execute(new MediumRunnable());
453 <        Thread.sleep(SHORT_DELAY_MS);
454 <        assertEquals(2, p2.getLargestPoolSize());
455 <        joinPool(p2);
449 >        final int THREADS = 3;
450 >        final ThreadPoolExecutor p =
451 >            new CustomTPE(THREADS, THREADS,
452 >                          LONG_DELAY_MS, MILLISECONDS,
453 >                          new ArrayBlockingQueue<Runnable>(10));
454 >        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
455 >        final CountDownLatch done = new CountDownLatch(1);
456 >        try {
457 >            assertEquals(0, p.getLargestPoolSize());
458 >            for (int i = 0; i < THREADS; i++)
459 >                p.execute(new CheckedRunnable() {
460 >                    public void realRun() throws InterruptedException {
461 >                        threadsStarted.countDown();
462 >                        done.await();
463 >                        assertEquals(THREADS, p.getLargestPoolSize());
464 >                    }});
465 >            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
466 >            assertEquals(THREADS, p.getLargestPoolSize());
467 >        } finally {
468 >            done.countDown();
469 >            joinPool(p);
470 >            assertEquals(THREADS, p.getLargestPoolSize());
471 >        }
472      }
473  
474      /**
475 <     *   getMaximumPoolSize returns value given in constructor if not
476 <     *   otherwise set
475 >     * getMaximumPoolSize returns value given in constructor if not
476 >     * otherwise set
477       */
478      public void testGetMaximumPoolSize() {
479 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
480 <        assertEquals(2, p2.getMaximumPoolSize());
481 <        joinPool(p2);
479 >        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
480 >        assertEquals(2, p.getMaximumPoolSize());
481 >        joinPool(p);
482      }
483  
484      /**
485 <     *   getPoolSize increases, but doesn't overestimate, when threads
486 <     *   become active
485 >     * getPoolSize increases, but doesn't overestimate, when threads
486 >     * become active
487       */
488 <    public void testGetPoolSize() {
489 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
490 <        assertEquals(0, p1.getPoolSize());
491 <        p1.execute(new MediumRunnable());
492 <        assertEquals(1, p1.getPoolSize());
493 <        joinPool(p1);
488 >    public void testGetPoolSize() throws InterruptedException {
489 >        final ThreadPoolExecutor p =
490 >            new CustomTPE(1, 1,
491 >                          LONG_DELAY_MS, MILLISECONDS,
492 >                          new ArrayBlockingQueue<Runnable>(10));
493 >        final CountDownLatch threadStarted = new CountDownLatch(1);
494 >        final CountDownLatch done = new CountDownLatch(1);
495 >        try {
496 >            assertEquals(0, p.getPoolSize());
497 >            p.execute(new CheckedRunnable() {
498 >                public void realRun() throws InterruptedException {
499 >                    threadStarted.countDown();
500 >                    assertEquals(1, p.getPoolSize());
501 >                    done.await();
502 >                }});
503 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
504 >            assertEquals(1, p.getPoolSize());
505 >        } finally {
506 >            done.countDown();
507 >            joinPool(p);
508 >        }
509      }
510  
511      /**
512 <     *  getTaskCount increases, but doesn't overestimate, when tasks submitted
512 >     * getTaskCount increases, but doesn't overestimate, when tasks submitted
513       */
514      public void testGetTaskCount() throws InterruptedException {
515 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
516 <        assertEquals(0, p1.getTaskCount());
517 <        p1.execute(new MediumRunnable());
518 <        Thread.sleep(SHORT_DELAY_MS);
519 <        assertEquals(1, p1.getTaskCount());
520 <        joinPool(p1);
515 >        final ThreadPoolExecutor p =
516 >            new CustomTPE(1, 1,
517 >                          LONG_DELAY_MS, MILLISECONDS,
518 >                          new ArrayBlockingQueue<Runnable>(10));
519 >        final CountDownLatch threadStarted = new CountDownLatch(1);
520 >        final CountDownLatch done = new CountDownLatch(1);
521 >        try {
522 >            assertEquals(0, p.getTaskCount());
523 >            p.execute(new CheckedRunnable() {
524 >                public void realRun() throws InterruptedException {
525 >                    threadStarted.countDown();
526 >                    assertEquals(1, p.getTaskCount());
527 >                    done.await();
528 >                }});
529 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
530 >            assertEquals(1, p.getTaskCount());
531 >        } finally {
532 >            done.countDown();
533 >            joinPool(p);
534 >        }
535      }
536  
537      /**
538 <     *   isShutDown is false before shutdown, true after
538 >     * isShutdown is false before shutdown, true after
539       */
540      public void testIsShutdown() {
541  
542 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
543 <        assertFalse(p1.isShutdown());
544 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
545 <        assertTrue(p1.isShutdown());
546 <        joinPool(p1);
542 >        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
543 >        assertFalse(p.isShutdown());
544 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
545 >        assertTrue(p.isShutdown());
546 >        joinPool(p);
547      }
548  
415
549      /**
550 <     *  isTerminated is false before termination, true after
550 >     * isTerminated is false before termination, true after
551       */
552      public void testIsTerminated() throws InterruptedException {
553 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
554 <        assertFalse(p1.isTerminated());
555 <        try {
556 <            p1.execute(new MediumRunnable());
557 <        } finally {
558 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
559 <        }
560 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
561 <        assertTrue(p1.isTerminated());
553 >        final ThreadPoolExecutor p =
554 >            new CustomTPE(1, 1,
555 >                          LONG_DELAY_MS, MILLISECONDS,
556 >                          new ArrayBlockingQueue<Runnable>(10));
557 >        final CountDownLatch threadStarted = new CountDownLatch(1);
558 >        final CountDownLatch done = new CountDownLatch(1);
559 >        try {
560 >            assertFalse(p.isTerminating());
561 >            p.execute(new CheckedRunnable() {
562 >                public void realRun() throws InterruptedException {
563 >                    assertFalse(p.isTerminating());
564 >                    threadStarted.countDown();
565 >                    done.await();
566 >                }});
567 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
568 >            assertFalse(p.isTerminating());
569 >            done.countDown();
570 >        } finally {
571 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
572 >        }
573 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
574 >        assertTrue(p.isTerminated());
575 >        assertFalse(p.isTerminating());
576      }
577  
578      /**
579 <     *  isTerminating is not true when running or when terminated
579 >     * isTerminating is not true when running or when terminated
580       */
581      public void testIsTerminating() throws InterruptedException {
582 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
583 <        assertFalse(p1.isTerminating());
584 <        try {
585 <            p1.execute(new SmallRunnable());
586 <            assertFalse(p1.isTerminating());
587 <        } finally {
588 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
589 <        }
590 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
591 <        assertTrue(p1.isTerminated());
592 <        assertFalse(p1.isTerminating());
582 >        final ThreadPoolExecutor p =
583 >            new CustomTPE(1, 1,
584 >                          LONG_DELAY_MS, MILLISECONDS,
585 >                          new ArrayBlockingQueue<Runnable>(10));
586 >        final CountDownLatch threadStarted = new CountDownLatch(1);
587 >        final CountDownLatch done = new CountDownLatch(1);
588 >        try {
589 >            assertFalse(p.isTerminating());
590 >            p.execute(new CheckedRunnable() {
591 >                public void realRun() throws InterruptedException {
592 >                    assertFalse(p.isTerminating());
593 >                    threadStarted.countDown();
594 >                    done.await();
595 >                }});
596 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
597 >            assertFalse(p.isTerminating());
598 >            done.countDown();
599 >        } finally {
600 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
601 >        }
602 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
603 >        assertTrue(p.isTerminated());
604 >        assertFalse(p.isTerminating());
605      }
606  
607      /**
608       * getQueue returns the work queue, which contains queued tasks
609       */
610      public void testGetQueue() throws InterruptedException {
611 <        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
612 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, q);
613 <        FutureTask[] tasks = new FutureTask[5];
614 <        for (int i = 0; i < 5; i++) {
615 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
616 <            p1.execute(tasks[i]);
617 <        }
618 <        try {
619 <            Thread.sleep(SHORT_DELAY_MS);
620 <            BlockingQueue<Runnable> wq = p1.getQueue();
621 <            assertSame(q, wq);
622 <            assertFalse(wq.contains(tasks[0]));
623 <            assertTrue(wq.contains(tasks[4]));
624 <            for (int i = 1; i < 5; ++i)
625 <                tasks[i].cancel(true);
626 <            p1.shutdownNow();
611 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
612 >        final ThreadPoolExecutor p =
613 >            new CustomTPE(1, 1,
614 >                          LONG_DELAY_MS, MILLISECONDS,
615 >                          q);
616 >        final CountDownLatch threadStarted = new CountDownLatch(1);
617 >        final CountDownLatch done = new CountDownLatch(1);
618 >        try {
619 >            FutureTask[] tasks = new FutureTask[5];
620 >            for (int i = 0; i < tasks.length; i++) {
621 >                Callable task = new CheckedCallable<Boolean>() {
622 >                    public Boolean realCall() throws InterruptedException {
623 >                        threadStarted.countDown();
624 >                        assertSame(q, p.getQueue());
625 >                        done.await();
626 >                        return Boolean.TRUE;
627 >                    }};
628 >                tasks[i] = new FutureTask(task);
629 >                p.execute(tasks[i]);
630 >            }
631 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
632 >            assertSame(q, p.getQueue());
633 >            assertFalse(q.contains(tasks[0]));
634 >            assertTrue(q.contains(tasks[tasks.length - 1]));
635 >            assertEquals(tasks.length - 1, q.size());
636          } finally {
637 <            joinPool(p1);
637 >            done.countDown();
638 >            joinPool(p);
639          }
640      }
641  
# Line 475 | Line 644 | public class ThreadPoolExecutorSubclassT
644       */
645      public void testRemove() throws InterruptedException {
646          BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
647 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, q);
648 <        FutureTask[] tasks = new FutureTask[5];
649 <        for (int i = 0; i < 5; i++) {
650 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
651 <            p1.execute(tasks[i]);
652 <        }
653 <        try {
654 <            Thread.sleep(SHORT_DELAY_MS);
655 <            assertFalse(p1.remove(tasks[0]));
647 >        final ThreadPoolExecutor p =
648 >            new CustomTPE(1, 1,
649 >                          LONG_DELAY_MS, MILLISECONDS,
650 >                          q);
651 >        Runnable[] tasks = new Runnable[6];
652 >        final CountDownLatch threadStarted = new CountDownLatch(1);
653 >        final CountDownLatch done = new CountDownLatch(1);
654 >        try {
655 >            for (int i = 0; i < tasks.length; i++) {
656 >                tasks[i] = new CheckedRunnable() {
657 >                        public void realRun() throws InterruptedException {
658 >                            threadStarted.countDown();
659 >                            done.await();
660 >                        }};
661 >                p.execute(tasks[i]);
662 >            }
663 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
664 >            assertFalse(p.remove(tasks[0]));
665              assertTrue(q.contains(tasks[4]));
666              assertTrue(q.contains(tasks[3]));
667 <            assertTrue(p1.remove(tasks[4]));
668 <            assertFalse(p1.remove(tasks[4]));
667 >            assertTrue(p.remove(tasks[4]));
668 >            assertFalse(p.remove(tasks[4]));
669              assertFalse(q.contains(tasks[4]));
670              assertTrue(q.contains(tasks[3]));
671 <            assertTrue(p1.remove(tasks[3]));
671 >            assertTrue(p.remove(tasks[3]));
672              assertFalse(q.contains(tasks[3]));
673          } finally {
674 <            joinPool(p1);
674 >            done.countDown();
675 >            joinPool(p);
676          }
677      }
678  
679      /**
680 <     *   purge removes cancelled tasks from the queue
680 >     * purge removes cancelled tasks from the queue
681       */
682 <    public void testPurge() {
683 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
682 >    public void testPurge() throws InterruptedException {
683 >        final CountDownLatch threadStarted = new CountDownLatch(1);
684 >        final CountDownLatch done = new CountDownLatch(1);
685 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
686 >        final ThreadPoolExecutor p =
687 >            new CustomTPE(1, 1,
688 >                          LONG_DELAY_MS, MILLISECONDS,
689 >                          q);
690          FutureTask[] tasks = new FutureTask[5];
691 <        for (int i = 0; i < 5; i++) {
692 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
693 <            p1.execute(tasks[i]);
691 >        try {
692 >            for (int i = 0; i < tasks.length; i++) {
693 >                Callable task = new CheckedCallable<Boolean>() {
694 >                    public Boolean realCall() throws InterruptedException {
695 >                        threadStarted.countDown();
696 >                        done.await();
697 >                        return Boolean.TRUE;
698 >                    }};
699 >                tasks[i] = new FutureTask(task);
700 >                p.execute(tasks[i]);
701 >            }
702 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
703 >            assertEquals(tasks.length, p.getTaskCount());
704 >            assertEquals(tasks.length - 1, q.size());
705 >            assertEquals(1L, p.getActiveCount());
706 >            assertEquals(0L, p.getCompletedTaskCount());
707 >            tasks[4].cancel(true);
708 >            tasks[3].cancel(false);
709 >            p.purge();
710 >            assertEquals(tasks.length - 3, q.size());
711 >            assertEquals(tasks.length - 2, p.getTaskCount());
712 >            p.purge();         // Nothing to do
713 >            assertEquals(tasks.length - 3, q.size());
714 >            assertEquals(tasks.length - 2, p.getTaskCount());
715 >        } finally {
716 >            done.countDown();
717 >            joinPool(p);
718          }
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);
719      }
720  
721      /**
722 <     *  shutDownNow returns a list containing tasks that were not run
722 >     * shutdownNow returns a list containing tasks that were not run,
723 >     * and those tasks are drained from the queue
724       */
725 <    public void testShutDownNow() {
726 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
727 <        List l;
728 <        try {
729 <            for (int i = 0; i < 5; i++)
730 <                p1.execute(new MediumPossiblyInterruptedRunnable());
731 <        }
732 <        finally {
725 >    public void testShutdownNow() throws InterruptedException {
726 >        final int poolSize = 2;
727 >        final int count = 5;
728 >        final AtomicInteger ran = new AtomicInteger(0);
729 >        ThreadPoolExecutor p =
730 >            new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS,
731 >                          new ArrayBlockingQueue<Runnable>(10));
732 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
733 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
734 >            threadsStarted.countDown();
735              try {
736 <                l = p1.shutdownNow();
737 <            } catch (SecurityException ok) { return; }
738 <
739 <        }
740 <        assertTrue(p1.isShutdown());
741 <        assertTrue(l.size() <= 4);
736 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
737 >            } catch (InterruptedException success) {}
738 >            ran.getAndIncrement();
739 >        }};
740 >        for (int i = 0; i < count; i++)
741 >            p.execute(waiter);
742 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
743 >        assertEquals(poolSize, p.getActiveCount());
744 >        assertEquals(0, p.getCompletedTaskCount());
745 >        final List<Runnable> queuedTasks;
746 >        try {
747 >            queuedTasks = p.shutdownNow();
748 >        } catch (SecurityException ok) {
749 >            return; // Allowed in case test doesn't have privs
750 >        }
751 >        assertTrue(p.isShutdown());
752 >        assertTrue(p.getQueue().isEmpty());
753 >        assertEquals(count - poolSize, queuedTasks.size());
754 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
755 >        assertTrue(p.isTerminated());
756 >        assertEquals(poolSize, ran.get());
757 >        assertEquals(poolSize, p.getCompletedTaskCount());
758      }
759  
760      // Exception Tests
761  
540
762      /**
763       * Constructor throws if corePoolSize argument is less than zero
764       */
765      public void testConstructor1() {
766          try {
767 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
767 >            new CustomTPE(-1, 1, 1L, SECONDS,
768 >                          new ArrayBlockingQueue<Runnable>(10));
769              shouldThrow();
770          } catch (IllegalArgumentException success) {}
771      }
# Line 553 | Line 775 | public class ThreadPoolExecutorSubclassT
775       */
776      public void testConstructor2() {
777          try {
778 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
778 >            new CustomTPE(1, -1, 1L, SECONDS,
779 >                          new ArrayBlockingQueue<Runnable>(10));
780              shouldThrow();
781          } catch (IllegalArgumentException success) {}
782      }
# Line 563 | Line 786 | public class ThreadPoolExecutorSubclassT
786       */
787      public void testConstructor3() {
788          try {
789 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
789 >            new CustomTPE(1, 0, 1L, SECONDS,
790 >                          new ArrayBlockingQueue<Runnable>(10));
791              shouldThrow();
792          } catch (IllegalArgumentException success) {}
793      }
# Line 573 | Line 797 | public class ThreadPoolExecutorSubclassT
797       */
798      public void testConstructor4() {
799          try {
800 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
800 >            new CustomTPE(1, 2, -1L, SECONDS,
801 >                          new ArrayBlockingQueue<Runnable>(10));
802              shouldThrow();
803          } catch (IllegalArgumentException success) {}
804      }
# Line 583 | Line 808 | public class ThreadPoolExecutorSubclassT
808       */
809      public void testConstructor5() {
810          try {
811 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
811 >            new CustomTPE(2, 1, 1L, SECONDS,
812 >                          new ArrayBlockingQueue<Runnable>(10));
813              shouldThrow();
814          } catch (IllegalArgumentException success) {}
815      }
# Line 593 | Line 819 | public class ThreadPoolExecutorSubclassT
819       */
820      public void testConstructorNullPointerException() {
821          try {
822 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null);
822 >            new CustomTPE(1, 2, 1L, SECONDS, null);
823              shouldThrow();
824          } catch (NullPointerException success) {}
825      }
826  
601
602
827      /**
828       * Constructor throws if corePoolSize argument is less than zero
829       */
830      public void testConstructor6() {
831          try {
832 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
832 >            new CustomTPE(-1, 1, 1L, SECONDS,
833 >                          new ArrayBlockingQueue<Runnable>(10),
834 >                          new SimpleThreadFactory());
835              shouldThrow();
836          } catch (IllegalArgumentException success) {}
837      }
# Line 615 | Line 841 | public class ThreadPoolExecutorSubclassT
841       */
842      public void testConstructor7() {
843          try {
844 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
844 >            new CustomTPE(1,-1, 1L, SECONDS,
845 >                          new ArrayBlockingQueue<Runnable>(10),
846 >                          new SimpleThreadFactory());
847              shouldThrow();
848          } catch (IllegalArgumentException success) {}
849      }
# Line 625 | Line 853 | public class ThreadPoolExecutorSubclassT
853       */
854      public void testConstructor8() {
855          try {
856 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
856 >            new CustomTPE(1, 0, 1L, SECONDS,
857 >                          new ArrayBlockingQueue<Runnable>(10),
858 >                          new SimpleThreadFactory());
859              shouldThrow();
860          } catch (IllegalArgumentException success) {}
861      }
# Line 635 | Line 865 | public class ThreadPoolExecutorSubclassT
865       */
866      public void testConstructor9() {
867          try {
868 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
868 >            new CustomTPE(1, 2, -1L, SECONDS,
869 >                          new ArrayBlockingQueue<Runnable>(10),
870 >                          new SimpleThreadFactory());
871              shouldThrow();
872          } catch (IllegalArgumentException success) {}
873      }
# Line 645 | Line 877 | public class ThreadPoolExecutorSubclassT
877       */
878      public void testConstructor10() {
879          try {
880 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
880 >            new CustomTPE(2, 1, 1L, SECONDS,
881 >                          new ArrayBlockingQueue<Runnable>(10),
882 >                          new SimpleThreadFactory());
883              shouldThrow();
884          } catch (IllegalArgumentException success) {}
885      }
# Line 655 | Line 889 | public class ThreadPoolExecutorSubclassT
889       */
890      public void testConstructorNullPointerException2() {
891          try {
892 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
892 >            new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory());
893              shouldThrow();
894          } catch (NullPointerException success) {}
895      }
# Line 665 | Line 899 | public class ThreadPoolExecutorSubclassT
899       */
900      public void testConstructorNullPointerException3() {
901          try {
902 <            ThreadFactory f = null;
903 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
902 >            new CustomTPE(1, 2, 1L, SECONDS,
903 >                          new ArrayBlockingQueue<Runnable>(10),
904 >                          (ThreadFactory) null);
905              shouldThrow();
906          } catch (NullPointerException success) {}
907      }
908  
674
909      /**
910       * Constructor throws if corePoolSize argument is less than zero
911       */
912      public void testConstructor11() {
913          try {
914 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
914 >            new CustomTPE(-1, 1, 1L, SECONDS,
915 >                          new ArrayBlockingQueue<Runnable>(10),
916 >                          new NoOpREHandler());
917              shouldThrow();
918          } catch (IllegalArgumentException success) {}
919      }
# Line 687 | Line 923 | public class ThreadPoolExecutorSubclassT
923       */
924      public void testConstructor12() {
925          try {
926 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
926 >            new CustomTPE(1, -1, 1L, SECONDS,
927 >                          new ArrayBlockingQueue<Runnable>(10),
928 >                          new NoOpREHandler());
929              shouldThrow();
930          } catch (IllegalArgumentException success) {}
931      }
# Line 697 | Line 935 | public class ThreadPoolExecutorSubclassT
935       */
936      public void testConstructor13() {
937          try {
938 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
938 >            new CustomTPE(1, 0, 1L, SECONDS,
939 >                          new ArrayBlockingQueue<Runnable>(10),
940 >                          new NoOpREHandler());
941              shouldThrow();
942          } catch (IllegalArgumentException success) {}
943      }
# Line 707 | Line 947 | public class ThreadPoolExecutorSubclassT
947       */
948      public void testConstructor14() {
949          try {
950 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
950 >            new CustomTPE(1, 2, -1L, SECONDS,
951 >                          new ArrayBlockingQueue<Runnable>(10),
952 >                          new NoOpREHandler());
953              shouldThrow();
954          } catch (IllegalArgumentException success) {}
955      }
# Line 717 | Line 959 | public class ThreadPoolExecutorSubclassT
959       */
960      public void testConstructor15() {
961          try {
962 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
962 >            new CustomTPE(2, 1, 1L, SECONDS,
963 >                          new ArrayBlockingQueue<Runnable>(10),
964 >                          new NoOpREHandler());
965              shouldThrow();
966          } catch (IllegalArgumentException success) {}
967      }
# Line 727 | Line 971 | public class ThreadPoolExecutorSubclassT
971       */
972      public void testConstructorNullPointerException4() {
973          try {
974 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
974 >            new CustomTPE(1, 2, 1L, SECONDS,
975 >                          null,
976 >                          new NoOpREHandler());
977              shouldThrow();
978          } catch (NullPointerException success) {}
979      }
# Line 737 | Line 983 | public class ThreadPoolExecutorSubclassT
983       */
984      public void testConstructorNullPointerException5() {
985          try {
986 <            RejectedExecutionHandler r = null;
987 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
986 >            new CustomTPE(1, 2, 1L, SECONDS,
987 >                          new ArrayBlockingQueue<Runnable>(10),
988 >                          (RejectedExecutionHandler) null);
989              shouldThrow();
990          } catch (NullPointerException success) {}
991      }
992  
746
993      /**
994       * Constructor throws if corePoolSize argument is less than zero
995       */
996      public void testConstructor16() {
997          try {
998 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
998 >            new CustomTPE(-1, 1, 1L, SECONDS,
999 >                          new ArrayBlockingQueue<Runnable>(10),
1000 >                          new SimpleThreadFactory(),
1001 >                          new NoOpREHandler());
1002              shouldThrow();
1003          } catch (IllegalArgumentException success) {}
1004      }
# Line 759 | Line 1008 | public class ThreadPoolExecutorSubclassT
1008       */
1009      public void testConstructor17() {
1010          try {
1011 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1011 >            new CustomTPE(1, -1, 1L, SECONDS,
1012 >                          new ArrayBlockingQueue<Runnable>(10),
1013 >                          new SimpleThreadFactory(),
1014 >                          new NoOpREHandler());
1015              shouldThrow();
1016          } catch (IllegalArgumentException success) {}
1017      }
# Line 769 | Line 1021 | public class ThreadPoolExecutorSubclassT
1021       */
1022      public void testConstructor18() {
1023          try {
1024 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1024 >            new CustomTPE(1, 0, 1L, SECONDS,
1025 >                          new ArrayBlockingQueue<Runnable>(10),
1026 >                          new SimpleThreadFactory(),
1027 >                          new NoOpREHandler());
1028              shouldThrow();
1029          } catch (IllegalArgumentException success) {}
1030      }
# Line 779 | Line 1034 | public class ThreadPoolExecutorSubclassT
1034       */
1035      public void testConstructor19() {
1036          try {
1037 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1037 >            new CustomTPE(1, 2, -1L, SECONDS,
1038 >                          new ArrayBlockingQueue<Runnable>(10),
1039 >                          new SimpleThreadFactory(),
1040 >                          new NoOpREHandler());
1041              shouldThrow();
1042          } catch (IllegalArgumentException success) {}
1043      }
# Line 789 | Line 1047 | public class ThreadPoolExecutorSubclassT
1047       */
1048      public void testConstructor20() {
1049          try {
1050 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1050 >            new CustomTPE(2, 1, 1L, SECONDS,
1051 >                          new ArrayBlockingQueue<Runnable>(10),
1052 >                          new SimpleThreadFactory(),
1053 >                          new NoOpREHandler());
1054              shouldThrow();
1055          } catch (IllegalArgumentException success) {}
1056      }
1057  
1058      /**
1059 <     * Constructor throws if workQueue is set to null
1059 >     * Constructor throws if workQueue is null
1060       */
1061      public void testConstructorNullPointerException6() {
1062          try {
1063 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
1063 >            new CustomTPE(1, 2, 1L, SECONDS,
1064 >                          null,
1065 >                          new SimpleThreadFactory(),
1066 >                          new NoOpREHandler());
1067              shouldThrow();
1068          } catch (NullPointerException success) {}
1069      }
1070  
1071      /**
1072 <     * Constructor throws if handler is set to null
1072 >     * Constructor throws if handler is null
1073       */
1074      public void testConstructorNullPointerException7() {
1075          try {
1076 <            RejectedExecutionHandler r = null;
1077 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
1076 >            new CustomTPE(1, 2, 1L, SECONDS,
1077 >                          new ArrayBlockingQueue<Runnable>(10),
1078 >                          new SimpleThreadFactory(),
1079 >                          (RejectedExecutionHandler) null);
1080              shouldThrow();
1081          } catch (NullPointerException success) {}
1082      }
1083  
1084      /**
1085 <     * Constructor throws if ThreadFactory is set top null
1085 >     * Constructor throws if ThreadFactory is null
1086       */
1087      public void testConstructorNullPointerException8() {
1088          try {
1089 <            ThreadFactory f = null;
1090 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
1089 >            new CustomTPE(1, 2, 1L, SECONDS,
1090 >                          new ArrayBlockingQueue<Runnable>(10),
1091 >                          (ThreadFactory) null,
1092 >                          new NoOpREHandler());
1093              shouldThrow();
1094          } catch (NullPointerException success) {}
1095      }
1096  
829
1097      /**
1098 <     *  execute throws RejectedExecutionException
832 <     *  if saturated.
1098 >     * execute throws RejectedExecutionException if saturated.
1099       */
1100      public void testSaturatedExecute() {
1101 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
1102 <        try {
1103 <
1104 <            for (int i = 0; i < 5; ++i) {
1105 <                p.execute(new MediumRunnable());
1101 >        ThreadPoolExecutor p =
1102 >            new CustomTPE(1, 1,
1103 >                          LONG_DELAY_MS, MILLISECONDS,
1104 >                          new ArrayBlockingQueue<Runnable>(1));
1105 >        final CountDownLatch done = new CountDownLatch(1);
1106 >        try {
1107 >            Runnable task = new CheckedRunnable() {
1108 >                public void realRun() throws InterruptedException {
1109 >                    done.await();
1110 >                }};
1111 >            for (int i = 0; i < 2; ++i)
1112 >                p.execute(task);
1113 >            for (int i = 0; i < 2; ++i) {
1114 >                try {
1115 >                    p.execute(task);
1116 >                    shouldThrow();
1117 >                } catch (RejectedExecutionException success) {}
1118 >                assertTrue(p.getTaskCount() <= 2);
1119              }
1120 <            shouldThrow();
1121 <        } catch (RejectedExecutionException success) {}
1122 <        joinPool(p);
1120 >        } finally {
1121 >            done.countDown();
1122 >            joinPool(p);
1123 >        }
1124      }
1125  
1126      /**
1127 <     *  executor using CallerRunsPolicy runs task if saturated.
1127 >     * executor using CallerRunsPolicy runs task if saturated.
1128       */
1129      public void testSaturatedExecute2() {
1130          RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1131 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1131 >        ThreadPoolExecutor p = new CustomTPE(1, 1,
1132 >                                             LONG_DELAY_MS, MILLISECONDS,
1133 >                                             new ArrayBlockingQueue<Runnable>(1),
1134 >                                             h);
1135          try {
853
1136              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1137 <            for (int i = 0; i < 5; ++i) {
1137 >            for (int i = 0; i < tasks.length; ++i)
1138                  tasks[i] = new TrackedNoOpRunnable();
857            }
1139              TrackedLongRunnable mr = new TrackedLongRunnable();
1140              p.execute(mr);
1141 <            for (int i = 0; i < 5; ++i) {
1141 >            for (int i = 0; i < tasks.length; ++i)
1142                  p.execute(tasks[i]);
1143 <            }
863 <            for (int i = 1; i < 5; ++i) {
1143 >            for (int i = 1; i < tasks.length; ++i)
1144                  assertTrue(tasks[i].done);
865            }
1145              try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1146          } finally {
1147              joinPool(p);
# Line 870 | Line 1149 | public class ThreadPoolExecutorSubclassT
1149      }
1150  
1151      /**
1152 <     *  executor using DiscardPolicy drops task if saturated.
1152 >     * executor using DiscardPolicy drops task if saturated.
1153       */
1154      public void testSaturatedExecute3() {
1155          RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1156 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1156 >        ThreadPoolExecutor p =
1157 >            new CustomTPE(1, 1,
1158 >                          LONG_DELAY_MS, MILLISECONDS,
1159 >                          new ArrayBlockingQueue<Runnable>(1),
1160 >                          h);
1161          try {
879
1162              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1163 <            for (int i = 0; i < 5; ++i) {
1163 >            for (int i = 0; i < tasks.length; ++i)
1164                  tasks[i] = new TrackedNoOpRunnable();
883            }
1165              p.execute(new TrackedLongRunnable());
1166 <            for (int i = 0; i < 5; ++i) {
1167 <                p.execute(tasks[i]);
1168 <            }
1169 <            for (int i = 0; i < 5; ++i) {
889 <                assertFalse(tasks[i].done);
890 <            }
1166 >            for (TrackedNoOpRunnable task : tasks)
1167 >                p.execute(task);
1168 >            for (TrackedNoOpRunnable task : tasks)
1169 >                assertFalse(task.done);
1170              try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1171          } finally {
1172              joinPool(p);
# Line 895 | Line 1174 | public class ThreadPoolExecutorSubclassT
1174      }
1175  
1176      /**
1177 <     *  executor using DiscardOldestPolicy drops oldest task if saturated.
1177 >     * executor using DiscardOldestPolicy drops oldest task if saturated.
1178       */
1179      public void testSaturatedExecute4() {
1180          RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
# Line 916 | Line 1195 | public class ThreadPoolExecutorSubclassT
1195      }
1196  
1197      /**
1198 <     *  execute throws RejectedExecutionException if shutdown
1198 >     * execute throws RejectedExecutionException if shutdown
1199       */
1200      public void testRejectedExecutionExceptionOnShutdown() {
1201 <        ThreadPoolExecutor tpe =
1201 >        ThreadPoolExecutor p =
1202              new CustomTPE(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
1203 <        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1203 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1204          try {
1205 <            tpe.execute(new NoOpRunnable());
1205 >            p.execute(new NoOpRunnable());
1206              shouldThrow();
1207          } catch (RejectedExecutionException success) {}
1208  
1209 <        joinPool(tpe);
1209 >        joinPool(p);
1210      }
1211  
1212      /**
1213 <     *  execute using CallerRunsPolicy drops task on shutdown
1213 >     * execute using CallerRunsPolicy drops task on shutdown
1214       */
1215      public void testCallerRunsOnShutdown() {
1216          RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
# Line 948 | Line 1227 | public class ThreadPoolExecutorSubclassT
1227      }
1228  
1229      /**
1230 <     *  execute using DiscardPolicy drops task on shutdown
1230 >     * execute using DiscardPolicy drops task on shutdown
1231       */
1232      public void testDiscardOnShutdown() {
1233          RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
# Line 964 | Line 1243 | public class ThreadPoolExecutorSubclassT
1243          }
1244      }
1245  
967
1246      /**
1247 <     *  execute using DiscardOldestPolicy drops task on shutdown
1247 >     * execute using DiscardOldestPolicy drops task on shutdown
1248       */
1249      public void testDiscardOldestOnShutdown() {
1250          RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
# Line 982 | Line 1260 | public class ThreadPoolExecutorSubclassT
1260          }
1261      }
1262  
985
1263      /**
1264 <     *  execute (null) throws NPE
1264 >     * execute(null) throws NPE
1265       */
1266      public void testExecuteNull() {
1267 <        ThreadPoolExecutor tpe = null;
1267 >        ThreadPoolExecutor p =
1268 >            new CustomTPE(1, 2, 1L, SECONDS,
1269 >                          new ArrayBlockingQueue<Runnable>(10));
1270          try {
1271 <            tpe = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
993 <            tpe.execute(null);
1271 >            p.execute(null);
1272              shouldThrow();
1273          } catch (NullPointerException success) {}
1274  
1275 <        joinPool(tpe);
1275 >        joinPool(p);
1276      }
1277  
1278      /**
1279 <     *  setCorePoolSize of negative value throws IllegalArgumentException
1279 >     * setCorePoolSize of negative value throws IllegalArgumentException
1280       */
1281      public void testCorePoolSizeIllegalArgumentException() {
1282 <        ThreadPoolExecutor tpe =
1282 >        ThreadPoolExecutor p =
1283              new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1284          try {
1285 <            tpe.setCorePoolSize(-1);
1285 >            p.setCorePoolSize(-1);
1286              shouldThrow();
1287          } catch (IllegalArgumentException success) {
1288          } finally {
1289 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1289 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1290          }
1291 <        joinPool(tpe);
1291 >        joinPool(p);
1292      }
1293  
1294      /**
1295 <     *  setMaximumPoolSize(int) throws IllegalArgumentException if
1296 <     *  given a value less the core pool size
1295 >     * setMaximumPoolSize(int) throws IllegalArgumentException
1296 >     * if given a value less the core pool size
1297       */
1298      public void testMaximumPoolSizeIllegalArgumentException() {
1299 <        ThreadPoolExecutor tpe =
1299 >        ThreadPoolExecutor p =
1300              new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1301          try {
1302 <            tpe.setMaximumPoolSize(1);
1302 >            p.setMaximumPoolSize(1);
1303              shouldThrow();
1304          } catch (IllegalArgumentException success) {
1305          } finally {
1306 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1306 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1307          }
1308 <        joinPool(tpe);
1308 >        joinPool(p);
1309      }
1310  
1311      /**
1312 <     *  setMaximumPoolSize throws IllegalArgumentException
1313 <     *  if given a negative value
1312 >     * setMaximumPoolSize throws IllegalArgumentException
1313 >     * if given a negative value
1314       */
1315      public void testMaximumPoolSizeIllegalArgumentException2() {
1316 <        ThreadPoolExecutor tpe =
1316 >        ThreadPoolExecutor p =
1317              new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1318          try {
1319 <            tpe.setMaximumPoolSize(-1);
1319 >            p.setMaximumPoolSize(-1);
1320              shouldThrow();
1321          } catch (IllegalArgumentException success) {
1322          } finally {
1323 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1323 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1324          }
1325 <        joinPool(tpe);
1325 >        joinPool(p);
1326      }
1327  
1050
1328      /**
1329 <     *  setKeepAliveTime  throws IllegalArgumentException
1330 <     *  when given a negative value
1329 >     * setKeepAliveTime throws IllegalArgumentException
1330 >     * when given a negative value
1331       */
1332      public void testKeepAliveTimeIllegalArgumentException() {
1333 <        ThreadPoolExecutor tpe =
1333 >        ThreadPoolExecutor p =
1334              new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1335  
1336          try {
1337 <            tpe.setKeepAliveTime(-1,MILLISECONDS);
1337 >            p.setKeepAliveTime(-1,MILLISECONDS);
1338              shouldThrow();
1339          } catch (IllegalArgumentException success) {
1340          } finally {
1341 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1341 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1342          }
1343 <        joinPool(tpe);
1343 >        joinPool(p);
1344      }
1345  
1346      /**
1347       * terminated() is called on termination
1348       */
1349      public void testTerminated() {
1350 <        CustomTPE tpe = new CustomTPE();
1351 <        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1352 <        assertTrue(tpe.terminatedCalled);
1353 <        joinPool(tpe);
1350 >        CustomTPE p = new CustomTPE();
1351 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1352 >        assertTrue(p.terminatedCalled());
1353 >        joinPool(p);
1354      }
1355  
1356      /**
1357       * beforeExecute and afterExecute are called when executing task
1358       */
1359      public void testBeforeAfter() throws InterruptedException {
1360 <        CustomTPE tpe = new CustomTPE();
1360 >        CustomTPE p = new CustomTPE();
1361          try {
1362 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1363 <            tpe.execute(r);
1364 <            Thread.sleep(SHORT_DELAY_MS);
1365 <            assertTrue(r.done);
1366 <            assertTrue(tpe.beforeCalled);
1367 <            assertTrue(tpe.afterCalled);
1368 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1362 >            final CountDownLatch done = new CountDownLatch(1);
1363 >            p.execute(new CheckedRunnable() {
1364 >                public void realRun() {
1365 >                    done.countDown();
1366 >                }});
1367 >            await(p.afterCalled);
1368 >            assertEquals(0, done.getCount());
1369 >            assertTrue(p.afterCalled());
1370 >            assertTrue(p.beforeCalled());
1371 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1372          } finally {
1373 <            joinPool(tpe);
1373 >            joinPool(p);
1374          }
1375      }
1376  
# Line 1136 | Line 1416 | public class ThreadPoolExecutorSubclassT
1416          }
1417      }
1418  
1139
1419      /**
1420       * invokeAny(null) throws NPE
1421       */
# Line 1169 | Line 1448 | public class ThreadPoolExecutorSubclassT
1448       * invokeAny(c) throws NPE if c has null elements
1449       */
1450      public void testInvokeAny3() throws Exception {
1451 <        final CountDownLatch latch = new CountDownLatch(1);
1451 >        CountDownLatch latch = new CountDownLatch(1);
1452          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1453 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1454 +        l.add(latchAwaitingStringTask(latch));
1455 +        l.add(null);
1456          try {
1175            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1176            l.add(new Callable<String>() {
1177                      public String call() {
1178                          try {
1179                              latch.await();
1180                          } catch (InterruptedException ok) {}
1181                          return TEST_STRING;
1182                      }});
1183            l.add(null);
1457              e.invokeAny(l);
1458              shouldThrow();
1459          } catch (NullPointerException success) {
# Line 1195 | Line 1468 | public class ThreadPoolExecutorSubclassT
1468       */
1469      public void testInvokeAny4() throws Exception {
1470          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1471 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1472 +        l.add(new NPETask());
1473          try {
1199            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1200            l.add(new NPETask());
1474              e.invokeAny(l);
1475              shouldThrow();
1476          } catch (ExecutionException success) {
# Line 1213 | Line 1486 | public class ThreadPoolExecutorSubclassT
1486      public void testInvokeAny5() throws Exception {
1487          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1488          try {
1489 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1489 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1490              l.add(new StringTask());
1491              l.add(new StringTask());
1492              String result = e.invokeAny(l);
# Line 1255 | Line 1528 | public class ThreadPoolExecutorSubclassT
1528       */
1529      public void testInvokeAll3() throws Exception {
1530          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1531 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1532 +        l.add(new StringTask());
1533 +        l.add(null);
1534          try {
1259            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1260            l.add(new StringTask());
1261            l.add(null);
1535              e.invokeAll(l);
1536              shouldThrow();
1537          } catch (NullPointerException success) {
# Line 1272 | Line 1545 | public class ThreadPoolExecutorSubclassT
1545       */
1546      public void testInvokeAll4() throws Exception {
1547          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1548 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1549 +        l.add(new NPETask());
1550 +        List<Future<String>> futures = e.invokeAll(l);
1551 +        assertEquals(1, futures.size());
1552          try {
1553 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1277 <            l.add(new NPETask());
1278 <            List<Future<String>> result = e.invokeAll(l);
1279 <            assertEquals(1, result.size());
1280 <            for (Future<String> future : result)
1281 <                future.get();
1553 >            futures.get(0).get();
1554              shouldThrow();
1555          } catch (ExecutionException success) {
1556 +            assertTrue(success.getCause() instanceof NullPointerException);
1557          } finally {
1558              joinPool(e);
1559          }
# Line 1292 | Line 1565 | public class ThreadPoolExecutorSubclassT
1565      public void testInvokeAll5() throws Exception {
1566          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1567          try {
1568 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1568 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1569              l.add(new StringTask());
1570              l.add(new StringTask());
1571 <            List<Future<String>> result = e.invokeAll(l);
1572 <            assertEquals(2, result.size());
1573 <            for (Future<String> future : result)
1571 >            List<Future<String>> futures = e.invokeAll(l);
1572 >            assertEquals(2, futures.size());
1573 >            for (Future<String> future : futures)
1574                  assertSame(TEST_STRING, future.get());
1575          } finally {
1576              joinPool(e);
1577          }
1578      }
1579  
1307
1308
1580      /**
1581       * timed invokeAny(null) throws NPE
1582       */
# Line 1325 | Line 1596 | public class ThreadPoolExecutorSubclassT
1596       */
1597      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1598          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1599 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1600 +        l.add(new StringTask());
1601          try {
1329            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1330            l.add(new StringTask());
1602              e.invokeAny(l, MEDIUM_DELAY_MS, null);
1603              shouldThrow();
1604          } catch (NullPointerException success) {
# Line 1354 | Line 1625 | public class ThreadPoolExecutorSubclassT
1625       * timed invokeAny(c) throws NPE if c has null elements
1626       */
1627      public void testTimedInvokeAny3() throws Exception {
1628 <        final CountDownLatch latch = new CountDownLatch(1);
1628 >        CountDownLatch latch = new CountDownLatch(1);
1629          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1630 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1631 +        l.add(latchAwaitingStringTask(latch));
1632 +        l.add(null);
1633          try {
1360            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1361            l.add(new Callable<String>() {
1362                      public String call() {
1363                          try {
1364                              latch.await();
1365                          } catch (InterruptedException ok) {}
1366                          return TEST_STRING;
1367                      }});
1368            l.add(null);
1634              e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1635              shouldThrow();
1636          } catch (NullPointerException success) {
# Line 1380 | Line 1645 | public class ThreadPoolExecutorSubclassT
1645       */
1646      public void testTimedInvokeAny4() throws Exception {
1647          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1648 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1649 +        l.add(new NPETask());
1650          try {
1384            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1385            l.add(new NPETask());
1651              e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1652              shouldThrow();
1653          } catch (ExecutionException success) {
# Line 1398 | Line 1663 | public class ThreadPoolExecutorSubclassT
1663      public void testTimedInvokeAny5() throws Exception {
1664          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1665          try {
1666 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1666 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1667              l.add(new StringTask());
1668              l.add(new StringTask());
1669              String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
# Line 1427 | Line 1692 | public class ThreadPoolExecutorSubclassT
1692       */
1693      public void testTimedInvokeAllNullTimeUnit() throws Exception {
1694          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1695 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1696 +        l.add(new StringTask());
1697          try {
1431            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1432            l.add(new StringTask());
1698              e.invokeAll(l, MEDIUM_DELAY_MS, null);
1699              shouldThrow();
1700          } catch (NullPointerException success) {
# Line 1456 | Line 1721 | public class ThreadPoolExecutorSubclassT
1721       */
1722      public void testTimedInvokeAll3() throws Exception {
1723          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1724 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1725 +        l.add(new StringTask());
1726 +        l.add(null);
1727          try {
1460            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1461            l.add(new StringTask());
1462            l.add(null);
1728              e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1729              shouldThrow();
1730          } catch (NullPointerException success) {
# Line 1473 | Line 1738 | public class ThreadPoolExecutorSubclassT
1738       */
1739      public void testTimedInvokeAll4() throws Exception {
1740          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1741 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1742 +        l.add(new NPETask());
1743 +        List<Future<String>> futures =
1744 +            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1745 +        assertEquals(1, futures.size());
1746          try {
1747 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1478 <            l.add(new NPETask());
1479 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1480 <            assertEquals(1, result.size());
1481 <            for (Future<String> future : result)
1482 <                future.get();
1747 >            futures.get(0).get();
1748              shouldThrow();
1749          } catch (ExecutionException success) {
1750              assertTrue(success.getCause() instanceof NullPointerException);
# Line 1494 | Line 1759 | public class ThreadPoolExecutorSubclassT
1759      public void testTimedInvokeAll5() throws Exception {
1760          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1761          try {
1762 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1762 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1763              l.add(new StringTask());
1764              l.add(new StringTask());
1765 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1766 <            assertEquals(2, result.size());
1767 <            for (Future<String> future : result)
1765 >            List<Future<String>> futures =
1766 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1767 >            assertEquals(2, futures.size());
1768 >            for (Future<String> future : futures)
1769                  assertSame(TEST_STRING, future.get());
1770          } finally {
1771              joinPool(e);
# Line 1512 | Line 1778 | public class ThreadPoolExecutorSubclassT
1778      public void testTimedInvokeAll6() throws Exception {
1779          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1780          try {
1781 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1782 <            l.add(new StringTask());
1783 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1784 <            l.add(new StringTask());
1785 <            List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1786 <            assertEquals(3, result.size());
1787 <            Iterator<Future<String>> it = result.iterator();
1788 <            Future<String> f1 = it.next();
1789 <            Future<String> f2 = it.next();
1790 <            Future<String> f3 = it.next();
1791 <            assertTrue(f1.isDone());
1792 <            assertTrue(f2.isDone());
1793 <            assertTrue(f3.isDone());
1794 <            assertFalse(f1.isCancelled());
1795 <            assertTrue(f2.isCancelled());
1781 >            for (long timeout = timeoutMillis();;) {
1782 >                List<Callable<String>> tasks = new ArrayList<>();
1783 >                tasks.add(new StringTask("0"));
1784 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1785 >                tasks.add(new StringTask("2"));
1786 >                long startTime = System.nanoTime();
1787 >                List<Future<String>> futures =
1788 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1789 >                assertEquals(tasks.size(), futures.size());
1790 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1791 >                for (Future future : futures)
1792 >                    assertTrue(future.isDone());
1793 >                assertTrue(futures.get(1).isCancelled());
1794 >                try {
1795 >                    assertEquals("0", futures.get(0).get());
1796 >                    assertEquals("2", futures.get(2).get());
1797 >                    break;
1798 >                } catch (CancellationException retryWithLongerTimeout) {
1799 >                    timeout *= 2;
1800 >                    if (timeout >= LONG_DELAY_MS / 2)
1801 >                        fail("expected exactly one task to be cancelled");
1802 >                }
1803 >            }
1804          } finally {
1805              joinPool(e);
1806          }
# Line 1537 | Line 1811 | public class ThreadPoolExecutorSubclassT
1811       * thread factory fails to create more
1812       */
1813      public void testFailingThreadFactory() throws InterruptedException {
1814 <        ExecutorService e = new CustomTPE(100, 100, LONG_DELAY_MS, MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory());
1815 <        try {
1816 <            for (int k = 0; k < 100; ++k) {
1817 <                e.execute(new NoOpRunnable());
1818 <            }
1819 <            Thread.sleep(LONG_DELAY_MS);
1814 >        final ExecutorService e =
1815 >            new CustomTPE(100, 100,
1816 >                          LONG_DELAY_MS, MILLISECONDS,
1817 >                          new LinkedBlockingQueue<Runnable>(),
1818 >                          new FailingThreadFactory());
1819 >        try {
1820 >            final int TASKS = 100;
1821 >            final CountDownLatch done = new CountDownLatch(TASKS);
1822 >            for (int k = 0; k < TASKS; ++k)
1823 >                e.execute(new CheckedRunnable() {
1824 >                    public void realRun() {
1825 >                        done.countDown();
1826 >                    }});
1827 >            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1828          } finally {
1829              joinPool(e);
1830          }
# Line 1552 | Line 1834 | public class ThreadPoolExecutorSubclassT
1834       * allowsCoreThreadTimeOut is by default false.
1835       */
1836      public void testAllowsCoreThreadTimeOut() {
1837 <        ThreadPoolExecutor tpe = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1838 <        assertFalse(tpe.allowsCoreThreadTimeOut());
1839 <        joinPool(tpe);
1837 >        ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1838 >        assertFalse(p.allowsCoreThreadTimeOut());
1839 >        joinPool(p);
1840      }
1841  
1842      /**
1843       * allowCoreThreadTimeOut(true) causes idle threads to time out
1844       */
1845 <    public void testAllowCoreThreadTimeOut_true() throws InterruptedException {
1846 <        ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1847 <        tpe.allowCoreThreadTimeOut(true);
1848 <        tpe.execute(new NoOpRunnable());
1849 <        try {
1850 <            Thread.sleep(MEDIUM_DELAY_MS);
1851 <            assertEquals(0, tpe.getPoolSize());
1845 >    public void testAllowCoreThreadTimeOut_true() throws Exception {
1846 >        long keepAliveTime = timeoutMillis();
1847 >        final ThreadPoolExecutor p =
1848 >            new CustomTPE(2, 10,
1849 >                          keepAliveTime, MILLISECONDS,
1850 >                          new ArrayBlockingQueue<Runnable>(10));
1851 >        final CountDownLatch threadStarted = new CountDownLatch(1);
1852 >        try {
1853 >            p.allowCoreThreadTimeOut(true);
1854 >            p.execute(new CheckedRunnable() {
1855 >                public void realRun() {
1856 >                    threadStarted.countDown();
1857 >                    assertEquals(1, p.getPoolSize());
1858 >                }});
1859 >            await(threadStarted);
1860 >            delay(keepAliveTime);
1861 >            long startTime = System.nanoTime();
1862 >            while (p.getPoolSize() > 0
1863 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1864 >                Thread.yield();
1865 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1866 >            assertEquals(0, p.getPoolSize());
1867          } finally {
1868 <            joinPool(tpe);
1868 >            joinPool(p);
1869          }
1870      }
1871  
1872      /**
1873       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1874       */
1875 <    public void testAllowCoreThreadTimeOut_false() throws InterruptedException {
1876 <        ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1877 <        tpe.allowCoreThreadTimeOut(false);
1878 <        tpe.execute(new NoOpRunnable());
1879 <        try {
1880 <            Thread.sleep(MEDIUM_DELAY_MS);
1881 <            assertTrue(tpe.getPoolSize() >= 1);
1875 >    public void testAllowCoreThreadTimeOut_false() throws Exception {
1876 >        long keepAliveTime = timeoutMillis();
1877 >        final ThreadPoolExecutor p =
1878 >            new CustomTPE(2, 10,
1879 >                          keepAliveTime, MILLISECONDS,
1880 >                          new ArrayBlockingQueue<Runnable>(10));
1881 >        final CountDownLatch threadStarted = new CountDownLatch(1);
1882 >        try {
1883 >            p.allowCoreThreadTimeOut(false);
1884 >            p.execute(new CheckedRunnable() {
1885 >                public void realRun() throws InterruptedException {
1886 >                    threadStarted.countDown();
1887 >                    assertTrue(p.getPoolSize() >= 1);
1888 >                }});
1889 >            delay(2 * keepAliveTime);
1890 >            assertTrue(p.getPoolSize() >= 1);
1891          } finally {
1892 <            joinPool(tpe);
1892 >            joinPool(p);
1893 >        }
1894 >    }
1895 >
1896 >    /**
1897 >     * get(cancelled task) throws CancellationException
1898 >     * (in part, a test of CustomTPE itself)
1899 >     */
1900 >    public void testGet_cancelled() throws Exception {
1901 >        final ExecutorService e =
1902 >            new CustomTPE(1, 1,
1903 >                          LONG_DELAY_MS, MILLISECONDS,
1904 >                          new LinkedBlockingQueue<Runnable>());
1905 >        try {
1906 >            final CountDownLatch blockerStarted = new CountDownLatch(1);
1907 >            final CountDownLatch done = new CountDownLatch(1);
1908 >            final List<Future<?>> futures = new ArrayList<>();
1909 >            for (int i = 0; i < 2; i++) {
1910 >                Runnable r = new CheckedRunnable() { public void realRun()
1911 >                                                         throws Throwable {
1912 >                    blockerStarted.countDown();
1913 >                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
1914 >                }};
1915 >                futures.add(e.submit(r));
1916 >            }
1917 >            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
1918 >            for (Future<?> future : futures) future.cancel(false);
1919 >            for (Future<?> future : futures) {
1920 >                try {
1921 >                    future.get();
1922 >                    shouldThrow();
1923 >                } catch (CancellationException success) {}
1924 >                try {
1925 >                    future.get(LONG_DELAY_MS, MILLISECONDS);
1926 >                    shouldThrow();
1927 >                } catch (CancellationException success) {}
1928 >                assertTrue(future.isCancelled());
1929 >                assertTrue(future.isDone());
1930 >            }
1931 >            done.countDown();
1932 >        } finally {
1933 >            joinPool(e);
1934          }
1935      }
1936  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines