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.53 by jsr166, Sun Oct 4 01:23:41 2015 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
8  
9 < import java.util.concurrent.*;
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 >        final CountDownLatch threadStarted = new CountDownLatch(1);
329 >        final CountDownLatch threadProceed = new CountDownLatch(1);
330 >        final CountDownLatch threadDone = new CountDownLatch(1);
331 >        try {
332 >            assertEquals(0, p.getCompletedTaskCount());
333 >            p.execute(new CheckedRunnable() {
334 >                public void realRun() throws InterruptedException {
335 >                    threadStarted.countDown();
336 >                    assertEquals(0, p.getCompletedTaskCount());
337 >                    threadProceed.await();
338 >                    threadDone.countDown();
339 >                }});
340 >            await(threadStarted);
341 >            assertEquals(0, p.getCompletedTaskCount());
342 >            threadProceed.countDown();
343 >            threadDone.await();
344 >            long startTime = System.nanoTime();
345 >            while (p.getCompletedTaskCount() != 1) {
346 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
347 >                    fail("timed out");
348 >                Thread.yield();
349 >            }
350 >        } finally {
351 >            joinPool(p);
352          }
268        assertEquals(1, p2.getCompletedTaskCount());
269        try { p2.shutdown(); } catch (SecurityException ok) { return; }
270        joinPool(p2);
353      }
354  
355      /**
356 <     *   getCorePoolSize returns size given in constructor if not otherwise set
356 >     * getCorePoolSize returns size given in constructor if not otherwise set
357       */
358      public void testGetCorePoolSize() {
359 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
360 <        assertEquals(1, p1.getCorePoolSize());
361 <        joinPool(p1);
359 >        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
360 >        assertEquals(1, p.getCorePoolSize());
361 >        joinPool(p);
362      }
363  
364      /**
365 <     *   getKeepAliveTime returns value given in constructor if not otherwise set
365 >     * getKeepAliveTime returns value given in constructor if not otherwise set
366       */
367      public void testGetKeepAliveTime() {
368 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
369 <        assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS));
370 <        joinPool(p2);
368 >        ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
369 >        assertEquals(1, p.getKeepAliveTime(SECONDS));
370 >        joinPool(p);
371      }
372  
291
373      /**
374       * getThreadFactory returns factory in constructor if not set
375       */
376      public void testGetThreadFactory() {
377          ThreadFactory tf = new SimpleThreadFactory();
378 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler());
378 >        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler());
379          assertSame(tf, p.getThreadFactory());
380          joinPool(p);
381      }
# Line 303 | Line 384 | public class ThreadPoolExecutorSubclassT
384       * setThreadFactory sets the thread factory returned by getThreadFactory
385       */
386      public void testSetThreadFactory() {
387 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
387 >        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
388          ThreadFactory tf = new SimpleThreadFactory();
389          p.setThreadFactory(tf);
390          assertSame(tf, p.getThreadFactory());
391          joinPool(p);
392      }
393  
313
394      /**
395       * setThreadFactory(null) throws NPE
396       */
397      public void testSetThreadFactoryNull() {
398 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
398 >        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
399          try {
400              p.setThreadFactory(null);
401              shouldThrow();
# Line 330 | Line 410 | public class ThreadPoolExecutorSubclassT
410       */
411      public void testGetRejectedExecutionHandler() {
412          RejectedExecutionHandler h = new NoOpREHandler();
413 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h);
413 >        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h);
414          assertSame(h, p.getRejectedExecutionHandler());
415          joinPool(p);
416      }
# Line 340 | Line 420 | public class ThreadPoolExecutorSubclassT
420       * getRejectedExecutionHandler
421       */
422      public void testSetRejectedExecutionHandler() {
423 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
423 >        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
424          RejectedExecutionHandler h = new NoOpREHandler();
425          p.setRejectedExecutionHandler(h);
426          assertSame(h, p.getRejectedExecutionHandler());
427          joinPool(p);
428      }
429  
350
430      /**
431       * setRejectedExecutionHandler(null) throws NPE
432       */
433      public void testSetRejectedExecutionHandlerNull() {
434 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
434 >        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
435          try {
436              p.setRejectedExecutionHandler(null);
437              shouldThrow();
# Line 362 | Line 441 | public class ThreadPoolExecutorSubclassT
441          }
442      }
443  
365
444      /**
445 <     *   getLargestPoolSize increases, but doesn't overestimate, when
446 <     *   multiple threads active
445 >     * getLargestPoolSize increases, but doesn't overestimate, when
446 >     * multiple threads active
447       */
448 <    public void testGetLargestPoolSize() {
449 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
450 <        try {
451 <            assertEquals(0, p2.getLargestPoolSize());
452 <            p2.execute(new MediumRunnable());
453 <            p2.execute(new MediumRunnable());
454 <            Thread.sleep(SHORT_DELAY_MS);
455 <            assertEquals(2, p2.getLargestPoolSize());
456 <        } catch (Exception e) {
457 <            unexpectedException();
448 >    public void testGetLargestPoolSize() throws InterruptedException {
449 >        final int THREADS = 3;
450 >        final ThreadPoolExecutor p =
451 >            new CustomTPE(THREADS, THREADS,
452 >                          LONG_DELAY_MS, MILLISECONDS,
453 >                          new ArrayBlockingQueue<Runnable>(10));
454 >        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
455 >        final CountDownLatch done = new CountDownLatch(1);
456 >        try {
457 >            assertEquals(0, p.getLargestPoolSize());
458 >            for (int i = 0; i < THREADS; i++)
459 >                p.execute(new CheckedRunnable() {
460 >                    public void realRun() throws InterruptedException {
461 >                        threadsStarted.countDown();
462 >                        done.await();
463 >                        assertEquals(THREADS, p.getLargestPoolSize());
464 >                    }});
465 >            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
466 >            assertEquals(THREADS, p.getLargestPoolSize());
467 >        } finally {
468 >            done.countDown();
469 >            joinPool(p);
470 >            assertEquals(THREADS, p.getLargestPoolSize());
471          }
381        joinPool(p2);
472      }
473  
474      /**
475 <     *   getMaximumPoolSize returns value given in constructor if not
476 <     *   otherwise set
475 >     * getMaximumPoolSize returns value given in constructor if not
476 >     * otherwise set
477       */
478      public void testGetMaximumPoolSize() {
479 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
480 <        assertEquals(2, p2.getMaximumPoolSize());
481 <        joinPool(p2);
479 >        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
480 >        assertEquals(2, p.getMaximumPoolSize());
481 >        joinPool(p);
482      }
483  
484      /**
485 <     *   getPoolSize increases, but doesn't overestimate, when threads
486 <     *   become active
485 >     * getPoolSize increases, but doesn't overestimate, when threads
486 >     * become active
487       */
488 <    public void testGetPoolSize() {
489 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
490 <        assertEquals(0, p1.getPoolSize());
491 <        p1.execute(new MediumRunnable());
492 <        assertEquals(1, p1.getPoolSize());
493 <        joinPool(p1);
488 >    public void testGetPoolSize() throws InterruptedException {
489 >        final ThreadPoolExecutor p =
490 >            new CustomTPE(1, 1,
491 >                          LONG_DELAY_MS, MILLISECONDS,
492 >                          new ArrayBlockingQueue<Runnable>(10));
493 >        final CountDownLatch threadStarted = new CountDownLatch(1);
494 >        final CountDownLatch done = new CountDownLatch(1);
495 >        try {
496 >            assertEquals(0, p.getPoolSize());
497 >            p.execute(new CheckedRunnable() {
498 >                public void realRun() throws InterruptedException {
499 >                    threadStarted.countDown();
500 >                    assertEquals(1, p.getPoolSize());
501 >                    done.await();
502 >                }});
503 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
504 >            assertEquals(1, p.getPoolSize());
505 >        } finally {
506 >            done.countDown();
507 >            joinPool(p);
508 >        }
509      }
510  
511      /**
512 <     *  getTaskCount increases, but doesn't overestimate, when tasks submitted
512 >     * getTaskCount increases, but doesn't overestimate, when tasks submitted
513       */
514 <    public void testGetTaskCount() {
515 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
516 <        try {
517 <            assertEquals(0, p1.getTaskCount());
518 <            p1.execute(new MediumRunnable());
519 <            Thread.sleep(SHORT_DELAY_MS);
520 <            assertEquals(1, p1.getTaskCount());
521 <        } catch (Exception e) {
522 <            unexpectedException();
514 >    public void testGetTaskCount() throws InterruptedException {
515 >        final ThreadPoolExecutor p =
516 >            new CustomTPE(1, 1,
517 >                          LONG_DELAY_MS, MILLISECONDS,
518 >                          new ArrayBlockingQueue<Runnable>(10));
519 >        final CountDownLatch threadStarted = new CountDownLatch(1);
520 >        final CountDownLatch done = new CountDownLatch(1);
521 >        try {
522 >            assertEquals(0, p.getTaskCount());
523 >            p.execute(new CheckedRunnable() {
524 >                public void realRun() throws InterruptedException {
525 >                    threadStarted.countDown();
526 >                    assertEquals(1, p.getTaskCount());
527 >                    done.await();
528 >                }});
529 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
530 >            assertEquals(1, p.getTaskCount());
531 >        } finally {
532 >            done.countDown();
533 >            joinPool(p);
534          }
419        joinPool(p1);
535      }
536  
537      /**
538 <     *   isShutDown is false before shutdown, true after
538 >     * isShutdown is false before shutdown, true after
539       */
540      public void testIsShutdown() {
541  
542 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
543 <        assertFalse(p1.isShutdown());
544 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
545 <        assertTrue(p1.isShutdown());
546 <        joinPool(p1);
542 >        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
543 >        assertFalse(p.isShutdown());
544 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
545 >        assertTrue(p.isShutdown());
546 >        joinPool(p);
547      }
548  
434
549      /**
550 <     *  isTerminated is false before termination, true after
550 >     * isTerminated is false before termination, true after
551       */
552 <    public void testIsTerminated() {
553 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
554 <        assertFalse(p1.isTerminated());
555 <        try {
556 <            p1.execute(new MediumRunnable());
557 <        } finally {
558 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
559 <        }
560 <        try {
561 <            assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
562 <            assertTrue(p1.isTerminated());
563 <        } catch (Exception e) {
564 <            unexpectedException();
565 <        }
552 >    public void testIsTerminated() throws InterruptedException {
553 >        final ThreadPoolExecutor p =
554 >            new CustomTPE(1, 1,
555 >                          LONG_DELAY_MS, MILLISECONDS,
556 >                          new ArrayBlockingQueue<Runnable>(10));
557 >        final CountDownLatch threadStarted = new CountDownLatch(1);
558 >        final CountDownLatch done = new CountDownLatch(1);
559 >        try {
560 >            assertFalse(p.isTerminating());
561 >            p.execute(new CheckedRunnable() {
562 >                public void realRun() throws InterruptedException {
563 >                    assertFalse(p.isTerminating());
564 >                    threadStarted.countDown();
565 >                    done.await();
566 >                }});
567 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
568 >            assertFalse(p.isTerminating());
569 >            done.countDown();
570 >        } finally {
571 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
572 >        }
573 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
574 >        assertTrue(p.isTerminated());
575 >        assertFalse(p.isTerminating());
576      }
577  
578      /**
579 <     *  isTerminating is not true when running or when terminated
580 <     */
581 <    public void testIsTerminating() {
582 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
583 <        assertFalse(p1.isTerminating());
584 <        try {
585 <            p1.execute(new SmallRunnable());
586 <            assertFalse(p1.isTerminating());
587 <        } finally {
588 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
589 <        }
590 <        try {
591 <            assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
592 <            assertTrue(p1.isTerminated());
593 <            assertFalse(p1.isTerminating());
594 <        } catch (Exception e) {
595 <            unexpectedException();
596 <        }
579 >     * isTerminating is not true when running or when terminated
580 >     */
581 >    public void testIsTerminating() throws InterruptedException {
582 >        final ThreadPoolExecutor p =
583 >            new CustomTPE(1, 1,
584 >                          LONG_DELAY_MS, MILLISECONDS,
585 >                          new ArrayBlockingQueue<Runnable>(10));
586 >        final CountDownLatch threadStarted = new CountDownLatch(1);
587 >        final CountDownLatch done = new CountDownLatch(1);
588 >        try {
589 >            assertFalse(p.isTerminating());
590 >            p.execute(new CheckedRunnable() {
591 >                public void realRun() throws InterruptedException {
592 >                    assertFalse(p.isTerminating());
593 >                    threadStarted.countDown();
594 >                    done.await();
595 >                }});
596 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
597 >            assertFalse(p.isTerminating());
598 >            done.countDown();
599 >        } finally {
600 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
601 >        }
602 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
603 >        assertTrue(p.isTerminated());
604 >        assertFalse(p.isTerminating());
605      }
606  
607      /**
608       * getQueue returns the work queue, which contains queued tasks
609       */
610 <    public void testGetQueue() {
611 <        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
612 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q);
613 <        FutureTask[] tasks = new FutureTask[5];
614 <        for (int i = 0; i < 5; i++) {
615 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
616 <            p1.execute(tasks[i]);
617 <        }
618 <        try {
619 <            Thread.sleep(SHORT_DELAY_MS);
620 <            BlockingQueue<Runnable> wq = p1.getQueue();
621 <            assertSame(q, wq);
622 <            assertFalse(wq.contains(tasks[0]));
623 <            assertTrue(wq.contains(tasks[4]));
624 <            for (int i = 1; i < 5; ++i)
625 <                tasks[i].cancel(true);
626 <            p1.shutdownNow();
627 <        } catch (Exception e) {
628 <            unexpectedException();
610 >    public void testGetQueue() throws InterruptedException {
611 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
612 >        final ThreadPoolExecutor p =
613 >            new CustomTPE(1, 1,
614 >                          LONG_DELAY_MS, MILLISECONDS,
615 >                          q);
616 >        final CountDownLatch threadStarted = new CountDownLatch(1);
617 >        final CountDownLatch done = new CountDownLatch(1);
618 >        try {
619 >            FutureTask[] tasks = new FutureTask[5];
620 >            for (int i = 0; i < tasks.length; i++) {
621 >                Callable task = new CheckedCallable<Boolean>() {
622 >                    public Boolean realCall() throws InterruptedException {
623 >                        threadStarted.countDown();
624 >                        assertSame(q, p.getQueue());
625 >                        done.await();
626 >                        return Boolean.TRUE;
627 >                    }};
628 >                tasks[i] = new FutureTask(task);
629 >                p.execute(tasks[i]);
630 >            }
631 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
632 >            assertSame(q, p.getQueue());
633 >            assertFalse(q.contains(tasks[0]));
634 >            assertTrue(q.contains(tasks[tasks.length - 1]));
635 >            assertEquals(tasks.length - 1, q.size());
636          } finally {
637 <            joinPool(p1);
637 >            done.countDown();
638 >            joinPool(p);
639          }
640      }
641  
642      /**
643       * remove(task) removes queued task, and fails to remove active task
644       */
645 <    public void testRemove() {
645 >    public void testRemove() throws InterruptedException {
646          BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
647 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q);
648 <        FutureTask[] tasks = new FutureTask[5];
649 <        for (int i = 0; i < 5; i++) {
650 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
651 <            p1.execute(tasks[i]);
652 <        }
653 <        try {
654 <            Thread.sleep(SHORT_DELAY_MS);
655 <            assertFalse(p1.remove(tasks[0]));
647 >        final ThreadPoolExecutor p =
648 >            new CustomTPE(1, 1,
649 >                          LONG_DELAY_MS, MILLISECONDS,
650 >                          q);
651 >        Runnable[] tasks = new Runnable[6];
652 >        final CountDownLatch threadStarted = new CountDownLatch(1);
653 >        final CountDownLatch done = new CountDownLatch(1);
654 >        try {
655 >            for (int i = 0; i < tasks.length; i++) {
656 >                tasks[i] = new CheckedRunnable() {
657 >                        public void realRun() throws InterruptedException {
658 >                            threadStarted.countDown();
659 >                            done.await();
660 >                        }};
661 >                p.execute(tasks[i]);
662 >            }
663 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
664 >            assertFalse(p.remove(tasks[0]));
665              assertTrue(q.contains(tasks[4]));
666              assertTrue(q.contains(tasks[3]));
667 <            assertTrue(p1.remove(tasks[4]));
668 <            assertFalse(p1.remove(tasks[4]));
667 >            assertTrue(p.remove(tasks[4]));
668 >            assertFalse(p.remove(tasks[4]));
669              assertFalse(q.contains(tasks[4]));
670              assertTrue(q.contains(tasks[3]));
671 <            assertTrue(p1.remove(tasks[3]));
671 >            assertTrue(p.remove(tasks[3]));
672              assertFalse(q.contains(tasks[3]));
524        } catch (Exception e) {
525            unexpectedException();
673          } finally {
674 <            joinPool(p1);
674 >            done.countDown();
675 >            joinPool(p);
676          }
677      }
678  
679      /**
680 <     *   purge removes cancelled tasks from the queue
680 >     * purge removes cancelled tasks from the queue
681       */
682 <    public void testPurge() {
683 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
682 >    public void testPurge() throws InterruptedException {
683 >        final CountDownLatch threadStarted = new CountDownLatch(1);
684 >        final CountDownLatch done = new CountDownLatch(1);
685 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
686 >        final ThreadPoolExecutor p =
687 >            new CustomTPE(1, 1,
688 >                          LONG_DELAY_MS, MILLISECONDS,
689 >                          q);
690          FutureTask[] tasks = new FutureTask[5];
691 <        for (int i = 0; i < 5; i++) {
692 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
693 <            p1.execute(tasks[i]);
691 >        try {
692 >            for (int i = 0; i < tasks.length; i++) {
693 >                Callable task = new CheckedCallable<Boolean>() {
694 >                    public Boolean realCall() throws InterruptedException {
695 >                        threadStarted.countDown();
696 >                        done.await();
697 >                        return Boolean.TRUE;
698 >                    }};
699 >                tasks[i] = new FutureTask(task);
700 >                p.execute(tasks[i]);
701 >            }
702 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
703 >            assertEquals(tasks.length, p.getTaskCount());
704 >            assertEquals(tasks.length - 1, q.size());
705 >            assertEquals(1L, p.getActiveCount());
706 >            assertEquals(0L, p.getCompletedTaskCount());
707 >            tasks[4].cancel(true);
708 >            tasks[3].cancel(false);
709 >            p.purge();
710 >            assertEquals(tasks.length - 3, q.size());
711 >            assertEquals(tasks.length - 2, p.getTaskCount());
712 >            p.purge();         // Nothing to do
713 >            assertEquals(tasks.length - 3, q.size());
714 >            assertEquals(tasks.length - 2, p.getTaskCount());
715 >        } finally {
716 >            done.countDown();
717 >            joinPool(p);
718          }
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);
719      }
720  
721      /**
722 <     *  shutDownNow returns a list containing tasks that were not run
722 >     * shutdownNow returns a list containing tasks that were not run,
723 >     * and those tasks are drained from the queue
724       */
725 <    public void testShutDownNow() {
726 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
727 <        List l;
728 <        try {
729 <            for (int i = 0; i < 5; i++)
730 <                p1.execute(new MediumPossiblyInterruptedRunnable());
731 <        }
732 <        finally {
725 >    public void testShutdownNow() throws InterruptedException {
726 >        final int poolSize = 2;
727 >        final int count = 5;
728 >        final AtomicInteger ran = new AtomicInteger(0);
729 >        ThreadPoolExecutor p =
730 >            new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS,
731 >                          new ArrayBlockingQueue<Runnable>(10));
732 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
733 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
734 >            threadsStarted.countDown();
735              try {
736 <                l = p1.shutdownNow();
737 <            } catch (SecurityException ok) { return; }
738 <
739 <        }
740 <        assertTrue(p1.isShutdown());
741 <        assertTrue(l.size() <= 4);
736 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
737 >            } catch (InterruptedException success) {}
738 >            ran.getAndIncrement();
739 >        }};
740 >        for (int i = 0; i < count; i++)
741 >            p.execute(waiter);
742 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
743 >        assertEquals(poolSize, p.getActiveCount());
744 >        assertEquals(0, p.getCompletedTaskCount());
745 >        final List<Runnable> queuedTasks;
746 >        try {
747 >            queuedTasks = p.shutdownNow();
748 >        } catch (SecurityException ok) {
749 >            return; // Allowed in case test doesn't have privs
750 >        }
751 >        assertTrue(p.isShutdown());
752 >        assertTrue(p.getQueue().isEmpty());
753 >        assertEquals(count - poolSize, queuedTasks.size());
754 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
755 >        assertTrue(p.isTerminated());
756 >        assertEquals(poolSize, ran.get());
757 >        assertEquals(poolSize, p.getCompletedTaskCount());
758      }
759  
760      // Exception Tests
761  
571
762      /**
763       * Constructor throws if corePoolSize argument is less than zero
764       */
765      public void testConstructor1() {
766          try {
767 <            new CustomTPE(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
767 >            new CustomTPE(-1, 1, 1L, SECONDS,
768 >                          new ArrayBlockingQueue<Runnable>(10));
769              shouldThrow();
770 <        }
580 <        catch (IllegalArgumentException success) {}
770 >        } catch (IllegalArgumentException success) {}
771      }
772  
773      /**
# Line 585 | Line 775 | public class ThreadPoolExecutorSubclassT
775       */
776      public void testConstructor2() {
777          try {
778 <            new CustomTPE(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
778 >            new CustomTPE(1, -1, 1L, SECONDS,
779 >                          new ArrayBlockingQueue<Runnable>(10));
780              shouldThrow();
781 <        }
591 <        catch (IllegalArgumentException success) {}
781 >        } catch (IllegalArgumentException success) {}
782      }
783  
784      /**
# Line 596 | Line 786 | public class ThreadPoolExecutorSubclassT
786       */
787      public void testConstructor3() {
788          try {
789 <            new CustomTPE(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
789 >            new CustomTPE(1, 0, 1L, SECONDS,
790 >                          new ArrayBlockingQueue<Runnable>(10));
791              shouldThrow();
792 <        }
602 <        catch (IllegalArgumentException success) {}
792 >        } catch (IllegalArgumentException success) {}
793      }
794  
795      /**
# Line 607 | Line 797 | public class ThreadPoolExecutorSubclassT
797       */
798      public void testConstructor4() {
799          try {
800 <            new CustomTPE(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
800 >            new CustomTPE(1, 2, -1L, SECONDS,
801 >                          new ArrayBlockingQueue<Runnable>(10));
802              shouldThrow();
803 <        }
613 <        catch (IllegalArgumentException success) {}
803 >        } catch (IllegalArgumentException success) {}
804      }
805  
806      /**
# Line 618 | Line 808 | public class ThreadPoolExecutorSubclassT
808       */
809      public void testConstructor5() {
810          try {
811 <            new CustomTPE(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
811 >            new CustomTPE(2, 1, 1L, SECONDS,
812 >                          new ArrayBlockingQueue<Runnable>(10));
813              shouldThrow();
814 <        }
624 <        catch (IllegalArgumentException success) {}
814 >        } catch (IllegalArgumentException success) {}
815      }
816  
817      /**
# Line 629 | Line 819 | public class ThreadPoolExecutorSubclassT
819       */
820      public void testConstructorNullPointerException() {
821          try {
822 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null);
822 >            new CustomTPE(1, 2, 1L, SECONDS, null);
823              shouldThrow();
824 <        }
635 <        catch (NullPointerException success) {}
824 >        } catch (NullPointerException success) {}
825      }
826  
638
639
827      /**
828       * Constructor throws if corePoolSize argument is less than zero
829       */
830      public void testConstructor6() {
831          try {
832 <            new CustomTPE(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
832 >            new CustomTPE(-1, 1, 1L, SECONDS,
833 >                          new ArrayBlockingQueue<Runnable>(10),
834 >                          new SimpleThreadFactory());
835              shouldThrow();
836          } catch (IllegalArgumentException success) {}
837      }
# Line 652 | Line 841 | public class ThreadPoolExecutorSubclassT
841       */
842      public void testConstructor7() {
843          try {
844 <            new CustomTPE(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
844 >            new CustomTPE(1,-1, 1L, SECONDS,
845 >                          new ArrayBlockingQueue<Runnable>(10),
846 >                          new SimpleThreadFactory());
847              shouldThrow();
848 <        }
658 <        catch (IllegalArgumentException success) {}
848 >        } catch (IllegalArgumentException success) {}
849      }
850  
851      /**
# Line 663 | Line 853 | public class ThreadPoolExecutorSubclassT
853       */
854      public void testConstructor8() {
855          try {
856 <            new CustomTPE(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
856 >            new CustomTPE(1, 0, 1L, SECONDS,
857 >                          new ArrayBlockingQueue<Runnable>(10),
858 >                          new SimpleThreadFactory());
859              shouldThrow();
860 <        }
669 <        catch (IllegalArgumentException success) {}
860 >        } catch (IllegalArgumentException success) {}
861      }
862  
863      /**
# Line 674 | Line 865 | public class ThreadPoolExecutorSubclassT
865       */
866      public void testConstructor9() {
867          try {
868 <            new CustomTPE(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
868 >            new CustomTPE(1, 2, -1L, SECONDS,
869 >                          new ArrayBlockingQueue<Runnable>(10),
870 >                          new SimpleThreadFactory());
871              shouldThrow();
872 <        }
680 <        catch (IllegalArgumentException success) {}
872 >        } catch (IllegalArgumentException success) {}
873      }
874  
875      /**
# Line 685 | Line 877 | public class ThreadPoolExecutorSubclassT
877       */
878      public void testConstructor10() {
879          try {
880 <            new CustomTPE(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
880 >            new CustomTPE(2, 1, 1L, SECONDS,
881 >                          new ArrayBlockingQueue<Runnable>(10),
882 >                          new SimpleThreadFactory());
883              shouldThrow();
884 <        }
691 <        catch (IllegalArgumentException success) {}
884 >        } catch (IllegalArgumentException success) {}
885      }
886  
887      /**
# Line 696 | Line 889 | public class ThreadPoolExecutorSubclassT
889       */
890      public void testConstructorNullPointerException2() {
891          try {
892 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory());
892 >            new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory());
893              shouldThrow();
894 <        }
702 <        catch (NullPointerException success) {}
894 >        } catch (NullPointerException success) {}
895      }
896  
897      /**
# Line 707 | Line 899 | public class ThreadPoolExecutorSubclassT
899       */
900      public void testConstructorNullPointerException3() {
901          try {
902 <            ThreadFactory f = null;
903 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
902 >            new CustomTPE(1, 2, 1L, SECONDS,
903 >                          new ArrayBlockingQueue<Runnable>(10),
904 >                          (ThreadFactory) null);
905              shouldThrow();
906 <        }
714 <        catch (NullPointerException success) {}
906 >        } catch (NullPointerException success) {}
907      }
908  
717
909      /**
910       * Constructor throws if corePoolSize argument is less than zero
911       */
912      public void testConstructor11() {
913          try {
914 <            new CustomTPE(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
914 >            new CustomTPE(-1, 1, 1L, SECONDS,
915 >                          new ArrayBlockingQueue<Runnable>(10),
916 >                          new NoOpREHandler());
917              shouldThrow();
918 <        }
726 <        catch (IllegalArgumentException success) {}
918 >        } catch (IllegalArgumentException success) {}
919      }
920  
921      /**
# Line 731 | Line 923 | public class ThreadPoolExecutorSubclassT
923       */
924      public void testConstructor12() {
925          try {
926 <            new CustomTPE(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
926 >            new CustomTPE(1, -1, 1L, SECONDS,
927 >                          new ArrayBlockingQueue<Runnable>(10),
928 >                          new NoOpREHandler());
929              shouldThrow();
930 <        }
737 <        catch (IllegalArgumentException success) {}
930 >        } catch (IllegalArgumentException success) {}
931      }
932  
933      /**
# Line 742 | Line 935 | public class ThreadPoolExecutorSubclassT
935       */
936      public void testConstructor13() {
937          try {
938 <            new CustomTPE(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
938 >            new CustomTPE(1, 0, 1L, SECONDS,
939 >                          new ArrayBlockingQueue<Runnable>(10),
940 >                          new NoOpREHandler());
941              shouldThrow();
942 <        }
748 <        catch (IllegalArgumentException success) {}
942 >        } catch (IllegalArgumentException success) {}
943      }
944  
945      /**
# Line 753 | Line 947 | public class ThreadPoolExecutorSubclassT
947       */
948      public void testConstructor14() {
949          try {
950 <            new CustomTPE(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
950 >            new CustomTPE(1, 2, -1L, SECONDS,
951 >                          new ArrayBlockingQueue<Runnable>(10),
952 >                          new NoOpREHandler());
953              shouldThrow();
954 <        }
759 <        catch (IllegalArgumentException success) {}
954 >        } catch (IllegalArgumentException success) {}
955      }
956  
957      /**
# Line 764 | Line 959 | public class ThreadPoolExecutorSubclassT
959       */
960      public void testConstructor15() {
961          try {
962 <            new CustomTPE(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
962 >            new CustomTPE(2, 1, 1L, SECONDS,
963 >                          new ArrayBlockingQueue<Runnable>(10),
964 >                          new NoOpREHandler());
965              shouldThrow();
966 <        }
770 <        catch (IllegalArgumentException success) {}
966 >        } catch (IllegalArgumentException success) {}
967      }
968  
969      /**
# Line 775 | Line 971 | public class ThreadPoolExecutorSubclassT
971       */
972      public void testConstructorNullPointerException4() {
973          try {
974 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new NoOpREHandler());
974 >            new CustomTPE(1, 2, 1L, SECONDS,
975 >                          null,
976 >                          new NoOpREHandler());
977              shouldThrow();
978 <        }
781 <        catch (NullPointerException success) {}
978 >        } catch (NullPointerException success) {}
979      }
980  
981      /**
# Line 786 | Line 983 | public class ThreadPoolExecutorSubclassT
983       */
984      public void testConstructorNullPointerException5() {
985          try {
986 <            RejectedExecutionHandler r = null;
987 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
986 >            new CustomTPE(1, 2, 1L, SECONDS,
987 >                          new ArrayBlockingQueue<Runnable>(10),
988 >                          (RejectedExecutionHandler) null);
989              shouldThrow();
990 <        }
793 <        catch (NullPointerException success) {}
990 >        } catch (NullPointerException success) {}
991      }
992  
796
993      /**
994       * Constructor throws if corePoolSize argument is less than zero
995       */
996      public void testConstructor16() {
997          try {
998 <            new CustomTPE(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
998 >            new CustomTPE(-1, 1, 1L, SECONDS,
999 >                          new ArrayBlockingQueue<Runnable>(10),
1000 >                          new SimpleThreadFactory(),
1001 >                          new NoOpREHandler());
1002              shouldThrow();
1003 <        }
805 <        catch (IllegalArgumentException success) {}
1003 >        } catch (IllegalArgumentException success) {}
1004      }
1005  
1006      /**
# Line 810 | Line 1008 | public class ThreadPoolExecutorSubclassT
1008       */
1009      public void testConstructor17() {
1010          try {
1011 <            new CustomTPE(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1011 >            new CustomTPE(1, -1, 1L, SECONDS,
1012 >                          new ArrayBlockingQueue<Runnable>(10),
1013 >                          new SimpleThreadFactory(),
1014 >                          new NoOpREHandler());
1015              shouldThrow();
1016 <        }
816 <        catch (IllegalArgumentException success) {}
1016 >        } catch (IllegalArgumentException success) {}
1017      }
1018  
1019      /**
# Line 821 | Line 1021 | public class ThreadPoolExecutorSubclassT
1021       */
1022      public void testConstructor18() {
1023          try {
1024 <            new CustomTPE(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1024 >            new CustomTPE(1, 0, 1L, SECONDS,
1025 >                          new ArrayBlockingQueue<Runnable>(10),
1026 >                          new SimpleThreadFactory(),
1027 >                          new NoOpREHandler());
1028              shouldThrow();
1029 <        }
827 <        catch (IllegalArgumentException success) {}
1029 >        } catch (IllegalArgumentException success) {}
1030      }
1031  
1032      /**
# Line 832 | Line 1034 | public class ThreadPoolExecutorSubclassT
1034       */
1035      public void testConstructor19() {
1036          try {
1037 <            new CustomTPE(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1037 >            new CustomTPE(1, 2, -1L, SECONDS,
1038 >                          new ArrayBlockingQueue<Runnable>(10),
1039 >                          new SimpleThreadFactory(),
1040 >                          new NoOpREHandler());
1041              shouldThrow();
1042 <        }
838 <        catch (IllegalArgumentException success) {}
1042 >        } catch (IllegalArgumentException success) {}
1043      }
1044  
1045      /**
# Line 843 | Line 1047 | public class ThreadPoolExecutorSubclassT
1047       */
1048      public void testConstructor20() {
1049          try {
1050 <            new CustomTPE(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1050 >            new CustomTPE(2, 1, 1L, SECONDS,
1051 >                          new ArrayBlockingQueue<Runnable>(10),
1052 >                          new SimpleThreadFactory(),
1053 >                          new NoOpREHandler());
1054              shouldThrow();
1055 <        }
849 <        catch (IllegalArgumentException success) {}
1055 >        } catch (IllegalArgumentException success) {}
1056      }
1057  
1058      /**
1059 <     * Constructor throws if workQueue is set to null
1059 >     * Constructor throws if workQueue is null
1060       */
1061      public void testConstructorNullPointerException6() {
1062          try {
1063 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
1063 >            new CustomTPE(1, 2, 1L, SECONDS,
1064 >                          null,
1065 >                          new SimpleThreadFactory(),
1066 >                          new NoOpREHandler());
1067              shouldThrow();
1068 <        }
860 <        catch (NullPointerException success) {}
1068 >        } catch (NullPointerException success) {}
1069      }
1070  
1071      /**
1072 <     * Constructor throws if handler is set to null
1072 >     * Constructor throws if handler is null
1073       */
1074      public void testConstructorNullPointerException7() {
1075          try {
1076 <            RejectedExecutionHandler r = null;
1077 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
1076 >            new CustomTPE(1, 2, 1L, SECONDS,
1077 >                          new ArrayBlockingQueue<Runnable>(10),
1078 >                          new SimpleThreadFactory(),
1079 >                          (RejectedExecutionHandler) null);
1080              shouldThrow();
1081 <        }
872 <        catch (NullPointerException success) {}
1081 >        } catch (NullPointerException success) {}
1082      }
1083  
1084      /**
1085 <     * Constructor throws if ThreadFactory is set top null
1085 >     * Constructor throws if ThreadFactory is null
1086       */
1087      public void testConstructorNullPointerException8() {
1088          try {
1089 <            ThreadFactory f = null;
1090 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
1089 >            new CustomTPE(1, 2, 1L, SECONDS,
1090 >                          new ArrayBlockingQueue<Runnable>(10),
1091 >                          (ThreadFactory) null,
1092 >                          new NoOpREHandler());
1093              shouldThrow();
1094 <        }
884 <        catch (NullPointerException successdn8) {}
1094 >        } catch (NullPointerException success) {}
1095      }
1096  
887
1097      /**
1098 <     *  execute throws RejectedExecutionException
890 <     *  if saturated.
1098 >     * execute throws RejectedExecutionException if saturated.
1099       */
1100      public void testSaturatedExecute() {
1101 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
1102 <        try {
1103 <
1104 <            for (int i = 0; i < 5; ++i) {
1105 <                p.execute(new MediumRunnable());
1101 >        ThreadPoolExecutor p =
1102 >            new CustomTPE(1, 1,
1103 >                          LONG_DELAY_MS, MILLISECONDS,
1104 >                          new ArrayBlockingQueue<Runnable>(1));
1105 >        final CountDownLatch done = new CountDownLatch(1);
1106 >        try {
1107 >            Runnable task = new CheckedRunnable() {
1108 >                public void realRun() throws InterruptedException {
1109 >                    done.await();
1110 >                }};
1111 >            for (int i = 0; i < 2; ++i)
1112 >                p.execute(task);
1113 >            for (int i = 0; i < 2; ++i) {
1114 >                try {
1115 >                    p.execute(task);
1116 >                    shouldThrow();
1117 >                } catch (RejectedExecutionException success) {}
1118 >                assertTrue(p.getTaskCount() <= 2);
1119              }
1120 <            shouldThrow();
1121 <        } catch (RejectedExecutionException success) {}
1122 <        joinPool(p);
1120 >        } finally {
1121 >            done.countDown();
1122 >            joinPool(p);
1123 >        }
1124      }
1125  
1126      /**
1127 <     *  executor using CallerRunsPolicy runs task if saturated.
1127 >     * executor using CallerRunsPolicy runs task if saturated.
1128       */
1129      public void testSaturatedExecute2() {
1130          RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1131 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1131 >        ThreadPoolExecutor p = new CustomTPE(1, 1,
1132 >                                             LONG_DELAY_MS, MILLISECONDS,
1133 >                                             new ArrayBlockingQueue<Runnable>(1),
1134 >                                             h);
1135          try {
911
1136              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1137 <            for (int i = 0; i < 5; ++i) {
1137 >            for (int i = 0; i < tasks.length; ++i)
1138                  tasks[i] = new TrackedNoOpRunnable();
915            }
1139              TrackedLongRunnable mr = new TrackedLongRunnable();
1140              p.execute(mr);
1141 <            for (int i = 0; i < 5; ++i) {
1141 >            for (int i = 0; i < tasks.length; ++i)
1142                  p.execute(tasks[i]);
1143 <            }
921 <            for (int i = 1; i < 5; ++i) {
1143 >            for (int i = 1; i < tasks.length; ++i)
1144                  assertTrue(tasks[i].done);
923            }
1145              try { p.shutdownNow(); } catch (SecurityException ok) { return; }
925        } catch (RejectedExecutionException ex) {
926            unexpectedException();
1146          } finally {
1147              joinPool(p);
1148          }
1149      }
1150  
1151      /**
1152 <     *  executor using DiscardPolicy drops task if saturated.
1152 >     * executor using DiscardPolicy drops task if saturated.
1153       */
1154      public void testSaturatedExecute3() {
1155          RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1156 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1156 >        ThreadPoolExecutor p =
1157 >            new CustomTPE(1, 1,
1158 >                          LONG_DELAY_MS, MILLISECONDS,
1159 >                          new ArrayBlockingQueue<Runnable>(1),
1160 >                          h);
1161          try {
939
1162              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1163 <            for (int i = 0; i < 5; ++i) {
1163 >            for (int i = 0; i < tasks.length; ++i)
1164                  tasks[i] = new TrackedNoOpRunnable();
943            }
1165              p.execute(new TrackedLongRunnable());
1166 <            for (int i = 0; i < 5; ++i) {
1167 <                p.execute(tasks[i]);
1168 <            }
1169 <            for (int i = 0; i < 5; ++i) {
949 <                assertFalse(tasks[i].done);
950 <            }
1166 >            for (TrackedNoOpRunnable task : tasks)
1167 >                p.execute(task);
1168 >            for (TrackedNoOpRunnable task : tasks)
1169 >                assertFalse(task.done);
1170              try { p.shutdownNow(); } catch (SecurityException ok) { return; }
952        } catch (RejectedExecutionException ex) {
953            unexpectedException();
1171          } finally {
1172              joinPool(p);
1173          }
1174      }
1175  
1176      /**
1177 <     *  executor using DiscardOldestPolicy drops oldest task if saturated.
1177 >     * executor using DiscardOldestPolicy drops oldest task if saturated.
1178       */
1179      public void testSaturatedExecute4() {
1180          RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
1181 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1181 >        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1182          try {
1183              p.execute(new TrackedLongRunnable());
1184              TrackedLongRunnable r2 = new TrackedLongRunnable();
# Line 972 | Line 1189 | public class ThreadPoolExecutorSubclassT
1189              assertFalse(p.getQueue().contains(r2));
1190              assertTrue(p.getQueue().contains(r3));
1191              try { p.shutdownNow(); } catch (SecurityException ok) { return; }
975        } catch (RejectedExecutionException ex) {
976            unexpectedException();
1192          } finally {
1193              joinPool(p);
1194          }
1195      }
1196  
1197      /**
1198 <     *  execute throws RejectedExecutionException if shutdown
1198 >     * execute throws RejectedExecutionException if shutdown
1199       */
1200      public void testRejectedExecutionExceptionOnShutdown() {
1201 <        ThreadPoolExecutor tpe =
1202 <            new CustomTPE(1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
1203 <        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1204 <        try {
1205 <            tpe.execute(new NoOpRunnable());
1206 <            shouldThrow();
1207 <        } catch (RejectedExecutionException success) {}
1201 >        ThreadPoolExecutor p =
1202 >            new CustomTPE(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
1203 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1204 >        try {
1205 >            p.execute(new NoOpRunnable());
1206 >            shouldThrow();
1207 >        } catch (RejectedExecutionException success) {}
1208  
1209 <        joinPool(tpe);
1209 >        joinPool(p);
1210      }
1211  
1212      /**
1213 <     *  execute using CallerRunsPolicy drops task on shutdown
1213 >     * execute using CallerRunsPolicy drops task on shutdown
1214       */
1215      public void testCallerRunsOnShutdown() {
1216          RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1217 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1217 >        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1218  
1219          try { p.shutdown(); } catch (SecurityException ok) { return; }
1220 <        try {
1220 >        try {
1221              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1222 <            p.execute(r);
1222 >            p.execute(r);
1223              assertFalse(r.done);
1009        } catch (RejectedExecutionException success) {
1010            unexpectedException();
1224          } finally {
1225              joinPool(p);
1226          }
1227      }
1228  
1229      /**
1230 <     *  execute using DiscardPolicy drops task on shutdown
1230 >     * execute using DiscardPolicy drops task on shutdown
1231       */
1232      public void testDiscardOnShutdown() {
1233          RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1234 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1234 >        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1235  
1236          try { p.shutdown(); } catch (SecurityException ok) { return; }
1237 <        try {
1237 >        try {
1238              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1239 <            p.execute(r);
1239 >            p.execute(r);
1240              assertFalse(r.done);
1028        } catch (RejectedExecutionException success) {
1029            unexpectedException();
1241          } finally {
1242              joinPool(p);
1243          }
1244      }
1245  
1035
1246      /**
1247 <     *  execute using DiscardOldestPolicy drops task on shutdown
1247 >     * execute using DiscardOldestPolicy drops task on shutdown
1248       */
1249      public void testDiscardOldestOnShutdown() {
1250          RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
1251 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1251 >        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1252  
1253          try { p.shutdown(); } catch (SecurityException ok) { return; }
1254 <        try {
1254 >        try {
1255              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1256 <            p.execute(r);
1256 >            p.execute(r);
1257              assertFalse(r.done);
1048        } catch (RejectedExecutionException success) {
1049            unexpectedException();
1258          } finally {
1259              joinPool(p);
1260          }
1261      }
1262  
1055
1263      /**
1264 <     *  execute (null) throws NPE
1264 >     * execute(null) throws NPE
1265       */
1266      public void testExecuteNull() {
1267 <        ThreadPoolExecutor tpe = null;
1267 >        ThreadPoolExecutor p =
1268 >            new CustomTPE(1, 2, 1L, SECONDS,
1269 >                          new ArrayBlockingQueue<Runnable>(10));
1270          try {
1271 <            tpe = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1063 <            tpe.execute(null);
1271 >            p.execute(null);
1272              shouldThrow();
1273 <        } catch (NullPointerException success) {}
1273 >        } catch (NullPointerException success) {}
1274  
1275 <        joinPool(tpe);
1275 >        joinPool(p);
1276      }
1277  
1278      /**
1279 <     *  setCorePoolSize of negative value throws IllegalArgumentException
1279 >     * setCorePoolSize of negative value throws IllegalArgumentException
1280       */
1281      public void testCorePoolSizeIllegalArgumentException() {
1282 <        ThreadPoolExecutor tpe = null;
1283 <        try {
1284 <            tpe = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1285 <        } catch (Exception e) {}
1286 <        try {
1287 <            tpe.setCorePoolSize(-1);
1080 <            shouldThrow();
1081 <        } catch (IllegalArgumentException success) {
1282 >        ThreadPoolExecutor p =
1283 >            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1284 >        try {
1285 >            p.setCorePoolSize(-1);
1286 >            shouldThrow();
1287 >        } catch (IllegalArgumentException success) {
1288          } finally {
1289 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1289 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1290          }
1291 <        joinPool(tpe);
1291 >        joinPool(p);
1292      }
1293  
1294      /**
1295 <     *  setMaximumPoolSize(int) throws IllegalArgumentException if
1296 <     *  given a value less the core pool size
1295 >     * setMaximumPoolSize(int) throws IllegalArgumentException
1296 >     * if given a value less the core pool size
1297       */
1298      public void testMaximumPoolSizeIllegalArgumentException() {
1299 <        ThreadPoolExecutor tpe = null;
1300 <        try {
1095 <            tpe = new CustomTPE(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1096 <        } catch (Exception e) {}
1299 >        ThreadPoolExecutor p =
1300 >            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1301          try {
1302 <            tpe.setMaximumPoolSize(1);
1302 >            p.setMaximumPoolSize(1);
1303              shouldThrow();
1304          } catch (IllegalArgumentException success) {
1305          } finally {
1306 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1306 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1307          }
1308 <        joinPool(tpe);
1308 >        joinPool(p);
1309      }
1310  
1311      /**
1312 <     *  setMaximumPoolSize throws IllegalArgumentException
1313 <     *  if given a negative value
1312 >     * setMaximumPoolSize throws IllegalArgumentException
1313 >     * if given a negative value
1314       */
1315      public void testMaximumPoolSizeIllegalArgumentException2() {
1316 <        ThreadPoolExecutor tpe = null;
1317 <        try {
1114 <            tpe = new CustomTPE(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1115 <        } catch (Exception e) {}
1316 >        ThreadPoolExecutor p =
1317 >            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1318          try {
1319 <            tpe.setMaximumPoolSize(-1);
1319 >            p.setMaximumPoolSize(-1);
1320              shouldThrow();
1321          } catch (IllegalArgumentException success) {
1322          } finally {
1323 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1323 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1324          }
1325 <        joinPool(tpe);
1325 >        joinPool(p);
1326      }
1327  
1126
1328      /**
1329 <     *  setKeepAliveTime  throws IllegalArgumentException
1330 <     *  when given a negative value
1329 >     * setKeepAliveTime throws IllegalArgumentException
1330 >     * when given a negative value
1331       */
1332      public void testKeepAliveTimeIllegalArgumentException() {
1333 <        ThreadPoolExecutor tpe = null;
1334 <        try {
1134 <            tpe = new CustomTPE(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1135 <        } catch (Exception e) {}
1333 >        ThreadPoolExecutor p =
1334 >            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1335  
1336 <        try {
1337 <            tpe.setKeepAliveTime(-1,TimeUnit.MILLISECONDS);
1336 >        try {
1337 >            p.setKeepAliveTime(-1,MILLISECONDS);
1338              shouldThrow();
1339          } catch (IllegalArgumentException success) {
1340          } finally {
1341 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1341 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1342          }
1343 <        joinPool(tpe);
1343 >        joinPool(p);
1344      }
1345  
1346      /**
1347       * terminated() is called on termination
1348       */
1349      public void testTerminated() {
1350 <        CustomTPE tpe = new CustomTPE();
1351 <        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1352 <        assertTrue(tpe.terminatedCalled);
1353 <        joinPool(tpe);
1350 >        CustomTPE p = new CustomTPE();
1351 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1352 >        assertTrue(p.terminatedCalled());
1353 >        joinPool(p);
1354      }
1355  
1356      /**
1357       * beforeExecute and afterExecute are called when executing task
1358       */
1359 <    public void testBeforeAfter() {
1360 <        CustomTPE tpe = new CustomTPE();
1359 >    public void testBeforeAfter() throws InterruptedException {
1360 >        CustomTPE p = new CustomTPE();
1361          try {
1362 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1363 <            tpe.execute(r);
1364 <            Thread.sleep(SHORT_DELAY_MS);
1365 <            assertTrue(r.done);
1366 <            assertTrue(tpe.beforeCalled);
1367 <            assertTrue(tpe.afterCalled);
1368 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1369 <        }
1370 <        catch (Exception ex) {
1371 <            unexpectedException();
1362 >            final CountDownLatch done = new CountDownLatch(1);
1363 >            p.execute(new CheckedRunnable() {
1364 >                public void realRun() {
1365 >                    done.countDown();
1366 >                }});
1367 >            await(p.afterCalled);
1368 >            assertEquals(0, done.getCount());
1369 >            assertTrue(p.afterCalled());
1370 >            assertTrue(p.beforeCalled());
1371 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1372          } finally {
1373 <            joinPool(tpe);
1373 >            joinPool(p);
1374          }
1375      }
1376  
1377      /**
1378       * completed submit of callable returns result
1379       */
1380 <    public void testSubmitCallable() {
1381 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1380 >    public void testSubmitCallable() throws Exception {
1381 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1382          try {
1383              Future<String> future = e.submit(new StringTask());
1384              String result = future.get();
1385              assertSame(TEST_STRING, result);
1187        }
1188        catch (ExecutionException ex) {
1189            unexpectedException();
1190        }
1191        catch (InterruptedException ex) {
1192            unexpectedException();
1386          } finally {
1387              joinPool(e);
1388          }
# Line 1198 | Line 1391 | public class ThreadPoolExecutorSubclassT
1391      /**
1392       * completed submit of runnable returns successfully
1393       */
1394 <    public void testSubmitRunnable() {
1395 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1394 >    public void testSubmitRunnable() throws Exception {
1395 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1396          try {
1397              Future<?> future = e.submit(new NoOpRunnable());
1398              future.get();
1399              assertTrue(future.isDone());
1207        }
1208        catch (ExecutionException ex) {
1209            unexpectedException();
1210        }
1211        catch (InterruptedException ex) {
1212            unexpectedException();
1400          } finally {
1401              joinPool(e);
1402          }
# Line 1218 | Line 1405 | public class ThreadPoolExecutorSubclassT
1405      /**
1406       * completed submit of (runnable, result) returns result
1407       */
1408 <    public void testSubmitRunnable2() {
1409 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1408 >    public void testSubmitRunnable2() throws Exception {
1409 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1410          try {
1411              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1412              String result = future.get();
1413              assertSame(TEST_STRING, result);
1227        }
1228        catch (ExecutionException ex) {
1229            unexpectedException();
1230        }
1231        catch (InterruptedException ex) {
1232            unexpectedException();
1414          } finally {
1415              joinPool(e);
1416          }
1417      }
1418  
1238
1239
1240
1241
1419      /**
1420       * invokeAny(null) throws NPE
1421       */
1422 <    public void testInvokeAny1() {
1423 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1422 >    public void testInvokeAny1() throws Exception {
1423 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1424          try {
1425              e.invokeAny(null);
1426 +            shouldThrow();
1427          } catch (NullPointerException success) {
1250        } catch (Exception ex) {
1251            unexpectedException();
1428          } finally {
1429              joinPool(e);
1430          }
# Line 1257 | Line 1433 | public class ThreadPoolExecutorSubclassT
1433      /**
1434       * invokeAny(empty collection) throws IAE
1435       */
1436 <    public void testInvokeAny2() {
1437 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1436 >    public void testInvokeAny2() throws Exception {
1437 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1438          try {
1439              e.invokeAny(new ArrayList<Callable<String>>());
1440 +            shouldThrow();
1441          } catch (IllegalArgumentException success) {
1265        } catch (Exception ex) {
1266            unexpectedException();
1442          } finally {
1443              joinPool(e);
1444          }
# Line 1272 | Line 1447 | public class ThreadPoolExecutorSubclassT
1447      /**
1448       * invokeAny(c) throws NPE if c has null elements
1449       */
1450 <    public void testInvokeAny3() {
1451 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1450 >    public void testInvokeAny3() throws Exception {
1451 >        CountDownLatch latch = new CountDownLatch(1);
1452 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1453 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1454 >        l.add(latchAwaitingStringTask(latch));
1455 >        l.add(null);
1456          try {
1278            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1279            l.add(new StringTask());
1280            l.add(null);
1457              e.invokeAny(l);
1458 +            shouldThrow();
1459          } catch (NullPointerException success) {
1283        } catch (Exception ex) {
1284            unexpectedException();
1460          } finally {
1461 +            latch.countDown();
1462              joinPool(e);
1463          }
1464      }
# Line 1290 | Line 1466 | public class ThreadPoolExecutorSubclassT
1466      /**
1467       * invokeAny(c) throws ExecutionException if no task completes
1468       */
1469 <    public void testInvokeAny4() {
1470 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1469 >    public void testInvokeAny4() throws Exception {
1470 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1471 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1472 >        l.add(new NPETask());
1473          try {
1296            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1297            l.add(new NPETask());
1474              e.invokeAny(l);
1475 +            shouldThrow();
1476          } catch (ExecutionException success) {
1477 <        } catch (Exception ex) {
1301 <            unexpectedException();
1477 >            assertTrue(success.getCause() instanceof NullPointerException);
1478          } finally {
1479              joinPool(e);
1480          }
# Line 1307 | Line 1483 | public class ThreadPoolExecutorSubclassT
1483      /**
1484       * invokeAny(c) returns result of some task
1485       */
1486 <    public void testInvokeAny5() {
1487 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1486 >    public void testInvokeAny5() throws Exception {
1487 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1488          try {
1489 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1489 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1490              l.add(new StringTask());
1491              l.add(new StringTask());
1492              String result = e.invokeAny(l);
1493              assertSame(TEST_STRING, result);
1318        } catch (ExecutionException success) {
1319        } catch (Exception ex) {
1320            unexpectedException();
1494          } finally {
1495              joinPool(e);
1496          }
# Line 1326 | Line 1499 | public class ThreadPoolExecutorSubclassT
1499      /**
1500       * invokeAll(null) throws NPE
1501       */
1502 <    public void testInvokeAll1() {
1503 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1502 >    public void testInvokeAll1() throws Exception {
1503 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1504          try {
1505              e.invokeAll(null);
1506 +            shouldThrow();
1507          } catch (NullPointerException success) {
1334        } catch (Exception ex) {
1335            unexpectedException();
1508          } finally {
1509              joinPool(e);
1510          }
# Line 1341 | Line 1513 | public class ThreadPoolExecutorSubclassT
1513      /**
1514       * invokeAll(empty collection) returns empty collection
1515       */
1516 <    public void testInvokeAll2() {
1517 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1516 >    public void testInvokeAll2() throws Exception {
1517 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1518          try {
1519              List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1520              assertTrue(r.isEmpty());
1349        } catch (Exception ex) {
1350            unexpectedException();
1521          } finally {
1522              joinPool(e);
1523          }
# Line 1356 | Line 1526 | public class ThreadPoolExecutorSubclassT
1526      /**
1527       * invokeAll(c) throws NPE if c has null elements
1528       */
1529 <    public void testInvokeAll3() {
1530 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1529 >    public void testInvokeAll3() throws Exception {
1530 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1531 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1532 >        l.add(new StringTask());
1533 >        l.add(null);
1534          try {
1362            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1363            l.add(new StringTask());
1364            l.add(null);
1535              e.invokeAll(l);
1536 +            shouldThrow();
1537          } catch (NullPointerException success) {
1367        } catch (Exception ex) {
1368            unexpectedException();
1538          } finally {
1539              joinPool(e);
1540          }
# Line 1374 | Line 1543 | public class ThreadPoolExecutorSubclassT
1543      /**
1544       * get of element of invokeAll(c) throws exception on failed task
1545       */
1546 <    public void testInvokeAll4() {
1547 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1546 >    public void testInvokeAll4() throws Exception {
1547 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1548 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1549 >        l.add(new NPETask());
1550 >        List<Future<String>> futures = e.invokeAll(l);
1551 >        assertEquals(1, futures.size());
1552          try {
1553 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1554 <            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();
1553 >            futures.get(0).get();
1554 >            shouldThrow();
1555          } catch (ExecutionException success) {
1556 <        } catch (Exception ex) {
1388 <            unexpectedException();
1556 >            assertTrue(success.getCause() instanceof NullPointerException);
1557          } finally {
1558              joinPool(e);
1559          }
# Line 1394 | Line 1562 | public class ThreadPoolExecutorSubclassT
1562      /**
1563       * invokeAll(c) returns results of all completed tasks
1564       */
1565 <    public void testInvokeAll5() {
1566 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1565 >    public void testInvokeAll5() throws Exception {
1566 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1567          try {
1568 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1568 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1569              l.add(new StringTask());
1570              l.add(new StringTask());
1571 <            List<Future<String>> result = e.invokeAll(l);
1572 <            assertEquals(2, result.size());
1573 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1574 <                assertSame(TEST_STRING, it.next().get());
1407 <        } catch (ExecutionException success) {
1408 <        } catch (Exception ex) {
1409 <            unexpectedException();
1571 >            List<Future<String>> futures = e.invokeAll(l);
1572 >            assertEquals(2, futures.size());
1573 >            for (Future<String> future : futures)
1574 >                assertSame(TEST_STRING, future.get());
1575          } finally {
1576              joinPool(e);
1577          }
1578      }
1579  
1415
1416
1580      /**
1581       * timed invokeAny(null) throws NPE
1582       */
1583 <    public void testTimedInvokeAny1() {
1584 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1583 >    public void testTimedInvokeAny1() throws Exception {
1584 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1585          try {
1586 <            e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1586 >            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1587 >            shouldThrow();
1588          } catch (NullPointerException success) {
1425        } catch (Exception ex) {
1426            unexpectedException();
1589          } finally {
1590              joinPool(e);
1591          }
# Line 1432 | Line 1594 | public class ThreadPoolExecutorSubclassT
1594      /**
1595       * timed invokeAny(,,null) throws NPE
1596       */
1597 <    public void testTimedInvokeAnyNullTimeUnit() {
1598 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1597 >    public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1598 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1599 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1600 >        l.add(new StringTask());
1601          try {
1438            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1439            l.add(new StringTask());
1602              e.invokeAny(l, MEDIUM_DELAY_MS, null);
1603 +            shouldThrow();
1604          } catch (NullPointerException success) {
1442        } catch (Exception ex) {
1443            unexpectedException();
1605          } finally {
1606              joinPool(e);
1607          }
# Line 1449 | Line 1610 | public class ThreadPoolExecutorSubclassT
1610      /**
1611       * timed invokeAny(empty collection) throws IAE
1612       */
1613 <    public void testTimedInvokeAny2() {
1614 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1613 >    public void testTimedInvokeAny2() throws Exception {
1614 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1615          try {
1616 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1616 >            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1617 >            shouldThrow();
1618          } catch (IllegalArgumentException success) {
1457        } catch (Exception ex) {
1458            unexpectedException();
1619          } finally {
1620              joinPool(e);
1621          }
# Line 1464 | Line 1624 | public class ThreadPoolExecutorSubclassT
1624      /**
1625       * timed invokeAny(c) throws NPE if c has null elements
1626       */
1627 <    public void testTimedInvokeAny3() {
1628 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1627 >    public void testTimedInvokeAny3() throws Exception {
1628 >        CountDownLatch latch = new CountDownLatch(1);
1629 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1630 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1631 >        l.add(latchAwaitingStringTask(latch));
1632 >        l.add(null);
1633          try {
1634 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1635 <            l.add(new StringTask());
1472 <            l.add(null);
1473 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1634 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1635 >            shouldThrow();
1636          } catch (NullPointerException success) {
1475        } catch (Exception ex) {
1476            ex.printStackTrace();
1477            unexpectedException();
1637          } finally {
1638 +            latch.countDown();
1639              joinPool(e);
1640          }
1641      }
# Line 1483 | Line 1643 | public class ThreadPoolExecutorSubclassT
1643      /**
1644       * timed invokeAny(c) throws ExecutionException if no task completes
1645       */
1646 <    public void testTimedInvokeAny4() {
1647 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1646 >    public void testTimedInvokeAny4() throws Exception {
1647 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1648 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1649 >        l.add(new NPETask());
1650          try {
1651 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1652 <            l.add(new NPETask());
1491 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1651 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1652 >            shouldThrow();
1653          } catch (ExecutionException success) {
1654 <        } catch (Exception ex) {
1494 <            unexpectedException();
1654 >            assertTrue(success.getCause() instanceof NullPointerException);
1655          } finally {
1656              joinPool(e);
1657          }
# Line 1500 | Line 1660 | public class ThreadPoolExecutorSubclassT
1660      /**
1661       * timed invokeAny(c) returns result of some task
1662       */
1663 <    public void testTimedInvokeAny5() {
1664 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1663 >    public void testTimedInvokeAny5() throws Exception {
1664 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1665          try {
1666 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1666 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1667              l.add(new StringTask());
1668              l.add(new StringTask());
1669 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1669 >            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1670              assertSame(TEST_STRING, result);
1511        } catch (ExecutionException success) {
1512        } catch (Exception ex) {
1513            unexpectedException();
1671          } finally {
1672              joinPool(e);
1673          }
# Line 1519 | Line 1676 | public class ThreadPoolExecutorSubclassT
1676      /**
1677       * timed invokeAll(null) throws NPE
1678       */
1679 <    public void testTimedInvokeAll1() {
1680 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1679 >    public void testTimedInvokeAll1() throws Exception {
1680 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1681          try {
1682 <            e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1682 >            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1683 >            shouldThrow();
1684          } catch (NullPointerException success) {
1527        } catch (Exception ex) {
1528            unexpectedException();
1685          } finally {
1686              joinPool(e);
1687          }
# Line 1534 | Line 1690 | public class ThreadPoolExecutorSubclassT
1690      /**
1691       * timed invokeAll(,,null) throws NPE
1692       */
1693 <    public void testTimedInvokeAllNullTimeUnit() {
1694 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1693 >    public void testTimedInvokeAllNullTimeUnit() throws Exception {
1694 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1695 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1696 >        l.add(new StringTask());
1697          try {
1540            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1541            l.add(new StringTask());
1698              e.invokeAll(l, MEDIUM_DELAY_MS, null);
1699 +            shouldThrow();
1700          } catch (NullPointerException success) {
1544        } catch (Exception ex) {
1545            unexpectedException();
1701          } finally {
1702              joinPool(e);
1703          }
# Line 1551 | Line 1706 | public class ThreadPoolExecutorSubclassT
1706      /**
1707       * timed invokeAll(empty collection) returns empty collection
1708       */
1709 <    public void testTimedInvokeAll2() {
1710 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1709 >    public void testTimedInvokeAll2() throws Exception {
1710 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1711          try {
1712 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1712 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1713              assertTrue(r.isEmpty());
1559        } catch (Exception ex) {
1560            unexpectedException();
1714          } finally {
1715              joinPool(e);
1716          }
# Line 1566 | Line 1719 | public class ThreadPoolExecutorSubclassT
1719      /**
1720       * timed invokeAll(c) throws NPE if c has null elements
1721       */
1722 <    public void testTimedInvokeAll3() {
1723 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1722 >    public void testTimedInvokeAll3() throws Exception {
1723 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1724 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1725 >        l.add(new StringTask());
1726 >        l.add(null);
1727          try {
1728 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1729 <            l.add(new StringTask());
1574 <            l.add(null);
1575 <            e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1728 >            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1729 >            shouldThrow();
1730          } catch (NullPointerException success) {
1577        } catch (Exception ex) {
1578            unexpectedException();
1731          } finally {
1732              joinPool(e);
1733          }
# Line 1584 | Line 1736 | public class ThreadPoolExecutorSubclassT
1736      /**
1737       * get of element of invokeAll(c) throws exception on failed task
1738       */
1739 <    public void testTimedInvokeAll4() {
1740 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1739 >    public void testTimedInvokeAll4() throws Exception {
1740 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1741 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1742 >        l.add(new NPETask());
1743 >        List<Future<String>> futures =
1744 >            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1745 >        assertEquals(1, futures.size());
1746          try {
1747 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1748 <            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();
1747 >            futures.get(0).get();
1748 >            shouldThrow();
1749          } catch (ExecutionException success) {
1750 <        } catch (Exception ex) {
1598 <            unexpectedException();
1750 >            assertTrue(success.getCause() instanceof NullPointerException);
1751          } finally {
1752              joinPool(e);
1753          }
# Line 1604 | Line 1756 | public class ThreadPoolExecutorSubclassT
1756      /**
1757       * timed invokeAll(c) returns results of all completed tasks
1758       */
1759 <    public void testTimedInvokeAll5() {
1760 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1759 >    public void testTimedInvokeAll5() throws Exception {
1760 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1761          try {
1762 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1762 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1763              l.add(new StringTask());
1764              l.add(new StringTask());
1765 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1766 <            assertEquals(2, result.size());
1767 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1768 <                assertSame(TEST_STRING, it.next().get());
1769 <        } catch (ExecutionException success) {
1618 <        } catch (Exception ex) {
1619 <            unexpectedException();
1765 >            List<Future<String>> futures =
1766 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1767 >            assertEquals(2, futures.size());
1768 >            for (Future<String> future : futures)
1769 >                assertSame(TEST_STRING, future.get());
1770          } finally {
1771              joinPool(e);
1772          }
# Line 1625 | Line 1775 | public class ThreadPoolExecutorSubclassT
1775      /**
1776       * timed invokeAll(c) cancels tasks not completed by timeout
1777       */
1778 <    public void testTimedInvokeAll6() {
1779 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1778 >    public void testTimedInvokeAll6() throws Exception {
1779 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1780          try {
1781 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1782 <            l.add(new StringTask());
1783 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1784 <            l.add(new StringTask());
1785 <            List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
1786 <            assertEquals(3, result.size());
1787 <            Iterator<Future<String>> it = result.iterator();
1788 <            Future<String> f1 = it.next();
1789 <            Future<String> f2 = it.next();
1790 <            Future<String> f3 = it.next();
1791 <            assertTrue(f1.isDone());
1792 <            assertTrue(f2.isDone());
1793 <            assertTrue(f3.isDone());
1794 <            assertFalse(f1.isCancelled());
1795 <            assertTrue(f2.isCancelled());
1796 <        } catch (Exception ex) {
1797 <            unexpectedException();
1781 >            for (long timeout = timeoutMillis();;) {
1782 >                List<Callable<String>> tasks = new ArrayList<>();
1783 >                tasks.add(new StringTask("0"));
1784 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1785 >                tasks.add(new StringTask("2"));
1786 >                long startTime = System.nanoTime();
1787 >                List<Future<String>> futures =
1788 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1789 >                assertEquals(tasks.size(), futures.size());
1790 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1791 >                for (Future future : futures)
1792 >                    assertTrue(future.isDone());
1793 >                assertTrue(futures.get(1).isCancelled());
1794 >                try {
1795 >                    assertEquals("0", futures.get(0).get());
1796 >                    assertEquals("2", futures.get(2).get());
1797 >                    break;
1798 >                } catch (CancellationException retryWithLongerTimeout) {
1799 >                    timeout *= 2;
1800 >                    if (timeout >= LONG_DELAY_MS / 2)
1801 >                        fail("expected exactly one task to be cancelled");
1802 >                }
1803 >            }
1804          } finally {
1805              joinPool(e);
1806          }
# Line 1654 | Line 1810 | public class ThreadPoolExecutorSubclassT
1810       * Execution continues if there is at least one thread even if
1811       * thread factory fails to create more
1812       */
1813 <    public void testFailingThreadFactory() {
1814 <        ExecutorService e = new CustomTPE(100, 100, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory());
1815 <        try {
1816 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1817 <            for (int k = 0; k < 100; ++k) {
1818 <                e.execute(new NoOpRunnable());
1819 <            }
1820 <            Thread.sleep(LONG_DELAY_MS);
1821 <        } catch (Exception ex) {
1822 <            unexpectedException();
1813 >    public void testFailingThreadFactory() throws InterruptedException {
1814 >        final ExecutorService e =
1815 >            new CustomTPE(100, 100,
1816 >                          LONG_DELAY_MS, MILLISECONDS,
1817 >                          new LinkedBlockingQueue<Runnable>(),
1818 >                          new FailingThreadFactory());
1819 >        try {
1820 >            final int TASKS = 100;
1821 >            final CountDownLatch done = new CountDownLatch(TASKS);
1822 >            for (int k = 0; k < TASKS; ++k)
1823 >                e.execute(new CheckedRunnable() {
1824 >                    public void realRun() {
1825 >                        done.countDown();
1826 >                    }});
1827 >            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1828          } finally {
1829              joinPool(e);
1830          }
# Line 1673 | Line 1834 | public class ThreadPoolExecutorSubclassT
1834       * allowsCoreThreadTimeOut is by default false.
1835       */
1836      public void testAllowsCoreThreadTimeOut() {
1837 <        ThreadPoolExecutor tpe = new CustomTPE(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1838 <        assertFalse(tpe.allowsCoreThreadTimeOut());
1839 <        joinPool(tpe);
1837 >        ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1838 >        assertFalse(p.allowsCoreThreadTimeOut());
1839 >        joinPool(p);
1840      }
1841  
1842      /**
1843       * allowCoreThreadTimeOut(true) causes idle threads to time out
1844       */
1845 <    public void testAllowCoreThreadTimeOut_true() {
1846 <        ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1847 <        tpe.allowCoreThreadTimeOut(true);
1848 <        tpe.execute(new NoOpRunnable());
1849 <        try {
1850 <            Thread.sleep(MEDIUM_DELAY_MS);
1851 <            assertEquals(0, tpe.getPoolSize());
1852 <        } catch (InterruptedException e) {
1853 <            unexpectedException();
1845 >    public void testAllowCoreThreadTimeOut_true() throws Exception {
1846 >        long keepAliveTime = timeoutMillis();
1847 >        final ThreadPoolExecutor p =
1848 >            new CustomTPE(2, 10,
1849 >                          keepAliveTime, MILLISECONDS,
1850 >                          new ArrayBlockingQueue<Runnable>(10));
1851 >        final CountDownLatch threadStarted = new CountDownLatch(1);
1852 >        try {
1853 >            p.allowCoreThreadTimeOut(true);
1854 >            p.execute(new CheckedRunnable() {
1855 >                public void realRun() {
1856 >                    threadStarted.countDown();
1857 >                    assertEquals(1, p.getPoolSize());
1858 >                }});
1859 >            await(threadStarted);
1860 >            delay(keepAliveTime);
1861 >            long startTime = System.nanoTime();
1862 >            while (p.getPoolSize() > 0
1863 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1864 >                Thread.yield();
1865 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1866 >            assertEquals(0, p.getPoolSize());
1867          } finally {
1868 <            joinPool(tpe);
1868 >            joinPool(p);
1869          }
1870      }
1871  
1872      /**
1873       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1874       */
1875 <    public void testAllowCoreThreadTimeOut_false() {
1876 <        ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1877 <        tpe.allowCoreThreadTimeOut(false);
1878 <        tpe.execute(new NoOpRunnable());
1879 <        try {
1880 <            Thread.sleep(MEDIUM_DELAY_MS);
1881 <            assertTrue(tpe.getPoolSize() >= 1);
1882 <        } catch (InterruptedException e) {
1883 <            unexpectedException();
1875 >    public void testAllowCoreThreadTimeOut_false() throws Exception {
1876 >        long keepAliveTime = timeoutMillis();
1877 >        final ThreadPoolExecutor p =
1878 >            new CustomTPE(2, 10,
1879 >                          keepAliveTime, MILLISECONDS,
1880 >                          new ArrayBlockingQueue<Runnable>(10));
1881 >        final CountDownLatch threadStarted = new CountDownLatch(1);
1882 >        try {
1883 >            p.allowCoreThreadTimeOut(false);
1884 >            p.execute(new CheckedRunnable() {
1885 >                public void realRun() throws InterruptedException {
1886 >                    threadStarted.countDown();
1887 >                    assertTrue(p.getPoolSize() >= 1);
1888 >                }});
1889 >            delay(2 * keepAliveTime);
1890 >            assertTrue(p.getPoolSize() >= 1);
1891 >        } finally {
1892 >            joinPool(p);
1893 >        }
1894 >    }
1895 >
1896 >    /**
1897 >     * get(cancelled task) throws CancellationException
1898 >     * (in part, a test of CustomTPE itself)
1899 >     */
1900 >    public void testGet_cancelled() throws Exception {
1901 >        final ExecutorService e =
1902 >            new CustomTPE(1, 1,
1903 >                          LONG_DELAY_MS, MILLISECONDS,
1904 >                          new LinkedBlockingQueue<Runnable>());
1905 >        try {
1906 >            final CountDownLatch blockerStarted = new CountDownLatch(1);
1907 >            final CountDownLatch done = new CountDownLatch(1);
1908 >            final List<Future<?>> futures = new ArrayList<>();
1909 >            for (int i = 0; i < 2; i++) {
1910 >                Runnable r = new CheckedRunnable() { public void realRun()
1911 >                                                         throws Throwable {
1912 >                    blockerStarted.countDown();
1913 >                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
1914 >                }};
1915 >                futures.add(e.submit(r));
1916 >            }
1917 >            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
1918 >            for (Future<?> future : futures) future.cancel(false);
1919 >            for (Future<?> future : futures) {
1920 >                try {
1921 >                    future.get();
1922 >                    shouldThrow();
1923 >                } catch (CancellationException success) {}
1924 >                try {
1925 >                    future.get(LONG_DELAY_MS, MILLISECONDS);
1926 >                    shouldThrow();
1927 >                } catch (CancellationException success) {}
1928 >                assertTrue(future.isCancelled());
1929 >                assertTrue(future.isDone());
1930 >            }
1931 >            done.countDown();
1932          } finally {
1933 <            joinPool(tpe);
1933 >            joinPool(e);
1934          }
1935      }
1936  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines