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.18 by jsr166, Wed Aug 25 00:07:03 2010 UTC vs.
Revision 1.62 by jsr166, Sun Oct 4 02:04:56 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.*;
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 < import java.util.concurrent.locks.*;
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(ThreadPoolExecutorSubclassTest.class);
# Line 37 | Line 61 | public class ThreadPoolExecutorSubclassT
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; }};
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 60 | Line 84 | public class ThreadPoolExecutorSubclassT
84              finally { lock.unlock() ; }
85          }
86          public void run() {
63            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() ; }
71            if (!runme) return;
94              V v = null;
95              Exception e = null;
96              try {
# Line 79 | Line 101 | public class ThreadPoolExecutorSubclassT
101              }
102              lock.lock();
103              try {
104 <                result = v;
105 <                exception = e;
106 <                done = true;
107 <                thread = null;
108 <                cond.signalAll();
104 >                if (!done) {
105 >                    result = v;
106 >                    exception = e;
107 >                    done = true;
108 >                    thread = null;
109 >                    cond.signalAll();
110 >                }
111              }
112              finally { lock.unlock(); }
113          }
# Line 92 | 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 103 | Line 129 | public class ThreadPoolExecutorSubclassT
129              long nanos = unit.toNanos(timeout);
130              lock.lock();
131              try {
132 <                for (;;) {
133 <                    if (done) break;
108 <                    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 117 | Line 144 | public class ThreadPoolExecutorSubclassT
144          }
145      }
146  
120
147      static class CustomTPE extends ThreadPoolExecutor {
148          protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
149              return new CustomTask<V>(c);
# Line 164 | 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, MILLISECONDS, new SynchronousQueue<Runnable>());
199          }
200          protected void beforeExecute(Thread t, Runnable r) {
201 <            beforeCalled = true;
201 >            beforeCalled.countDown();
202          }
203          protected void afterExecute(Runnable r, Throwable t) {
204 <            afterCalled = true;
204 >            afterCalled.countDown();
205          }
206          protected void terminated() {
207 <            terminatedCalled = true;
207 >            terminatedCalled.countDown();
208          }
209  
210 +        public boolean beforeCalled() {
211 +            return beforeCalled.getCount() == 0;
212 +        }
213 +        public boolean afterCalled() {
214 +            return afterCalled.getCount() == 0;
215 +        }
216 +        public boolean terminatedCalled() {
217 +            return terminatedCalled.getCount() == 0;
218 +        }
219      }
220  
221      static class FailingThreadFactory implements ThreadFactory {
# Line 190 | Line 226 | public class ThreadPoolExecutorSubclassT
226          }
227      }
228  
193
229      /**
230 <     *  execute successfully executes a runnable
230 >     * execute successfully executes a runnable
231       */
232      public void testExecute() throws InterruptedException {
233 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
234 <        try {
235 <            p1.execute(new ShortRunnable());
236 <            Thread.sleep(SMALL_DELAY_MS);
237 <        } finally {
238 <            joinPool(p1);
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          }
244      }
245  
246      /**
247 <     *  getActiveCount increases but doesn't overestimate, when a
248 <     *  thread becomes active
247 >     * getActiveCount increases but doesn't overestimate, when a
248 >     * thread becomes active
249       */
250      public void testGetActiveCount() throws InterruptedException {
251 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
252 <        assertEquals(0, p2.getActiveCount());
253 <        p2.execute(new MediumRunnable());
254 <        Thread.sleep(SHORT_DELAY_MS);
255 <        assertEquals(1, p2.getActiveCount());
256 <        joinPool(p2);
251 >        final ThreadPoolExecutor p =
252 >            new CustomTPE(2, 2,
253 >                          LONG_DELAY_MS, MILLISECONDS,
254 >                          new ArrayBlockingQueue<Runnable>(10));
255 >        final CountDownLatch threadStarted = new CountDownLatch(1);
256 >        final CountDownLatch done = new CountDownLatch(1);
257 >        try (PoolCleaner cleaner = cleaner(p)) {
258 >            assertEquals(0, p.getActiveCount());
259 >            p.execute(new CheckedRunnable() {
260 >                public void realRun() throws InterruptedException {
261 >                    threadStarted.countDown();
262 >                    assertEquals(1, p.getActiveCount());
263 >                    done.await();
264 >                }});
265 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
266 >            assertEquals(1, p.getActiveCount());
267 >            done.countDown();
268 >        }
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, 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, 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
320 >     * getCompletedTaskCount increases, but doesn't overestimate,
321 >     * when tasks complete
322       */
323      public void testGetCompletedTaskCount() throws InterruptedException {
324 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
325 <        assertEquals(0, p2.getCompletedTaskCount());
326 <        p2.execute(new ShortRunnable());
327 <        Thread.sleep(SMALL_DELAY_MS);
328 <        assertEquals(1, p2.getCompletedTaskCount());
329 <        try { p2.shutdown(); } catch (SecurityException ok) { return; }
330 <        joinPool(p2);
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 >        }
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, 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, 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  
280
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, 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, 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  
302
410      /**
411       * setThreadFactory(null) throws NPE
412       */
413      public void testSetThreadFactoryNull() {
414 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, 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 318 | 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, 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 329 | Line 443 | public class ThreadPoolExecutorSubclassT
443       * getRejectedExecutionHandler
444       */
445      public void testSetRejectedExecutionHandler() {
446 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, 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  
339
457      /**
458       * setRejectedExecutionHandler(null) throws NPE
459       */
460      public void testSetRejectedExecutionHandlerNull() {
461 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, 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  
354
473      /**
474 <     *   getLargestPoolSize increases, but doesn't overestimate, when
475 <     *   multiple threads active
474 >     * getLargestPoolSize increases, but doesn't overestimate, when
475 >     * multiple threads active
476       */
477      public void testGetLargestPoolSize() throws InterruptedException {
478 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
479 <        assertEquals(0, p2.getLargestPoolSize());
480 <        p2.execute(new MediumRunnable());
481 <        p2.execute(new MediumRunnable());
482 <        Thread.sleep(SHORT_DELAY_MS);
483 <        assertEquals(2, p2.getLargestPoolSize());
484 <        joinPool(p2);
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 >        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, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
507 <        assertEquals(2, p2.getMaximumPoolSize());
508 <        joinPool(p2);
506 >        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
507 >        assertEquals(2, p.getMaximumPoolSize());
508 >        joinPool(p);
509      }
510  
511      /**
512 <     *   getPoolSize increases, but doesn't overestimate, when threads
513 <     *   become active
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, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
517 <        assertEquals(0, p1.getPoolSize());
518 <        p1.execute(new MediumRunnable());
519 <        assertEquals(1, p1.getPoolSize());
520 <        joinPool(p1);
515 >    public void testGetPoolSize() throws InterruptedException {
516 >        final ThreadPoolExecutor p =
517 >            new CustomTPE(1, 1,
518 >                          LONG_DELAY_MS, MILLISECONDS,
519 >                          new ArrayBlockingQueue<Runnable>(10));
520 >        final CountDownLatch threadStarted = new CountDownLatch(1);
521 >        final CountDownLatch done = new CountDownLatch(1);
522 >        try {
523 >            assertEquals(0, p.getPoolSize());
524 >            p.execute(new CheckedRunnable() {
525 >                public void realRun() throws InterruptedException {
526 >                    threadStarted.countDown();
527 >                    assertEquals(1, p.getPoolSize());
528 >                    done.await();
529 >                }});
530 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
531 >            assertEquals(1, p.getPoolSize());
532 >        } finally {
533 >            done.countDown();
534 >            joinPool(p);
535 >        }
536      }
537  
538      /**
539 <     *  getTaskCount increases, but doesn't overestimate, when tasks submitted
539 >     * getTaskCount increases, but doesn't overestimate, when tasks submitted
540       */
541      public void testGetTaskCount() throws InterruptedException {
542 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
543 <        assertEquals(0, p1.getTaskCount());
544 <        p1.execute(new MediumRunnable());
545 <        Thread.sleep(SHORT_DELAY_MS);
546 <        assertEquals(1, p1.getTaskCount());
547 <        joinPool(p1);
542 >        final ThreadPoolExecutor p =
543 >            new CustomTPE(1, 1,
544 >                          LONG_DELAY_MS, MILLISECONDS,
545 >                          new ArrayBlockingQueue<Runnable>(10));
546 >        final CountDownLatch threadStarted = new CountDownLatch(1);
547 >        final CountDownLatch done = new CountDownLatch(1);
548 >        try {
549 >            assertEquals(0, p.getTaskCount());
550 >            p.execute(new CheckedRunnable() {
551 >                public void realRun() throws InterruptedException {
552 >                    threadStarted.countDown();
553 >                    assertEquals(1, p.getTaskCount());
554 >                    done.await();
555 >                }});
556 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
557 >            assertEquals(1, p.getTaskCount());
558 >        } finally {
559 >            done.countDown();
560 >            joinPool(p);
561 >        }
562      }
563  
564      /**
565 <     *   isShutDown is false before shutdown, true after
565 >     * isShutdown is false before shutdown, true after
566       */
567      public void testIsShutdown() {
568  
569 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
570 <        assertFalse(p1.isShutdown());
571 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
572 <        assertTrue(p1.isShutdown());
573 <        joinPool(p1);
569 >        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
570 >        assertFalse(p.isShutdown());
571 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
572 >        assertTrue(p.isShutdown());
573 >        joinPool(p);
574      }
575  
415
576      /**
577 <     *  isTerminated is false before termination, true after
577 >     * isTerminated is false before termination, true after
578       */
579      public void testIsTerminated() throws InterruptedException {
580 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
581 <        assertFalse(p1.isTerminated());
582 <        try {
583 <            p1.execute(new MediumRunnable());
584 <        } finally {
585 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
586 <        }
587 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
588 <        assertTrue(p1.isTerminated());
580 >        final ThreadPoolExecutor p =
581 >            new CustomTPE(1, 1,
582 >                          LONG_DELAY_MS, MILLISECONDS,
583 >                          new ArrayBlockingQueue<Runnable>(10));
584 >        final CountDownLatch threadStarted = new CountDownLatch(1);
585 >        final CountDownLatch done = new CountDownLatch(1);
586 >        try {
587 >            assertFalse(p.isTerminating());
588 >            p.execute(new CheckedRunnable() {
589 >                public void realRun() throws InterruptedException {
590 >                    assertFalse(p.isTerminating());
591 >                    threadStarted.countDown();
592 >                    done.await();
593 >                }});
594 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
595 >            assertFalse(p.isTerminating());
596 >            done.countDown();
597 >        } finally {
598 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
599 >        }
600 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
601 >        assertTrue(p.isTerminated());
602 >        assertFalse(p.isTerminating());
603      }
604  
605      /**
606 <     *  isTerminating is not true when running or when terminated
606 >     * isTerminating is not true when running or when terminated
607       */
608      public void testIsTerminating() throws InterruptedException {
609 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
610 <        assertFalse(p1.isTerminating());
611 <        try {
612 <            p1.execute(new SmallRunnable());
613 <            assertFalse(p1.isTerminating());
614 <        } finally {
615 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
616 <        }
617 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
618 <        assertTrue(p1.isTerminated());
619 <        assertFalse(p1.isTerminating());
609 >        final ThreadPoolExecutor p =
610 >            new CustomTPE(1, 1,
611 >                          LONG_DELAY_MS, MILLISECONDS,
612 >                          new ArrayBlockingQueue<Runnable>(10));
613 >        final CountDownLatch threadStarted = new CountDownLatch(1);
614 >        final CountDownLatch done = new CountDownLatch(1);
615 >        try {
616 >            assertFalse(p.isTerminating());
617 >            p.execute(new CheckedRunnable() {
618 >                public void realRun() throws InterruptedException {
619 >                    assertFalse(p.isTerminating());
620 >                    threadStarted.countDown();
621 >                    done.await();
622 >                }});
623 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
624 >            assertFalse(p.isTerminating());
625 >            done.countDown();
626 >        } finally {
627 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
628 >        }
629 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
630 >        assertTrue(p.isTerminated());
631 >        assertFalse(p.isTerminating());
632      }
633  
634      /**
635       * getQueue returns the work queue, which contains queued tasks
636       */
637      public void testGetQueue() throws InterruptedException {
638 <        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
639 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, q);
640 <        FutureTask[] tasks = new FutureTask[5];
641 <        for (int i = 0; i < 5; i++) {
642 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
643 <            p1.execute(tasks[i]);
644 <        }
645 <        try {
646 <            Thread.sleep(SHORT_DELAY_MS);
647 <            BlockingQueue<Runnable> wq = p1.getQueue();
648 <            assertSame(q, wq);
649 <            assertFalse(wq.contains(tasks[0]));
650 <            assertTrue(wq.contains(tasks[4]));
651 <            for (int i = 1; i < 5; ++i)
652 <                tasks[i].cancel(true);
653 <            p1.shutdownNow();
638 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
639 >        final ThreadPoolExecutor p =
640 >            new CustomTPE(1, 1,
641 >                          LONG_DELAY_MS, MILLISECONDS,
642 >                          q);
643 >        final CountDownLatch threadStarted = new CountDownLatch(1);
644 >        final CountDownLatch done = new CountDownLatch(1);
645 >        try {
646 >            FutureTask[] tasks = new FutureTask[5];
647 >            for (int i = 0; i < tasks.length; i++) {
648 >                Callable task = new CheckedCallable<Boolean>() {
649 >                    public Boolean realCall() throws InterruptedException {
650 >                        threadStarted.countDown();
651 >                        assertSame(q, p.getQueue());
652 >                        done.await();
653 >                        return Boolean.TRUE;
654 >                    }};
655 >                tasks[i] = new FutureTask(task);
656 >                p.execute(tasks[i]);
657 >            }
658 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
659 >            assertSame(q, p.getQueue());
660 >            assertFalse(q.contains(tasks[0]));
661 >            assertTrue(q.contains(tasks[tasks.length - 1]));
662 >            assertEquals(tasks.length - 1, q.size());
663          } finally {
664 <            joinPool(p1);
664 >            done.countDown();
665 >            joinPool(p);
666          }
667      }
668  
# Line 475 | Line 671 | public class ThreadPoolExecutorSubclassT
671       */
672      public void testRemove() throws InterruptedException {
673          BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
674 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, q);
675 <        FutureTask[] tasks = new FutureTask[5];
676 <        for (int i = 0; i < 5; i++) {
677 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
678 <            p1.execute(tasks[i]);
679 <        }
680 <        try {
681 <            Thread.sleep(SHORT_DELAY_MS);
682 <            assertFalse(p1.remove(tasks[0]));
674 >        final ThreadPoolExecutor p =
675 >            new CustomTPE(1, 1,
676 >                          LONG_DELAY_MS, MILLISECONDS,
677 >                          q);
678 >        Runnable[] tasks = new Runnable[6];
679 >        final CountDownLatch threadStarted = new CountDownLatch(1);
680 >        final CountDownLatch done = new CountDownLatch(1);
681 >        try {
682 >            for (int i = 0; i < tasks.length; i++) {
683 >                tasks[i] = new CheckedRunnable() {
684 >                        public void realRun() throws InterruptedException {
685 >                            threadStarted.countDown();
686 >                            done.await();
687 >                        }};
688 >                p.execute(tasks[i]);
689 >            }
690 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
691 >            assertFalse(p.remove(tasks[0]));
692              assertTrue(q.contains(tasks[4]));
693              assertTrue(q.contains(tasks[3]));
694 <            assertTrue(p1.remove(tasks[4]));
695 <            assertFalse(p1.remove(tasks[4]));
694 >            assertTrue(p.remove(tasks[4]));
695 >            assertFalse(p.remove(tasks[4]));
696              assertFalse(q.contains(tasks[4]));
697              assertTrue(q.contains(tasks[3]));
698 <            assertTrue(p1.remove(tasks[3]));
698 >            assertTrue(p.remove(tasks[3]));
699              assertFalse(q.contains(tasks[3]));
700          } finally {
701 <            joinPool(p1);
701 >            done.countDown();
702 >            joinPool(p);
703          }
704      }
705  
706      /**
707 <     *   purge removes cancelled tasks from the queue
707 >     * purge removes cancelled tasks from the queue
708       */
709 <    public void testPurge() {
710 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
709 >    public void testPurge() throws InterruptedException {
710 >        final CountDownLatch threadStarted = new CountDownLatch(1);
711 >        final CountDownLatch done = new CountDownLatch(1);
712 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
713 >        final ThreadPoolExecutor p =
714 >            new CustomTPE(1, 1,
715 >                          LONG_DELAY_MS, MILLISECONDS,
716 >                          q);
717          FutureTask[] tasks = new FutureTask[5];
718 <        for (int i = 0; i < 5; i++) {
719 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
720 <            p1.execute(tasks[i]);
718 >        try {
719 >            for (int i = 0; i < tasks.length; i++) {
720 >                Callable task = new CheckedCallable<Boolean>() {
721 >                    public Boolean realCall() throws InterruptedException {
722 >                        threadStarted.countDown();
723 >                        done.await();
724 >                        return Boolean.TRUE;
725 >                    }};
726 >                tasks[i] = new FutureTask(task);
727 >                p.execute(tasks[i]);
728 >            }
729 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
730 >            assertEquals(tasks.length, p.getTaskCount());
731 >            assertEquals(tasks.length - 1, q.size());
732 >            assertEquals(1L, p.getActiveCount());
733 >            assertEquals(0L, p.getCompletedTaskCount());
734 >            tasks[4].cancel(true);
735 >            tasks[3].cancel(false);
736 >            p.purge();
737 >            assertEquals(tasks.length - 3, q.size());
738 >            assertEquals(tasks.length - 2, p.getTaskCount());
739 >            p.purge();         // Nothing to do
740 >            assertEquals(tasks.length - 3, q.size());
741 >            assertEquals(tasks.length - 2, p.getTaskCount());
742 >        } finally {
743 >            done.countDown();
744 >            joinPool(p);
745          }
510        tasks[4].cancel(true);
511        tasks[3].cancel(true);
512        p1.purge();
513        long count = p1.getTaskCount();
514        assertTrue(count >= 2 && count < 5);
515        joinPool(p1);
746      }
747  
748      /**
749 <     *  shutDownNow returns a list containing tasks that were not run
749 >     * shutdownNow returns a list containing tasks that were not run,
750 >     * and those tasks are drained from the queue
751       */
752 <    public void testShutDownNow() {
753 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
754 <        List l;
755 <        try {
756 <            for (int i = 0; i < 5; i++)
757 <                p1.execute(new MediumPossiblyInterruptedRunnable());
758 <        }
759 <        finally {
752 >    public void testShutdownNow() throws InterruptedException {
753 >        final int poolSize = 2;
754 >        final int count = 5;
755 >        final AtomicInteger ran = new AtomicInteger(0);
756 >        ThreadPoolExecutor p =
757 >            new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS,
758 >                          new ArrayBlockingQueue<Runnable>(10));
759 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
760 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
761 >            threadsStarted.countDown();
762              try {
763 <                l = p1.shutdownNow();
764 <            } catch (SecurityException ok) { return; }
765 <        }
766 <        assertTrue(p1.isShutdown());
767 <        assertTrue(l.size() <= 4);
763 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
764 >            } catch (InterruptedException success) {}
765 >            ran.getAndIncrement();
766 >        }};
767 >        for (int i = 0; i < count; i++)
768 >            p.execute(waiter);
769 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
770 >        assertEquals(poolSize, p.getActiveCount());
771 >        assertEquals(0, p.getCompletedTaskCount());
772 >        final List<Runnable> queuedTasks;
773 >        try {
774 >            queuedTasks = p.shutdownNow();
775 >        } catch (SecurityException ok) {
776 >            return; // Allowed in case test doesn't have privs
777 >        }
778 >        assertTrue(p.isShutdown());
779 >        assertTrue(p.getQueue().isEmpty());
780 >        assertEquals(count - poolSize, queuedTasks.size());
781 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
782 >        assertTrue(p.isTerminated());
783 >        assertEquals(poolSize, ran.get());
784 >        assertEquals(poolSize, p.getCompletedTaskCount());
785      }
786  
787      // Exception Tests
788  
539
789      /**
790       * Constructor throws if corePoolSize argument is less than zero
791       */
792      public void testConstructor1() {
793          try {
794 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
794 >            new CustomTPE(-1, 1, 1L, SECONDS,
795 >                          new ArrayBlockingQueue<Runnable>(10));
796              shouldThrow();
797          } catch (IllegalArgumentException success) {}
798      }
# Line 552 | Line 802 | public class ThreadPoolExecutorSubclassT
802       */
803      public void testConstructor2() {
804          try {
805 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
805 >            new CustomTPE(1, -1, 1L, SECONDS,
806 >                          new ArrayBlockingQueue<Runnable>(10));
807              shouldThrow();
808          } catch (IllegalArgumentException success) {}
809      }
# Line 562 | Line 813 | public class ThreadPoolExecutorSubclassT
813       */
814      public void testConstructor3() {
815          try {
816 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
816 >            new CustomTPE(1, 0, 1L, SECONDS,
817 >                          new ArrayBlockingQueue<Runnable>(10));
818              shouldThrow();
819          } catch (IllegalArgumentException success) {}
820      }
# Line 572 | Line 824 | public class ThreadPoolExecutorSubclassT
824       */
825      public void testConstructor4() {
826          try {
827 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
827 >            new CustomTPE(1, 2, -1L, SECONDS,
828 >                          new ArrayBlockingQueue<Runnable>(10));
829              shouldThrow();
830          } catch (IllegalArgumentException success) {}
831      }
# Line 582 | Line 835 | public class ThreadPoolExecutorSubclassT
835       */
836      public void testConstructor5() {
837          try {
838 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
838 >            new CustomTPE(2, 1, 1L, SECONDS,
839 >                          new ArrayBlockingQueue<Runnable>(10));
840              shouldThrow();
841          } catch (IllegalArgumentException success) {}
842      }
# Line 592 | Line 846 | public class ThreadPoolExecutorSubclassT
846       */
847      public void testConstructorNullPointerException() {
848          try {
849 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null);
849 >            new CustomTPE(1, 2, 1L, SECONDS, null);
850              shouldThrow();
851          } catch (NullPointerException success) {}
852      }
853  
600
601
854      /**
855       * Constructor throws if corePoolSize argument is less than zero
856       */
857      public void testConstructor6() {
858          try {
859 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
859 >            new CustomTPE(-1, 1, 1L, SECONDS,
860 >                          new ArrayBlockingQueue<Runnable>(10),
861 >                          new SimpleThreadFactory());
862              shouldThrow();
863          } catch (IllegalArgumentException success) {}
864      }
# Line 614 | Line 868 | public class ThreadPoolExecutorSubclassT
868       */
869      public void testConstructor7() {
870          try {
871 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
871 >            new CustomTPE(1,-1, 1L, SECONDS,
872 >                          new ArrayBlockingQueue<Runnable>(10),
873 >                          new SimpleThreadFactory());
874              shouldThrow();
875          } catch (IllegalArgumentException success) {}
876      }
# Line 624 | Line 880 | public class ThreadPoolExecutorSubclassT
880       */
881      public void testConstructor8() {
882          try {
883 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
883 >            new CustomTPE(1, 0, 1L, SECONDS,
884 >                          new ArrayBlockingQueue<Runnable>(10),
885 >                          new SimpleThreadFactory());
886              shouldThrow();
887          } catch (IllegalArgumentException success) {}
888      }
# Line 634 | Line 892 | public class ThreadPoolExecutorSubclassT
892       */
893      public void testConstructor9() {
894          try {
895 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
895 >            new CustomTPE(1, 2, -1L, SECONDS,
896 >                          new ArrayBlockingQueue<Runnable>(10),
897 >                          new SimpleThreadFactory());
898              shouldThrow();
899          } catch (IllegalArgumentException success) {}
900      }
# Line 644 | Line 904 | public class ThreadPoolExecutorSubclassT
904       */
905      public void testConstructor10() {
906          try {
907 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
907 >            new CustomTPE(2, 1, 1L, SECONDS,
908 >                          new ArrayBlockingQueue<Runnable>(10),
909 >                          new SimpleThreadFactory());
910              shouldThrow();
911          } catch (IllegalArgumentException success) {}
912      }
# Line 654 | Line 916 | public class ThreadPoolExecutorSubclassT
916       */
917      public void testConstructorNullPointerException2() {
918          try {
919 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
919 >            new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory());
920              shouldThrow();
921          } catch (NullPointerException success) {}
922      }
# Line 664 | Line 926 | public class ThreadPoolExecutorSubclassT
926       */
927      public void testConstructorNullPointerException3() {
928          try {
929 <            ThreadFactory f = null;
930 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
929 >            new CustomTPE(1, 2, 1L, SECONDS,
930 >                          new ArrayBlockingQueue<Runnable>(10),
931 >                          (ThreadFactory) null);
932              shouldThrow();
933          } catch (NullPointerException success) {}
934      }
935  
673
936      /**
937       * Constructor throws if corePoolSize argument is less than zero
938       */
939      public void testConstructor11() {
940          try {
941 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
941 >            new CustomTPE(-1, 1, 1L, SECONDS,
942 >                          new ArrayBlockingQueue<Runnable>(10),
943 >                          new NoOpREHandler());
944              shouldThrow();
945          } catch (IllegalArgumentException success) {}
946      }
# Line 686 | Line 950 | public class ThreadPoolExecutorSubclassT
950       */
951      public void testConstructor12() {
952          try {
953 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
953 >            new CustomTPE(1, -1, 1L, SECONDS,
954 >                          new ArrayBlockingQueue<Runnable>(10),
955 >                          new NoOpREHandler());
956              shouldThrow();
957          } catch (IllegalArgumentException success) {}
958      }
# Line 696 | Line 962 | public class ThreadPoolExecutorSubclassT
962       */
963      public void testConstructor13() {
964          try {
965 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
965 >            new CustomTPE(1, 0, 1L, SECONDS,
966 >                          new ArrayBlockingQueue<Runnable>(10),
967 >                          new NoOpREHandler());
968              shouldThrow();
969          } catch (IllegalArgumentException success) {}
970      }
# Line 706 | Line 974 | public class ThreadPoolExecutorSubclassT
974       */
975      public void testConstructor14() {
976          try {
977 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
977 >            new CustomTPE(1, 2, -1L, SECONDS,
978 >                          new ArrayBlockingQueue<Runnable>(10),
979 >                          new NoOpREHandler());
980              shouldThrow();
981          } catch (IllegalArgumentException success) {}
982      }
# Line 716 | Line 986 | public class ThreadPoolExecutorSubclassT
986       */
987      public void testConstructor15() {
988          try {
989 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
989 >            new CustomTPE(2, 1, 1L, SECONDS,
990 >                          new ArrayBlockingQueue<Runnable>(10),
991 >                          new NoOpREHandler());
992              shouldThrow();
993          } catch (IllegalArgumentException success) {}
994      }
# Line 726 | Line 998 | public class ThreadPoolExecutorSubclassT
998       */
999      public void testConstructorNullPointerException4() {
1000          try {
1001 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
1001 >            new CustomTPE(1, 2, 1L, SECONDS,
1002 >                          null,
1003 >                          new NoOpREHandler());
1004              shouldThrow();
1005          } catch (NullPointerException success) {}
1006      }
# Line 736 | Line 1010 | public class ThreadPoolExecutorSubclassT
1010       */
1011      public void testConstructorNullPointerException5() {
1012          try {
1013 <            RejectedExecutionHandler r = null;
1014 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
1013 >            new CustomTPE(1, 2, 1L, SECONDS,
1014 >                          new ArrayBlockingQueue<Runnable>(10),
1015 >                          (RejectedExecutionHandler) null);
1016              shouldThrow();
1017          } catch (NullPointerException success) {}
1018      }
1019  
745
1020      /**
1021       * Constructor throws if corePoolSize argument is less than zero
1022       */
1023      public void testConstructor16() {
1024          try {
1025 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1025 >            new CustomTPE(-1, 1, 1L, SECONDS,
1026 >                          new ArrayBlockingQueue<Runnable>(10),
1027 >                          new SimpleThreadFactory(),
1028 >                          new NoOpREHandler());
1029              shouldThrow();
1030          } catch (IllegalArgumentException success) {}
1031      }
# Line 758 | Line 1035 | public class ThreadPoolExecutorSubclassT
1035       */
1036      public void testConstructor17() {
1037          try {
1038 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1038 >            new CustomTPE(1, -1, 1L, SECONDS,
1039 >                          new ArrayBlockingQueue<Runnable>(10),
1040 >                          new SimpleThreadFactory(),
1041 >                          new NoOpREHandler());
1042              shouldThrow();
1043          } catch (IllegalArgumentException success) {}
1044      }
# Line 768 | Line 1048 | public class ThreadPoolExecutorSubclassT
1048       */
1049      public void testConstructor18() {
1050          try {
1051 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1051 >            new CustomTPE(1, 0, 1L, SECONDS,
1052 >                          new ArrayBlockingQueue<Runnable>(10),
1053 >                          new SimpleThreadFactory(),
1054 >                          new NoOpREHandler());
1055              shouldThrow();
1056          } catch (IllegalArgumentException success) {}
1057      }
# Line 778 | Line 1061 | public class ThreadPoolExecutorSubclassT
1061       */
1062      public void testConstructor19() {
1063          try {
1064 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1064 >            new CustomTPE(1, 2, -1L, SECONDS,
1065 >                          new ArrayBlockingQueue<Runnable>(10),
1066 >                          new SimpleThreadFactory(),
1067 >                          new NoOpREHandler());
1068              shouldThrow();
1069          } catch (IllegalArgumentException success) {}
1070      }
# Line 788 | Line 1074 | public class ThreadPoolExecutorSubclassT
1074       */
1075      public void testConstructor20() {
1076          try {
1077 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1077 >            new CustomTPE(2, 1, 1L, SECONDS,
1078 >                          new ArrayBlockingQueue<Runnable>(10),
1079 >                          new SimpleThreadFactory(),
1080 >                          new NoOpREHandler());
1081              shouldThrow();
1082          } catch (IllegalArgumentException success) {}
1083      }
1084  
1085      /**
1086 <     * Constructor throws if workQueue is set to null
1086 >     * Constructor throws if workQueue is null
1087       */
1088      public void testConstructorNullPointerException6() {
1089          try {
1090 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
1090 >            new CustomTPE(1, 2, 1L, SECONDS,
1091 >                          null,
1092 >                          new SimpleThreadFactory(),
1093 >                          new NoOpREHandler());
1094              shouldThrow();
1095          } catch (NullPointerException success) {}
1096      }
1097  
1098      /**
1099 <     * Constructor throws if handler is set to null
1099 >     * Constructor throws if handler is null
1100       */
1101      public void testConstructorNullPointerException7() {
1102          try {
1103 <            RejectedExecutionHandler r = null;
1104 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
1103 >            new CustomTPE(1, 2, 1L, SECONDS,
1104 >                          new ArrayBlockingQueue<Runnable>(10),
1105 >                          new SimpleThreadFactory(),
1106 >                          (RejectedExecutionHandler) null);
1107              shouldThrow();
1108          } catch (NullPointerException success) {}
1109      }
1110  
1111      /**
1112 <     * Constructor throws if ThreadFactory is set top null
1112 >     * Constructor throws if ThreadFactory is null
1113       */
1114      public void testConstructorNullPointerException8() {
1115          try {
1116 <            ThreadFactory f = null;
1117 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
1116 >            new CustomTPE(1, 2, 1L, SECONDS,
1117 >                          new ArrayBlockingQueue<Runnable>(10),
1118 >                          (ThreadFactory) null,
1119 >                          new NoOpREHandler());
1120              shouldThrow();
1121          } catch (NullPointerException success) {}
1122      }
1123  
828
1124      /**
1125 <     *  execute throws RejectedExecutionException
831 <     *  if saturated.
1125 >     * execute throws RejectedExecutionException if saturated.
1126       */
1127      public void testSaturatedExecute() {
1128 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
1129 <        try {
1130 <
1131 <            for (int i = 0; i < 5; ++i) {
1132 <                p.execute(new MediumRunnable());
1128 >        ThreadPoolExecutor p =
1129 >            new CustomTPE(1, 1,
1130 >                          LONG_DELAY_MS, MILLISECONDS,
1131 >                          new ArrayBlockingQueue<Runnable>(1));
1132 >        final CountDownLatch done = new CountDownLatch(1);
1133 >        try {
1134 >            Runnable task = new CheckedRunnable() {
1135 >                public void realRun() throws InterruptedException {
1136 >                    done.await();
1137 >                }};
1138 >            for (int i = 0; i < 2; ++i)
1139 >                p.execute(task);
1140 >            for (int i = 0; i < 2; ++i) {
1141 >                try {
1142 >                    p.execute(task);
1143 >                    shouldThrow();
1144 >                } catch (RejectedExecutionException success) {}
1145 >                assertTrue(p.getTaskCount() <= 2);
1146              }
1147 <            shouldThrow();
1148 <        } catch (RejectedExecutionException success) {}
1149 <        joinPool(p);
1147 >        } finally {
1148 >            done.countDown();
1149 >            joinPool(p);
1150 >        }
1151      }
1152  
1153      /**
1154 <     *  executor using CallerRunsPolicy runs task if saturated.
1154 >     * executor using CallerRunsPolicy runs task if saturated.
1155       */
1156      public void testSaturatedExecute2() {
1157          RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1158 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1158 >        ThreadPoolExecutor p = new CustomTPE(1, 1,
1159 >                                             LONG_DELAY_MS, MILLISECONDS,
1160 >                                             new ArrayBlockingQueue<Runnable>(1),
1161 >                                             h);
1162          try {
852
1163              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1164 <            for (int i = 0; i < 5; ++i) {
1164 >            for (int i = 0; i < tasks.length; ++i)
1165                  tasks[i] = new TrackedNoOpRunnable();
856            }
1166              TrackedLongRunnable mr = new TrackedLongRunnable();
1167              p.execute(mr);
1168 <            for (int i = 0; i < 5; ++i) {
1168 >            for (int i = 0; i < tasks.length; ++i)
1169                  p.execute(tasks[i]);
1170 <            }
862 <            for (int i = 1; i < 5; ++i) {
1170 >            for (int i = 1; i < tasks.length; ++i)
1171                  assertTrue(tasks[i].done);
864            }
1172              try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1173          } finally {
1174              joinPool(p);
# Line 869 | Line 1176 | public class ThreadPoolExecutorSubclassT
1176      }
1177  
1178      /**
1179 <     *  executor using DiscardPolicy drops task if saturated.
1179 >     * executor using DiscardPolicy drops task if saturated.
1180       */
1181      public void testSaturatedExecute3() {
1182          RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1183 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1183 >        ThreadPoolExecutor p =
1184 >            new CustomTPE(1, 1,
1185 >                          LONG_DELAY_MS, MILLISECONDS,
1186 >                          new ArrayBlockingQueue<Runnable>(1),
1187 >                          h);
1188          try {
878
1189              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1190 <            for (int i = 0; i < 5; ++i) {
1190 >            for (int i = 0; i < tasks.length; ++i)
1191                  tasks[i] = new TrackedNoOpRunnable();
882            }
1192              p.execute(new TrackedLongRunnable());
1193 <            for (int i = 0; i < 5; ++i) {
1194 <                p.execute(tasks[i]);
1195 <            }
1196 <            for (int i = 0; i < 5; ++i) {
888 <                assertFalse(tasks[i].done);
889 <            }
1193 >            for (TrackedNoOpRunnable task : tasks)
1194 >                p.execute(task);
1195 >            for (TrackedNoOpRunnable task : tasks)
1196 >                assertFalse(task.done);
1197              try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1198          } finally {
1199              joinPool(p);
# Line 894 | Line 1201 | public class ThreadPoolExecutorSubclassT
1201      }
1202  
1203      /**
1204 <     *  executor using DiscardOldestPolicy drops oldest task if saturated.
1204 >     * executor using DiscardOldestPolicy drops oldest task if saturated.
1205       */
1206      public void testSaturatedExecute4() {
1207          RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
# Line 915 | Line 1222 | public class ThreadPoolExecutorSubclassT
1222      }
1223  
1224      /**
1225 <     *  execute throws RejectedExecutionException if shutdown
1225 >     * execute throws RejectedExecutionException if shutdown
1226       */
1227      public void testRejectedExecutionExceptionOnShutdown() {
1228 <        ThreadPoolExecutor tpe =
1228 >        ThreadPoolExecutor p =
1229              new CustomTPE(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
1230 <        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1230 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1231          try {
1232 <            tpe.execute(new NoOpRunnable());
1232 >            p.execute(new NoOpRunnable());
1233              shouldThrow();
1234          } catch (RejectedExecutionException success) {}
1235  
1236 <        joinPool(tpe);
1236 >        joinPool(p);
1237      }
1238  
1239      /**
1240 <     *  execute using CallerRunsPolicy drops task on shutdown
1240 >     * execute using CallerRunsPolicy drops task on shutdown
1241       */
1242      public void testCallerRunsOnShutdown() {
1243          RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
# Line 947 | Line 1254 | public class ThreadPoolExecutorSubclassT
1254      }
1255  
1256      /**
1257 <     *  execute using DiscardPolicy drops task on shutdown
1257 >     * execute using DiscardPolicy drops task on shutdown
1258       */
1259      public void testDiscardOnShutdown() {
1260          RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
# Line 963 | Line 1270 | public class ThreadPoolExecutorSubclassT
1270          }
1271      }
1272  
966
1273      /**
1274 <     *  execute using DiscardOldestPolicy drops task on shutdown
1274 >     * execute using DiscardOldestPolicy drops task on shutdown
1275       */
1276      public void testDiscardOldestOnShutdown() {
1277          RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
# Line 981 | Line 1287 | public class ThreadPoolExecutorSubclassT
1287          }
1288      }
1289  
984
1290      /**
1291       * execute(null) throws NPE
1292       */
1293      public void testExecuteNull() {
1294 <        ThreadPoolExecutor tpe = null;
1294 >        ThreadPoolExecutor p =
1295 >            new CustomTPE(1, 2, 1L, SECONDS,
1296 >                          new ArrayBlockingQueue<Runnable>(10));
1297          try {
1298 <            tpe = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
992 <            tpe.execute(null);
1298 >            p.execute(null);
1299              shouldThrow();
1300          } catch (NullPointerException success) {}
1301  
1302 <        joinPool(tpe);
1302 >        joinPool(p);
1303      }
1304  
1305      /**
1306 <     *  setCorePoolSize of negative value throws IllegalArgumentException
1306 >     * setCorePoolSize of negative value throws IllegalArgumentException
1307       */
1308      public void testCorePoolSizeIllegalArgumentException() {
1309 <        ThreadPoolExecutor tpe =
1309 >        ThreadPoolExecutor p =
1310              new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1311          try {
1312 <            tpe.setCorePoolSize(-1);
1312 >            p.setCorePoolSize(-1);
1313              shouldThrow();
1314          } catch (IllegalArgumentException success) {
1315          } finally {
1316 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1316 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1317          }
1318 <        joinPool(tpe);
1318 >        joinPool(p);
1319      }
1320  
1321      /**
1322 <     *  setMaximumPoolSize(int) throws IllegalArgumentException if
1323 <     *  given a value less the core pool size
1322 >     * setMaximumPoolSize(int) throws IllegalArgumentException
1323 >     * if given a value less the core pool size
1324       */
1325      public void testMaximumPoolSizeIllegalArgumentException() {
1326 <        ThreadPoolExecutor tpe =
1326 >        ThreadPoolExecutor p =
1327              new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1328          try {
1329 <            tpe.setMaximumPoolSize(1);
1329 >            p.setMaximumPoolSize(1);
1330              shouldThrow();
1331          } catch (IllegalArgumentException success) {
1332          } finally {
1333 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1333 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1334          }
1335 <        joinPool(tpe);
1335 >        joinPool(p);
1336      }
1337  
1338      /**
1339 <     *  setMaximumPoolSize throws IllegalArgumentException
1340 <     *  if given a negative value
1339 >     * setMaximumPoolSize throws IllegalArgumentException
1340 >     * if given a negative value
1341       */
1342      public void testMaximumPoolSizeIllegalArgumentException2() {
1343 <        ThreadPoolExecutor tpe =
1343 >        ThreadPoolExecutor p =
1344              new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1345          try {
1346 <            tpe.setMaximumPoolSize(-1);
1346 >            p.setMaximumPoolSize(-1);
1347              shouldThrow();
1348          } catch (IllegalArgumentException success) {
1349          } finally {
1350 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1350 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1351          }
1352 <        joinPool(tpe);
1352 >        joinPool(p);
1353      }
1354  
1049
1355      /**
1356 <     *  setKeepAliveTime  throws IllegalArgumentException
1357 <     *  when given a negative value
1356 >     * setKeepAliveTime throws IllegalArgumentException
1357 >     * when given a negative value
1358       */
1359      public void testKeepAliveTimeIllegalArgumentException() {
1360 <        ThreadPoolExecutor tpe =
1360 >        ThreadPoolExecutor p =
1361              new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1362  
1363          try {
1364 <            tpe.setKeepAliveTime(-1,MILLISECONDS);
1364 >            p.setKeepAliveTime(-1,MILLISECONDS);
1365              shouldThrow();
1366          } catch (IllegalArgumentException success) {
1367          } finally {
1368 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1368 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1369          }
1370 <        joinPool(tpe);
1370 >        joinPool(p);
1371      }
1372  
1373      /**
1374       * terminated() is called on termination
1375       */
1376      public void testTerminated() {
1377 <        CustomTPE tpe = new CustomTPE();
1378 <        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1379 <        assertTrue(tpe.terminatedCalled);
1380 <        joinPool(tpe);
1377 >        CustomTPE p = new CustomTPE();
1378 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1379 >        assertTrue(p.terminatedCalled());
1380 >        joinPool(p);
1381      }
1382  
1383      /**
1384       * beforeExecute and afterExecute are called when executing task
1385       */
1386      public void testBeforeAfter() throws InterruptedException {
1387 <        CustomTPE tpe = new CustomTPE();
1387 >        CustomTPE p = new CustomTPE();
1388          try {
1389 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1390 <            tpe.execute(r);
1391 <            Thread.sleep(SHORT_DELAY_MS);
1392 <            assertTrue(r.done);
1393 <            assertTrue(tpe.beforeCalled);
1394 <            assertTrue(tpe.afterCalled);
1395 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1389 >            final CountDownLatch done = new CountDownLatch(1);
1390 >            p.execute(new CheckedRunnable() {
1391 >                public void realRun() {
1392 >                    done.countDown();
1393 >                }});
1394 >            await(p.afterCalled);
1395 >            assertEquals(0, done.getCount());
1396 >            assertTrue(p.afterCalled());
1397 >            assertTrue(p.beforeCalled());
1398 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1399          } finally {
1400 <            joinPool(tpe);
1400 >            joinPool(p);
1401          }
1402      }
1403  
# Line 1135 | Line 1443 | public class ThreadPoolExecutorSubclassT
1443          }
1444      }
1445  
1138
1446      /**
1447       * invokeAny(null) throws NPE
1448       */
# Line 1297 | Line 1604 | public class ThreadPoolExecutorSubclassT
1604          }
1605      }
1606  
1300
1301
1607      /**
1608       * timed invokeAny(null) throws NPE
1609       */
# Line 1485 | Line 1790 | public class ThreadPoolExecutorSubclassT
1790              l.add(new StringTask());
1791              l.add(new StringTask());
1792              List<Future<String>> futures =
1793 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1793 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1794              assertEquals(2, futures.size());
1795              for (Future<String> future : futures)
1796                  assertSame(TEST_STRING, future.get());
# Line 1500 | Line 1805 | public class ThreadPoolExecutorSubclassT
1805      public void testTimedInvokeAll6() throws Exception {
1806          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1807          try {
1808 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1809 <            l.add(new StringTask());
1810 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1811 <            l.add(new StringTask());
1812 <            List<Future<String>> futures =
1813 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1814 <            assertEquals(3, futures.size());
1815 <            Iterator<Future<String>> it = futures.iterator();
1816 <            Future<String> f1 = it.next();
1817 <            Future<String> f2 = it.next();
1818 <            Future<String> f3 = it.next();
1819 <            assertTrue(f1.isDone());
1820 <            assertTrue(f2.isDone());
1821 <            assertTrue(f3.isDone());
1822 <            assertFalse(f1.isCancelled());
1823 <            assertTrue(f2.isCancelled());
1808 >            for (long timeout = timeoutMillis();;) {
1809 >                List<Callable<String>> tasks = new ArrayList<>();
1810 >                tasks.add(new StringTask("0"));
1811 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1812 >                tasks.add(new StringTask("2"));
1813 >                long startTime = System.nanoTime();
1814 >                List<Future<String>> futures =
1815 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1816 >                assertEquals(tasks.size(), futures.size());
1817 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1818 >                for (Future future : futures)
1819 >                    assertTrue(future.isDone());
1820 >                assertTrue(futures.get(1).isCancelled());
1821 >                try {
1822 >                    assertEquals("0", futures.get(0).get());
1823 >                    assertEquals("2", futures.get(2).get());
1824 >                    break;
1825 >                } catch (CancellationException retryWithLongerTimeout) {
1826 >                    timeout *= 2;
1827 >                    if (timeout >= LONG_DELAY_MS / 2)
1828 >                        fail("expected exactly one task to be cancelled");
1829 >                }
1830 >            }
1831          } finally {
1832              joinPool(e);
1833          }
# Line 1526 | Line 1838 | public class ThreadPoolExecutorSubclassT
1838       * thread factory fails to create more
1839       */
1840      public void testFailingThreadFactory() throws InterruptedException {
1841 <        ExecutorService e = new CustomTPE(100, 100, LONG_DELAY_MS, MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory());
1842 <        try {
1843 <            for (int k = 0; k < 100; ++k) {
1844 <                e.execute(new NoOpRunnable());
1845 <            }
1846 <            Thread.sleep(LONG_DELAY_MS);
1841 >        final ExecutorService e =
1842 >            new CustomTPE(100, 100,
1843 >                          LONG_DELAY_MS, MILLISECONDS,
1844 >                          new LinkedBlockingQueue<Runnable>(),
1845 >                          new FailingThreadFactory());
1846 >        try {
1847 >            final int TASKS = 100;
1848 >            final CountDownLatch done = new CountDownLatch(TASKS);
1849 >            for (int k = 0; k < TASKS; ++k)
1850 >                e.execute(new CheckedRunnable() {
1851 >                    public void realRun() {
1852 >                        done.countDown();
1853 >                    }});
1854 >            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1855          } finally {
1856              joinPool(e);
1857          }
# Line 1541 | Line 1861 | public class ThreadPoolExecutorSubclassT
1861       * allowsCoreThreadTimeOut is by default false.
1862       */
1863      public void testAllowsCoreThreadTimeOut() {
1864 <        ThreadPoolExecutor tpe = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1865 <        assertFalse(tpe.allowsCoreThreadTimeOut());
1866 <        joinPool(tpe);
1864 >        ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1865 >        assertFalse(p.allowsCoreThreadTimeOut());
1866 >        joinPool(p);
1867      }
1868  
1869      /**
1870       * allowCoreThreadTimeOut(true) causes idle threads to time out
1871       */
1872 <    public void testAllowCoreThreadTimeOut_true() throws InterruptedException {
1873 <        ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1874 <        tpe.allowCoreThreadTimeOut(true);
1875 <        tpe.execute(new NoOpRunnable());
1876 <        try {
1877 <            Thread.sleep(MEDIUM_DELAY_MS);
1878 <            assertEquals(0, tpe.getPoolSize());
1872 >    public void testAllowCoreThreadTimeOut_true() throws Exception {
1873 >        long keepAliveTime = timeoutMillis();
1874 >        final ThreadPoolExecutor p =
1875 >            new CustomTPE(2, 10,
1876 >                          keepAliveTime, MILLISECONDS,
1877 >                          new ArrayBlockingQueue<Runnable>(10));
1878 >        final CountDownLatch threadStarted = new CountDownLatch(1);
1879 >        try {
1880 >            p.allowCoreThreadTimeOut(true);
1881 >            p.execute(new CheckedRunnable() {
1882 >                public void realRun() {
1883 >                    threadStarted.countDown();
1884 >                    assertEquals(1, p.getPoolSize());
1885 >                }});
1886 >            await(threadStarted);
1887 >            delay(keepAliveTime);
1888 >            long startTime = System.nanoTime();
1889 >            while (p.getPoolSize() > 0
1890 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1891 >                Thread.yield();
1892 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1893 >            assertEquals(0, p.getPoolSize());
1894          } finally {
1895 <            joinPool(tpe);
1895 >            joinPool(p);
1896          }
1897      }
1898  
1899      /**
1900       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1901       */
1902 <    public void testAllowCoreThreadTimeOut_false() throws InterruptedException {
1903 <        ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1904 <        tpe.allowCoreThreadTimeOut(false);
1905 <        tpe.execute(new NoOpRunnable());
1906 <        try {
1907 <            Thread.sleep(MEDIUM_DELAY_MS);
1908 <            assertTrue(tpe.getPoolSize() >= 1);
1902 >    public void testAllowCoreThreadTimeOut_false() throws Exception {
1903 >        long keepAliveTime = timeoutMillis();
1904 >        final ThreadPoolExecutor p =
1905 >            new CustomTPE(2, 10,
1906 >                          keepAliveTime, MILLISECONDS,
1907 >                          new ArrayBlockingQueue<Runnable>(10));
1908 >        final CountDownLatch threadStarted = new CountDownLatch(1);
1909 >        try {
1910 >            p.allowCoreThreadTimeOut(false);
1911 >            p.execute(new CheckedRunnable() {
1912 >                public void realRun() throws InterruptedException {
1913 >                    threadStarted.countDown();
1914 >                    assertTrue(p.getPoolSize() >= 1);
1915 >                }});
1916 >            delay(2 * keepAliveTime);
1917 >            assertTrue(p.getPoolSize() >= 1);
1918 >        } finally {
1919 >            joinPool(p);
1920 >        }
1921 >    }
1922 >
1923 >    /**
1924 >     * get(cancelled task) throws CancellationException
1925 >     * (in part, a test of CustomTPE itself)
1926 >     */
1927 >    public void testGet_cancelled() throws Exception {
1928 >        final ExecutorService e =
1929 >            new CustomTPE(1, 1,
1930 >                          LONG_DELAY_MS, MILLISECONDS,
1931 >                          new LinkedBlockingQueue<Runnable>());
1932 >        try {
1933 >            final CountDownLatch blockerStarted = new CountDownLatch(1);
1934 >            final CountDownLatch done = new CountDownLatch(1);
1935 >            final List<Future<?>> futures = new ArrayList<>();
1936 >            for (int i = 0; i < 2; i++) {
1937 >                Runnable r = new CheckedRunnable() { public void realRun()
1938 >                                                         throws Throwable {
1939 >                    blockerStarted.countDown();
1940 >                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
1941 >                }};
1942 >                futures.add(e.submit(r));
1943 >            }
1944 >            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
1945 >            for (Future<?> future : futures) future.cancel(false);
1946 >            for (Future<?> future : futures) {
1947 >                try {
1948 >                    future.get();
1949 >                    shouldThrow();
1950 >                } catch (CancellationException success) {}
1951 >                try {
1952 >                    future.get(LONG_DELAY_MS, MILLISECONDS);
1953 >                    shouldThrow();
1954 >                } catch (CancellationException success) {}
1955 >                assertTrue(future.isCancelled());
1956 >                assertTrue(future.isDone());
1957 >            }
1958 >            done.countDown();
1959          } finally {
1960 <            joinPool(tpe);
1960 >            joinPool(e);
1961          }
1962      }
1963  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines