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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines