ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java (file contents):
Revision 1.2 by jsr166, Mon Nov 2 20:28:32 2009 UTC vs.
Revision 1.71 by jsr166, Sun Oct 4 02:34:48 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines