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.4 by jsr166, Mon Nov 16 05:30:08 2009 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.*;
10 < import java.util.concurrent.locks.*;
9 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
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(ThreadPoolExecutorTest.class);
45 >        return new TestSuite(ThreadPoolExecutorSubclassTest.class);
46      }
47  
48      static class CustomTask<V> implements RunnableFuture<V> {
# Line 29 | Line 54 | public class ThreadPoolExecutorSubclassT
54          V result;
55          Thread thread;
56          Exception exception;
57 <        CustomTask(Callable<V> c) { callable = c; }
58 <        CustomTask(final Runnable r, final V res) { callable = new Callable<V>() {
59 <            public V call() throws Exception { r.run(); return res; }};
57 >        CustomTask(Callable<V> c) {
58 >            if (c == null) throw new NullPointerException();
59 >            callable = c;
60 >        }
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; }};
65          }
66          public boolean isDone() {
67              lock.lock(); try { return done; } finally { lock.unlock() ; }
# Line 54 | Line 84 | public class ThreadPoolExecutorSubclassT
84              finally { lock.unlock() ; }
85          }
86          public void run() {
57            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() ; }
65            if (!runme) return;
94              V v = null;
95              Exception e = null;
96              try {
# Line 73 | 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 86 | 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 97 | Line 129 | public class ThreadPoolExecutorSubclassT
129              long nanos = unit.toNanos(timeout);
130              lock.lock();
131              try {
132 <                for (;;) {
133 <                    if (done) break;
102 <                    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 111 | Line 144 | public class ThreadPoolExecutorSubclassT
144          }
145      }
146  
114
147      static class CustomTPE extends ThreadPoolExecutor {
148          protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
149              return new CustomTask<V>(c);
# Line 158 | 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, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>());
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 184 | Line 226 | public class ThreadPoolExecutorSubclassT
226          }
227      }
228  
187
229      /**
230 <     *  execute successfully executes a runnable
230 >     * execute successfully executes a runnable
231       */
232 <    public void testExecute() {
233 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
234 <        try {
235 <            p1.execute(new Runnable() {
236 <                    public void run() {
237 <                        try {
238 <                            Thread.sleep(SHORT_DELAY_MS);
239 <                        } catch (InterruptedException e) {
240 <                            threadUnexpectedException();
241 <                        }
242 <                    }
202 <                });
203 <            Thread.sleep(SMALL_DELAY_MS);
204 <        } catch (InterruptedException e) {
205 <            unexpectedException();
232 >    public void testExecute() throws InterruptedException {
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          }
207        joinPool(p1);
244      }
245  
246      /**
247 <     *  getActiveCount increases but doesn't overestimate, when a
248 <     *  thread becomes active
249 <     */
250 <    public void testGetActiveCount() {
251 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
252 <        assertEquals(0, p2.getActiveCount());
253 <        p2.execute(new MediumRunnable());
254 <        try {
255 <            Thread.sleep(SHORT_DELAY_MS);
256 <        } catch (Exception e) {
257 <            unexpectedException();
247 >     * getActiveCount increases but doesn't overestimate, when a
248 >     * thread becomes active
249 >     */
250 >    public void testGetActiveCount() throws InterruptedException {
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          }
223        assertEquals(1, p2.getActiveCount());
224        joinPool(p2);
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, TimeUnit.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, TimeUnit.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
322 <     */
323 <    public void testGetCompletedTaskCount() {
324 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
325 <        assertEquals(0, p2.getCompletedTaskCount());
326 <        p2.execute(new ShortRunnable());
327 <        try {
328 <            Thread.sleep(SMALL_DELAY_MS);
329 <        } catch (Exception e) {
330 <            unexpectedException();
320 >     * getCompletedTaskCount increases, but doesn't overestimate,
321 >     * when tasks complete
322 >     */
323 >    public void testGetCompletedTaskCount() throws InterruptedException {
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          }
268        assertEquals(1, p2.getCompletedTaskCount());
269        try { p2.shutdown(); } catch (SecurityException ok) { return; }
270        joinPool(p2);
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, TimeUnit.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, TimeUnit.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  
291
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, TimeUnit.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, TimeUnit.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  
313
410      /**
411       * setThreadFactory(null) throws NPE
412       */
413      public void testSetThreadFactoryNull() {
414 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
414 >        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
415          try {
416              p.setThreadFactory(null);
417              shouldThrow();
# Line 330 | Line 426 | public class ThreadPoolExecutorSubclassT
426       */
427      public void testGetRejectedExecutionHandler() {
428          RejectedExecutionHandler h = new NoOpREHandler();
429 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h);
429 >        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h);
430          assertSame(h, p.getRejectedExecutionHandler());
431          joinPool(p);
432      }
# Line 340 | Line 436 | public class ThreadPoolExecutorSubclassT
436       * getRejectedExecutionHandler
437       */
438      public void testSetRejectedExecutionHandler() {
439 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
439 >        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
440          RejectedExecutionHandler h = new NoOpREHandler();
441          p.setRejectedExecutionHandler(h);
442          assertSame(h, p.getRejectedExecutionHandler());
443          joinPool(p);
444      }
445  
350
446      /**
447       * setRejectedExecutionHandler(null) throws NPE
448       */
449      public void testSetRejectedExecutionHandlerNull() {
450 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
450 >        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
451          try {
452              p.setRejectedExecutionHandler(null);
453              shouldThrow();
# Line 362 | Line 457 | public class ThreadPoolExecutorSubclassT
457          }
458      }
459  
365
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() {
465 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
466 <        try {
467 <            assertEquals(0, p2.getLargestPoolSize());
468 <            p2.execute(new MediumRunnable());
469 <            p2.execute(new MediumRunnable());
470 <            Thread.sleep(SHORT_DELAY_MS);
471 <            assertEquals(2, p2.getLargestPoolSize());
472 <        } catch (Exception e) {
473 <            unexpectedException();
464 >    public void testGetLargestPoolSize() throws InterruptedException {
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          }
381        joinPool(p2);
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, TimeUnit.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, TimeUnit.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() {
531 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
532 <        try {
533 <            assertEquals(0, p1.getTaskCount());
534 <            p1.execute(new MediumRunnable());
535 <            Thread.sleep(SHORT_DELAY_MS);
536 <            assertEquals(1, p1.getTaskCount());
537 <        } catch (Exception e) {
538 <            unexpectedException();
530 >    public void testGetTaskCount() throws InterruptedException {
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          }
419        joinPool(p1);
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, TimeUnit.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  
434
565      /**
566 <     *  isTerminated is false before termination, true after
566 >     * isTerminated is false before termination, true after
567       */
568 <    public void testIsTerminated() {
569 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.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 <        try {
577 <            assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
578 <            assertTrue(p1.isTerminated());
579 <        } catch (Exception e) {
580 <            unexpectedException();
581 <        }
568 >    public void testIsTerminated() throws InterruptedException {
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
596 <     */
597 <    public void testIsTerminating() {
598 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.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 <        try {
607 <            assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
608 <            assertTrue(p1.isTerminated());
609 <            assertFalse(p1.isTerminating());
610 <        } catch (Exception e) {
611 <            unexpectedException();
612 <        }
595 >     * isTerminating is not true when running or when terminated
596 >     */
597 >    public void testIsTerminating() throws InterruptedException {
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() {
627 <        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
628 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.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();
643 <        } catch (Exception e) {
644 <            unexpectedException();
626 >    public void testGetQueue() throws InterruptedException {
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  
658      /**
659       * remove(task) removes queued task, and fails to remove active task
660       */
661 <    public void testRemove() {
661 >    public void testRemove() throws InterruptedException {
662          BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
663 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.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]));
524        } catch (Exception e) {
525            unexpectedException();
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, TimeUnit.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          }
541        tasks[4].cancel(true);
542        tasks[3].cancel(true);
543        p1.purge();
544        long count = p1.getTaskCount();
545        assertTrue(count >= 2 && count < 5);
546        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, TimeUnit.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 <        }
756 <        assertTrue(p1.isShutdown());
757 <        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  
571
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, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
783 >            new CustomTPE(-1, 1, 1L, SECONDS,
784 >                          new ArrayBlockingQueue<Runnable>(10));
785              shouldThrow();
786 <        }
580 <        catch (IllegalArgumentException success) {}
786 >        } catch (IllegalArgumentException success) {}
787      }
788  
789      /**
# Line 585 | Line 791 | public class ThreadPoolExecutorSubclassT
791       */
792      public void testConstructor2() {
793          try {
794 <            new CustomTPE(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
794 >            new CustomTPE(1, -1, 1L, SECONDS,
795 >                          new ArrayBlockingQueue<Runnable>(10));
796              shouldThrow();
797 <        }
591 <        catch (IllegalArgumentException success) {}
797 >        } catch (IllegalArgumentException success) {}
798      }
799  
800      /**
# Line 596 | Line 802 | public class ThreadPoolExecutorSubclassT
802       */
803      public void testConstructor3() {
804          try {
805 <            new CustomTPE(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
805 >            new CustomTPE(1, 0, 1L, SECONDS,
806 >                          new ArrayBlockingQueue<Runnable>(10));
807              shouldThrow();
808 <        }
602 <        catch (IllegalArgumentException success) {}
808 >        } catch (IllegalArgumentException success) {}
809      }
810  
811      /**
# Line 607 | Line 813 | public class ThreadPoolExecutorSubclassT
813       */
814      public void testConstructor4() {
815          try {
816 <            new CustomTPE(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
816 >            new CustomTPE(1, 2, -1L, SECONDS,
817 >                          new ArrayBlockingQueue<Runnable>(10));
818              shouldThrow();
819 <        }
613 <        catch (IllegalArgumentException success) {}
819 >        } catch (IllegalArgumentException success) {}
820      }
821  
822      /**
# Line 618 | Line 824 | public class ThreadPoolExecutorSubclassT
824       */
825      public void testConstructor5() {
826          try {
827 <            new CustomTPE(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
827 >            new CustomTPE(2, 1, 1L, SECONDS,
828 >                          new ArrayBlockingQueue<Runnable>(10));
829              shouldThrow();
830 <        }
624 <        catch (IllegalArgumentException success) {}
830 >        } catch (IllegalArgumentException success) {}
831      }
832  
833      /**
# Line 629 | Line 835 | public class ThreadPoolExecutorSubclassT
835       */
836      public void testConstructorNullPointerException() {
837          try {
838 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null);
838 >            new CustomTPE(1, 2, 1L, SECONDS, null);
839              shouldThrow();
840 <        }
635 <        catch (NullPointerException success) {}
840 >        } catch (NullPointerException success) {}
841      }
842  
638
639
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, TimeUnit.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 652 | Line 857 | public class ThreadPoolExecutorSubclassT
857       */
858      public void testConstructor7() {
859          try {
860 <            new CustomTPE(1,-1,LONG_DELAY_MS, TimeUnit.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 <        }
658 <        catch (IllegalArgumentException success) {}
864 >        } catch (IllegalArgumentException success) {}
865      }
866  
867      /**
# Line 663 | Line 869 | public class ThreadPoolExecutorSubclassT
869       */
870      public void testConstructor8() {
871          try {
872 <            new CustomTPE(1,0,LONG_DELAY_MS, TimeUnit.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 <        }
669 <        catch (IllegalArgumentException success) {}
876 >        } catch (IllegalArgumentException success) {}
877      }
878  
879      /**
# Line 674 | Line 881 | public class ThreadPoolExecutorSubclassT
881       */
882      public void testConstructor9() {
883          try {
884 <            new CustomTPE(1,2,-1L,TimeUnit.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 <        }
680 <        catch (IllegalArgumentException success) {}
888 >        } catch (IllegalArgumentException success) {}
889      }
890  
891      /**
# Line 685 | Line 893 | public class ThreadPoolExecutorSubclassT
893       */
894      public void testConstructor10() {
895          try {
896 <            new CustomTPE(2,1,LONG_DELAY_MS, TimeUnit.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 <        }
691 <        catch (IllegalArgumentException success) {}
900 >        } catch (IllegalArgumentException success) {}
901      }
902  
903      /**
# Line 696 | Line 905 | public class ThreadPoolExecutorSubclassT
905       */
906      public void testConstructorNullPointerException2() {
907          try {
908 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory());
908 >            new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory());
909              shouldThrow();
910 <        }
702 <        catch (NullPointerException success) {}
910 >        } catch (NullPointerException success) {}
911      }
912  
913      /**
# Line 707 | Line 915 | public class ThreadPoolExecutorSubclassT
915       */
916      public void testConstructorNullPointerException3() {
917          try {
918 <            ThreadFactory f = null;
919 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
918 >            new CustomTPE(1, 2, 1L, SECONDS,
919 >                          new ArrayBlockingQueue<Runnable>(10),
920 >                          (ThreadFactory) null);
921              shouldThrow();
922 <        }
714 <        catch (NullPointerException success) {}
922 >        } catch (NullPointerException success) {}
923      }
924  
717
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, TimeUnit.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 <        }
726 <        catch (IllegalArgumentException success) {}
934 >        } catch (IllegalArgumentException success) {}
935      }
936  
937      /**
# Line 731 | Line 939 | public class ThreadPoolExecutorSubclassT
939       */
940      public void testConstructor12() {
941          try {
942 <            new CustomTPE(1,-1,LONG_DELAY_MS, TimeUnit.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 <        }
737 <        catch (IllegalArgumentException success) {}
946 >        } catch (IllegalArgumentException success) {}
947      }
948  
949      /**
# Line 742 | Line 951 | public class ThreadPoolExecutorSubclassT
951       */
952      public void testConstructor13() {
953          try {
954 <            new CustomTPE(1,0,LONG_DELAY_MS, TimeUnit.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 <        }
748 <        catch (IllegalArgumentException success) {}
958 >        } catch (IllegalArgumentException success) {}
959      }
960  
961      /**
# Line 753 | Line 963 | public class ThreadPoolExecutorSubclassT
963       */
964      public void testConstructor14() {
965          try {
966 <            new CustomTPE(1,2,-1L,TimeUnit.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 <        }
759 <        catch (IllegalArgumentException success) {}
970 >        } catch (IllegalArgumentException success) {}
971      }
972  
973      /**
# Line 764 | Line 975 | public class ThreadPoolExecutorSubclassT
975       */
976      public void testConstructor15() {
977          try {
978 <            new CustomTPE(2,1,LONG_DELAY_MS, TimeUnit.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 <        }
770 <        catch (IllegalArgumentException success) {}
982 >        } catch (IllegalArgumentException success) {}
983      }
984  
985      /**
# Line 775 | Line 987 | public class ThreadPoolExecutorSubclassT
987       */
988      public void testConstructorNullPointerException4() {
989          try {
990 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new NoOpREHandler());
990 >            new CustomTPE(1, 2, 1L, SECONDS,
991 >                          null,
992 >                          new NoOpREHandler());
993              shouldThrow();
994 <        }
781 <        catch (NullPointerException success) {}
994 >        } catch (NullPointerException success) {}
995      }
996  
997      /**
# Line 786 | Line 999 | public class ThreadPoolExecutorSubclassT
999       */
1000      public void testConstructorNullPointerException5() {
1001          try {
1002 <            RejectedExecutionHandler r = null;
1003 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
1002 >            new CustomTPE(1, 2, 1L, SECONDS,
1003 >                          new ArrayBlockingQueue<Runnable>(10),
1004 >                          (RejectedExecutionHandler) null);
1005              shouldThrow();
1006 <        }
793 <        catch (NullPointerException success) {}
1006 >        } catch (NullPointerException success) {}
1007      }
1008  
796
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, TimeUnit.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 <        }
805 <        catch (IllegalArgumentException success) {}
1019 >        } catch (IllegalArgumentException success) {}
1020      }
1021  
1022      /**
# Line 810 | Line 1024 | public class ThreadPoolExecutorSubclassT
1024       */
1025      public void testConstructor17() {
1026          try {
1027 <            new CustomTPE(1,-1,LONG_DELAY_MS, TimeUnit.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 <        }
816 <        catch (IllegalArgumentException success) {}
1032 >        } catch (IllegalArgumentException success) {}
1033      }
1034  
1035      /**
# Line 821 | Line 1037 | public class ThreadPoolExecutorSubclassT
1037       */
1038      public void testConstructor18() {
1039          try {
1040 <            new CustomTPE(1,0,LONG_DELAY_MS, TimeUnit.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 <        }
827 <        catch (IllegalArgumentException success) {}
1045 >        } catch (IllegalArgumentException success) {}
1046      }
1047  
1048      /**
# Line 832 | Line 1050 | public class ThreadPoolExecutorSubclassT
1050       */
1051      public void testConstructor19() {
1052          try {
1053 <            new CustomTPE(1,2,-1L,TimeUnit.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 <        }
838 <        catch (IllegalArgumentException success) {}
1058 >        } catch (IllegalArgumentException success) {}
1059      }
1060  
1061      /**
# Line 843 | Line 1063 | public class ThreadPoolExecutorSubclassT
1063       */
1064      public void testConstructor20() {
1065          try {
1066 <            new CustomTPE(2,1,LONG_DELAY_MS, TimeUnit.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 <        }
849 <        catch (IllegalArgumentException success) {}
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, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
1079 >            new CustomTPE(1, 2, 1L, SECONDS,
1080 >                          null,
1081 >                          new SimpleThreadFactory(),
1082 >                          new NoOpREHandler());
1083              shouldThrow();
1084 <        }
860 <        catch (NullPointerException success) {}
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, TimeUnit.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 <        }
872 <        catch (NullPointerException success) {}
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, TimeUnit.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 <        }
884 <        catch (NullPointerException successdn8) {}
1110 >        } catch (NullPointerException success) {}
1111      }
1112  
887
1113      /**
1114 <     *  execute throws RejectedExecutionException
890 <     *  if saturated.
1114 >     * execute throws RejectedExecutionException if saturated.
1115       */
1116      public void testSaturatedExecute() {
1117 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.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, TimeUnit.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 {
911
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();
915            }
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 <            }
921 <            for (int i = 1; i < 5; ++i) {
1159 >            for (int i = 1; i < tasks.length; ++i)
1160                  assertTrue(tasks[i].done);
923            }
1161              try { p.shutdownNow(); } catch (SecurityException ok) { return; }
925        } catch (RejectedExecutionException ex) {
926            unexpectedException();
1162          } finally {
1163              joinPool(p);
1164          }
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, TimeUnit.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 {
939
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();
943            }
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) {
949 <                assertFalse(tasks[i].done);
950 <            }
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; }
952        } catch (RejectedExecutionException ex) {
953            unexpectedException();
1187          } finally {
1188              joinPool(p);
1189          }
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();
1197 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1197 >        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1198          try {
1199              p.execute(new TrackedLongRunnable());
1200              TrackedLongRunnable r2 = new TrackedLongRunnable();
# Line 972 | Line 1205 | public class ThreadPoolExecutorSubclassT
1205              assertFalse(p.getQueue().contains(r2));
1206              assertTrue(p.getQueue().contains(r3));
1207              try { p.shutdownNow(); } catch (SecurityException ok) { return; }
975        } catch (RejectedExecutionException ex) {
976            unexpectedException();
1208          } finally {
1209              joinPool(p);
1210          }
1211      }
1212  
1213      /**
1214 <     *  execute throws RejectedExecutionException if shutdown
1214 >     * execute throws RejectedExecutionException if shutdown
1215       */
1216      public void testRejectedExecutionExceptionOnShutdown() {
1217 <        ThreadPoolExecutor tpe =
1218 <            new CustomTPE(1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
1219 <        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1220 <        try {
1221 <            tpe.execute(new NoOpRunnable());
1222 <            shouldThrow();
1223 <        } catch (RejectedExecutionException success) {}
1217 >        ThreadPoolExecutor p =
1218 >            new CustomTPE(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
1219 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1220 >        try {
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();
1233 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1233 >        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1234  
1235          try { p.shutdown(); } catch (SecurityException ok) { return; }
1236 <        try {
1236 >        try {
1237              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1238 <            p.execute(r);
1238 >            p.execute(r);
1239              assertFalse(r.done);
1009        } catch (RejectedExecutionException success) {
1010            unexpectedException();
1240          } finally {
1241              joinPool(p);
1242          }
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();
1250 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1250 >        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1251  
1252          try { p.shutdown(); } catch (SecurityException ok) { return; }
1253 <        try {
1253 >        try {
1254              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1255 <            p.execute(r);
1255 >            p.execute(r);
1256              assertFalse(r.done);
1028        } catch (RejectedExecutionException success) {
1029            unexpectedException();
1257          } finally {
1258              joinPool(p);
1259          }
1260      }
1261  
1035
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();
1267 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1267 >        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1268  
1269          try { p.shutdown(); } catch (SecurityException ok) { return; }
1270 <        try {
1270 >        try {
1271              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1272 <            p.execute(r);
1272 >            p.execute(r);
1273              assertFalse(r.done);
1048        } catch (RejectedExecutionException success) {
1049            unexpectedException();
1274          } finally {
1275              joinPool(p);
1276          }
1277      }
1278  
1055
1279      /**
1280 <     *  execute (null) throws NPE
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, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1063 <            tpe.execute(null);
1287 >            p.execute(null);
1288              shouldThrow();
1289 <        } catch (NullPointerException success) {}
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 = null;
1299 <        try {
1300 <            tpe = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1301 <        } catch (Exception e) {}
1302 <        try {
1303 <            tpe.setCorePoolSize(-1);
1080 <            shouldThrow();
1081 <        } catch (IllegalArgumentException success) {
1298 >        ThreadPoolExecutor p =
1299 >            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1300 >        try {
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 = null;
1316 <        try {
1095 <            tpe = new CustomTPE(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1096 <        } catch (Exception e) {}
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 = null;
1333 <        try {
1114 <            tpe = new CustomTPE(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1115 <        } catch (Exception e) {}
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  
1126
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 = null;
1350 <        try {
1134 <            tpe = new CustomTPE(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1135 <        } catch (Exception e) {}
1349 >        ThreadPoolExecutor p =
1350 >            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1351  
1352 <        try {
1353 <            tpe.setKeepAliveTime(-1,TimeUnit.MILLISECONDS);
1352 >        try {
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() {
1376 <        CustomTPE tpe = new CustomTPE();
1375 >    public void testBeforeAfter() throws InterruptedException {
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; }
1385 <        }
1386 <        catch (Exception ex) {
1387 <            unexpectedException();
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  
1393      /**
1394       * completed submit of callable returns result
1395       */
1396 <    public void testSubmitCallable() {
1397 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1396 >    public void testSubmitCallable() throws Exception {
1397 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1398          try {
1399              Future<String> future = e.submit(new StringTask());
1400              String result = future.get();
1401              assertSame(TEST_STRING, result);
1187        }
1188        catch (ExecutionException ex) {
1189            unexpectedException();
1190        }
1191        catch (InterruptedException ex) {
1192            unexpectedException();
1402          } finally {
1403              joinPool(e);
1404          }
# Line 1198 | Line 1407 | public class ThreadPoolExecutorSubclassT
1407      /**
1408       * completed submit of runnable returns successfully
1409       */
1410 <    public void testSubmitRunnable() {
1411 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1410 >    public void testSubmitRunnable() throws Exception {
1411 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1412          try {
1413              Future<?> future = e.submit(new NoOpRunnable());
1414              future.get();
1415              assertTrue(future.isDone());
1207        }
1208        catch (ExecutionException ex) {
1209            unexpectedException();
1210        }
1211        catch (InterruptedException ex) {
1212            unexpectedException();
1416          } finally {
1417              joinPool(e);
1418          }
# Line 1218 | Line 1421 | public class ThreadPoolExecutorSubclassT
1421      /**
1422       * completed submit of (runnable, result) returns result
1423       */
1424 <    public void testSubmitRunnable2() {
1425 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1424 >    public void testSubmitRunnable2() throws Exception {
1425 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1426          try {
1427              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1428              String result = future.get();
1429              assertSame(TEST_STRING, result);
1227        }
1228        catch (ExecutionException ex) {
1229            unexpectedException();
1230        }
1231        catch (InterruptedException ex) {
1232            unexpectedException();
1430          } finally {
1431              joinPool(e);
1432          }
1433      }
1434  
1238
1239
1240
1241
1435      /**
1436       * invokeAny(null) throws NPE
1437       */
1438 <    public void testInvokeAny1() {
1439 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1438 >    public void testInvokeAny1() throws Exception {
1439 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1440          try {
1441              e.invokeAny(null);
1442 +            shouldThrow();
1443          } catch (NullPointerException success) {
1250        } catch (Exception ex) {
1251            unexpectedException();
1444          } finally {
1445              joinPool(e);
1446          }
# Line 1257 | Line 1449 | public class ThreadPoolExecutorSubclassT
1449      /**
1450       * invokeAny(empty collection) throws IAE
1451       */
1452 <    public void testInvokeAny2() {
1453 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1452 >    public void testInvokeAny2() throws Exception {
1453 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1454          try {
1455              e.invokeAny(new ArrayList<Callable<String>>());
1456 +            shouldThrow();
1457          } catch (IllegalArgumentException success) {
1265        } catch (Exception ex) {
1266            unexpectedException();
1458          } finally {
1459              joinPool(e);
1460          }
# Line 1272 | Line 1463 | public class ThreadPoolExecutorSubclassT
1463      /**
1464       * invokeAny(c) throws NPE if c has null elements
1465       */
1466 <    public void testInvokeAny3() {
1467 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1466 >    public void testInvokeAny3() throws Exception {
1467 >        CountDownLatch latch = new CountDownLatch(1);
1468 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1469 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1470 >        l.add(latchAwaitingStringTask(latch));
1471 >        l.add(null);
1472          try {
1278            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1279            l.add(new StringTask());
1280            l.add(null);
1473              e.invokeAny(l);
1474 +            shouldThrow();
1475          } catch (NullPointerException success) {
1283        } catch (Exception ex) {
1284            unexpectedException();
1476          } finally {
1477 +            latch.countDown();
1478              joinPool(e);
1479          }
1480      }
# Line 1290 | Line 1482 | public class ThreadPoolExecutorSubclassT
1482      /**
1483       * invokeAny(c) throws ExecutionException if no task completes
1484       */
1485 <    public void testInvokeAny4() {
1486 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1485 >    public void testInvokeAny4() throws Exception {
1486 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1487 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1488 >        l.add(new NPETask());
1489          try {
1296            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1297            l.add(new NPETask());
1490              e.invokeAny(l);
1491 +            shouldThrow();
1492          } catch (ExecutionException success) {
1493 <        } catch (Exception ex) {
1301 <            unexpectedException();
1493 >            assertTrue(success.getCause() instanceof NullPointerException);
1494          } finally {
1495              joinPool(e);
1496          }
# Line 1307 | Line 1499 | public class ThreadPoolExecutorSubclassT
1499      /**
1500       * invokeAny(c) returns result of some task
1501       */
1502 <    public void testInvokeAny5() {
1503 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1502 >    public void testInvokeAny5() throws Exception {
1503 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1504          try {
1505 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1505 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1506              l.add(new StringTask());
1507              l.add(new StringTask());
1508              String result = e.invokeAny(l);
1509              assertSame(TEST_STRING, result);
1318        } catch (ExecutionException success) {
1319        } catch (Exception ex) {
1320            unexpectedException();
1510          } finally {
1511              joinPool(e);
1512          }
# Line 1326 | Line 1515 | public class ThreadPoolExecutorSubclassT
1515      /**
1516       * invokeAll(null) throws NPE
1517       */
1518 <    public void testInvokeAll1() {
1519 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1518 >    public void testInvokeAll1() throws Exception {
1519 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1520          try {
1521              e.invokeAll(null);
1522 +            shouldThrow();
1523          } catch (NullPointerException success) {
1334        } catch (Exception ex) {
1335            unexpectedException();
1524          } finally {
1525              joinPool(e);
1526          }
# Line 1341 | Line 1529 | public class ThreadPoolExecutorSubclassT
1529      /**
1530       * invokeAll(empty collection) returns empty collection
1531       */
1532 <    public void testInvokeAll2() {
1533 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1532 >    public void testInvokeAll2() throws Exception {
1533 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1534          try {
1535              List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1536              assertTrue(r.isEmpty());
1349        } catch (Exception ex) {
1350            unexpectedException();
1537          } finally {
1538              joinPool(e);
1539          }
# Line 1356 | Line 1542 | public class ThreadPoolExecutorSubclassT
1542      /**
1543       * invokeAll(c) throws NPE if c has null elements
1544       */
1545 <    public void testInvokeAll3() {
1546 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1545 >    public void testInvokeAll3() throws Exception {
1546 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1547 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1548 >        l.add(new StringTask());
1549 >        l.add(null);
1550          try {
1362            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1363            l.add(new StringTask());
1364            l.add(null);
1551              e.invokeAll(l);
1552 +            shouldThrow();
1553          } catch (NullPointerException success) {
1367        } catch (Exception ex) {
1368            unexpectedException();
1554          } finally {
1555              joinPool(e);
1556          }
# Line 1374 | Line 1559 | public class ThreadPoolExecutorSubclassT
1559      /**
1560       * get of element of invokeAll(c) throws exception on failed task
1561       */
1562 <    public void testInvokeAll4() {
1563 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1562 >    public void testInvokeAll4() throws Exception {
1563 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1564 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1565 >        l.add(new NPETask());
1566 >        List<Future<String>> futures = e.invokeAll(l);
1567 >        assertEquals(1, futures.size());
1568          try {
1569 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1570 <            l.add(new NPETask());
1382 <            List<Future<String>> result = e.invokeAll(l);
1383 <            assertEquals(1, result.size());
1384 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1385 <                it.next().get();
1569 >            futures.get(0).get();
1570 >            shouldThrow();
1571          } catch (ExecutionException success) {
1572 <        } catch (Exception ex) {
1388 <            unexpectedException();
1572 >            assertTrue(success.getCause() instanceof NullPointerException);
1573          } finally {
1574              joinPool(e);
1575          }
# Line 1394 | Line 1578 | public class ThreadPoolExecutorSubclassT
1578      /**
1579       * invokeAll(c) returns results of all completed tasks
1580       */
1581 <    public void testInvokeAll5() {
1582 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1581 >    public void testInvokeAll5() throws Exception {
1582 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1583          try {
1584 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1584 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1585              l.add(new StringTask());
1586              l.add(new StringTask());
1587 <            List<Future<String>> result = e.invokeAll(l);
1588 <            assertEquals(2, result.size());
1589 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1590 <                assertSame(TEST_STRING, it.next().get());
1407 <        } catch (ExecutionException success) {
1408 <        } catch (Exception ex) {
1409 <            unexpectedException();
1587 >            List<Future<String>> futures = e.invokeAll(l);
1588 >            assertEquals(2, futures.size());
1589 >            for (Future<String> future : futures)
1590 >                assertSame(TEST_STRING, future.get());
1591          } finally {
1592              joinPool(e);
1593          }
1594      }
1595  
1415
1416
1596      /**
1597       * timed invokeAny(null) throws NPE
1598       */
1599 <    public void testTimedInvokeAny1() {
1600 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1599 >    public void testTimedInvokeAny1() throws Exception {
1600 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1601          try {
1602 <            e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1602 >            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1603 >            shouldThrow();
1604          } catch (NullPointerException success) {
1425        } catch (Exception ex) {
1426            unexpectedException();
1605          } finally {
1606              joinPool(e);
1607          }
# Line 1432 | Line 1610 | public class ThreadPoolExecutorSubclassT
1610      /**
1611       * timed invokeAny(,,null) throws NPE
1612       */
1613 <    public void testTimedInvokeAnyNullTimeUnit() {
1614 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1613 >    public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1614 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1615 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1616 >        l.add(new StringTask());
1617          try {
1438            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1439            l.add(new StringTask());
1618              e.invokeAny(l, MEDIUM_DELAY_MS, null);
1619 +            shouldThrow();
1620          } catch (NullPointerException success) {
1442        } catch (Exception ex) {
1443            unexpectedException();
1621          } finally {
1622              joinPool(e);
1623          }
# Line 1449 | Line 1626 | public class ThreadPoolExecutorSubclassT
1626      /**
1627       * timed invokeAny(empty collection) throws IAE
1628       */
1629 <    public void testTimedInvokeAny2() {
1630 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1629 >    public void testTimedInvokeAny2() throws Exception {
1630 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1631          try {
1632 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1632 >            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1633 >            shouldThrow();
1634          } catch (IllegalArgumentException success) {
1457        } catch (Exception ex) {
1458            unexpectedException();
1635          } finally {
1636              joinPool(e);
1637          }
# Line 1464 | Line 1640 | public class ThreadPoolExecutorSubclassT
1640      /**
1641       * timed invokeAny(c) throws NPE if c has null elements
1642       */
1643 <    public void testTimedInvokeAny3() {
1644 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1643 >    public void testTimedInvokeAny3() throws Exception {
1644 >        CountDownLatch latch = new CountDownLatch(1);
1645 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1646 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1647 >        l.add(latchAwaitingStringTask(latch));
1648 >        l.add(null);
1649          try {
1650 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1651 <            l.add(new StringTask());
1472 <            l.add(null);
1473 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1650 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1651 >            shouldThrow();
1652          } catch (NullPointerException success) {
1475        } catch (Exception ex) {
1476            ex.printStackTrace();
1477            unexpectedException();
1653          } finally {
1654 +            latch.countDown();
1655              joinPool(e);
1656          }
1657      }
# Line 1483 | Line 1659 | public class ThreadPoolExecutorSubclassT
1659      /**
1660       * timed invokeAny(c) throws ExecutionException if no task completes
1661       */
1662 <    public void testTimedInvokeAny4() {
1663 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1662 >    public void testTimedInvokeAny4() throws Exception {
1663 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1664 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1665 >        l.add(new NPETask());
1666          try {
1667 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1668 <            l.add(new NPETask());
1491 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1667 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1668 >            shouldThrow();
1669          } catch (ExecutionException success) {
1670 <        } catch (Exception ex) {
1494 <            unexpectedException();
1670 >            assertTrue(success.getCause() instanceof NullPointerException);
1671          } finally {
1672              joinPool(e);
1673          }
# Line 1500 | Line 1676 | public class ThreadPoolExecutorSubclassT
1676      /**
1677       * timed invokeAny(c) returns result of some task
1678       */
1679 <    public void testTimedInvokeAny5() {
1680 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1679 >    public void testTimedInvokeAny5() throws Exception {
1680 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1681          try {
1682 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1682 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1683              l.add(new StringTask());
1684              l.add(new StringTask());
1685 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1685 >            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1686              assertSame(TEST_STRING, result);
1511        } catch (ExecutionException success) {
1512        } catch (Exception ex) {
1513            unexpectedException();
1687          } finally {
1688              joinPool(e);
1689          }
# Line 1519 | Line 1692 | public class ThreadPoolExecutorSubclassT
1692      /**
1693       * timed invokeAll(null) throws NPE
1694       */
1695 <    public void testTimedInvokeAll1() {
1696 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1695 >    public void testTimedInvokeAll1() throws Exception {
1696 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1697          try {
1698 <            e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1698 >            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1699 >            shouldThrow();
1700          } catch (NullPointerException success) {
1527        } catch (Exception ex) {
1528            unexpectedException();
1701          } finally {
1702              joinPool(e);
1703          }
# Line 1534 | Line 1706 | public class ThreadPoolExecutorSubclassT
1706      /**
1707       * timed invokeAll(,,null) throws NPE
1708       */
1709 <    public void testTimedInvokeAllNullTimeUnit() {
1710 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1709 >    public void testTimedInvokeAllNullTimeUnit() throws Exception {
1710 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1711 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1712 >        l.add(new StringTask());
1713          try {
1540            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1541            l.add(new StringTask());
1714              e.invokeAll(l, MEDIUM_DELAY_MS, null);
1715 +            shouldThrow();
1716          } catch (NullPointerException success) {
1544        } catch (Exception ex) {
1545            unexpectedException();
1717          } finally {
1718              joinPool(e);
1719          }
# Line 1551 | Line 1722 | public class ThreadPoolExecutorSubclassT
1722      /**
1723       * timed invokeAll(empty collection) returns empty collection
1724       */
1725 <    public void testTimedInvokeAll2() {
1726 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1725 >    public void testTimedInvokeAll2() throws Exception {
1726 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1727          try {
1728 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1728 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1729              assertTrue(r.isEmpty());
1559        } catch (Exception ex) {
1560            unexpectedException();
1730          } finally {
1731              joinPool(e);
1732          }
# Line 1566 | Line 1735 | public class ThreadPoolExecutorSubclassT
1735      /**
1736       * timed invokeAll(c) throws NPE if c has null elements
1737       */
1738 <    public void testTimedInvokeAll3() {
1739 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1738 >    public void testTimedInvokeAll3() throws Exception {
1739 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1740 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1741 >        l.add(new StringTask());
1742 >        l.add(null);
1743          try {
1744 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1745 <            l.add(new StringTask());
1574 <            l.add(null);
1575 <            e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1744 >            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1745 >            shouldThrow();
1746          } catch (NullPointerException success) {
1577        } catch (Exception ex) {
1578            unexpectedException();
1747          } finally {
1748              joinPool(e);
1749          }
# Line 1584 | Line 1752 | public class ThreadPoolExecutorSubclassT
1752      /**
1753       * get of element of invokeAll(c) throws exception on failed task
1754       */
1755 <    public void testTimedInvokeAll4() {
1756 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1755 >    public void testTimedInvokeAll4() throws Exception {
1756 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1757 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1758 >        l.add(new NPETask());
1759 >        List<Future<String>> futures =
1760 >            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1761 >        assertEquals(1, futures.size());
1762          try {
1763 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1764 <            l.add(new NPETask());
1592 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1593 <            assertEquals(1, result.size());
1594 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1595 <                it.next().get();
1763 >            futures.get(0).get();
1764 >            shouldThrow();
1765          } catch (ExecutionException success) {
1766 <        } catch (Exception ex) {
1598 <            unexpectedException();
1766 >            assertTrue(success.getCause() instanceof NullPointerException);
1767          } finally {
1768              joinPool(e);
1769          }
# Line 1604 | Line 1772 | public class ThreadPoolExecutorSubclassT
1772      /**
1773       * timed invokeAll(c) returns results of all completed tasks
1774       */
1775 <    public void testTimedInvokeAll5() {
1776 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1775 >    public void testTimedInvokeAll5() throws Exception {
1776 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1777          try {
1778 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1778 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1779              l.add(new StringTask());
1780              l.add(new StringTask());
1781 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1782 <            assertEquals(2, result.size());
1783 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1784 <                assertSame(TEST_STRING, it.next().get());
1785 <        } catch (ExecutionException success) {
1618 <        } catch (Exception ex) {
1619 <            unexpectedException();
1781 >            List<Future<String>> futures =
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());
1786          } finally {
1787              joinPool(e);
1788          }
# Line 1625 | Line 1791 | public class ThreadPoolExecutorSubclassT
1791      /**
1792       * timed invokeAll(c) cancels tasks not completed by timeout
1793       */
1794 <    public void testTimedInvokeAll6() {
1795 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1794 >    public void testTimedInvokeAll6() throws Exception {
1795 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1796          try {
1797 <            ArrayList<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>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
1802 <            assertEquals(3, result.size());
1803 <            Iterator<Future<String>> it = result.iterator();
1804 <            Future<String> f1 = it.next();
1805 <            Future<String> f2 = it.next();
1806 <            Future<String> f3 = it.next();
1807 <            assertTrue(f1.isDone());
1808 <            assertTrue(f2.isDone());
1809 <            assertTrue(f3.isDone());
1810 <            assertFalse(f1.isCancelled());
1811 <            assertTrue(f2.isCancelled());
1812 <        } catch (Exception ex) {
1813 <            unexpectedException();
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 1654 | Line 1826 | public class ThreadPoolExecutorSubclassT
1826       * Execution continues if there is at least one thread even if
1827       * thread factory fails to create more
1828       */
1829 <    public void testFailingThreadFactory() {
1830 <        ExecutorService e = new CustomTPE(100, 100, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory());
1831 <        try {
1832 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1833 <            for (int k = 0; k < 100; ++k) {
1834 <                e.execute(new NoOpRunnable());
1835 <            }
1836 <            Thread.sleep(LONG_DELAY_MS);
1837 <        } catch (Exception ex) {
1838 <            unexpectedException();
1829 >    public void testFailingThreadFactory() throws InterruptedException {
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 1673 | Line 1850 | public class ThreadPoolExecutorSubclassT
1850       * allowsCoreThreadTimeOut is by default false.
1851       */
1852      public void testAllowsCoreThreadTimeOut() {
1853 <        ThreadPoolExecutor tpe = new CustomTPE(2, 2, 1000, TimeUnit.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() {
1862 <        ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, TimeUnit.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());
1868 <        } catch (InterruptedException e) {
1869 <            unexpectedException();
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() {
1892 <        ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, TimeUnit.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);
1898 <        } catch (InterruptedException e) {
1899 <            unexpectedException();
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