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.19 by jsr166, Sat Oct 9 19:30:35 2010 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.*;
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
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  
# Line 209 | Line 248 | public class ThreadPoolExecutorSubclassT
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 >        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 >        }
269      }
270  
271      /**
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
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      /**
# Line 250 | Line 321 | public class ThreadPoolExecutorSubclassT
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
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
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
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      /**
# Line 371 | Line 503 | public class ThreadPoolExecutorSubclassT
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 >        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() {
524 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
525 <        assertEquals(0, p1.getPoolSize());
526 <        p1.execute(new MediumRunnable());
527 <        assertEquals(1, p1.getPoolSize());
528 <        joinPool(p1);
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 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
549 <        assertEquals(0, p1.getTaskCount());
550 <        p1.execute(new MediumRunnable());
551 <        Thread.sleep(SHORT_DELAY_MS);
552 <        assertEquals(1, p1.getTaskCount());
553 <        joinPool(p1);
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 >        }
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, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
574 <        assertFalse(p1.isShutdown());
575 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
576 <        assertTrue(p1.isShutdown());
577 <        joinPool(p1);
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  
415
583      /**
584       * isTerminated is false before termination, true after
585       */
586      public void testIsTerminated() throws InterruptedException {
587 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
588 <        assertFalse(p1.isTerminated());
589 <        try {
590 <            p1.execute(new MediumRunnable());
591 <        } finally {
592 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
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          }
427        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
428        assertTrue(p1.isTerminated());
609      }
610  
611      /**
612       * isTerminating is not true when running or when terminated
613       */
614      public void testIsTerminating() throws InterruptedException {
615 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
616 <        assertFalse(p1.isTerminating());
617 <        try {
618 <            p1.execute(new SmallRunnable());
619 <            assertFalse(p1.isTerminating());
620 <        } finally {
621 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
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          }
443        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
444        assertTrue(p1.isTerminated());
445        assertFalse(p1.isTerminating());
637      }
638  
639      /**
640       * getQueue returns the work queue, which contains queued tasks
641       */
642      public void testGetQueue() throws InterruptedException {
643 <        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
644 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, 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 <        } finally {
660 <            joinPool(p1);
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  
# Line 475 | Line 674 | public class ThreadPoolExecutorSubclassT
674       */
675      public void testRemove() throws InterruptedException {
676          BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
677 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, 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]));
703          } finally {
704 <            joinPool(p1);
704 >            done.countDown();
705 >            joinPool(p);
706          }
707      }
708  
709      /**
710       * purge removes cancelled tasks from the queue
711       */
712 <    public void testPurge() {
713 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, 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          }
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);
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, 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 <        assertTrue(p1.isShutdown());
770 <        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  
539
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, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
797 >            new CustomTPE(-1, 1, 1L, SECONDS,
798 >                          new ArrayBlockingQueue<Runnable>(10));
799              shouldThrow();
800          } catch (IllegalArgumentException success) {}
801      }
# Line 552 | Line 805 | public class ThreadPoolExecutorSubclassT
805       */
806      public void testConstructor2() {
807          try {
808 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
808 >            new CustomTPE(1, -1, 1L, SECONDS,
809 >                          new ArrayBlockingQueue<Runnable>(10));
810              shouldThrow();
811          } catch (IllegalArgumentException success) {}
812      }
# Line 562 | Line 816 | public class ThreadPoolExecutorSubclassT
816       */
817      public void testConstructor3() {
818          try {
819 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
819 >            new CustomTPE(1, 0, 1L, SECONDS,
820 >                          new ArrayBlockingQueue<Runnable>(10));
821              shouldThrow();
822          } catch (IllegalArgumentException success) {}
823      }
# Line 572 | Line 827 | public class ThreadPoolExecutorSubclassT
827       */
828      public void testConstructor4() {
829          try {
830 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
830 >            new CustomTPE(1, 2, -1L, SECONDS,
831 >                          new ArrayBlockingQueue<Runnable>(10));
832              shouldThrow();
833          } catch (IllegalArgumentException success) {}
834      }
# Line 582 | Line 838 | public class ThreadPoolExecutorSubclassT
838       */
839      public void testConstructor5() {
840          try {
841 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
841 >            new CustomTPE(2, 1, 1L, SECONDS,
842 >                          new ArrayBlockingQueue<Runnable>(10));
843              shouldThrow();
844          } catch (IllegalArgumentException success) {}
845      }
# Line 592 | Line 849 | public class ThreadPoolExecutorSubclassT
849       */
850      public void testConstructorNullPointerException() {
851          try {
852 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null);
852 >            new CustomTPE(1, 2, 1L, SECONDS, null);
853              shouldThrow();
854          } catch (NullPointerException success) {}
855      }
856  
600
601
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, 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) {}
867      }
# Line 614 | Line 871 | public class ThreadPoolExecutorSubclassT
871       */
872      public void testConstructor7() {
873          try {
874 <            new CustomTPE(1,-1,LONG_DELAY_MS, 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          } catch (IllegalArgumentException success) {}
879      }
# Line 624 | Line 883 | public class ThreadPoolExecutorSubclassT
883       */
884      public void testConstructor8() {
885          try {
886 <            new CustomTPE(1,0,LONG_DELAY_MS, 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          } catch (IllegalArgumentException success) {}
891      }
# Line 634 | Line 895 | public class ThreadPoolExecutorSubclassT
895       */
896      public void testConstructor9() {
897          try {
898 <            new CustomTPE(1,2,-1L,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          } catch (IllegalArgumentException success) {}
903      }
# Line 644 | Line 907 | public class ThreadPoolExecutorSubclassT
907       */
908      public void testConstructor10() {
909          try {
910 <            new CustomTPE(2,1,LONG_DELAY_MS, 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          } catch (IllegalArgumentException success) {}
915      }
# Line 654 | Line 919 | public class ThreadPoolExecutorSubclassT
919       */
920      public void testConstructorNullPointerException2() {
921          try {
922 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
922 >            new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory());
923              shouldThrow();
924          } catch (NullPointerException success) {}
925      }
# Line 664 | Line 929 | public class ThreadPoolExecutorSubclassT
929       */
930      public void testConstructorNullPointerException3() {
931          try {
932 <            ThreadFactory f = null;
933 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
932 >            new CustomTPE(1, 2, 1L, SECONDS,
933 >                          new ArrayBlockingQueue<Runnable>(10),
934 >                          (ThreadFactory) null);
935              shouldThrow();
936          } catch (NullPointerException success) {}
937      }
938  
673
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, 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          } catch (IllegalArgumentException success) {}
949      }
# Line 686 | Line 953 | public class ThreadPoolExecutorSubclassT
953       */
954      public void testConstructor12() {
955          try {
956 <            new CustomTPE(1,-1,LONG_DELAY_MS, 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          } catch (IllegalArgumentException success) {}
961      }
# Line 696 | Line 965 | public class ThreadPoolExecutorSubclassT
965       */
966      public void testConstructor13() {
967          try {
968 <            new CustomTPE(1,0,LONG_DELAY_MS, 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          } catch (IllegalArgumentException success) {}
973      }
# Line 706 | Line 977 | public class ThreadPoolExecutorSubclassT
977       */
978      public void testConstructor14() {
979          try {
980 <            new CustomTPE(1,2,-1L,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          } catch (IllegalArgumentException success) {}
985      }
# Line 716 | Line 989 | public class ThreadPoolExecutorSubclassT
989       */
990      public void testConstructor15() {
991          try {
992 <            new CustomTPE(2,1,LONG_DELAY_MS, 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          } catch (IllegalArgumentException success) {}
997      }
# Line 726 | Line 1001 | public class ThreadPoolExecutorSubclassT
1001       */
1002      public void testConstructorNullPointerException4() {
1003          try {
1004 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
1004 >            new CustomTPE(1, 2, 1L, SECONDS,
1005 >                          null,
1006 >                          new NoOpREHandler());
1007              shouldThrow();
1008          } catch (NullPointerException success) {}
1009      }
# Line 736 | Line 1013 | public class ThreadPoolExecutorSubclassT
1013       */
1014      public void testConstructorNullPointerException5() {
1015          try {
1016 <            RejectedExecutionHandler r = null;
1017 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
1016 >            new CustomTPE(1, 2, 1L, SECONDS,
1017 >                          new ArrayBlockingQueue<Runnable>(10),
1018 >                          (RejectedExecutionHandler) null);
1019              shouldThrow();
1020          } catch (NullPointerException success) {}
1021      }
1022  
745
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, 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          } catch (IllegalArgumentException success) {}
1034      }
# Line 758 | Line 1038 | public class ThreadPoolExecutorSubclassT
1038       */
1039      public void testConstructor17() {
1040          try {
1041 <            new CustomTPE(1,-1,LONG_DELAY_MS, 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          } catch (IllegalArgumentException success) {}
1047      }
# Line 768 | Line 1051 | public class ThreadPoolExecutorSubclassT
1051       */
1052      public void testConstructor18() {
1053          try {
1054 <            new CustomTPE(1,0,LONG_DELAY_MS, 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          } catch (IllegalArgumentException success) {}
1060      }
# Line 778 | Line 1064 | public class ThreadPoolExecutorSubclassT
1064       */
1065      public void testConstructor19() {
1066          try {
1067 <            new CustomTPE(1,2,-1L,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          } catch (IllegalArgumentException success) {}
1073      }
# Line 788 | Line 1077 | public class ThreadPoolExecutorSubclassT
1077       */
1078      public void testConstructor20() {
1079          try {
1080 <            new CustomTPE(2,1,LONG_DELAY_MS, 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          } 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, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
1093 >            new CustomTPE(1, 2, 1L, SECONDS,
1094 >                          null,
1095 >                          new SimpleThreadFactory(),
1096 >                          new NoOpREHandler());
1097              shouldThrow();
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, 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          } 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, 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          } catch (NullPointerException success) {}
1125      }
1126  
828
1127      /**
1128       * execute throws RejectedExecutionException if saturated.
1129       */
1130      public void testSaturatedExecute() {
1131 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, 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      /**
# Line 846 | Line 1158 | public class ThreadPoolExecutorSubclassT
1158       */
1159      public void testSaturatedExecute2() {
1160          RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1161 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, 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 {
851
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();
855            }
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 <            }
861 <            for (int i = 1; i < 5; ++i) {
1173 >            for (int i = 1; i < tasks.length; ++i)
1174                  assertTrue(tasks[i].done);
863            }
1175              try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1176          } finally {
1177              joinPool(p);
# Line 872 | Line 1183 | public class ThreadPoolExecutorSubclassT
1183       */
1184      public void testSaturatedExecute3() {
1185          RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1186 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, 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 {
877
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();
881            }
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) {
887 <                assertFalse(tasks[i].done);
888 <            }
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);
# Line 917 | Line 1228 | public class ThreadPoolExecutorSubclassT
1228       * execute throws RejectedExecutionException if shutdown
1229       */
1230      public void testRejectedExecutionExceptionOnShutdown() {
1231 <        ThreadPoolExecutor tpe =
1231 >        ThreadPoolExecutor p =
1232              new CustomTPE(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
1233 <        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1233 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1234          try {
1235 <            tpe.execute(new NoOpRunnable());
1235 >            p.execute(new NoOpRunnable());
1236              shouldThrow();
1237          } catch (RejectedExecutionException success) {}
1238  
1239 <        joinPool(tpe);
1239 >        joinPool(p);
1240      }
1241  
1242      /**
# Line 962 | Line 1273 | public class ThreadPoolExecutorSubclassT
1273          }
1274      }
1275  
965
1276      /**
1277       * execute using DiscardOldestPolicy drops task on shutdown
1278       */
# Line 980 | Line 1290 | public class ThreadPoolExecutorSubclassT
1290          }
1291      }
1292  
983
1293      /**
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, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
991 <            tpe.execute(null);
1301 >            p.execute(null);
1302              shouldThrow();
1303          } catch (NullPointerException success) {}
1304  
1305 <        joinPool(tpe);
1305 >        joinPool(p);
1306      }
1307  
1308      /**
1309       * setCorePoolSize of negative value throws IllegalArgumentException
1310       */
1311      public void testCorePoolSizeIllegalArgumentException() {
1312 <        ThreadPoolExecutor tpe =
1312 >        ThreadPoolExecutor p =
1313              new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1314          try {
1315 <            tpe.setCorePoolSize(-1);
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      /**
# Line 1016 | Line 1326 | public class ThreadPoolExecutorSubclassT
1326       * if given a value less the core pool size
1327       */
1328      public void testMaximumPoolSizeIllegalArgumentException() {
1329 <        ThreadPoolExecutor tpe =
1329 >        ThreadPoolExecutor p =
1330              new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1331          try {
1332 <            tpe.setMaximumPoolSize(1);
1332 >            p.setMaximumPoolSize(1);
1333              shouldThrow();
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      /**
# Line 1033 | Line 1343 | public class ThreadPoolExecutorSubclassT
1343       * if given a negative value
1344       */
1345      public void testMaximumPoolSizeIllegalArgumentException2() {
1346 <        ThreadPoolExecutor tpe =
1346 >        ThreadPoolExecutor p =
1347              new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1348          try {
1349 <            tpe.setMaximumPoolSize(-1);
1349 >            p.setMaximumPoolSize(-1);
1350              shouldThrow();
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  
1048
1358      /**
1359       * setKeepAliveTime throws IllegalArgumentException
1360       * when given a negative value
1361       */
1362      public void testKeepAliveTimeIllegalArgumentException() {
1363 <        ThreadPoolExecutor tpe =
1363 >        ThreadPoolExecutor p =
1364              new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1365  
1366          try {
1367 <            tpe.setKeepAliveTime(-1,MILLISECONDS);
1367 >            p.setKeepAliveTime(-1,MILLISECONDS);
1368              shouldThrow();
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() throws InterruptedException {
1390 <        CustomTPE tpe = new CustomTPE();
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; }
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  
# Line 1134 | Line 1446 | public class ThreadPoolExecutorSubclassT
1446          }
1447      }
1448  
1137
1449      /**
1450       * invokeAny(null) throws NPE
1451       */
# Line 1296 | Line 1607 | public class ThreadPoolExecutorSubclassT
1607          }
1608      }
1609  
1299
1300
1610      /**
1611       * timed invokeAny(null) throws NPE
1612       */
# Line 1484 | Line 1793 | public class ThreadPoolExecutorSubclassT
1793              l.add(new StringTask());
1794              l.add(new StringTask());
1795              List<Future<String>> futures =
1796 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
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());
# Line 1499 | Line 1808 | public class ThreadPoolExecutorSubclassT
1808      public void testTimedInvokeAll6() throws Exception {
1809          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1810          try {
1811 <            List<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>> futures =
1816 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1817 <            assertEquals(3, futures.size());
1818 <            Iterator<Future<String>> it = futures.iterator();
1819 <            Future<String> f1 = it.next();
1820 <            Future<String> f2 = it.next();
1821 <            Future<String> f3 = it.next();
1822 <            assertTrue(f1.isDone());
1823 <            assertTrue(f2.isDone());
1824 <            assertTrue(f3.isDone());
1825 <            assertFalse(f1.isCancelled());
1826 <            assertTrue(f2.isCancelled());
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 1525 | Line 1841 | public class ThreadPoolExecutorSubclassT
1841       * thread factory fails to create more
1842       */
1843      public void testFailingThreadFactory() throws InterruptedException {
1844 <        ExecutorService e = new CustomTPE(100, 100, LONG_DELAY_MS, MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory());
1845 <        try {
1846 <            for (int k = 0; k < 100; ++k) {
1847 <                e.execute(new NoOpRunnable());
1848 <            }
1849 <            Thread.sleep(LONG_DELAY_MS);
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 1540 | Line 1864 | public class ThreadPoolExecutorSubclassT
1864       * allowsCoreThreadTimeOut is by default false.
1865       */
1866      public void testAllowsCoreThreadTimeOut() {
1867 <        ThreadPoolExecutor tpe = new CustomTPE(2, 2, 1000, 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() throws InterruptedException {
1876 <        ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, 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());
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() throws InterruptedException {
1906 <        ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, 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);
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(tpe);
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(e);
1964          }
1965      }
1966  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines