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.20 by jsr166, Sat Oct 9 22:27:16 2010 UTC vs.
Revision 1.37 by jsr166, Fri May 15 18:21:19 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.CountDownLatch;
18 > import java.util.concurrent.ExecutionException;
19 > import java.util.concurrent.Executors;
20 > import java.util.concurrent.ExecutorService;
21 > import java.util.concurrent.Future;
22 > import java.util.concurrent.FutureTask;
23 > import java.util.concurrent.LinkedBlockingQueue;
24 > import java.util.concurrent.RejectedExecutionException;
25 > import java.util.concurrent.RejectedExecutionHandler;
26 > import java.util.concurrent.RunnableFuture;
27 > import java.util.concurrent.SynchronousQueue;
28 > import java.util.concurrent.ThreadFactory;
29 > import java.util.concurrent.ThreadPoolExecutor;
30 > import java.util.concurrent.TimeoutException;
31 > import java.util.concurrent.TimeUnit;
32 > import java.util.concurrent.locks.Condition;
33 > import java.util.concurrent.locks.ReentrantLock;
34 >
35 > import junit.framework.Test;
36 > import junit.framework.TestSuite;
37  
38   public class ThreadPoolExecutorSubclassTest extends JSR166TestCase {
39      public static void main(String[] args) {
40 <        junit.textui.TestRunner.run(suite());
40 >        main(suite(), args);
41      }
42      public static Test suite() {
43          return new TestSuite(ThreadPoolExecutorSubclassTest.class);
# Line 37 | Line 59 | public class ThreadPoolExecutorSubclassT
59          CustomTask(final Runnable r, final V res) {
60              if (r == null) throw new NullPointerException();
61              callable = new Callable<V>() {
62 <            public V call() throws Exception { r.run(); return res; }};
62 >                public V call() throws Exception { r.run(); return res; }};
63          }
64          public boolean isDone() {
65              lock.lock(); try { return done; } finally { lock.unlock() ; }
# Line 60 | Line 82 | public class ThreadPoolExecutorSubclassT
82              finally { lock.unlock() ; }
83          }
84          public void run() {
63            boolean runme;
85              lock.lock();
86              try {
87 <                runme = !done;
88 <                if (!runme)
89 <                    thread = Thread.currentThread();
87 >                if (done)
88 >                    return;
89 >                thread = Thread.currentThread();
90              }
91              finally { lock.unlock() ; }
71            if (!runme) return;
92              V v = null;
93              Exception e = null;
94              try {
# Line 117 | Line 137 | public class ThreadPoolExecutorSubclassT
137          }
138      }
139  
120
140      static class CustomTPE extends ThreadPoolExecutor {
141          protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
142              return new CustomTask<V>(c);
# Line 164 | Line 183 | public class ThreadPoolExecutorSubclassT
183                workQueue, threadFactory, handler);
184          }
185  
186 <        volatile boolean beforeCalled = false;
187 <        volatile boolean afterCalled = false;
188 <        volatile boolean terminatedCalled = false;
186 >        final CountDownLatch beforeCalled = new CountDownLatch(1);
187 >        final CountDownLatch afterCalled = new CountDownLatch(1);
188 >        final CountDownLatch terminatedCalled = new CountDownLatch(1);
189 >
190          public CustomTPE() {
191              super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
192          }
193          protected void beforeExecute(Thread t, Runnable r) {
194 <            beforeCalled = true;
194 >            beforeCalled.countDown();
195          }
196          protected void afterExecute(Runnable r, Throwable t) {
197 <            afterCalled = true;
197 >            afterCalled.countDown();
198          }
199          protected void terminated() {
200 <            terminatedCalled = true;
200 >            terminatedCalled.countDown();
201          }
202  
203 +        public boolean beforeCalled() {
204 +            return beforeCalled.getCount() == 0;
205 +        }
206 +        public boolean afterCalled() {
207 +            return afterCalled.getCount() == 0;
208 +        }
209 +        public boolean terminatedCalled() {
210 +            return terminatedCalled.getCount() == 0;
211 +        }
212      }
213  
214      static class FailingThreadFactory implements ThreadFactory {
# Line 190 | Line 219 | public class ThreadPoolExecutorSubclassT
219          }
220      }
221  
193
222      /**
223       * execute successfully executes a runnable
224       */
225      public void testExecute() throws InterruptedException {
226 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
226 >        final ThreadPoolExecutor p =
227 >            new CustomTPE(1, 1,
228 >                          LONG_DELAY_MS, MILLISECONDS,
229 >                          new ArrayBlockingQueue<Runnable>(10));
230 >        final CountDownLatch done = new CountDownLatch(1);
231 >        final Runnable task = new CheckedRunnable() {
232 >            public void realRun() {
233 >                done.countDown();
234 >            }};
235          try {
236 <            p1.execute(new ShortRunnable());
237 <            Thread.sleep(SMALL_DELAY_MS);
236 >            p.execute(task);
237 >            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
238          } finally {
239 <            joinPool(p1);
239 >            joinPool(p);
240          }
241      }
242  
# Line 209 | Line 245 | public class ThreadPoolExecutorSubclassT
245       * thread becomes active
246       */
247      public void testGetActiveCount() throws InterruptedException {
248 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
249 <        assertEquals(0, p2.getActiveCount());
250 <        p2.execute(new MediumRunnable());
251 <        Thread.sleep(SHORT_DELAY_MS);
252 <        assertEquals(1, p2.getActiveCount());
253 <        joinPool(p2);
248 >        final ThreadPoolExecutor p =
249 >            new CustomTPE(2, 2,
250 >                          LONG_DELAY_MS, MILLISECONDS,
251 >                          new ArrayBlockingQueue<Runnable>(10));
252 >        final CountDownLatch threadStarted = new CountDownLatch(1);
253 >        final CountDownLatch done = new CountDownLatch(1);
254 >        try {
255 >            assertEquals(0, p.getActiveCount());
256 >            p.execute(new CheckedRunnable() {
257 >                public void realRun() throws InterruptedException {
258 >                    threadStarted.countDown();
259 >                    assertEquals(1, p.getActiveCount());
260 >                    done.await();
261 >                }});
262 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
263 >            assertEquals(1, p.getActiveCount());
264 >        } finally {
265 >            done.countDown();
266 >            joinPool(p);
267 >        }
268      }
269  
270      /**
271       * prestartCoreThread starts a thread if under corePoolSize, else doesn't
272       */
273      public void testPrestartCoreThread() {
274 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
275 <        assertEquals(0, p2.getPoolSize());
276 <        assertTrue(p2.prestartCoreThread());
277 <        assertEquals(1, p2.getPoolSize());
278 <        assertTrue(p2.prestartCoreThread());
279 <        assertEquals(2, p2.getPoolSize());
280 <        assertFalse(p2.prestartCoreThread());
281 <        assertEquals(2, p2.getPoolSize());
282 <        joinPool(p2);
274 >        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
275 >        assertEquals(0, p.getPoolSize());
276 >        assertTrue(p.prestartCoreThread());
277 >        assertEquals(1, p.getPoolSize());
278 >        assertTrue(p.prestartCoreThread());
279 >        assertEquals(2, p.getPoolSize());
280 >        assertFalse(p.prestartCoreThread());
281 >        assertEquals(2, p.getPoolSize());
282 >        joinPool(p);
283      }
284  
285      /**
286       * prestartAllCoreThreads starts all corePoolSize threads
287       */
288      public void testPrestartAllCoreThreads() {
289 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
290 <        assertEquals(0, p2.getPoolSize());
291 <        p2.prestartAllCoreThreads();
292 <        assertEquals(2, p2.getPoolSize());
293 <        p2.prestartAllCoreThreads();
294 <        assertEquals(2, p2.getPoolSize());
295 <        joinPool(p2);
289 >        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
290 >        assertEquals(0, p.getPoolSize());
291 >        p.prestartAllCoreThreads();
292 >        assertEquals(2, p.getPoolSize());
293 >        p.prestartAllCoreThreads();
294 >        assertEquals(2, p.getPoolSize());
295 >        joinPool(p);
296      }
297  
298      /**
# Line 250 | Line 300 | public class ThreadPoolExecutorSubclassT
300       * when tasks complete
301       */
302      public void testGetCompletedTaskCount() throws InterruptedException {
303 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
304 <        assertEquals(0, p2.getCompletedTaskCount());
305 <        p2.execute(new ShortRunnable());
306 <        Thread.sleep(SMALL_DELAY_MS);
307 <        assertEquals(1, p2.getCompletedTaskCount());
308 <        try { p2.shutdown(); } catch (SecurityException ok) { return; }
309 <        joinPool(p2);
303 >        final ThreadPoolExecutor p =
304 >            new CustomTPE(2, 2,
305 >                          LONG_DELAY_MS, MILLISECONDS,
306 >                          new ArrayBlockingQueue<Runnable>(10));
307 >        final CountDownLatch threadStarted = new CountDownLatch(1);
308 >        final CountDownLatch threadProceed = new CountDownLatch(1);
309 >        final CountDownLatch threadDone = new CountDownLatch(1);
310 >        try {
311 >            assertEquals(0, p.getCompletedTaskCount());
312 >            p.execute(new CheckedRunnable() {
313 >                public void realRun() throws InterruptedException {
314 >                    threadStarted.countDown();
315 >                    assertEquals(0, p.getCompletedTaskCount());
316 >                    threadProceed.await();
317 >                    threadDone.countDown();
318 >                }});
319 >            await(threadStarted);
320 >            assertEquals(0, p.getCompletedTaskCount());
321 >            threadProceed.countDown();
322 >            threadDone.await();
323 >            long startTime = System.nanoTime();
324 >            while (p.getCompletedTaskCount() != 1) {
325 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
326 >                    fail("timed out");
327 >                Thread.yield();
328 >            }
329 >        } finally {
330 >            joinPool(p);
331 >        }
332      }
333  
334      /**
335       * getCorePoolSize returns size given in constructor if not otherwise set
336       */
337      public void testGetCorePoolSize() {
338 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
339 <        assertEquals(1, p1.getCorePoolSize());
340 <        joinPool(p1);
338 >        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
339 >        assertEquals(1, p.getCorePoolSize());
340 >        joinPool(p);
341      }
342  
343      /**
344       * getKeepAliveTime returns value given in constructor if not otherwise set
345       */
346      public void testGetKeepAliveTime() {
347 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
348 <        assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS));
349 <        joinPool(p2);
347 >        ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
348 >        assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS));
349 >        joinPool(p);
350      }
351  
280
352      /**
353       * getThreadFactory returns factory in constructor if not set
354       */
# Line 299 | Line 370 | public class ThreadPoolExecutorSubclassT
370          joinPool(p);
371      }
372  
302
373      /**
374       * setThreadFactory(null) throws NPE
375       */
# Line 336 | Line 406 | public class ThreadPoolExecutorSubclassT
406          joinPool(p);
407      }
408  
339
409      /**
410       * setRejectedExecutionHandler(null) throws NPE
411       */
# Line 351 | Line 420 | public class ThreadPoolExecutorSubclassT
420          }
421      }
422  
354
423      /**
424       * getLargestPoolSize increases, but doesn't overestimate, when
425       * multiple threads active
426       */
427      public void testGetLargestPoolSize() throws InterruptedException {
428 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
429 <        assertEquals(0, p2.getLargestPoolSize());
430 <        p2.execute(new MediumRunnable());
431 <        p2.execute(new MediumRunnable());
432 <        Thread.sleep(SHORT_DELAY_MS);
433 <        assertEquals(2, p2.getLargestPoolSize());
434 <        joinPool(p2);
428 >        final int THREADS = 3;
429 >        final ThreadPoolExecutor p =
430 >            new CustomTPE(THREADS, THREADS,
431 >                          LONG_DELAY_MS, MILLISECONDS,
432 >                          new ArrayBlockingQueue<Runnable>(10));
433 >        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
434 >        final CountDownLatch done = new CountDownLatch(1);
435 >        try {
436 >            assertEquals(0, p.getLargestPoolSize());
437 >            for (int i = 0; i < THREADS; i++)
438 >                p.execute(new CheckedRunnable() {
439 >                    public void realRun() throws InterruptedException {
440 >                        threadsStarted.countDown();
441 >                        done.await();
442 >                        assertEquals(THREADS, p.getLargestPoolSize());
443 >                    }});
444 >            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
445 >            assertEquals(THREADS, p.getLargestPoolSize());
446 >        } finally {
447 >            done.countDown();
448 >            joinPool(p);
449 >            assertEquals(THREADS, p.getLargestPoolSize());
450 >        }
451      }
452  
453      /**
# Line 371 | Line 455 | public class ThreadPoolExecutorSubclassT
455       * otherwise set
456       */
457      public void testGetMaximumPoolSize() {
458 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
459 <        assertEquals(2, p2.getMaximumPoolSize());
460 <        joinPool(p2);
458 >        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
459 >        assertEquals(2, p.getMaximumPoolSize());
460 >        joinPool(p);
461      }
462  
463      /**
464       * getPoolSize increases, but doesn't overestimate, when threads
465       * become active
466       */
467 <    public void testGetPoolSize() {
468 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
469 <        assertEquals(0, p1.getPoolSize());
470 <        p1.execute(new MediumRunnable());
471 <        assertEquals(1, p1.getPoolSize());
472 <        joinPool(p1);
467 >    public void testGetPoolSize() throws InterruptedException {
468 >        final ThreadPoolExecutor p =
469 >            new CustomTPE(1, 1,
470 >                          LONG_DELAY_MS, MILLISECONDS,
471 >                          new ArrayBlockingQueue<Runnable>(10));
472 >        final CountDownLatch threadStarted = new CountDownLatch(1);
473 >        final CountDownLatch done = new CountDownLatch(1);
474 >        try {
475 >            assertEquals(0, p.getPoolSize());
476 >            p.execute(new CheckedRunnable() {
477 >                public void realRun() throws InterruptedException {
478 >                    threadStarted.countDown();
479 >                    assertEquals(1, p.getPoolSize());
480 >                    done.await();
481 >                }});
482 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
483 >            assertEquals(1, p.getPoolSize());
484 >        } finally {
485 >            done.countDown();
486 >            joinPool(p);
487 >        }
488      }
489  
490      /**
491       * getTaskCount increases, but doesn't overestimate, when tasks submitted
492       */
493      public void testGetTaskCount() throws InterruptedException {
494 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
495 <        assertEquals(0, p1.getTaskCount());
496 <        p1.execute(new MediumRunnable());
497 <        Thread.sleep(SHORT_DELAY_MS);
498 <        assertEquals(1, p1.getTaskCount());
499 <        joinPool(p1);
494 >        final ThreadPoolExecutor p =
495 >            new CustomTPE(1, 1,
496 >                          LONG_DELAY_MS, MILLISECONDS,
497 >                          new ArrayBlockingQueue<Runnable>(10));
498 >        final CountDownLatch threadStarted = new CountDownLatch(1);
499 >        final CountDownLatch done = new CountDownLatch(1);
500 >        try {
501 >            assertEquals(0, p.getTaskCount());
502 >            p.execute(new CheckedRunnable() {
503 >                public void realRun() throws InterruptedException {
504 >                    threadStarted.countDown();
505 >                    assertEquals(1, p.getTaskCount());
506 >                    done.await();
507 >                }});
508 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
509 >            assertEquals(1, p.getTaskCount());
510 >        } finally {
511 >            done.countDown();
512 >            joinPool(p);
513 >        }
514      }
515  
516      /**
517 <     * isShutDown is false before shutdown, true after
517 >     * isShutdown is false before shutdown, true after
518       */
519      public void testIsShutdown() {
520  
521 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
522 <        assertFalse(p1.isShutdown());
523 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
524 <        assertTrue(p1.isShutdown());
525 <        joinPool(p1);
521 >        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
522 >        assertFalse(p.isShutdown());
523 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
524 >        assertTrue(p.isShutdown());
525 >        joinPool(p);
526      }
527  
415
528      /**
529       * isTerminated is false before termination, true after
530       */
531      public void testIsTerminated() throws InterruptedException {
532 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
533 <        assertFalse(p1.isTerminated());
534 <        try {
535 <            p1.execute(new MediumRunnable());
536 <        } finally {
537 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
538 <        }
539 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
540 <        assertTrue(p1.isTerminated());
532 >        final ThreadPoolExecutor p =
533 >            new CustomTPE(1, 1,
534 >                          LONG_DELAY_MS, MILLISECONDS,
535 >                          new ArrayBlockingQueue<Runnable>(10));
536 >        final CountDownLatch threadStarted = new CountDownLatch(1);
537 >        final CountDownLatch done = new CountDownLatch(1);
538 >        try {
539 >            assertFalse(p.isTerminating());
540 >            p.execute(new CheckedRunnable() {
541 >                public void realRun() throws InterruptedException {
542 >                    assertFalse(p.isTerminating());
543 >                    threadStarted.countDown();
544 >                    done.await();
545 >                }});
546 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
547 >            assertFalse(p.isTerminating());
548 >            done.countDown();
549 >        } finally {
550 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
551 >        }
552 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
553 >        assertTrue(p.isTerminated());
554 >        assertFalse(p.isTerminating());
555      }
556  
557      /**
558       * isTerminating is not true when running or when terminated
559       */
560      public void testIsTerminating() throws InterruptedException {
561 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
562 <        assertFalse(p1.isTerminating());
563 <        try {
564 <            p1.execute(new SmallRunnable());
565 <            assertFalse(p1.isTerminating());
566 <        } finally {
567 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
568 <        }
569 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
570 <        assertTrue(p1.isTerminated());
571 <        assertFalse(p1.isTerminating());
561 >        final ThreadPoolExecutor p =
562 >            new CustomTPE(1, 1,
563 >                          LONG_DELAY_MS, MILLISECONDS,
564 >                          new ArrayBlockingQueue<Runnable>(10));
565 >        final CountDownLatch threadStarted = new CountDownLatch(1);
566 >        final CountDownLatch done = new CountDownLatch(1);
567 >        try {
568 >            assertFalse(p.isTerminating());
569 >            p.execute(new CheckedRunnable() {
570 >                public void realRun() throws InterruptedException {
571 >                    assertFalse(p.isTerminating());
572 >                    threadStarted.countDown();
573 >                    done.await();
574 >                }});
575 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
576 >            assertFalse(p.isTerminating());
577 >            done.countDown();
578 >        } finally {
579 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
580 >        }
581 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
582 >        assertTrue(p.isTerminated());
583 >        assertFalse(p.isTerminating());
584      }
585  
586      /**
587       * getQueue returns the work queue, which contains queued tasks
588       */
589      public void testGetQueue() throws InterruptedException {
590 <        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
591 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, q);
592 <        FutureTask[] tasks = new FutureTask[5];
593 <        for (int i = 0; i < 5; i++) {
594 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
595 <            p1.execute(tasks[i]);
596 <        }
597 <        try {
598 <            Thread.sleep(SHORT_DELAY_MS);
599 <            BlockingQueue<Runnable> wq = p1.getQueue();
600 <            assertSame(q, wq);
601 <            assertFalse(wq.contains(tasks[0]));
602 <            assertTrue(wq.contains(tasks[4]));
603 <            for (int i = 1; i < 5; ++i)
604 <                tasks[i].cancel(true);
605 <            p1.shutdownNow();
590 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
591 >        final ThreadPoolExecutor p =
592 >            new CustomTPE(1, 1,
593 >                          LONG_DELAY_MS, MILLISECONDS,
594 >                          q);
595 >        final CountDownLatch threadStarted = new CountDownLatch(1);
596 >        final CountDownLatch done = new CountDownLatch(1);
597 >        try {
598 >            FutureTask[] tasks = new FutureTask[5];
599 >            for (int i = 0; i < tasks.length; i++) {
600 >                Callable task = new CheckedCallable<Boolean>() {
601 >                    public Boolean realCall() throws InterruptedException {
602 >                        threadStarted.countDown();
603 >                        assertSame(q, p.getQueue());
604 >                        done.await();
605 >                        return Boolean.TRUE;
606 >                    }};
607 >                tasks[i] = new FutureTask(task);
608 >                p.execute(tasks[i]);
609 >            }
610 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
611 >            assertSame(q, p.getQueue());
612 >            assertFalse(q.contains(tasks[0]));
613 >            assertTrue(q.contains(tasks[tasks.length - 1]));
614 >            assertEquals(tasks.length - 1, q.size());
615          } finally {
616 <            joinPool(p1);
616 >            done.countDown();
617 >            joinPool(p);
618          }
619      }
620  
# Line 475 | Line 623 | public class ThreadPoolExecutorSubclassT
623       */
624      public void testRemove() throws InterruptedException {
625          BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
626 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, q);
627 <        FutureTask[] tasks = new FutureTask[5];
628 <        for (int i = 0; i < 5; i++) {
629 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
630 <            p1.execute(tasks[i]);
631 <        }
632 <        try {
633 <            Thread.sleep(SHORT_DELAY_MS);
634 <            assertFalse(p1.remove(tasks[0]));
626 >        final ThreadPoolExecutor p =
627 >            new CustomTPE(1, 1,
628 >                          LONG_DELAY_MS, MILLISECONDS,
629 >                          q);
630 >        Runnable[] tasks = new Runnable[6];
631 >        final CountDownLatch threadStarted = new CountDownLatch(1);
632 >        final CountDownLatch done = new CountDownLatch(1);
633 >        try {
634 >            for (int i = 0; i < tasks.length; i++) {
635 >                tasks[i] = new CheckedRunnable() {
636 >                        public void realRun() throws InterruptedException {
637 >                            threadStarted.countDown();
638 >                            done.await();
639 >                        }};
640 >                p.execute(tasks[i]);
641 >            }
642 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
643 >            assertFalse(p.remove(tasks[0]));
644              assertTrue(q.contains(tasks[4]));
645              assertTrue(q.contains(tasks[3]));
646 <            assertTrue(p1.remove(tasks[4]));
647 <            assertFalse(p1.remove(tasks[4]));
646 >            assertTrue(p.remove(tasks[4]));
647 >            assertFalse(p.remove(tasks[4]));
648              assertFalse(q.contains(tasks[4]));
649              assertTrue(q.contains(tasks[3]));
650 <            assertTrue(p1.remove(tasks[3]));
650 >            assertTrue(p.remove(tasks[3]));
651              assertFalse(q.contains(tasks[3]));
652          } finally {
653 <            joinPool(p1);
653 >            done.countDown();
654 >            joinPool(p);
655          }
656      }
657  
658      /**
659       * purge removes cancelled tasks from the queue
660       */
661 <    public void testPurge() {
662 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
661 >    public void testPurge() throws InterruptedException {
662 >        final CountDownLatch threadStarted = new CountDownLatch(1);
663 >        final CountDownLatch done = new CountDownLatch(1);
664 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
665 >        final ThreadPoolExecutor p =
666 >            new CustomTPE(1, 1,
667 >                          LONG_DELAY_MS, MILLISECONDS,
668 >                          q);
669          FutureTask[] tasks = new FutureTask[5];
670 <        for (int i = 0; i < 5; i++) {
671 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
672 <            p1.execute(tasks[i]);
670 >        try {
671 >            for (int i = 0; i < tasks.length; i++) {
672 >                Callable task = new CheckedCallable<Boolean>() {
673 >                    public Boolean realCall() throws InterruptedException {
674 >                        threadStarted.countDown();
675 >                        done.await();
676 >                        return Boolean.TRUE;
677 >                    }};
678 >                tasks[i] = new FutureTask(task);
679 >                p.execute(tasks[i]);
680 >            }
681 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
682 >            assertEquals(tasks.length, p.getTaskCount());
683 >            assertEquals(tasks.length - 1, q.size());
684 >            assertEquals(1L, p.getActiveCount());
685 >            assertEquals(0L, p.getCompletedTaskCount());
686 >            tasks[4].cancel(true);
687 >            tasks[3].cancel(false);
688 >            p.purge();
689 >            assertEquals(tasks.length - 3, q.size());
690 >            assertEquals(tasks.length - 2, p.getTaskCount());
691 >            p.purge();         // Nothing to do
692 >            assertEquals(tasks.length - 3, q.size());
693 >            assertEquals(tasks.length - 2, p.getTaskCount());
694 >        } finally {
695 >            done.countDown();
696 >            joinPool(p);
697          }
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);
698      }
699  
700      /**
701 <     * shutDownNow returns a list containing tasks that were not run
701 >     * shutdownNow returns a list containing tasks that were not run
702       */
703 <    public void testShutDownNow() {
704 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
703 >    public void testShutdownNow() {
704 >        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
705          List l;
706          try {
707              for (int i = 0; i < 5; i++)
708 <                p1.execute(new MediumPossiblyInterruptedRunnable());
708 >                p.execute(new MediumPossiblyInterruptedRunnable());
709          }
710          finally {
711              try {
712 <                l = p1.shutdownNow();
712 >                l = p.shutdownNow();
713              } catch (SecurityException ok) { return; }
714          }
715 <        assertTrue(p1.isShutdown());
715 >        assertTrue(p.isShutdown());
716          assertTrue(l.size() <= 4);
717      }
718  
719      // Exception Tests
720  
539
721      /**
722       * Constructor throws if corePoolSize argument is less than zero
723       */
724      public void testConstructor1() {
725          try {
726 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
726 >            new CustomTPE(-1, 1, 1L, SECONDS,
727 >                          new ArrayBlockingQueue<Runnable>(10));
728              shouldThrow();
729          } catch (IllegalArgumentException success) {}
730      }
# Line 552 | Line 734 | public class ThreadPoolExecutorSubclassT
734       */
735      public void testConstructor2() {
736          try {
737 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
737 >            new CustomTPE(1, -1, 1L, SECONDS,
738 >                          new ArrayBlockingQueue<Runnable>(10));
739              shouldThrow();
740          } catch (IllegalArgumentException success) {}
741      }
# Line 562 | Line 745 | public class ThreadPoolExecutorSubclassT
745       */
746      public void testConstructor3() {
747          try {
748 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
748 >            new CustomTPE(1, 0, 1L, SECONDS,
749 >                          new ArrayBlockingQueue<Runnable>(10));
750              shouldThrow();
751          } catch (IllegalArgumentException success) {}
752      }
# Line 572 | Line 756 | public class ThreadPoolExecutorSubclassT
756       */
757      public void testConstructor4() {
758          try {
759 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
759 >            new CustomTPE(1, 2, -1L, SECONDS,
760 >                          new ArrayBlockingQueue<Runnable>(10));
761              shouldThrow();
762          } catch (IllegalArgumentException success) {}
763      }
# Line 582 | Line 767 | public class ThreadPoolExecutorSubclassT
767       */
768      public void testConstructor5() {
769          try {
770 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
770 >            new CustomTPE(2, 1, 1L, SECONDS,
771 >                          new ArrayBlockingQueue<Runnable>(10));
772              shouldThrow();
773          } catch (IllegalArgumentException success) {}
774      }
# Line 592 | Line 778 | public class ThreadPoolExecutorSubclassT
778       */
779      public void testConstructorNullPointerException() {
780          try {
781 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null);
781 >            new CustomTPE(1, 2, 1L, SECONDS, null);
782              shouldThrow();
783          } catch (NullPointerException success) {}
784      }
785  
600
601
786      /**
787       * Constructor throws if corePoolSize argument is less than zero
788       */
789      public void testConstructor6() {
790          try {
791 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
791 >            new CustomTPE(-1, 1, 1L, SECONDS,
792 >                          new ArrayBlockingQueue<Runnable>(10),
793 >                          new SimpleThreadFactory());
794              shouldThrow();
795          } catch (IllegalArgumentException success) {}
796      }
# Line 614 | Line 800 | public class ThreadPoolExecutorSubclassT
800       */
801      public void testConstructor7() {
802          try {
803 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
803 >            new CustomTPE(1,-1, 1L, SECONDS,
804 >                          new ArrayBlockingQueue<Runnable>(10),
805 >                          new SimpleThreadFactory());
806              shouldThrow();
807          } catch (IllegalArgumentException success) {}
808      }
# Line 624 | Line 812 | public class ThreadPoolExecutorSubclassT
812       */
813      public void testConstructor8() {
814          try {
815 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
815 >            new CustomTPE(1, 0, 1L, SECONDS,
816 >                          new ArrayBlockingQueue<Runnable>(10),
817 >                          new SimpleThreadFactory());
818              shouldThrow();
819          } catch (IllegalArgumentException success) {}
820      }
# Line 634 | Line 824 | public class ThreadPoolExecutorSubclassT
824       */
825      public void testConstructor9() {
826          try {
827 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
827 >            new CustomTPE(1, 2, -1L, SECONDS,
828 >                          new ArrayBlockingQueue<Runnable>(10),
829 >                          new SimpleThreadFactory());
830              shouldThrow();
831          } catch (IllegalArgumentException success) {}
832      }
# Line 644 | Line 836 | public class ThreadPoolExecutorSubclassT
836       */
837      public void testConstructor10() {
838          try {
839 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
839 >            new CustomTPE(2, 1, 1L, SECONDS,
840 >                          new ArrayBlockingQueue<Runnable>(10),
841 >                          new SimpleThreadFactory());
842              shouldThrow();
843          } catch (IllegalArgumentException success) {}
844      }
# Line 654 | Line 848 | public class ThreadPoolExecutorSubclassT
848       */
849      public void testConstructorNullPointerException2() {
850          try {
851 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
851 >            new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory());
852              shouldThrow();
853          } catch (NullPointerException success) {}
854      }
# Line 664 | Line 858 | public class ThreadPoolExecutorSubclassT
858       */
859      public void testConstructorNullPointerException3() {
860          try {
861 <            ThreadFactory f = null;
862 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
861 >            new CustomTPE(1, 2, 1L, SECONDS,
862 >                          new ArrayBlockingQueue<Runnable>(10),
863 >                          (ThreadFactory) null);
864              shouldThrow();
865          } catch (NullPointerException success) {}
866      }
867  
673
868      /**
869       * Constructor throws if corePoolSize argument is less than zero
870       */
871      public void testConstructor11() {
872          try {
873 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
873 >            new CustomTPE(-1, 1, 1L, SECONDS,
874 >                          new ArrayBlockingQueue<Runnable>(10),
875 >                          new NoOpREHandler());
876              shouldThrow();
877          } catch (IllegalArgumentException success) {}
878      }
# Line 686 | Line 882 | public class ThreadPoolExecutorSubclassT
882       */
883      public void testConstructor12() {
884          try {
885 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
885 >            new CustomTPE(1, -1, 1L, SECONDS,
886 >                          new ArrayBlockingQueue<Runnable>(10),
887 >                          new NoOpREHandler());
888              shouldThrow();
889          } catch (IllegalArgumentException success) {}
890      }
# Line 696 | Line 894 | public class ThreadPoolExecutorSubclassT
894       */
895      public void testConstructor13() {
896          try {
897 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
897 >            new CustomTPE(1, 0, 1L, SECONDS,
898 >                          new ArrayBlockingQueue<Runnable>(10),
899 >                          new NoOpREHandler());
900              shouldThrow();
901          } catch (IllegalArgumentException success) {}
902      }
# Line 706 | Line 906 | public class ThreadPoolExecutorSubclassT
906       */
907      public void testConstructor14() {
908          try {
909 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
909 >            new CustomTPE(1, 2, -1L, SECONDS,
910 >                          new ArrayBlockingQueue<Runnable>(10),
911 >                          new NoOpREHandler());
912              shouldThrow();
913          } catch (IllegalArgumentException success) {}
914      }
# Line 716 | Line 918 | public class ThreadPoolExecutorSubclassT
918       */
919      public void testConstructor15() {
920          try {
921 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
921 >            new CustomTPE(2, 1, 1L, SECONDS,
922 >                          new ArrayBlockingQueue<Runnable>(10),
923 >                          new NoOpREHandler());
924              shouldThrow();
925          } catch (IllegalArgumentException success) {}
926      }
# Line 726 | Line 930 | public class ThreadPoolExecutorSubclassT
930       */
931      public void testConstructorNullPointerException4() {
932          try {
933 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
933 >            new CustomTPE(1, 2, 1L, SECONDS,
934 >                          null,
935 >                          new NoOpREHandler());
936              shouldThrow();
937          } catch (NullPointerException success) {}
938      }
# Line 736 | Line 942 | public class ThreadPoolExecutorSubclassT
942       */
943      public void testConstructorNullPointerException5() {
944          try {
945 <            RejectedExecutionHandler r = null;
946 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
945 >            new CustomTPE(1, 2, 1L, SECONDS,
946 >                          new ArrayBlockingQueue<Runnable>(10),
947 >                          (RejectedExecutionHandler) null);
948              shouldThrow();
949          } catch (NullPointerException success) {}
950      }
951  
745
952      /**
953       * Constructor throws if corePoolSize argument is less than zero
954       */
955      public void testConstructor16() {
956          try {
957 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
957 >            new CustomTPE(-1, 1, 1L, SECONDS,
958 >                          new ArrayBlockingQueue<Runnable>(10),
959 >                          new SimpleThreadFactory(),
960 >                          new NoOpREHandler());
961              shouldThrow();
962          } catch (IllegalArgumentException success) {}
963      }
# Line 758 | Line 967 | public class ThreadPoolExecutorSubclassT
967       */
968      public void testConstructor17() {
969          try {
970 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
970 >            new CustomTPE(1, -1, 1L, SECONDS,
971 >                          new ArrayBlockingQueue<Runnable>(10),
972 >                          new SimpleThreadFactory(),
973 >                          new NoOpREHandler());
974              shouldThrow();
975          } catch (IllegalArgumentException success) {}
976      }
# Line 768 | Line 980 | public class ThreadPoolExecutorSubclassT
980       */
981      public void testConstructor18() {
982          try {
983 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
983 >            new CustomTPE(1, 0, 1L, SECONDS,
984 >                          new ArrayBlockingQueue<Runnable>(10),
985 >                          new SimpleThreadFactory(),
986 >                          new NoOpREHandler());
987              shouldThrow();
988          } catch (IllegalArgumentException success) {}
989      }
# Line 778 | Line 993 | public class ThreadPoolExecutorSubclassT
993       */
994      public void testConstructor19() {
995          try {
996 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
996 >            new CustomTPE(1, 2, -1L, SECONDS,
997 >                          new ArrayBlockingQueue<Runnable>(10),
998 >                          new SimpleThreadFactory(),
999 >                          new NoOpREHandler());
1000              shouldThrow();
1001          } catch (IllegalArgumentException success) {}
1002      }
# Line 788 | Line 1006 | public class ThreadPoolExecutorSubclassT
1006       */
1007      public void testConstructor20() {
1008          try {
1009 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1009 >            new CustomTPE(2, 1, 1L, SECONDS,
1010 >                          new ArrayBlockingQueue<Runnable>(10),
1011 >                          new SimpleThreadFactory(),
1012 >                          new NoOpREHandler());
1013              shouldThrow();
1014          } catch (IllegalArgumentException success) {}
1015      }
# Line 798 | Line 1019 | public class ThreadPoolExecutorSubclassT
1019       */
1020      public void testConstructorNullPointerException6() {
1021          try {
1022 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
1022 >            new CustomTPE(1, 2, 1L, SECONDS,
1023 >                          null,
1024 >                          new SimpleThreadFactory(),
1025 >                          new NoOpREHandler());
1026              shouldThrow();
1027          } catch (NullPointerException success) {}
1028      }
# Line 808 | Line 1032 | public class ThreadPoolExecutorSubclassT
1032       */
1033      public void testConstructorNullPointerException7() {
1034          try {
1035 <            RejectedExecutionHandler r = null;
1036 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
1035 >            new CustomTPE(1, 2, 1L, SECONDS,
1036 >                          new ArrayBlockingQueue<Runnable>(10),
1037 >                          new SimpleThreadFactory(),
1038 >                          (RejectedExecutionHandler) null);
1039              shouldThrow();
1040          } catch (NullPointerException success) {}
1041      }
# Line 819 | Line 1045 | public class ThreadPoolExecutorSubclassT
1045       */
1046      public void testConstructorNullPointerException8() {
1047          try {
1048 <            ThreadFactory f = null;
1049 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
1048 >            new CustomTPE(1, 2, 1L, SECONDS,
1049 >                          new ArrayBlockingQueue<Runnable>(10),
1050 >                          (ThreadFactory) null,
1051 >                          new NoOpREHandler());
1052              shouldThrow();
1053          } catch (NullPointerException success) {}
1054      }
1055  
828
1056      /**
1057       * execute throws RejectedExecutionException if saturated.
1058       */
1059      public void testSaturatedExecute() {
1060 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
1061 <        try {
1062 <
1063 <            for (int i = 0; i < 5; ++i) {
1064 <                p.execute(new MediumRunnable());
1060 >        ThreadPoolExecutor p =
1061 >            new CustomTPE(1, 1,
1062 >                          LONG_DELAY_MS, MILLISECONDS,
1063 >                          new ArrayBlockingQueue<Runnable>(1));
1064 >        final CountDownLatch done = new CountDownLatch(1);
1065 >        try {
1066 >            Runnable task = new CheckedRunnable() {
1067 >                public void realRun() throws InterruptedException {
1068 >                    done.await();
1069 >                }};
1070 >            for (int i = 0; i < 2; ++i)
1071 >                p.execute(task);
1072 >            for (int i = 0; i < 2; ++i) {
1073 >                try {
1074 >                    p.execute(task);
1075 >                    shouldThrow();
1076 >                } catch (RejectedExecutionException success) {}
1077 >                assertTrue(p.getTaskCount() <= 2);
1078              }
1079 <            shouldThrow();
1080 <        } catch (RejectedExecutionException success) {}
1081 <        joinPool(p);
1079 >        } finally {
1080 >            done.countDown();
1081 >            joinPool(p);
1082 >        }
1083      }
1084  
1085      /**
# Line 846 | Line 1087 | public class ThreadPoolExecutorSubclassT
1087       */
1088      public void testSaturatedExecute2() {
1089          RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1090 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1090 >        ThreadPoolExecutor p = new CustomTPE(1, 1,
1091 >                                             LONG_DELAY_MS, MILLISECONDS,
1092 >                                             new ArrayBlockingQueue<Runnable>(1),
1093 >                                             h);
1094          try {
851
1095              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1096 <            for (int i = 0; i < 5; ++i) {
1096 >            for (int i = 0; i < tasks.length; ++i)
1097                  tasks[i] = new TrackedNoOpRunnable();
855            }
1098              TrackedLongRunnable mr = new TrackedLongRunnable();
1099              p.execute(mr);
1100 <            for (int i = 0; i < 5; ++i) {
1100 >            for (int i = 0; i < tasks.length; ++i)
1101                  p.execute(tasks[i]);
1102 <            }
861 <            for (int i = 1; i < 5; ++i) {
1102 >            for (int i = 1; i < tasks.length; ++i)
1103                  assertTrue(tasks[i].done);
863            }
1104              try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1105          } finally {
1106              joinPool(p);
# Line 872 | Line 1112 | public class ThreadPoolExecutorSubclassT
1112       */
1113      public void testSaturatedExecute3() {
1114          RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1115 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1115 >        ThreadPoolExecutor p =
1116 >            new CustomTPE(1, 1,
1117 >                          LONG_DELAY_MS, MILLISECONDS,
1118 >                          new ArrayBlockingQueue<Runnable>(1),
1119 >                          h);
1120          try {
877
1121              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1122 <            for (int i = 0; i < 5; ++i) {
1122 >            for (int i = 0; i < tasks.length; ++i)
1123                  tasks[i] = new TrackedNoOpRunnable();
881            }
1124              p.execute(new TrackedLongRunnable());
1125 <            for (int i = 0; i < 5; ++i) {
1126 <                p.execute(tasks[i]);
1127 <            }
1128 <            for (int i = 0; i < 5; ++i) {
887 <                assertFalse(tasks[i].done);
888 <            }
1125 >            for (TrackedNoOpRunnable task : tasks)
1126 >                p.execute(task);
1127 >            for (TrackedNoOpRunnable task : tasks)
1128 >                assertFalse(task.done);
1129              try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1130          } finally {
1131              joinPool(p);
# Line 917 | Line 1157 | public class ThreadPoolExecutorSubclassT
1157       * execute throws RejectedExecutionException if shutdown
1158       */
1159      public void testRejectedExecutionExceptionOnShutdown() {
1160 <        ThreadPoolExecutor tpe =
1160 >        ThreadPoolExecutor p =
1161              new CustomTPE(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
1162 <        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1162 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1163          try {
1164 <            tpe.execute(new NoOpRunnable());
1164 >            p.execute(new NoOpRunnable());
1165              shouldThrow();
1166          } catch (RejectedExecutionException success) {}
1167  
1168 <        joinPool(tpe);
1168 >        joinPool(p);
1169      }
1170  
1171      /**
# Line 962 | Line 1202 | public class ThreadPoolExecutorSubclassT
1202          }
1203      }
1204  
965
1205      /**
1206       * execute using DiscardOldestPolicy drops task on shutdown
1207       */
# Line 980 | Line 1219 | public class ThreadPoolExecutorSubclassT
1219          }
1220      }
1221  
983
1222      /**
1223       * execute(null) throws NPE
1224       */
1225      public void testExecuteNull() {
1226 <        ThreadPoolExecutor tpe = null;
1226 >        ThreadPoolExecutor p =
1227 >            new CustomTPE(1, 2, 1L, SECONDS,
1228 >                          new ArrayBlockingQueue<Runnable>(10));
1229          try {
1230 <            tpe = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
991 <            tpe.execute(null);
1230 >            p.execute(null);
1231              shouldThrow();
1232          } catch (NullPointerException success) {}
1233  
1234 <        joinPool(tpe);
1234 >        joinPool(p);
1235      }
1236  
1237      /**
1238       * setCorePoolSize of negative value throws IllegalArgumentException
1239       */
1240      public void testCorePoolSizeIllegalArgumentException() {
1241 <        ThreadPoolExecutor tpe =
1241 >        ThreadPoolExecutor p =
1242              new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1243          try {
1244 <            tpe.setCorePoolSize(-1);
1244 >            p.setCorePoolSize(-1);
1245              shouldThrow();
1246          } catch (IllegalArgumentException success) {
1247          } finally {
1248 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1248 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1249          }
1250 <        joinPool(tpe);
1250 >        joinPool(p);
1251      }
1252  
1253      /**
# Line 1016 | Line 1255 | public class ThreadPoolExecutorSubclassT
1255       * if given a value less the core pool size
1256       */
1257      public void testMaximumPoolSizeIllegalArgumentException() {
1258 <        ThreadPoolExecutor tpe =
1258 >        ThreadPoolExecutor p =
1259              new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1260          try {
1261 <            tpe.setMaximumPoolSize(1);
1261 >            p.setMaximumPoolSize(1);
1262              shouldThrow();
1263          } catch (IllegalArgumentException success) {
1264          } finally {
1265 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1265 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1266          }
1267 <        joinPool(tpe);
1267 >        joinPool(p);
1268      }
1269  
1270      /**
# Line 1033 | Line 1272 | public class ThreadPoolExecutorSubclassT
1272       * if given a negative value
1273       */
1274      public void testMaximumPoolSizeIllegalArgumentException2() {
1275 <        ThreadPoolExecutor tpe =
1275 >        ThreadPoolExecutor p =
1276              new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1277          try {
1278 <            tpe.setMaximumPoolSize(-1);
1278 >            p.setMaximumPoolSize(-1);
1279              shouldThrow();
1280          } catch (IllegalArgumentException success) {
1281          } finally {
1282 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1282 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1283          }
1284 <        joinPool(tpe);
1284 >        joinPool(p);
1285      }
1286  
1048
1287      /**
1288       * setKeepAliveTime throws IllegalArgumentException
1289       * when given a negative value
1290       */
1291      public void testKeepAliveTimeIllegalArgumentException() {
1292 <        ThreadPoolExecutor tpe =
1292 >        ThreadPoolExecutor p =
1293              new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1294  
1295          try {
1296 <            tpe.setKeepAliveTime(-1,MILLISECONDS);
1296 >            p.setKeepAliveTime(-1,MILLISECONDS);
1297              shouldThrow();
1298          } catch (IllegalArgumentException success) {
1299          } finally {
1300 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1300 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1301          }
1302 <        joinPool(tpe);
1302 >        joinPool(p);
1303      }
1304  
1305      /**
1306       * terminated() is called on termination
1307       */
1308      public void testTerminated() {
1309 <        CustomTPE tpe = new CustomTPE();
1310 <        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1311 <        assertTrue(tpe.terminatedCalled);
1312 <        joinPool(tpe);
1309 >        CustomTPE p = new CustomTPE();
1310 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1311 >        assertTrue(p.terminatedCalled());
1312 >        joinPool(p);
1313      }
1314  
1315      /**
1316       * beforeExecute and afterExecute are called when executing task
1317       */
1318      public void testBeforeAfter() throws InterruptedException {
1319 <        CustomTPE tpe = new CustomTPE();
1319 >        CustomTPE p = new CustomTPE();
1320          try {
1321 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1322 <            tpe.execute(r);
1323 <            Thread.sleep(SHORT_DELAY_MS);
1324 <            assertTrue(r.done);
1325 <            assertTrue(tpe.beforeCalled);
1326 <            assertTrue(tpe.afterCalled);
1327 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1321 >            final CountDownLatch done = new CountDownLatch(1);
1322 >            p.execute(new CheckedRunnable() {
1323 >                public void realRun() {
1324 >                    done.countDown();
1325 >                }});
1326 >            await(p.afterCalled);
1327 >            assertEquals(0, done.getCount());
1328 >            assertTrue(p.afterCalled());
1329 >            assertTrue(p.beforeCalled());
1330 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1331          } finally {
1332 <            joinPool(tpe);
1332 >            joinPool(p);
1333          }
1334      }
1335  
# Line 1134 | Line 1375 | public class ThreadPoolExecutorSubclassT
1375          }
1376      }
1377  
1137
1378      /**
1379       * invokeAny(null) throws NPE
1380       */
# Line 1296 | Line 1536 | public class ThreadPoolExecutorSubclassT
1536          }
1537      }
1538  
1299
1300
1539      /**
1540       * timed invokeAny(null) throws NPE
1541       */
# Line 1505 | Line 1743 | public class ThreadPoolExecutorSubclassT
1743              l.add(new StringTask());
1744              List<Future<String>> futures =
1745                  e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1746 <            assertEquals(3, futures.size());
1747 <            Iterator<Future<String>> it = futures.iterator();
1748 <            Future<String> f1 = it.next();
1749 <            Future<String> f2 = it.next();
1750 <            Future<String> f3 = it.next();
1513 <            assertTrue(f1.isDone());
1514 <            assertTrue(f2.isDone());
1515 <            assertTrue(f3.isDone());
1516 <            assertFalse(f1.isCancelled());
1517 <            assertTrue(f2.isCancelled());
1746 >            assertEquals(l.size(), futures.size());
1747 >            for (Future future : futures)
1748 >                assertTrue(future.isDone());
1749 >            assertFalse(futures.get(0).isCancelled());
1750 >            assertTrue(futures.get(1).isCancelled());
1751          } finally {
1752              joinPool(e);
1753          }
# Line 1525 | Line 1758 | public class ThreadPoolExecutorSubclassT
1758       * thread factory fails to create more
1759       */
1760      public void testFailingThreadFactory() throws InterruptedException {
1761 <        ExecutorService e = new CustomTPE(100, 100, LONG_DELAY_MS, MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory());
1762 <        try {
1763 <            for (int k = 0; k < 100; ++k) {
1764 <                e.execute(new NoOpRunnable());
1765 <            }
1766 <            Thread.sleep(LONG_DELAY_MS);
1761 >        final ExecutorService e =
1762 >            new CustomTPE(100, 100,
1763 >                          LONG_DELAY_MS, MILLISECONDS,
1764 >                          new LinkedBlockingQueue<Runnable>(),
1765 >                          new FailingThreadFactory());
1766 >        try {
1767 >            final int TASKS = 100;
1768 >            final CountDownLatch done = new CountDownLatch(TASKS);
1769 >            for (int k = 0; k < TASKS; ++k)
1770 >                e.execute(new CheckedRunnable() {
1771 >                    public void realRun() {
1772 >                        done.countDown();
1773 >                    }});
1774 >            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1775          } finally {
1776              joinPool(e);
1777          }
# Line 1540 | Line 1781 | public class ThreadPoolExecutorSubclassT
1781       * allowsCoreThreadTimeOut is by default false.
1782       */
1783      public void testAllowsCoreThreadTimeOut() {
1784 <        ThreadPoolExecutor tpe = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1785 <        assertFalse(tpe.allowsCoreThreadTimeOut());
1786 <        joinPool(tpe);
1784 >        ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1785 >        assertFalse(p.allowsCoreThreadTimeOut());
1786 >        joinPool(p);
1787      }
1788  
1789      /**
1790       * allowCoreThreadTimeOut(true) causes idle threads to time out
1791       */
1792 <    public void testAllowCoreThreadTimeOut_true() throws InterruptedException {
1793 <        ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1794 <        tpe.allowCoreThreadTimeOut(true);
1795 <        tpe.execute(new NoOpRunnable());
1796 <        try {
1797 <            Thread.sleep(MEDIUM_DELAY_MS);
1798 <            assertEquals(0, tpe.getPoolSize());
1792 >    public void testAllowCoreThreadTimeOut_true() throws Exception {
1793 >        long coreThreadTimeOut = SHORT_DELAY_MS;
1794 >        final ThreadPoolExecutor p =
1795 >            new CustomTPE(2, 10,
1796 >                          coreThreadTimeOut, MILLISECONDS,
1797 >                          new ArrayBlockingQueue<Runnable>(10));
1798 >        final CountDownLatch threadStarted = new CountDownLatch(1);
1799 >        try {
1800 >            p.allowCoreThreadTimeOut(true);
1801 >            p.execute(new CheckedRunnable() {
1802 >                public void realRun() throws InterruptedException {
1803 >                    threadStarted.countDown();
1804 >                    assertEquals(1, p.getPoolSize());
1805 >                }});
1806 >            await(threadStarted);
1807 >            delay(coreThreadTimeOut);
1808 >            long startTime = System.nanoTime();
1809 >            while (p.getPoolSize() > 0
1810 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1811 >                Thread.yield();
1812 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1813 >            assertEquals(0, p.getPoolSize());
1814          } finally {
1815 <            joinPool(tpe);
1815 >            joinPool(p);
1816          }
1817      }
1818  
1819      /**
1820       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1821       */
1822 <    public void testAllowCoreThreadTimeOut_false() throws InterruptedException {
1823 <        ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1824 <        tpe.allowCoreThreadTimeOut(false);
1825 <        tpe.execute(new NoOpRunnable());
1826 <        try {
1827 <            Thread.sleep(MEDIUM_DELAY_MS);
1828 <            assertTrue(tpe.getPoolSize() >= 1);
1822 >    public void testAllowCoreThreadTimeOut_false() throws Exception {
1823 >        long coreThreadTimeOut = SHORT_DELAY_MS;
1824 >        final ThreadPoolExecutor p =
1825 >            new CustomTPE(2, 10,
1826 >                          coreThreadTimeOut, MILLISECONDS,
1827 >                          new ArrayBlockingQueue<Runnable>(10));
1828 >        final CountDownLatch threadStarted = new CountDownLatch(1);
1829 >        try {
1830 >            p.allowCoreThreadTimeOut(false);
1831 >            p.execute(new CheckedRunnable() {
1832 >                public void realRun() throws InterruptedException {
1833 >                    threadStarted.countDown();
1834 >                    assertTrue(p.getPoolSize() >= 1);
1835 >                }});
1836 >            delay(2 * coreThreadTimeOut);
1837 >            assertTrue(p.getPoolSize() >= 1);
1838          } finally {
1839 <            joinPool(tpe);
1839 >            joinPool(p);
1840          }
1841      }
1842  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines