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.1 by dl, Fri May 20 16:30:17 2005 UTC vs.
Revision 1.27 by jsr166, Sat May 7 19:34:51 2011 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
5 < * Other contributors include Andrew Wright, Jeffrey Hayes,
6 < * Pat Fisher, Mike Judd.
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 static java.util.concurrent.TimeUnit.MILLISECONDS;
11   import java.util.concurrent.locks.*;
12  
13   import junit.framework.*;
# Line 14 | Line 15 | import java.util.*;
15  
16   public class ThreadPoolExecutorSubclassTest extends JSR166TestCase {
17      public static void main(String[] args) {
18 <        junit.textui.TestRunner.run (suite());  
18 >        junit.textui.TestRunner.run(suite());
19      }
20      public static Test suite() {
21 <        return new TestSuite(ThreadPoolExecutorTest.class);
21 >        return new TestSuite(ThreadPoolExecutorSubclassTest.class);
22      }
23  
24      static class CustomTask<V> implements RunnableFuture<V> {
# Line 29 | Line 30 | public class ThreadPoolExecutorSubclassT
30          V result;
31          Thread thread;
32          Exception exception;
33 <        CustomTask(Callable<V> c) { callable = c; }
34 <        CustomTask(final Runnable r, final V res) { callable = new Callable<V>() {
35 <            public V call() throws Exception { r.run(); return res; }};
33 >        CustomTask(Callable<V> c) {
34 >            if (c == null) throw new NullPointerException();
35 >            callable = c;
36 >        }
37 >        CustomTask(final Runnable r, final V res) {
38 >            if (r == null) throw new NullPointerException();
39 >            callable = new Callable<V>() {
40 >            public V call() throws Exception { r.run(); return res; }};
41          }
42          public boolean isDone() {
43              lock.lock(); try { return done; } finally { lock.unlock() ; }
# Line 45 | Line 51 | public class ThreadPoolExecutorSubclassT
51                  if (!done) {
52                      cancelled = true;
53                      done = true;
54 <                    if (mayInterrupt && thread != null)
54 >                    if (mayInterrupt && thread != null)
55                          thread.interrupt();
56                      return true;
57                  }
# Line 54 | Line 60 | public class ThreadPoolExecutorSubclassT
60              finally { lock.unlock() ; }
61          }
62          public void run() {
57            boolean runme;
63              lock.lock();
64              try {
65 <                runme = !done;
66 <                if (!runme)
67 <                    thread = Thread.currentThread();
65 >                if (done)
66 >                    return;
67 >                thread = Thread.currentThread();
68              }
69              finally { lock.unlock() ; }
65            if (!runme) return;
70              V v = null;
71              Exception e = null;
72              try {
73                  v = callable.call();
74              }
75 <            catch(Exception ex) {
75 >            catch (Exception ex) {
76                  e = ex;
77              }
78              lock.lock();
# Line 84 | Line 88 | public class ThreadPoolExecutorSubclassT
88          public V get() throws InterruptedException, ExecutionException {
89              lock.lock();
90              try {
91 <                while (!done)
91 >                while (!done)
92                      cond.await();
93                  if (exception != null)
94                      throw new ExecutionException(exception);
# Line 93 | Line 97 | public class ThreadPoolExecutorSubclassT
97              finally { lock.unlock(); }
98          }
99          public V get(long timeout, TimeUnit unit)
100 <            throws InterruptedException, ExecutionException, TimeoutException{
100 >            throws InterruptedException, ExecutionException, TimeoutException {
101              long nanos = unit.toNanos(timeout);
102              lock.lock();
103              try {
# Line 109 | Line 113 | public class ThreadPoolExecutorSubclassT
113              }
114              finally { lock.unlock(); }
115          }
116 <    }            
116 >    }
117 >
118  
114    
119      static class CustomTPE extends ThreadPoolExecutor {
120          protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
121              return new CustomTask<V>(c);
122          }
123          protected <V> RunnableFuture<V> newTaskFor(Runnable r, V v) {
124              return new CustomTask<V>(r, v);
125 <        }
126 <        
125 >        }
126 >
127          CustomTPE(int corePoolSize,
128                    int maximumPoolSize,
129                    long keepAliveTime,
130                    TimeUnit unit,
131                    BlockingQueue<Runnable> workQueue) {
132 <            super(corePoolSize, maximumPoolSize, keepAliveTime, unit,
132 >            super(corePoolSize, maximumPoolSize, keepAliveTime, unit,
133                    workQueue);
134          }
135          CustomTPE(int corePoolSize,
# Line 154 | Line 158 | public class ThreadPoolExecutorSubclassT
158                    BlockingQueue<Runnable> workQueue,
159                    ThreadFactory threadFactory,
160                    RejectedExecutionHandler handler) {
161 <            super(corePoolSize, maximumPoolSize, keepAliveTime, unit,
161 >            super(corePoolSize, maximumPoolSize, keepAliveTime, unit,
162                workQueue, threadFactory, handler);
163          }
164  
# Line 162 | Line 166 | public class ThreadPoolExecutorSubclassT
166          volatile boolean afterCalled = false;
167          volatile boolean terminatedCalled = false;
168          public CustomTPE() {
169 <            super(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>());
169 >            super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
170          }
171          protected void beforeExecute(Thread t, Runnable r) {
172              beforeCalled = true;
# Line 173 | Line 177 | public class ThreadPoolExecutorSubclassT
177          protected void terminated() {
178              terminatedCalled = true;
179          }
180 <        
180 >
181      }
182  
183 <    static class FailingThreadFactory implements ThreadFactory{
183 >    static class FailingThreadFactory implements ThreadFactory {
184          int calls = 0;
185 <        public Thread newThread(Runnable r){
185 >        public Thread newThread(Runnable r) {
186              if (++calls > 1) return null;
187              return new Thread(r);
188 <        }  
188 >        }
189      }
190 <    
190 >
191  
192      /**
193 <     *  execute successfully executes a runnable
193 >     * execute successfully executes a runnable
194       */
195 <    public void testExecute() {
196 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
195 >    public void testExecute() throws InterruptedException {
196 >        final ThreadPoolExecutor p =
197 >            new CustomTPE(1, 1,
198 >                          LONG_DELAY_MS, MILLISECONDS,
199 >                          new ArrayBlockingQueue<Runnable>(10));
200 >        final CountDownLatch done = new CountDownLatch(1);
201 >        final Runnable task = new CheckedRunnable() {
202 >            public void realRun() {
203 >                done.countDown();
204 >            }};
205          try {
206 <            p1.execute(new Runnable() {
207 <                    public void run() {
208 <                        try {
209 <                            Thread.sleep(SHORT_DELAY_MS);
210 <                        } catch(InterruptedException e){
199 <                            threadUnexpectedException();
200 <                        }
201 <                    }
202 <                });
203 <            Thread.sleep(SMALL_DELAY_MS);
204 <        } catch(InterruptedException e){
205 <            unexpectedException();
206 <        }
207 <        joinPool(p1);
206 >            p.execute(task);
207 >            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
208 >        } finally {
209 >            joinPool(p);
210 >        }
211      }
212  
213      /**
214 <     *  getActiveCount increases but doesn't overestimate, when a
215 <     *  thread becomes active
214 >     * getActiveCount increases but doesn't overestimate, when a
215 >     * thread becomes active
216       */
217 <    public void testGetActiveCount() {
218 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
219 <        assertEquals(0, p2.getActiveCount());
220 <        p2.execute(new MediumRunnable());
221 <        try {
222 <            Thread.sleep(SHORT_DELAY_MS);
223 <        } catch(Exception e){
224 <            unexpectedException();
217 >    public void testGetActiveCount() throws InterruptedException {
218 >        final ThreadPoolExecutor p =
219 >            new CustomTPE(2, 2,
220 >                          LONG_DELAY_MS, MILLISECONDS,
221 >                          new ArrayBlockingQueue<Runnable>(10));
222 >        final CountDownLatch threadStarted = new CountDownLatch(1);
223 >        final CountDownLatch done = new CountDownLatch(1);
224 >        try {
225 >            assertEquals(0, p.getActiveCount());
226 >            p.execute(new CheckedRunnable() {
227 >                public void realRun() throws InterruptedException {
228 >                    threadStarted.countDown();
229 >                    assertEquals(1, p.getActiveCount());
230 >                    done.await();
231 >                }});
232 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
233 >            assertEquals(1, p.getActiveCount());
234 >        } finally {
235 >            done.countDown();
236 >            joinPool(p);
237          }
223        assertEquals(1, p2.getActiveCount());
224        joinPool(p2);
238      }
239  
240      /**
241 <     *  prestartCoreThread starts a thread if under corePoolSize, else doesn't
241 >     * prestartCoreThread starts a thread if under corePoolSize, else doesn't
242       */
243      public void testPrestartCoreThread() {
244 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
245 <        assertEquals(0, p2.getPoolSize());
246 <        assertTrue(p2.prestartCoreThread());
247 <        assertEquals(1, p2.getPoolSize());
248 <        assertTrue(p2.prestartCoreThread());
249 <        assertEquals(2, p2.getPoolSize());
250 <        assertFalse(p2.prestartCoreThread());
251 <        assertEquals(2, p2.getPoolSize());
252 <        joinPool(p2);
244 >        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
245 >        assertEquals(0, p.getPoolSize());
246 >        assertTrue(p.prestartCoreThread());
247 >        assertEquals(1, p.getPoolSize());
248 >        assertTrue(p.prestartCoreThread());
249 >        assertEquals(2, p.getPoolSize());
250 >        assertFalse(p.prestartCoreThread());
251 >        assertEquals(2, p.getPoolSize());
252 >        joinPool(p);
253      }
254  
255      /**
256 <     *  prestartAllCoreThreads starts all corePoolSize threads
256 >     * prestartAllCoreThreads starts all corePoolSize threads
257       */
258      public void testPrestartAllCoreThreads() {
259 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
260 <        assertEquals(0, p2.getPoolSize());
261 <        p2.prestartAllCoreThreads();
262 <        assertEquals(2, p2.getPoolSize());
263 <        p2.prestartAllCoreThreads();
264 <        assertEquals(2, p2.getPoolSize());
265 <        joinPool(p2);
253 <    }
254 <    
255 <    /**
256 <     *   getCompletedTaskCount increases, but doesn't overestimate,
257 <     *   when tasks complete
258 <     */
259 <    public void testGetCompletedTaskCount() {
260 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
261 <        assertEquals(0, p2.getCompletedTaskCount());
262 <        p2.execute(new ShortRunnable());
263 <        try {
264 <            Thread.sleep(SMALL_DELAY_MS);
265 <        } catch(Exception e){
266 <            unexpectedException();
267 <        }
268 <        assertEquals(1, p2.getCompletedTaskCount());
269 <        try { p2.shutdown(); } catch(SecurityException ok) { return; }
270 <        joinPool(p2);
259 >        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
260 >        assertEquals(0, p.getPoolSize());
261 >        p.prestartAllCoreThreads();
262 >        assertEquals(2, p.getPoolSize());
263 >        p.prestartAllCoreThreads();
264 >        assertEquals(2, p.getPoolSize());
265 >        joinPool(p);
266      }
267 <    
267 >
268 >    /**
269 >     * getCompletedTaskCount increases, but doesn't overestimate,
270 >     * when tasks complete
271 >     */
272 >    public void testGetCompletedTaskCount() throws InterruptedException {
273 >        final ThreadPoolExecutor p =
274 >            new CustomTPE(2, 2,
275 >                          LONG_DELAY_MS, MILLISECONDS,
276 >                          new ArrayBlockingQueue<Runnable>(10));
277 >        final CountDownLatch threadStarted = new CountDownLatch(1);
278 >        final CountDownLatch threadProceed = new CountDownLatch(1);
279 >        final CountDownLatch threadDone = new CountDownLatch(1);
280 >        try {
281 >            assertEquals(0, p.getCompletedTaskCount());
282 >            p.execute(new CheckedRunnable() {
283 >                public void realRun() throws InterruptedException {
284 >                    threadStarted.countDown();
285 >                    assertEquals(0, p.getCompletedTaskCount());
286 >                    threadProceed.await();
287 >                    threadDone.countDown();
288 >                }});
289 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
290 >            assertEquals(0, p.getCompletedTaskCount());
291 >            threadProceed.countDown();
292 >            threadDone.await();
293 >            delay(SHORT_DELAY_MS);
294 >            assertEquals(1, p.getCompletedTaskCount());
295 >        } finally {
296 >            joinPool(p);
297 >        }
298 >    }
299 >
300      /**
301 <     *   getCorePoolSize returns size given in constructor if not otherwise set
301 >     * getCorePoolSize returns size given in constructor if not otherwise set
302       */
303      public void testGetCorePoolSize() {
304 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
305 <        assertEquals(1, p1.getCorePoolSize());
306 <        joinPool(p1);
304 >        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
305 >        assertEquals(1, p.getCorePoolSize());
306 >        joinPool(p);
307      }
308 <    
308 >
309      /**
310 <     *   getKeepAliveTime returns value given in constructor if not otherwise set
310 >     * getKeepAliveTime returns value given in constructor if not otherwise set
311       */
312      public void testGetKeepAliveTime() {
313 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
314 <        assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS));
315 <        joinPool(p2);
313 >        ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
314 >        assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS));
315 >        joinPool(p);
316      }
317  
318  
319 <    /**
319 >    /**
320       * getThreadFactory returns factory in constructor if not set
321       */
322      public void testGetThreadFactory() {
323          ThreadFactory tf = new SimpleThreadFactory();
324 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler());
324 >        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler());
325          assertSame(tf, p.getThreadFactory());
326          joinPool(p);
327      }
328  
329 <    /**
329 >    /**
330       * setThreadFactory sets the thread factory returned by getThreadFactory
331       */
332      public void testSetThreadFactory() {
333 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
333 >        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
334          ThreadFactory tf = new SimpleThreadFactory();
335          p.setThreadFactory(tf);
336          assertSame(tf, p.getThreadFactory());
# Line 311 | Line 338 | public class ThreadPoolExecutorSubclassT
338      }
339  
340  
341 <    /**
341 >    /**
342       * setThreadFactory(null) throws NPE
343       */
344      public void testSetThreadFactoryNull() {
345 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
345 >        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
346          try {
347              p.setThreadFactory(null);
348              shouldThrow();
# Line 325 | Line 352 | public class ThreadPoolExecutorSubclassT
352          }
353      }
354  
355 <    /**
355 >    /**
356       * getRejectedExecutionHandler returns handler in constructor if not set
357       */
358      public void testGetRejectedExecutionHandler() {
359          RejectedExecutionHandler h = new NoOpREHandler();
360 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h);
360 >        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h);
361          assertSame(h, p.getRejectedExecutionHandler());
362          joinPool(p);
363      }
364  
365 <    /**
365 >    /**
366       * setRejectedExecutionHandler sets the handler returned by
367       * getRejectedExecutionHandler
368       */
369      public void testSetRejectedExecutionHandler() {
370 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
370 >        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
371          RejectedExecutionHandler h = new NoOpREHandler();
372          p.setRejectedExecutionHandler(h);
373          assertSame(h, p.getRejectedExecutionHandler());
# Line 348 | Line 375 | public class ThreadPoolExecutorSubclassT
375      }
376  
377  
378 <    /**
378 >    /**
379       * setRejectedExecutionHandler(null) throws NPE
380       */
381      public void testSetRejectedExecutionHandlerNull() {
382 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
382 >        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
383          try {
384              p.setRejectedExecutionHandler(null);
385              shouldThrow();
# Line 362 | Line 389 | public class ThreadPoolExecutorSubclassT
389          }
390      }
391  
392 <    
392 >
393      /**
394 <     *   getLargestPoolSize increases, but doesn't overestimate, when
395 <     *   multiple threads active
394 >     * getLargestPoolSize increases, but doesn't overestimate, when
395 >     * multiple threads active
396       */
397 <    public void testGetLargestPoolSize() {
398 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
399 <        try {
400 <            assertEquals(0, p2.getLargestPoolSize());
401 <            p2.execute(new MediumRunnable());
402 <            p2.execute(new MediumRunnable());
403 <            Thread.sleep(SHORT_DELAY_MS);
404 <            assertEquals(2, p2.getLargestPoolSize());
405 <        } catch(Exception e){
406 <            unexpectedException();
407 <        }
408 <        joinPool(p2);
397 >    public void testGetLargestPoolSize() throws InterruptedException {
398 >        final int THREADS = 3;
399 >        final ThreadPoolExecutor p =
400 >            new CustomTPE(THREADS, THREADS,
401 >                          LONG_DELAY_MS, MILLISECONDS,
402 >                          new ArrayBlockingQueue<Runnable>(10));
403 >        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
404 >        final CountDownLatch done = new CountDownLatch(1);
405 >        try {
406 >            assertEquals(0, p.getLargestPoolSize());
407 >            for (int i = 0; i < THREADS; i++)
408 >                p.execute(new CheckedRunnable() {
409 >                    public void realRun() throws InterruptedException {
410 >                        threadsStarted.countDown();
411 >                        done.await();
412 >                        assertEquals(THREADS, p.getLargestPoolSize());
413 >                    }});
414 >            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
415 >            assertEquals(THREADS, p.getLargestPoolSize());
416 >        } finally {
417 >            done.countDown();
418 >            joinPool(p);
419 >            assertEquals(THREADS, p.getLargestPoolSize());
420 >        }
421      }
422 <    
422 >
423      /**
424 <     *   getMaximumPoolSize returns value given in constructor if not
425 <     *   otherwise set
424 >     * getMaximumPoolSize returns value given in constructor if not
425 >     * otherwise set
426       */
427      public void testGetMaximumPoolSize() {
428 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
429 <        assertEquals(2, p2.getMaximumPoolSize());
430 <        joinPool(p2);
428 >        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
429 >        assertEquals(2, p.getMaximumPoolSize());
430 >        joinPool(p);
431      }
432 <    
432 >
433      /**
434 <     *   getPoolSize increases, but doesn't overestimate, when threads
435 <     *   become active
434 >     * getPoolSize increases, but doesn't overestimate, when threads
435 >     * become active
436       */
437 <    public void testGetPoolSize() {
438 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
439 <        assertEquals(0, p1.getPoolSize());
440 <        p1.execute(new MediumRunnable());
441 <        assertEquals(1, p1.getPoolSize());
442 <        joinPool(p1);
437 >    public void testGetPoolSize() throws InterruptedException {
438 >        final ThreadPoolExecutor p =
439 >            new CustomTPE(1, 1,
440 >                          LONG_DELAY_MS, MILLISECONDS,
441 >                          new ArrayBlockingQueue<Runnable>(10));
442 >        final CountDownLatch threadStarted = new CountDownLatch(1);
443 >        final CountDownLatch done = new CountDownLatch(1);
444 >        try {
445 >            assertEquals(0, p.getPoolSize());
446 >            p.execute(new CheckedRunnable() {
447 >                public void realRun() throws InterruptedException {
448 >                    threadStarted.countDown();
449 >                    assertEquals(1, p.getPoolSize());
450 >                    done.await();
451 >                }});
452 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
453 >            assertEquals(1, p.getPoolSize());
454 >        } finally {
455 >            done.countDown();
456 >            joinPool(p);
457 >        }
458      }
459 <    
459 >
460      /**
461 <     *  getTaskCount increases, but doesn't overestimate, when tasks submitted
461 >     * getTaskCount increases, but doesn't overestimate, when tasks submitted
462       */
463 <    public void testGetTaskCount() {
464 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
465 <        try {
466 <            assertEquals(0, p1.getTaskCount());
467 <            p1.execute(new MediumRunnable());
468 <            Thread.sleep(SHORT_DELAY_MS);
469 <            assertEquals(1, p1.getTaskCount());
470 <        } catch(Exception e){
471 <            unexpectedException();
472 <        }
473 <        joinPool(p1);
463 >    public void testGetTaskCount() throws InterruptedException {
464 >        final ThreadPoolExecutor p =
465 >            new CustomTPE(1, 1,
466 >                          LONG_DELAY_MS, MILLISECONDS,
467 >                          new ArrayBlockingQueue<Runnable>(10));
468 >        final CountDownLatch threadStarted = new CountDownLatch(1);
469 >        final CountDownLatch done = new CountDownLatch(1);
470 >        try {
471 >            assertEquals(0, p.getTaskCount());
472 >            p.execute(new CheckedRunnable() {
473 >                public void realRun() throws InterruptedException {
474 >                    threadStarted.countDown();
475 >                    assertEquals(1, p.getTaskCount());
476 >                    done.await();
477 >                }});
478 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
479 >            assertEquals(1, p.getTaskCount());
480 >        } finally {
481 >            done.countDown();
482 >            joinPool(p);
483 >        }
484      }
485 <    
485 >
486      /**
487 <     *   isShutDown is false before shutdown, true after
487 >     * isShutDown is false before shutdown, true after
488       */
489      public void testIsShutdown() {
490 <        
491 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
492 <        assertFalse(p1.isShutdown());
493 <        try { p1.shutdown(); } catch(SecurityException ok) { return; }
494 <        assertTrue(p1.isShutdown());
495 <        joinPool(p1);
490 >
491 >        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
492 >        assertFalse(p.isShutdown());
493 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
494 >        assertTrue(p.isShutdown());
495 >        joinPool(p);
496      }
497  
498 <        
498 >
499      /**
500 <     *  isTerminated is false before termination, true after
500 >     * isTerminated is false before termination, true after
501       */
502 <    public void testIsTerminated() {
503 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
504 <        assertFalse(p1.isTerminated());
505 <        try {
506 <            p1.execute(new MediumRunnable());
507 <        } finally {
508 <            try { p1.shutdown(); } catch(SecurityException ok) { return; }
509 <        }
510 <        try {
511 <            assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
512 <            assertTrue(p1.isTerminated());
513 <        } catch(Exception e){
514 <            unexpectedException();
515 <        }      
502 >    public void testIsTerminated() throws InterruptedException {
503 >        final ThreadPoolExecutor p =
504 >            new CustomTPE(1, 1,
505 >                          LONG_DELAY_MS, MILLISECONDS,
506 >                          new ArrayBlockingQueue<Runnable>(10));
507 >        final CountDownLatch threadStarted = new CountDownLatch(1);
508 >        final CountDownLatch done = new CountDownLatch(1);
509 >        try {
510 >            assertFalse(p.isTerminating());
511 >            p.execute(new CheckedRunnable() {
512 >                public void realRun() throws InterruptedException {
513 >                    assertFalse(p.isTerminating());
514 >                    threadStarted.countDown();
515 >                    done.await();
516 >                }});
517 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
518 >            assertFalse(p.isTerminating());
519 >            done.countDown();
520 >        } finally {
521 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
522 >        }
523 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
524 >        assertTrue(p.isTerminated());
525 >        assertFalse(p.isTerminating());
526      }
527  
528      /**
529 <     *  isTerminating is not true when running or when terminated
530 <     */
531 <    public void testIsTerminating() {
532 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
533 <        assertFalse(p1.isTerminating());
534 <        try {
535 <            p1.execute(new SmallRunnable());
536 <            assertFalse(p1.isTerminating());
537 <        } finally {
538 <            try { p1.shutdown(); } catch(SecurityException ok) { return; }
539 <        }
540 <        try {
541 <            assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
542 <            assertTrue(p1.isTerminated());
543 <            assertFalse(p1.isTerminating());
544 <        } catch(Exception e){
545 <            unexpectedException();
546 <        }      
529 >     * isTerminating is not true when running or when terminated
530 >     */
531 >    public void testIsTerminating() throws InterruptedException {
532 >        final ThreadPoolExecutor p =
533 >            new CustomTPE(1, 1,
534 >                          LONG_DELAY_MS, MILLISECONDS,
535 >                          new ArrayBlockingQueue<Runnable>(10));
536 >        final CountDownLatch threadStarted = new CountDownLatch(1);
537 >        final CountDownLatch done = new CountDownLatch(1);
538 >        try {
539 >            assertFalse(p.isTerminating());
540 >            p.execute(new CheckedRunnable() {
541 >                public void realRun() throws InterruptedException {
542 >                    assertFalse(p.isTerminating());
543 >                    threadStarted.countDown();
544 >                    done.await();
545 >                }});
546 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
547 >            assertFalse(p.isTerminating());
548 >            done.countDown();
549 >        } finally {
550 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
551 >        }
552 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
553 >        assertTrue(p.isTerminated());
554 >        assertFalse(p.isTerminating());
555      }
556  
557      /**
558       * getQueue returns the work queue, which contains queued tasks
559       */
560 <    public void testGetQueue() {
561 <        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
562 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q);
563 <        FutureTask[] tasks = new FutureTask[5];
564 <        for(int i = 0; i < 5; i++){
565 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
566 <            p1.execute(tasks[i]);
567 <        }
568 <        try {
569 <            Thread.sleep(SHORT_DELAY_MS);
570 <            BlockingQueue<Runnable> wq = p1.getQueue();
571 <            assertSame(q, wq);
572 <            assertFalse(wq.contains(tasks[0]));
573 <            assertTrue(wq.contains(tasks[4]));
574 <            for (int i = 1; i < 5; ++i)
575 <                tasks[i].cancel(true);
576 <            p1.shutdownNow();
577 <        } catch(Exception e) {
578 <            unexpectedException();
560 >    public void testGetQueue() throws InterruptedException {
561 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
562 >        final ThreadPoolExecutor p =
563 >            new CustomTPE(1, 1,
564 >                          LONG_DELAY_MS, MILLISECONDS,
565 >                          q);
566 >        final CountDownLatch threadStarted = new CountDownLatch(1);
567 >        final CountDownLatch done = new CountDownLatch(1);
568 >        try {
569 >            FutureTask[] tasks = new FutureTask[5];
570 >            for (int i = 0; i < tasks.length; i++) {
571 >                Callable task = new CheckedCallable<Boolean>() {
572 >                    public Boolean realCall() throws InterruptedException {
573 >                        threadStarted.countDown();
574 >                        assertSame(q, p.getQueue());
575 >                        done.await();
576 >                        return Boolean.TRUE;
577 >                    }};
578 >                tasks[i] = new FutureTask(task);
579 >                p.execute(tasks[i]);
580 >            }
581 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
582 >            assertSame(q, p.getQueue());
583 >            assertFalse(q.contains(tasks[0]));
584 >            assertTrue(q.contains(tasks[tasks.length - 1]));
585 >            assertEquals(tasks.length - 1, q.size());
586          } finally {
587 <            joinPool(p1);
587 >            done.countDown();
588 >            joinPool(p);
589          }
590      }
591  
592      /**
593       * remove(task) removes queued task, and fails to remove active task
594       */
595 <    public void testRemove() {
595 >    public void testRemove() throws InterruptedException {
596          BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
597 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q);
598 <        FutureTask[] tasks = new FutureTask[5];
599 <        for(int i = 0; i < 5; i++){
600 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
601 <            p1.execute(tasks[i]);
602 <        }
603 <        try {
604 <            Thread.sleep(SHORT_DELAY_MS);
605 <            assertFalse(p1.remove(tasks[0]));
597 >        final ThreadPoolExecutor p =
598 >            new CustomTPE(1, 1,
599 >                          LONG_DELAY_MS, MILLISECONDS,
600 >                          q);
601 >        Runnable[] tasks = new Runnable[6];
602 >        final CountDownLatch threadStarted = new CountDownLatch(1);
603 >        final CountDownLatch done = new CountDownLatch(1);
604 >        try {
605 >            for (int i = 0; i < tasks.length; i++) {
606 >                tasks[i] = new CheckedRunnable() {
607 >                        public void realRun() throws InterruptedException {
608 >                            threadStarted.countDown();
609 >                            done.await();
610 >                        }};
611 >                p.execute(tasks[i]);
612 >            }
613 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
614 >            assertFalse(p.remove(tasks[0]));
615              assertTrue(q.contains(tasks[4]));
616              assertTrue(q.contains(tasks[3]));
617 <            assertTrue(p1.remove(tasks[4]));
618 <            assertFalse(p1.remove(tasks[4]));
617 >            assertTrue(p.remove(tasks[4]));
618 >            assertFalse(p.remove(tasks[4]));
619              assertFalse(q.contains(tasks[4]));
620              assertTrue(q.contains(tasks[3]));
621 <            assertTrue(p1.remove(tasks[3]));
621 >            assertTrue(p.remove(tasks[3]));
622              assertFalse(q.contains(tasks[3]));
524        } catch(Exception e) {
525            unexpectedException();
623          } finally {
624 <            joinPool(p1);
624 >            done.countDown();
625 >            joinPool(p);
626          }
627      }
628  
629      /**
630 <     *   purge removes cancelled tasks from the queue
630 >     * purge removes cancelled tasks from the queue
631       */
632 <    public void testPurge() {
633 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
632 >    public void testPurge() throws InterruptedException {
633 >        final CountDownLatch threadStarted = new CountDownLatch(1);
634 >        final CountDownLatch done = new CountDownLatch(1);
635 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
636 >        final ThreadPoolExecutor p =
637 >            new CustomTPE(1, 1,
638 >                          LONG_DELAY_MS, MILLISECONDS,
639 >                          q);
640          FutureTask[] tasks = new FutureTask[5];
641 <        for(int i = 0; i < 5; i++){
642 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
643 <            p1.execute(tasks[i]);
641 >        try {
642 >            for (int i = 0; i < tasks.length; i++) {
643 >                Callable task = new CheckedCallable<Boolean>() {
644 >                    public Boolean realCall() throws InterruptedException {
645 >                        threadStarted.countDown();
646 >                        done.await();
647 >                        return Boolean.TRUE;
648 >                    }};
649 >                tasks[i] = new FutureTask(task);
650 >                p.execute(tasks[i]);
651 >            }
652 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
653 >            assertEquals(tasks.length, p.getTaskCount());
654 >            assertEquals(tasks.length - 1, q.size());
655 >            assertEquals(1L, p.getActiveCount());
656 >            assertEquals(0L, p.getCompletedTaskCount());
657 >            tasks[4].cancel(true);
658 >            tasks[3].cancel(false);
659 >            p.purge();
660 >            assertEquals(tasks.length - 3, q.size());
661 >            assertEquals(tasks.length - 2, p.getTaskCount());
662 >            p.purge();         // Nothing to do
663 >            assertEquals(tasks.length - 3, q.size());
664 >            assertEquals(tasks.length - 2, p.getTaskCount());
665 >        } finally {
666 >            done.countDown();
667 >            joinPool(p);
668          }
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);
669      }
670  
671      /**
672 <     *  shutDownNow returns a list containing tasks that were not run
672 >     * shutDownNow returns a list containing tasks that were not run
673       */
674 <    public void testShutDownNow() {
675 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
674 >    public void testShutdownNow() {
675 >        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
676          List l;
677          try {
678 <            for(int i = 0; i < 5; i++)
679 <                p1.execute(new MediumPossiblyInterruptedRunnable());
678 >            for (int i = 0; i < 5; i++)
679 >                p.execute(new MediumPossiblyInterruptedRunnable());
680          }
681          finally {
682              try {
683 <                l = p1.shutdownNow();
683 >                l = p.shutdownNow();
684              } catch (SecurityException ok) { return; }
563            
685          }
686 <        assertTrue(p1.isShutdown());
687 <        assertTrue(l.size() <= 4);
686 >        assertTrue(p.isShutdown());
687 >        assertTrue(l.size() <= 4);
688      }
689  
690      // Exception Tests
570    
691  
692 <    /**
693 <     * Constructor throws if corePoolSize argument is less than zero
692 >
693 >    /**
694 >     * Constructor throws if corePoolSize argument is less than zero
695       */
696      public void testConstructor1() {
697          try {
698 <            new CustomTPE(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
698 >            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
699              shouldThrow();
700 <        }
580 <        catch (IllegalArgumentException success){}
700 >        } catch (IllegalArgumentException success) {}
701      }
702 <    
703 <    /**
704 <     * Constructor throws if maximumPoolSize is less than zero
702 >
703 >    /**
704 >     * Constructor throws if maximumPoolSize is less than zero
705       */
706      public void testConstructor2() {
707          try {
708 <            new CustomTPE(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
708 >            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
709              shouldThrow();
710 <        }
591 <        catch (IllegalArgumentException success){}
710 >        } catch (IllegalArgumentException success) {}
711      }
712 <    
713 <    /**
714 <     * Constructor throws if maximumPoolSize is equal to zero
712 >
713 >    /**
714 >     * Constructor throws if maximumPoolSize is equal to zero
715       */
716      public void testConstructor3() {
717          try {
718 <            new CustomTPE(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
718 >            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
719              shouldThrow();
720 <        }
602 <        catch (IllegalArgumentException success){}
720 >        } catch (IllegalArgumentException success) {}
721      }
722  
723 <    /**
724 <     * Constructor throws if keepAliveTime is less than zero
723 >    /**
724 >     * Constructor throws if keepAliveTime is less than zero
725       */
726      public void testConstructor4() {
727          try {
728 <            new CustomTPE(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
728 >            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
729              shouldThrow();
730 <        }
613 <        catch (IllegalArgumentException success){}
730 >        } catch (IllegalArgumentException success) {}
731      }
732  
733 <    /**
734 <     * Constructor throws if corePoolSize is greater than the maximumPoolSize
733 >    /**
734 >     * Constructor throws if corePoolSize is greater than the maximumPoolSize
735       */
736      public void testConstructor5() {
737          try {
738 <            new CustomTPE(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
738 >            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
739              shouldThrow();
740 <        }
624 <        catch (IllegalArgumentException success){}
740 >        } catch (IllegalArgumentException success) {}
741      }
742 <        
743 <    /**
744 <     * Constructor throws if workQueue is set to null
742 >
743 >    /**
744 >     * Constructor throws if workQueue is set to null
745       */
746      public void testConstructorNullPointerException() {
747          try {
748 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null);
748 >            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null);
749              shouldThrow();
750 <        }
635 <        catch (NullPointerException success){}  
750 >        } catch (NullPointerException success) {}
751      }
637    
752  
753 <    
754 <    /**
755 <     * Constructor throws if corePoolSize argument is less than zero
753 >
754 >
755 >    /**
756 >     * Constructor throws if corePoolSize argument is less than zero
757       */
758      public void testConstructor6() {
759          try {
760 <            new CustomTPE(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
760 >            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
761              shouldThrow();
762 <        } catch (IllegalArgumentException success){}
762 >        } catch (IllegalArgumentException success) {}
763      }
764 <    
765 <    /**
766 <     * Constructor throws if maximumPoolSize is less than zero
764 >
765 >    /**
766 >     * Constructor throws if maximumPoolSize is less than zero
767       */
768      public void testConstructor7() {
769          try {
770 <            new CustomTPE(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
770 >            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
771              shouldThrow();
772 <        }
658 <        catch (IllegalArgumentException success){}
772 >        } catch (IllegalArgumentException success) {}
773      }
774  
775 <    /**
776 <     * Constructor throws if maximumPoolSize is equal to zero
775 >    /**
776 >     * Constructor throws if maximumPoolSize is equal to zero
777       */
778      public void testConstructor8() {
779          try {
780 <            new CustomTPE(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
780 >            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
781              shouldThrow();
782 <        }
669 <        catch (IllegalArgumentException success){}
782 >        } catch (IllegalArgumentException success) {}
783      }
784  
785 <    /**
786 <     * Constructor throws if keepAliveTime is less than zero
785 >    /**
786 >     * Constructor throws if keepAliveTime is less than zero
787       */
788      public void testConstructor9() {
789          try {
790 <            new CustomTPE(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
790 >            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
791              shouldThrow();
792 <        }
680 <        catch (IllegalArgumentException success){}
792 >        } catch (IllegalArgumentException success) {}
793      }
794  
795 <    /**
796 <     * Constructor throws if corePoolSize is greater than the maximumPoolSize
795 >    /**
796 >     * Constructor throws if corePoolSize is greater than the maximumPoolSize
797       */
798      public void testConstructor10() {
799          try {
800 <            new CustomTPE(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
800 >            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
801              shouldThrow();
802 <        }
691 <        catch (IllegalArgumentException success){}
802 >        } catch (IllegalArgumentException success) {}
803      }
804  
805 <    /**
806 <     * Constructor throws if workQueue is set to null
805 >    /**
806 >     * Constructor throws if workQueue is set to null
807       */
808      public void testConstructorNullPointerException2() {
809          try {
810 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory());
810 >            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
811              shouldThrow();
812 <        }
702 <        catch (NullPointerException success){}  
812 >        } catch (NullPointerException success) {}
813      }
814  
815 <    /**
816 <     * Constructor throws if threadFactory is set to null
815 >    /**
816 >     * Constructor throws if threadFactory is set to null
817       */
818      public void testConstructorNullPointerException3() {
819          try {
820              ThreadFactory f = null;
821 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
821 >            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
822              shouldThrow();
823 <        }
714 <        catch (NullPointerException success){}  
823 >        } catch (NullPointerException success) {}
824      }
825 <
826 <    
827 <    /**
828 <     * Constructor throws if corePoolSize argument is less than zero
825 >
826 >
827 >    /**
828 >     * Constructor throws if corePoolSize argument is less than zero
829       */
830      public void testConstructor11() {
831          try {
832 <            new CustomTPE(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
832 >            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
833              shouldThrow();
834 <        }
726 <        catch (IllegalArgumentException success){}
834 >        } catch (IllegalArgumentException success) {}
835      }
836  
837 <    /**
838 <     * Constructor throws if maximumPoolSize is less than zero
837 >    /**
838 >     * Constructor throws if maximumPoolSize is less than zero
839       */
840      public void testConstructor12() {
841          try {
842 <            new CustomTPE(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
842 >            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
843              shouldThrow();
844 <        }
737 <        catch (IllegalArgumentException success){}
844 >        } catch (IllegalArgumentException success) {}
845      }
846  
847 <    /**
848 <     * Constructor throws if maximumPoolSize is equal to zero
847 >    /**
848 >     * Constructor throws if maximumPoolSize is equal to zero
849       */
850      public void testConstructor13() {
851          try {
852 <            new CustomTPE(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
852 >            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
853              shouldThrow();
854 <        }
748 <        catch (IllegalArgumentException success){}
854 >        } catch (IllegalArgumentException success) {}
855      }
856  
857 <    /**
858 <     * Constructor throws if keepAliveTime is less than zero
857 >    /**
858 >     * Constructor throws if keepAliveTime is less than zero
859       */
860      public void testConstructor14() {
861          try {
862 <            new CustomTPE(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
862 >            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
863              shouldThrow();
864 <        }
759 <        catch (IllegalArgumentException success){}
864 >        } catch (IllegalArgumentException success) {}
865      }
866  
867 <    /**
868 <     * Constructor throws if corePoolSize is greater than the maximumPoolSize
867 >    /**
868 >     * Constructor throws if corePoolSize is greater than the maximumPoolSize
869       */
870      public void testConstructor15() {
871          try {
872 <            new CustomTPE(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
872 >            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
873              shouldThrow();
874 <        }
770 <        catch (IllegalArgumentException success){}
874 >        } catch (IllegalArgumentException success) {}
875      }
876  
877 <    /**
878 <     * Constructor throws if workQueue is set to null
877 >    /**
878 >     * Constructor throws if workQueue is set to null
879       */
880      public void testConstructorNullPointerException4() {
881          try {
882 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new NoOpREHandler());
882 >            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
883              shouldThrow();
884 <        }
781 <        catch (NullPointerException success){}  
884 >        } catch (NullPointerException success) {}
885      }
886  
887 <    /**
888 <     * Constructor throws if handler is set to null
887 >    /**
888 >     * Constructor throws if handler is set to null
889       */
890      public void testConstructorNullPointerException5() {
891          try {
892              RejectedExecutionHandler r = null;
893 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
893 >            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
894              shouldThrow();
895 <        }
793 <        catch (NullPointerException success){}  
895 >        } catch (NullPointerException success) {}
896      }
897  
898 <    
899 <    /**
900 <     * Constructor throws if corePoolSize argument is less than zero
898 >
899 >    /**
900 >     * Constructor throws if corePoolSize argument is less than zero
901       */
902      public void testConstructor16() {
903          try {
904 <            new CustomTPE(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
904 >            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
905              shouldThrow();
906 <        }
805 <        catch (IllegalArgumentException success){}
906 >        } catch (IllegalArgumentException success) {}
907      }
908  
909 <    /**
910 <     * Constructor throws if maximumPoolSize is less than zero
909 >    /**
910 >     * Constructor throws if maximumPoolSize is less than zero
911       */
912      public void testConstructor17() {
913          try {
914 <            new CustomTPE(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
914 >            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
915              shouldThrow();
916 <        }
816 <        catch (IllegalArgumentException success){}
916 >        } catch (IllegalArgumentException success) {}
917      }
918  
919 <    /**
920 <     * Constructor throws if maximumPoolSize is equal to zero
919 >    /**
920 >     * Constructor throws if maximumPoolSize is equal to zero
921       */
922      public void testConstructor18() {
923          try {
924 <            new CustomTPE(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
924 >            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
925              shouldThrow();
926 <        }
827 <        catch (IllegalArgumentException success){}
926 >        } catch (IllegalArgumentException success) {}
927      }
928  
929 <    /**
930 <     * Constructor throws if keepAliveTime is less than zero
929 >    /**
930 >     * Constructor throws if keepAliveTime is less than zero
931       */
932      public void testConstructor19() {
933          try {
934 <            new CustomTPE(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
934 >            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
935              shouldThrow();
936 <        }
838 <        catch (IllegalArgumentException success){}
936 >        } catch (IllegalArgumentException success) {}
937      }
938  
939 <    /**
940 <     * Constructor throws if corePoolSize is greater than the maximumPoolSize
939 >    /**
940 >     * Constructor throws if corePoolSize is greater than the maximumPoolSize
941       */
942      public void testConstructor20() {
943          try {
944 <            new CustomTPE(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
944 >            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
945              shouldThrow();
946 <        }
849 <        catch (IllegalArgumentException success){}
946 >        } catch (IllegalArgumentException success) {}
947      }
948  
949 <    /**
950 <     * Constructor throws if workQueue is set to null
949 >    /**
950 >     * Constructor throws if workQueue is null
951       */
952      public void testConstructorNullPointerException6() {
953          try {
954 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
954 >            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
955              shouldThrow();
956 <        }
860 <        catch (NullPointerException success){}  
956 >        } catch (NullPointerException success) {}
957      }
958  
959 <    /**
960 <     * Constructor throws if handler is set to null
959 >    /**
960 >     * Constructor throws if handler is null
961       */
962      public void testConstructorNullPointerException7() {
963          try {
964              RejectedExecutionHandler r = null;
965 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
965 >            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
966              shouldThrow();
967 <        }
872 <        catch (NullPointerException success){}  
967 >        } catch (NullPointerException success) {}
968      }
969  
970 <    /**
971 <     * Constructor throws if ThreadFactory is set top null
970 >    /**
971 >     * Constructor throws if ThreadFactory is null
972       */
973      public void testConstructorNullPointerException8() {
974          try {
975 <            ThreadFactory f = null;
976 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
975 >            new CustomTPE(1, 2,
976 >                          LONG_DELAY_MS, MILLISECONDS,
977 >                          new ArrayBlockingQueue<Runnable>(10),
978 >                          (ThreadFactory) null,
979 >                          new NoOpREHandler());
980              shouldThrow();
981 <        }
884 <        catch (NullPointerException successdn8){}  
981 >        } catch (NullPointerException success) {}
982      }
983 <    
983 >
984  
985      /**
986 <     *  execute throws RejectedExecutionException
890 <     *  if saturated.
986 >     * execute throws RejectedExecutionException if saturated.
987       */
988      public void testSaturatedExecute() {
989 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
990 <        try {
991 <            
992 <            for(int i = 0; i < 5; ++i){
993 <                p.execute(new MediumRunnable());
989 >        ThreadPoolExecutor p =
990 >            new CustomTPE(1, 1,
991 >                          LONG_DELAY_MS, MILLISECONDS,
992 >                          new ArrayBlockingQueue<Runnable>(1));
993 >        final CountDownLatch done = new CountDownLatch(1);
994 >        try {
995 >            Runnable task = new CheckedRunnable() {
996 >                public void realRun() throws InterruptedException {
997 >                    done.await();
998 >                }};
999 >            for (int i = 0; i < 2; ++i)
1000 >                p.execute(task);
1001 >            for (int i = 0; i < 2; ++i) {
1002 >                try {
1003 >                    p.execute(task);
1004 >                    shouldThrow();
1005 >                } catch (RejectedExecutionException success) {}
1006 >                assertTrue(p.getTaskCount() <= 2);
1007              }
1008 <            shouldThrow();
1009 <        } catch(RejectedExecutionException success){}
1010 <        joinPool(p);
1008 >        } finally {
1009 >            done.countDown();
1010 >            joinPool(p);
1011 >        }
1012      }
1013  
1014      /**
1015 <     *  executor using CallerRunsPolicy runs task if saturated.
1015 >     * executor using CallerRunsPolicy runs task if saturated.
1016       */
1017      public void testSaturatedExecute2() {
1018          RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1019 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1019 >        ThreadPoolExecutor p = new CustomTPE(1, 1,
1020 >                                             LONG_DELAY_MS, MILLISECONDS,
1021 >                                             new ArrayBlockingQueue<Runnable>(1),
1022 >                                             h);
1023          try {
911            
1024              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1025 <            for(int i = 0; i < 5; ++i){
1025 >            for (int i = 0; i < tasks.length; ++i)
1026                  tasks[i] = new TrackedNoOpRunnable();
915            }
1027              TrackedLongRunnable mr = new TrackedLongRunnable();
1028              p.execute(mr);
1029 <            for(int i = 0; i < 5; ++i){
1029 >            for (int i = 0; i < tasks.length; ++i)
1030                  p.execute(tasks[i]);
1031 <            }
921 <            for(int i = 1; i < 5; ++i) {
1031 >            for (int i = 1; i < tasks.length; ++i)
1032                  assertTrue(tasks[i].done);
1033 <            }
924 <            try { p.shutdownNow(); } catch(SecurityException ok) { return; }
925 <        } catch(RejectedExecutionException ex){
926 <            unexpectedException();
1033 >            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1034          } finally {
1035              joinPool(p);
1036          }
1037      }
1038  
1039      /**
1040 <     *  executor using DiscardPolicy drops task if saturated.
1040 >     * executor using DiscardPolicy drops task if saturated.
1041       */
1042      public void testSaturatedExecute3() {
1043          RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1044 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1044 >        ThreadPoolExecutor p =
1045 >            new CustomTPE(1, 1,
1046 >                          LONG_DELAY_MS, MILLISECONDS,
1047 >                          new ArrayBlockingQueue<Runnable>(1),
1048 >                          h);
1049          try {
939            
1050              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1051 <            for(int i = 0; i < 5; ++i){
1051 >            for (int i = 0; i < tasks.length; ++i)
1052                  tasks[i] = new TrackedNoOpRunnable();
943            }
1053              p.execute(new TrackedLongRunnable());
1054 <            for(int i = 0; i < 5; ++i){
1055 <                p.execute(tasks[i]);
1056 <            }
1057 <            for(int i = 0; i < 5; ++i){
1058 <                assertFalse(tasks[i].done);
950 <            }
951 <            try { p.shutdownNow(); } catch(SecurityException ok) { return; }
952 <        } catch(RejectedExecutionException ex){
953 <            unexpectedException();
1054 >            for (TrackedNoOpRunnable task : tasks)
1055 >                p.execute(task);
1056 >            for (TrackedNoOpRunnable task : tasks)
1057 >                assertFalse(task.done);
1058 >            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1059          } finally {
1060              joinPool(p);
1061          }
1062      }
1063  
1064      /**
1065 <     *  executor using DiscardOldestPolicy drops oldest task if saturated.
1065 >     * executor using DiscardOldestPolicy drops oldest task if saturated.
1066       */
1067      public void testSaturatedExecute4() {
1068          RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
1069 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1069 >        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1070          try {
1071              p.execute(new TrackedLongRunnable());
1072              TrackedLongRunnable r2 = new TrackedLongRunnable();
# Line 971 | Line 1076 | public class ThreadPoolExecutorSubclassT
1076              p.execute(r3);
1077              assertFalse(p.getQueue().contains(r2));
1078              assertTrue(p.getQueue().contains(r3));
1079 <            try { p.shutdownNow(); } catch(SecurityException ok) { return; }
975 <        } catch(RejectedExecutionException ex){
976 <            unexpectedException();
1079 >            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1080          } finally {
1081              joinPool(p);
1082          }
1083      }
1084  
1085      /**
1086 <     *  execute throws RejectedExecutionException if shutdown
1086 >     * execute throws RejectedExecutionException if shutdown
1087       */
1088      public void testRejectedExecutionExceptionOnShutdown() {
1089 <        ThreadPoolExecutor tpe =
1090 <            new CustomTPE(1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
1091 <        try { tpe.shutdown(); } catch(SecurityException ok) { return; }
1092 <        try {
1093 <            tpe.execute(new NoOpRunnable());
1094 <            shouldThrow();
1095 <        } catch(RejectedExecutionException success){}
1096 <        
1097 <        joinPool(tpe);
1089 >        ThreadPoolExecutor p =
1090 >            new CustomTPE(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
1091 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1092 >        try {
1093 >            p.execute(new NoOpRunnable());
1094 >            shouldThrow();
1095 >        } catch (RejectedExecutionException success) {}
1096 >
1097 >        joinPool(p);
1098      }
1099  
1100      /**
1101 <     *  execute using CallerRunsPolicy drops task on shutdown
1101 >     * execute using CallerRunsPolicy drops task on shutdown
1102       */
1103      public void testCallerRunsOnShutdown() {
1104          RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1105 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1105 >        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1106  
1107 <        try { p.shutdown(); } catch(SecurityException ok) { return; }
1108 <        try {
1107 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1108 >        try {
1109              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1110 <            p.execute(r);
1110 >            p.execute(r);
1111              assertFalse(r.done);
1009        } catch(RejectedExecutionException success){
1010            unexpectedException();
1112          } finally {
1113              joinPool(p);
1114          }
1115      }
1116  
1117      /**
1118 <     *  execute using DiscardPolicy drops task on shutdown
1118 >     * execute using DiscardPolicy drops task on shutdown
1119       */
1120      public void testDiscardOnShutdown() {
1121          RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1122 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1122 >        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1123  
1124 <        try { p.shutdown(); } catch(SecurityException ok) { return; }
1125 <        try {
1124 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1125 >        try {
1126              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1127 <            p.execute(r);
1127 >            p.execute(r);
1128              assertFalse(r.done);
1028        } catch(RejectedExecutionException success){
1029            unexpectedException();
1129          } finally {
1130              joinPool(p);
1131          }
# Line 1034 | Line 1133 | public class ThreadPoolExecutorSubclassT
1133  
1134  
1135      /**
1136 <     *  execute using DiscardOldestPolicy drops task on shutdown
1136 >     * execute using DiscardOldestPolicy drops task on shutdown
1137       */
1138      public void testDiscardOldestOnShutdown() {
1139          RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
1140 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1140 >        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1141  
1142 <        try { p.shutdown(); } catch(SecurityException ok) { return; }
1143 <        try {
1142 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1143 >        try {
1144              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1145 <            p.execute(r);
1145 >            p.execute(r);
1146              assertFalse(r.done);
1048        } catch(RejectedExecutionException success){
1049            unexpectedException();
1147          } finally {
1148              joinPool(p);
1149          }
# Line 1054 | Line 1151 | public class ThreadPoolExecutorSubclassT
1151  
1152  
1153      /**
1154 <     *  execute (null) throws NPE
1154 >     * execute(null) throws NPE
1155       */
1156      public void testExecuteNull() {
1157 <        ThreadPoolExecutor tpe = null;
1157 >        ThreadPoolExecutor p = null;
1158          try {
1159 <            tpe = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1160 <            tpe.execute(null);
1159 >            p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1160 >            p.execute(null);
1161              shouldThrow();
1162 <        } catch(NullPointerException success){}
1163 <        
1164 <        joinPool(tpe);
1162 >        } catch (NullPointerException success) {}
1163 >
1164 >        joinPool(p);
1165      }
1166 <    
1166 >
1167      /**
1168 <     *  setCorePoolSize of negative value throws IllegalArgumentException
1168 >     * setCorePoolSize of negative value throws IllegalArgumentException
1169       */
1170      public void testCorePoolSizeIllegalArgumentException() {
1171 <        ThreadPoolExecutor tpe = null;
1172 <        try {
1076 <            tpe = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1077 <        } catch(Exception e){}
1078 <        try {
1079 <            tpe.setCorePoolSize(-1);
1080 <            shouldThrow();
1081 <        } catch(IllegalArgumentException success){
1082 <        } finally {
1083 <            try { tpe.shutdown(); } catch(SecurityException ok) { return; }
1084 <        }
1085 <        joinPool(tpe);
1086 <    }  
1087 <
1088 <    /**
1089 <     *  setMaximumPoolSize(int) throws IllegalArgumentException if
1090 <     *  given a value less the core pool size
1091 <     */  
1092 <    public void testMaximumPoolSizeIllegalArgumentException() {
1093 <        ThreadPoolExecutor tpe = null;
1171 >        ThreadPoolExecutor p =
1172 >            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1173          try {
1174 <            tpe = new CustomTPE(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1175 <        } catch(Exception e){}
1174 >            p.setCorePoolSize(-1);
1175 >            shouldThrow();
1176 >        } catch (IllegalArgumentException success) {
1177 >        } finally {
1178 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1179 >        }
1180 >        joinPool(p);
1181 >    }
1182 >
1183 >    /**
1184 >     * setMaximumPoolSize(int) throws IllegalArgumentException
1185 >     * if given a value less the core pool size
1186 >     */
1187 >    public void testMaximumPoolSizeIllegalArgumentException() {
1188 >        ThreadPoolExecutor p =
1189 >            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1190          try {
1191 <            tpe.setMaximumPoolSize(1);
1191 >            p.setMaximumPoolSize(1);
1192              shouldThrow();
1193 <        } catch(IllegalArgumentException success){
1193 >        } catch (IllegalArgumentException success) {
1194          } finally {
1195 <            try { tpe.shutdown(); } catch(SecurityException ok) { return; }
1195 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1196          }
1197 <        joinPool(tpe);
1197 >        joinPool(p);
1198      }
1199 <    
1199 >
1200      /**
1201 <     *  setMaximumPoolSize throws IllegalArgumentException
1202 <     *  if given a negative value
1201 >     * setMaximumPoolSize throws IllegalArgumentException
1202 >     * if given a negative value
1203       */
1204      public void testMaximumPoolSizeIllegalArgumentException2() {
1205 <        ThreadPoolExecutor tpe = null;
1205 >        ThreadPoolExecutor p =
1206 >            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1207          try {
1208 <            tpe = new CustomTPE(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1115 <        } catch(Exception e){}
1116 <        try {
1117 <            tpe.setMaximumPoolSize(-1);
1208 >            p.setMaximumPoolSize(-1);
1209              shouldThrow();
1210 <        } catch(IllegalArgumentException success){
1210 >        } catch (IllegalArgumentException success) {
1211          } finally {
1212 <            try { tpe.shutdown(); } catch(SecurityException ok) { return; }
1212 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1213          }
1214 <        joinPool(tpe);
1214 >        joinPool(p);
1215      }
1216 <    
1216 >
1217  
1218      /**
1219 <     *  setKeepAliveTime  throws IllegalArgumentException
1220 <     *  when given a negative value
1219 >     * setKeepAliveTime throws IllegalArgumentException
1220 >     * when given a negative value
1221       */
1222      public void testKeepAliveTimeIllegalArgumentException() {
1223 <        ThreadPoolExecutor tpe = null;
1223 >        ThreadPoolExecutor p =
1224 >            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1225 >
1226          try {
1227 <            tpe = new CustomTPE(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1135 <        } catch(Exception e){}
1136 <        
1137 <        try {
1138 <            tpe.setKeepAliveTime(-1,TimeUnit.MILLISECONDS);
1227 >            p.setKeepAliveTime(-1,MILLISECONDS);
1228              shouldThrow();
1229 <        } catch(IllegalArgumentException success){
1229 >        } catch (IllegalArgumentException success) {
1230          } finally {
1231 <            try { tpe.shutdown(); } catch(SecurityException ok) { return; }
1231 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1232          }
1233 <        joinPool(tpe);
1233 >        joinPool(p);
1234      }
1235  
1236      /**
1237       * terminated() is called on termination
1238       */
1239      public void testTerminated() {
1240 <        CustomTPE tpe = new CustomTPE();
1241 <        try { tpe.shutdown(); } catch(SecurityException ok) { return; }
1242 <        assertTrue(tpe.terminatedCalled);
1243 <        joinPool(tpe);
1240 >        CustomTPE p = new CustomTPE();
1241 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1242 >        assertTrue(p.terminatedCalled);
1243 >        joinPool(p);
1244      }
1245  
1246      /**
1247       * beforeExecute and afterExecute are called when executing task
1248       */
1249 <    public void testBeforeAfter() {
1250 <        CustomTPE tpe = new CustomTPE();
1249 >    public void testBeforeAfter() throws InterruptedException {
1250 >        CustomTPE p = new CustomTPE();
1251          try {
1252              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1253 <            tpe.execute(r);
1254 <            Thread.sleep(SHORT_DELAY_MS);
1253 >            p.execute(r);
1254 >            delay(SHORT_DELAY_MS);
1255              assertTrue(r.done);
1256 <            assertTrue(tpe.beforeCalled);
1257 <            assertTrue(tpe.afterCalled);
1258 <            try { tpe.shutdown(); } catch(SecurityException ok) { return; }
1170 <        }
1171 <        catch(Exception ex) {
1172 <            unexpectedException();
1256 >            assertTrue(p.beforeCalled);
1257 >            assertTrue(p.afterCalled);
1258 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1259          } finally {
1260 <            joinPool(tpe);
1260 >            joinPool(p);
1261          }
1262      }
1263  
1264      /**
1265       * completed submit of callable returns result
1266       */
1267 <    public void testSubmitCallable() {
1268 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1267 >    public void testSubmitCallable() throws Exception {
1268 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1269          try {
1270              Future<String> future = e.submit(new StringTask());
1271              String result = future.get();
1272              assertSame(TEST_STRING, result);
1187        }
1188        catch (ExecutionException ex) {
1189            unexpectedException();
1190        }
1191        catch (InterruptedException ex) {
1192            unexpectedException();
1273          } finally {
1274              joinPool(e);
1275          }
# Line 1198 | Line 1278 | public class ThreadPoolExecutorSubclassT
1278      /**
1279       * completed submit of runnable returns successfully
1280       */
1281 <    public void testSubmitRunnable() {
1282 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1281 >    public void testSubmitRunnable() throws Exception {
1282 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1283          try {
1284              Future<?> future = e.submit(new NoOpRunnable());
1285              future.get();
1286              assertTrue(future.isDone());
1207        }
1208        catch (ExecutionException ex) {
1209            unexpectedException();
1210        }
1211        catch (InterruptedException ex) {
1212            unexpectedException();
1287          } finally {
1288              joinPool(e);
1289          }
# Line 1218 | Line 1292 | public class ThreadPoolExecutorSubclassT
1292      /**
1293       * completed submit of (runnable, result) returns result
1294       */
1295 <    public void testSubmitRunnable2() {
1296 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1295 >    public void testSubmitRunnable2() throws Exception {
1296 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1297          try {
1298              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1299              String result = future.get();
1300              assertSame(TEST_STRING, result);
1227        }
1228        catch (ExecutionException ex) {
1229            unexpectedException();
1230        }
1231        catch (InterruptedException ex) {
1232            unexpectedException();
1301          } finally {
1302              joinPool(e);
1303          }
1304      }
1305  
1306  
1239
1240
1241
1307      /**
1308       * invokeAny(null) throws NPE
1309       */
1310 <    public void testInvokeAny1() {
1311 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1310 >    public void testInvokeAny1() throws Exception {
1311 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1312          try {
1313              e.invokeAny(null);
1314 +            shouldThrow();
1315          } catch (NullPointerException success) {
1250        } catch(Exception ex) {
1251            unexpectedException();
1316          } finally {
1317              joinPool(e);
1318          }
# Line 1257 | Line 1321 | public class ThreadPoolExecutorSubclassT
1321      /**
1322       * invokeAny(empty collection) throws IAE
1323       */
1324 <    public void testInvokeAny2() {
1325 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1324 >    public void testInvokeAny2() throws Exception {
1325 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1326          try {
1327              e.invokeAny(new ArrayList<Callable<String>>());
1328 +            shouldThrow();
1329          } catch (IllegalArgumentException success) {
1265        } catch(Exception ex) {
1266            unexpectedException();
1330          } finally {
1331              joinPool(e);
1332          }
# Line 1272 | Line 1335 | public class ThreadPoolExecutorSubclassT
1335      /**
1336       * invokeAny(c) throws NPE if c has null elements
1337       */
1338 <    public void testInvokeAny3() {
1339 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1338 >    public void testInvokeAny3() throws Exception {
1339 >        CountDownLatch latch = new CountDownLatch(1);
1340 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1341 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1342 >        l.add(latchAwaitingStringTask(latch));
1343 >        l.add(null);
1344          try {
1278            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1279            l.add(new StringTask());
1280            l.add(null);
1345              e.invokeAny(l);
1346 +            shouldThrow();
1347          } catch (NullPointerException success) {
1283        } catch(Exception ex) {
1284            unexpectedException();
1348          } finally {
1349 +            latch.countDown();
1350              joinPool(e);
1351          }
1352      }
# Line 1290 | Line 1354 | public class ThreadPoolExecutorSubclassT
1354      /**
1355       * invokeAny(c) throws ExecutionException if no task completes
1356       */
1357 <    public void testInvokeAny4() {
1358 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1357 >    public void testInvokeAny4() throws Exception {
1358 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1359 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1360 >        l.add(new NPETask());
1361          try {
1296            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1297            l.add(new NPETask());
1362              e.invokeAny(l);
1363 +            shouldThrow();
1364          } catch (ExecutionException success) {
1365 <        } catch(Exception ex) {
1301 <            unexpectedException();
1365 >            assertTrue(success.getCause() instanceof NullPointerException);
1366          } finally {
1367              joinPool(e);
1368          }
# Line 1307 | Line 1371 | public class ThreadPoolExecutorSubclassT
1371      /**
1372       * invokeAny(c) returns result of some task
1373       */
1374 <    public void testInvokeAny5() {
1375 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1374 >    public void testInvokeAny5() throws Exception {
1375 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1376          try {
1377 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1377 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1378              l.add(new StringTask());
1379              l.add(new StringTask());
1380              String result = e.invokeAny(l);
1381              assertSame(TEST_STRING, result);
1318        } catch (ExecutionException success) {
1319        } catch(Exception ex) {
1320            unexpectedException();
1382          } finally {
1383              joinPool(e);
1384          }
# Line 1326 | Line 1387 | public class ThreadPoolExecutorSubclassT
1387      /**
1388       * invokeAll(null) throws NPE
1389       */
1390 <    public void testInvokeAll1() {
1391 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1390 >    public void testInvokeAll1() throws Exception {
1391 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1392          try {
1393              e.invokeAll(null);
1394 +            shouldThrow();
1395          } catch (NullPointerException success) {
1334        } catch(Exception ex) {
1335            unexpectedException();
1396          } finally {
1397              joinPool(e);
1398          }
# Line 1341 | Line 1401 | public class ThreadPoolExecutorSubclassT
1401      /**
1402       * invokeAll(empty collection) returns empty collection
1403       */
1404 <    public void testInvokeAll2() {
1405 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1404 >    public void testInvokeAll2() throws Exception {
1405 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1406          try {
1407              List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1408              assertTrue(r.isEmpty());
1349        } catch(Exception ex) {
1350            unexpectedException();
1409          } finally {
1410              joinPool(e);
1411          }
# Line 1356 | Line 1414 | public class ThreadPoolExecutorSubclassT
1414      /**
1415       * invokeAll(c) throws NPE if c has null elements
1416       */
1417 <    public void testInvokeAll3() {
1418 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1417 >    public void testInvokeAll3() throws Exception {
1418 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1419 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1420 >        l.add(new StringTask());
1421 >        l.add(null);
1422          try {
1362            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1363            l.add(new StringTask());
1364            l.add(null);
1423              e.invokeAll(l);
1424 +            shouldThrow();
1425          } catch (NullPointerException success) {
1367        } catch(Exception ex) {
1368            unexpectedException();
1426          } finally {
1427              joinPool(e);
1428          }
# Line 1374 | Line 1431 | public class ThreadPoolExecutorSubclassT
1431      /**
1432       * get of element of invokeAll(c) throws exception on failed task
1433       */
1434 <    public void testInvokeAll4() {
1435 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1434 >    public void testInvokeAll4() throws Exception {
1435 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1436 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1437 >        l.add(new NPETask());
1438 >        List<Future<String>> futures = e.invokeAll(l);
1439 >        assertEquals(1, futures.size());
1440          try {
1441 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1442 <            l.add(new NPETask());
1443 <            List<Future<String>> result = e.invokeAll(l);
1444 <            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();
1441 >            futures.get(0).get();
1442 >            shouldThrow();
1443 >        } catch (ExecutionException success) {
1444 >            assertTrue(success.getCause() instanceof NullPointerException);
1445          } finally {
1446              joinPool(e);
1447          }
# Line 1394 | Line 1450 | public class ThreadPoolExecutorSubclassT
1450      /**
1451       * invokeAll(c) returns results of all completed tasks
1452       */
1453 <    public void testInvokeAll5() {
1454 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1453 >    public void testInvokeAll5() throws Exception {
1454 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1455          try {
1456 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1456 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1457              l.add(new StringTask());
1458              l.add(new StringTask());
1459 <            List<Future<String>> result = e.invokeAll(l);
1460 <            assertEquals(2, result.size());
1461 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1462 <                assertSame(TEST_STRING, it.next().get());
1407 <        } catch (ExecutionException success) {
1408 <        } catch(Exception ex) {
1409 <            unexpectedException();
1459 >            List<Future<String>> futures = e.invokeAll(l);
1460 >            assertEquals(2, futures.size());
1461 >            for (Future<String> future : futures)
1462 >                assertSame(TEST_STRING, future.get());
1463          } finally {
1464              joinPool(e);
1465          }
# Line 1417 | Line 1470 | public class ThreadPoolExecutorSubclassT
1470      /**
1471       * timed invokeAny(null) throws NPE
1472       */
1473 <    public void testTimedInvokeAny1() {
1474 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1473 >    public void testTimedInvokeAny1() throws Exception {
1474 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1475          try {
1476 <            e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1476 >            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1477 >            shouldThrow();
1478          } catch (NullPointerException success) {
1425        } catch(Exception ex) {
1426            unexpectedException();
1479          } finally {
1480              joinPool(e);
1481          }
# Line 1432 | Line 1484 | public class ThreadPoolExecutorSubclassT
1484      /**
1485       * timed invokeAny(,,null) throws NPE
1486       */
1487 <    public void testTimedInvokeAnyNullTimeUnit() {
1488 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1487 >    public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1488 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1489 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1490 >        l.add(new StringTask());
1491          try {
1438            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1439            l.add(new StringTask());
1492              e.invokeAny(l, MEDIUM_DELAY_MS, null);
1493 +            shouldThrow();
1494          } catch (NullPointerException success) {
1442        } catch(Exception ex) {
1443            unexpectedException();
1495          } finally {
1496              joinPool(e);
1497          }
# Line 1449 | Line 1500 | public class ThreadPoolExecutorSubclassT
1500      /**
1501       * timed invokeAny(empty collection) throws IAE
1502       */
1503 <    public void testTimedInvokeAny2() {
1504 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1503 >    public void testTimedInvokeAny2() throws Exception {
1504 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1505          try {
1506 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1506 >            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1507 >            shouldThrow();
1508          } catch (IllegalArgumentException success) {
1457        } catch(Exception ex) {
1458            unexpectedException();
1509          } finally {
1510              joinPool(e);
1511          }
# Line 1464 | Line 1514 | public class ThreadPoolExecutorSubclassT
1514      /**
1515       * timed invokeAny(c) throws NPE if c has null elements
1516       */
1517 <    public void testTimedInvokeAny3() {
1518 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1517 >    public void testTimedInvokeAny3() throws Exception {
1518 >        CountDownLatch latch = new CountDownLatch(1);
1519 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1520 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1521 >        l.add(latchAwaitingStringTask(latch));
1522 >        l.add(null);
1523          try {
1524 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1525 <            l.add(new StringTask());
1472 <            l.add(null);
1473 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1524 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1525 >            shouldThrow();
1526          } catch (NullPointerException success) {
1475        } catch(Exception ex) {
1476            ex.printStackTrace();
1477            unexpectedException();
1527          } finally {
1528 +            latch.countDown();
1529              joinPool(e);
1530          }
1531      }
# Line 1483 | Line 1533 | public class ThreadPoolExecutorSubclassT
1533      /**
1534       * timed invokeAny(c) throws ExecutionException if no task completes
1535       */
1536 <    public void testTimedInvokeAny4() {
1537 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1536 >    public void testTimedInvokeAny4() throws Exception {
1537 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1538 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1539 >        l.add(new NPETask());
1540          try {
1541 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1542 <            l.add(new NPETask());
1543 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1544 <        } catch(ExecutionException success) {
1493 <        } catch(Exception ex) {
1494 <            unexpectedException();
1541 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1542 >            shouldThrow();
1543 >        } catch (ExecutionException success) {
1544 >            assertTrue(success.getCause() instanceof NullPointerException);
1545          } finally {
1546              joinPool(e);
1547          }
# Line 1500 | Line 1550 | public class ThreadPoolExecutorSubclassT
1550      /**
1551       * timed invokeAny(c) returns result of some task
1552       */
1553 <    public void testTimedInvokeAny5() {
1554 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1553 >    public void testTimedInvokeAny5() throws Exception {
1554 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1555          try {
1556 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1556 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1557              l.add(new StringTask());
1558              l.add(new StringTask());
1559 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1559 >            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1560              assertSame(TEST_STRING, result);
1511        } catch (ExecutionException success) {
1512        } catch(Exception ex) {
1513            unexpectedException();
1561          } finally {
1562              joinPool(e);
1563          }
# Line 1519 | Line 1566 | public class ThreadPoolExecutorSubclassT
1566      /**
1567       * timed invokeAll(null) throws NPE
1568       */
1569 <    public void testTimedInvokeAll1() {
1570 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1569 >    public void testTimedInvokeAll1() throws Exception {
1570 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1571          try {
1572 <            e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1572 >            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1573 >            shouldThrow();
1574          } catch (NullPointerException success) {
1527        } catch(Exception ex) {
1528            unexpectedException();
1575          } finally {
1576              joinPool(e);
1577          }
# Line 1534 | Line 1580 | public class ThreadPoolExecutorSubclassT
1580      /**
1581       * timed invokeAll(,,null) throws NPE
1582       */
1583 <    public void testTimedInvokeAllNullTimeUnit() {
1584 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1583 >    public void testTimedInvokeAllNullTimeUnit() throws Exception {
1584 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1585 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1586 >        l.add(new StringTask());
1587          try {
1540            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1541            l.add(new StringTask());
1588              e.invokeAll(l, MEDIUM_DELAY_MS, null);
1589 +            shouldThrow();
1590          } catch (NullPointerException success) {
1544        } catch(Exception ex) {
1545            unexpectedException();
1591          } finally {
1592              joinPool(e);
1593          }
# Line 1551 | Line 1596 | public class ThreadPoolExecutorSubclassT
1596      /**
1597       * timed invokeAll(empty collection) returns empty collection
1598       */
1599 <    public void testTimedInvokeAll2() {
1600 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1599 >    public void testTimedInvokeAll2() throws Exception {
1600 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1601          try {
1602 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1602 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1603              assertTrue(r.isEmpty());
1559        } catch(Exception ex) {
1560            unexpectedException();
1604          } finally {
1605              joinPool(e);
1606          }
# Line 1566 | Line 1609 | public class ThreadPoolExecutorSubclassT
1609      /**
1610       * timed invokeAll(c) throws NPE if c has null elements
1611       */
1612 <    public void testTimedInvokeAll3() {
1613 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1612 >    public void testTimedInvokeAll3() throws Exception {
1613 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1614 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1615 >        l.add(new StringTask());
1616 >        l.add(null);
1617          try {
1618 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1619 <            l.add(new StringTask());
1574 <            l.add(null);
1575 <            e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1618 >            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1619 >            shouldThrow();
1620          } catch (NullPointerException success) {
1577        } catch(Exception ex) {
1578            unexpectedException();
1621          } finally {
1622              joinPool(e);
1623          }
# Line 1584 | Line 1626 | public class ThreadPoolExecutorSubclassT
1626      /**
1627       * get of element of invokeAll(c) throws exception on failed task
1628       */
1629 <    public void testTimedInvokeAll4() {
1630 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1629 >    public void testTimedInvokeAll4() throws Exception {
1630 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1631 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1632 >        l.add(new NPETask());
1633 >        List<Future<String>> futures =
1634 >            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1635 >        assertEquals(1, futures.size());
1636          try {
1637 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1638 <            l.add(new NPETask());
1639 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1640 <            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();
1637 >            futures.get(0).get();
1638 >            shouldThrow();
1639 >        } catch (ExecutionException success) {
1640 >            assertTrue(success.getCause() instanceof NullPointerException);
1641          } finally {
1642              joinPool(e);
1643          }
# Line 1604 | Line 1646 | public class ThreadPoolExecutorSubclassT
1646      /**
1647       * timed invokeAll(c) returns results of all completed tasks
1648       */
1649 <    public void testTimedInvokeAll5() {
1650 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1649 >    public void testTimedInvokeAll5() throws Exception {
1650 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1651          try {
1652 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1652 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1653              l.add(new StringTask());
1654              l.add(new StringTask());
1655 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1656 <            assertEquals(2, result.size());
1657 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1658 <                assertSame(TEST_STRING, it.next().get());
1659 <        } catch (ExecutionException success) {
1618 <        } catch(Exception ex) {
1619 <            unexpectedException();
1655 >            List<Future<String>> futures =
1656 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1657 >            assertEquals(2, futures.size());
1658 >            for (Future<String> future : futures)
1659 >                assertSame(TEST_STRING, future.get());
1660          } finally {
1661              joinPool(e);
1662          }
# Line 1625 | Line 1665 | public class ThreadPoolExecutorSubclassT
1665      /**
1666       * timed invokeAll(c) cancels tasks not completed by timeout
1667       */
1668 <    public void testTimedInvokeAll6() {
1669 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1668 >    public void testTimedInvokeAll6() throws Exception {
1669 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1670          try {
1671 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1671 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1672              l.add(new StringTask());
1673              l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1674              l.add(new StringTask());
1675 <            List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
1676 <            assertEquals(3, result.size());
1677 <            Iterator<Future<String>> it = result.iterator();
1675 >            List<Future<String>> futures =
1676 >                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1677 >            assertEquals(3, futures.size());
1678 >            Iterator<Future<String>> it = futures.iterator();
1679              Future<String> f1 = it.next();
1680              Future<String> f2 = it.next();
1681              Future<String> f3 = it.next();
# Line 1643 | Line 1684 | public class ThreadPoolExecutorSubclassT
1684              assertTrue(f3.isDone());
1685              assertFalse(f1.isCancelled());
1686              assertTrue(f2.isCancelled());
1646        } catch(Exception ex) {
1647            unexpectedException();
1687          } finally {
1688              joinPool(e);
1689          }
# Line 1654 | Line 1693 | public class ThreadPoolExecutorSubclassT
1693       * Execution continues if there is at least one thread even if
1694       * thread factory fails to create more
1695       */
1696 <    public void testFailingThreadFactory() {
1697 <        ExecutorService e = new CustomTPE(100, 100, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory());
1698 <        try {
1699 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1700 <            for (int k = 0; k < 100; ++k) {
1701 <                e.execute(new NoOpRunnable());
1702 <            }
1703 <            Thread.sleep(LONG_DELAY_MS);
1704 <        } catch(Exception ex) {
1705 <            unexpectedException();
1696 >    public void testFailingThreadFactory() throws InterruptedException {
1697 >        final ExecutorService e =
1698 >            new CustomTPE(100, 100,
1699 >                          LONG_DELAY_MS, MILLISECONDS,
1700 >                          new LinkedBlockingQueue<Runnable>(),
1701 >                          new FailingThreadFactory());
1702 >        try {
1703 >            final int TASKS = 100;
1704 >            final CountDownLatch done = new CountDownLatch(TASKS);
1705 >            for (int k = 0; k < TASKS; ++k)
1706 >                e.execute(new CheckedRunnable() {
1707 >                    public void realRun() {
1708 >                        done.countDown();
1709 >                    }});
1710 >            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1711          } finally {
1712              joinPool(e);
1713          }
# Line 1673 | Line 1717 | public class ThreadPoolExecutorSubclassT
1717       * allowsCoreThreadTimeOut is by default false.
1718       */
1719      public void testAllowsCoreThreadTimeOut() {
1720 <        ThreadPoolExecutor tpe = new CustomTPE(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1721 <        assertFalse(tpe.allowsCoreThreadTimeOut());
1722 <        joinPool(tpe);
1720 >        ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1721 >        assertFalse(p.allowsCoreThreadTimeOut());
1722 >        joinPool(p);
1723      }
1724  
1725      /**
1726       * allowCoreThreadTimeOut(true) causes idle threads to time out
1727       */
1728 <    public void testAllowCoreThreadTimeOut_true() {
1729 <        ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1730 <        tpe.allowCoreThreadTimeOut(true);
1731 <        tpe.execute(new NoOpRunnable());
1732 <        try {
1733 <            Thread.sleep(MEDIUM_DELAY_MS);
1734 <            assertEquals(0, tpe.getPoolSize());
1735 <        } catch(InterruptedException e){
1736 <            unexpectedException();
1728 >    public void testAllowCoreThreadTimeOut_true() throws Exception {
1729 >        final ThreadPoolExecutor p =
1730 >            new CustomTPE(2, 10,
1731 >                          SHORT_DELAY_MS, MILLISECONDS,
1732 >                          new ArrayBlockingQueue<Runnable>(10));
1733 >        final CountDownLatch threadStarted = new CountDownLatch(1);
1734 >        try {
1735 >            p.allowCoreThreadTimeOut(true);
1736 >            p.execute(new CheckedRunnable() {
1737 >                public void realRun() throws InterruptedException {
1738 >                    threadStarted.countDown();
1739 >                    assertEquals(1, p.getPoolSize());
1740 >                }});
1741 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
1742 >            for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) {
1743 >                if (p.getPoolSize() == 0)
1744 >                    break;
1745 >                delay(10);
1746 >            }
1747 >            assertEquals(0, p.getPoolSize());
1748          } finally {
1749 <            joinPool(tpe);
1749 >            joinPool(p);
1750          }
1751      }
1752  
1753      /**
1754       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1755       */
1756 <    public void testAllowCoreThreadTimeOut_false() {
1757 <        ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1758 <        tpe.allowCoreThreadTimeOut(false);
1759 <        tpe.execute(new NoOpRunnable());
1760 <        try {
1761 <            Thread.sleep(MEDIUM_DELAY_MS);
1762 <            assertTrue(tpe.getPoolSize() >= 1);
1763 <        } catch(InterruptedException e){
1764 <            unexpectedException();
1756 >    public void testAllowCoreThreadTimeOut_false() throws Exception {
1757 >        final ThreadPoolExecutor p =
1758 >            new CustomTPE(2, 10,
1759 >                          SHORT_DELAY_MS, MILLISECONDS,
1760 >                          new ArrayBlockingQueue<Runnable>(10));
1761 >        final CountDownLatch threadStarted = new CountDownLatch(1);
1762 >        try {
1763 >            p.allowCoreThreadTimeOut(false);
1764 >            p.execute(new CheckedRunnable() {
1765 >                public void realRun() throws InterruptedException {
1766 >                    threadStarted.countDown();
1767 >                    assertTrue(p.getPoolSize() >= 1);
1768 >                }});
1769 >            delay(SMALL_DELAY_MS);
1770 >            assertTrue(p.getPoolSize() >= 1);
1771          } finally {
1772 <            joinPool(tpe);
1772 >            joinPool(p);
1773          }
1774      }
1775  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines