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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines