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.14 by jsr166, Sat Nov 21 20:13:20 2009 UTC vs.
Revision 1.28 by jsr166, Sat May 7 19:49:37 2011 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   */
# Line 60 | Line 60 | public class ThreadPoolExecutorSubclassT
60              finally { lock.unlock() ; }
61          }
62          public void run() {
63            boolean runme;
63              lock.lock();
64              try {
65 <                runme = !done;
66 <                if (!runme)
67 <                    thread = Thread.currentThread();
65 >                if (done)
66 >                    return;
67 >                thread = Thread.currentThread();
68              }
69              finally { lock.unlock() ; }
71            if (!runme) return;
70              V v = null;
71              Exception e = null;
72              try {
# Line 192 | Line 190 | public class ThreadPoolExecutorSubclassT
190  
191  
192      /**
193 <     *  execute successfully executes a runnable
193 >     * execute successfully executes a runnable
194       */
195      public void testExecute() throws InterruptedException {
196 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
196 >        final ThreadPoolExecutor p =
197 >            new CustomTPE(1, 1,
198 >                          LONG_DELAY_MS, MILLISECONDS,
199 >                          new ArrayBlockingQueue<Runnable>(10));
200 >        final CountDownLatch done = new CountDownLatch(1);
201 >        final Runnable task = new CheckedRunnable() {
202 >            public void realRun() {
203 >                done.countDown();
204 >            }};
205          try {
206 <            p1.execute(new ShortRunnable());
207 <            Thread.sleep(SMALL_DELAY_MS);
206 >            p.execute(task);
207 >            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
208          } finally {
209 <            joinPool(p1);
209 >            joinPool(p);
210          }
211      }
212  
213      /**
214 <     *  getActiveCount increases but doesn't overestimate, when a
215 <     *  thread becomes active
214 >     * getActiveCount increases but doesn't overestimate, when a
215 >     * thread becomes active
216       */
217      public void testGetActiveCount() throws InterruptedException {
218 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
219 <        assertEquals(0, p2.getActiveCount());
220 <        p2.execute(new MediumRunnable());
221 <        Thread.sleep(SHORT_DELAY_MS);
222 <        assertEquals(1, p2.getActiveCount());
223 <        joinPool(p2);
218 >        final ThreadPoolExecutor p =
219 >            new CustomTPE(2, 2,
220 >                          LONG_DELAY_MS, MILLISECONDS,
221 >                          new ArrayBlockingQueue<Runnable>(10));
222 >        final CountDownLatch threadStarted = new CountDownLatch(1);
223 >        final CountDownLatch done = new CountDownLatch(1);
224 >        try {
225 >            assertEquals(0, p.getActiveCount());
226 >            p.execute(new CheckedRunnable() {
227 >                public void realRun() throws InterruptedException {
228 >                    threadStarted.countDown();
229 >                    assertEquals(1, p.getActiveCount());
230 >                    done.await();
231 >                }});
232 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
233 >            assertEquals(1, p.getActiveCount());
234 >        } finally {
235 >            done.countDown();
236 >            joinPool(p);
237 >        }
238      }
239  
240      /**
241 <     *  prestartCoreThread starts a thread if under corePoolSize, else doesn't
241 >     * prestartCoreThread starts a thread if under corePoolSize, else doesn't
242       */
243      public void testPrestartCoreThread() {
244 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
245 <        assertEquals(0, p2.getPoolSize());
246 <        assertTrue(p2.prestartCoreThread());
247 <        assertEquals(1, p2.getPoolSize());
248 <        assertTrue(p2.prestartCoreThread());
249 <        assertEquals(2, p2.getPoolSize());
250 <        assertFalse(p2.prestartCoreThread());
251 <        assertEquals(2, p2.getPoolSize());
252 <        joinPool(p2);
244 >        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
245 >        assertEquals(0, p.getPoolSize());
246 >        assertTrue(p.prestartCoreThread());
247 >        assertEquals(1, p.getPoolSize());
248 >        assertTrue(p.prestartCoreThread());
249 >        assertEquals(2, p.getPoolSize());
250 >        assertFalse(p.prestartCoreThread());
251 >        assertEquals(2, p.getPoolSize());
252 >        joinPool(p);
253      }
254  
255      /**
256 <     *  prestartAllCoreThreads starts all corePoolSize threads
256 >     * prestartAllCoreThreads starts all corePoolSize threads
257       */
258      public void testPrestartAllCoreThreads() {
259 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
260 <        assertEquals(0, p2.getPoolSize());
261 <        p2.prestartAllCoreThreads();
262 <        assertEquals(2, p2.getPoolSize());
263 <        p2.prestartAllCoreThreads();
264 <        assertEquals(2, p2.getPoolSize());
265 <        joinPool(p2);
259 >        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
260 >        assertEquals(0, p.getPoolSize());
261 >        p.prestartAllCoreThreads();
262 >        assertEquals(2, p.getPoolSize());
263 >        p.prestartAllCoreThreads();
264 >        assertEquals(2, p.getPoolSize());
265 >        joinPool(p);
266      }
267  
268      /**
269 <     *   getCompletedTaskCount increases, but doesn't overestimate,
270 <     *   when tasks complete
269 >     * getCompletedTaskCount increases, but doesn't overestimate,
270 >     * when tasks complete
271       */
272      public void testGetCompletedTaskCount() throws InterruptedException {
273 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
274 <        assertEquals(0, p2.getCompletedTaskCount());
275 <        p2.execute(new ShortRunnable());
276 <        Thread.sleep(SMALL_DELAY_MS);
277 <        assertEquals(1, p2.getCompletedTaskCount());
278 <        try { p2.shutdown(); } catch (SecurityException ok) { return; }
279 <        joinPool(p2);
273 >        final ThreadPoolExecutor p =
274 >            new CustomTPE(2, 2,
275 >                          LONG_DELAY_MS, MILLISECONDS,
276 >                          new ArrayBlockingQueue<Runnable>(10));
277 >        final CountDownLatch threadStarted = new CountDownLatch(1);
278 >        final CountDownLatch threadProceed = new CountDownLatch(1);
279 >        final CountDownLatch threadDone = new CountDownLatch(1);
280 >        try {
281 >            assertEquals(0, p.getCompletedTaskCount());
282 >            p.execute(new CheckedRunnable() {
283 >                public void realRun() throws InterruptedException {
284 >                    threadStarted.countDown();
285 >                    assertEquals(0, p.getCompletedTaskCount());
286 >                    threadProceed.await();
287 >                    threadDone.countDown();
288 >                }});
289 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
290 >            assertEquals(0, p.getCompletedTaskCount());
291 >            threadProceed.countDown();
292 >            threadDone.await();
293 >            delay(SHORT_DELAY_MS);
294 >            assertEquals(1, p.getCompletedTaskCount());
295 >        } finally {
296 >            joinPool(p);
297 >        }
298      }
299  
300      /**
301 <     *   getCorePoolSize returns size given in constructor if not otherwise set
301 >     * getCorePoolSize returns size given in constructor if not otherwise set
302       */
303      public void testGetCorePoolSize() {
304 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
305 <        assertEquals(1, p1.getCorePoolSize());
306 <        joinPool(p1);
304 >        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
305 >        assertEquals(1, p.getCorePoolSize());
306 >        joinPool(p);
307      }
308  
309      /**
310 <     *   getKeepAliveTime returns value given in constructor if not otherwise set
310 >     * getKeepAliveTime returns value given in constructor if not otherwise set
311       */
312      public void testGetKeepAliveTime() {
313 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
314 <        assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS));
315 <        joinPool(p2);
313 >        ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
314 >        assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS));
315 >        joinPool(p);
316      }
317  
318  
# Line 353 | Line 391 | public class ThreadPoolExecutorSubclassT
391  
392  
393      /**
394 <     *   getLargestPoolSize increases, but doesn't overestimate, when
395 <     *   multiple threads active
394 >     * getLargestPoolSize increases, but doesn't overestimate, when
395 >     * multiple threads active
396       */
397      public void testGetLargestPoolSize() throws InterruptedException {
398 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
399 <        assertEquals(0, p2.getLargestPoolSize());
400 <        p2.execute(new MediumRunnable());
401 <        p2.execute(new MediumRunnable());
402 <        Thread.sleep(SHORT_DELAY_MS);
403 <        assertEquals(2, p2.getLargestPoolSize());
404 <        joinPool(p2);
398 >        final int THREADS = 3;
399 >        final ThreadPoolExecutor p =
400 >            new CustomTPE(THREADS, THREADS,
401 >                          LONG_DELAY_MS, MILLISECONDS,
402 >                          new ArrayBlockingQueue<Runnable>(10));
403 >        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
404 >        final CountDownLatch done = new CountDownLatch(1);
405 >        try {
406 >            assertEquals(0, p.getLargestPoolSize());
407 >            for (int i = 0; i < THREADS; i++)
408 >                p.execute(new CheckedRunnable() {
409 >                    public void realRun() throws InterruptedException {
410 >                        threadsStarted.countDown();
411 >                        done.await();
412 >                        assertEquals(THREADS, p.getLargestPoolSize());
413 >                    }});
414 >            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
415 >            assertEquals(THREADS, p.getLargestPoolSize());
416 >        } finally {
417 >            done.countDown();
418 >            joinPool(p);
419 >            assertEquals(THREADS, p.getLargestPoolSize());
420 >        }
421      }
422  
423      /**
424 <     *   getMaximumPoolSize returns value given in constructor if not
425 <     *   otherwise set
424 >     * getMaximumPoolSize returns value given in constructor if not
425 >     * otherwise set
426       */
427      public void testGetMaximumPoolSize() {
428 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
429 <        assertEquals(2, p2.getMaximumPoolSize());
430 <        joinPool(p2);
428 >        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
429 >        assertEquals(2, p.getMaximumPoolSize());
430 >        joinPool(p);
431      }
432  
433      /**
434 <     *   getPoolSize increases, but doesn't overestimate, when threads
435 <     *   become active
434 >     * getPoolSize increases, but doesn't overestimate, when threads
435 >     * become active
436       */
437 <    public void testGetPoolSize() {
438 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
439 <        assertEquals(0, p1.getPoolSize());
440 <        p1.execute(new MediumRunnable());
441 <        assertEquals(1, p1.getPoolSize());
442 <        joinPool(p1);
437 >    public void testGetPoolSize() throws InterruptedException {
438 >        final ThreadPoolExecutor p =
439 >            new CustomTPE(1, 1,
440 >                          LONG_DELAY_MS, MILLISECONDS,
441 >                          new ArrayBlockingQueue<Runnable>(10));
442 >        final CountDownLatch threadStarted = new CountDownLatch(1);
443 >        final CountDownLatch done = new CountDownLatch(1);
444 >        try {
445 >            assertEquals(0, p.getPoolSize());
446 >            p.execute(new CheckedRunnable() {
447 >                public void realRun() throws InterruptedException {
448 >                    threadStarted.countDown();
449 >                    assertEquals(1, p.getPoolSize());
450 >                    done.await();
451 >                }});
452 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
453 >            assertEquals(1, p.getPoolSize());
454 >        } finally {
455 >            done.countDown();
456 >            joinPool(p);
457 >        }
458      }
459  
460      /**
461 <     *  getTaskCount increases, but doesn't overestimate, when tasks submitted
461 >     * getTaskCount increases, but doesn't overestimate, when tasks submitted
462       */
463      public void testGetTaskCount() throws InterruptedException {
464 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
465 <        assertEquals(0, p1.getTaskCount());
466 <        p1.execute(new MediumRunnable());
467 <        Thread.sleep(SHORT_DELAY_MS);
468 <        assertEquals(1, p1.getTaskCount());
469 <        joinPool(p1);
464 >        final ThreadPoolExecutor p =
465 >            new CustomTPE(1, 1,
466 >                          LONG_DELAY_MS, MILLISECONDS,
467 >                          new ArrayBlockingQueue<Runnable>(10));
468 >        final CountDownLatch threadStarted = new CountDownLatch(1);
469 >        final CountDownLatch done = new CountDownLatch(1);
470 >        try {
471 >            assertEquals(0, p.getTaskCount());
472 >            p.execute(new CheckedRunnable() {
473 >                public void realRun() throws InterruptedException {
474 >                    threadStarted.countDown();
475 >                    assertEquals(1, p.getTaskCount());
476 >                    done.await();
477 >                }});
478 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
479 >            assertEquals(1, p.getTaskCount());
480 >        } finally {
481 >            done.countDown();
482 >            joinPool(p);
483 >        }
484      }
485  
486      /**
487 <     *   isShutDown is false before shutdown, true after
487 >     * isShutdown is false before shutdown, true after
488       */
489      public void testIsShutdown() {
490  
491 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
492 <        assertFalse(p1.isShutdown());
493 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
494 <        assertTrue(p1.isShutdown());
495 <        joinPool(p1);
491 >        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
492 >        assertFalse(p.isShutdown());
493 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
494 >        assertTrue(p.isShutdown());
495 >        joinPool(p);
496      }
497  
498  
499      /**
500 <     *  isTerminated is false before termination, true after
500 >     * isTerminated is false before termination, true after
501       */
502      public void testIsTerminated() throws InterruptedException {
503 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
504 <        assertFalse(p1.isTerminated());
505 <        try {
506 <            p1.execute(new MediumRunnable());
507 <        } finally {
508 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
509 <        }
510 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
511 <        assertTrue(p1.isTerminated());
503 >        final ThreadPoolExecutor p =
504 >            new CustomTPE(1, 1,
505 >                          LONG_DELAY_MS, MILLISECONDS,
506 >                          new ArrayBlockingQueue<Runnable>(10));
507 >        final CountDownLatch threadStarted = new CountDownLatch(1);
508 >        final CountDownLatch done = new CountDownLatch(1);
509 >        try {
510 >            assertFalse(p.isTerminating());
511 >            p.execute(new CheckedRunnable() {
512 >                public void realRun() throws InterruptedException {
513 >                    assertFalse(p.isTerminating());
514 >                    threadStarted.countDown();
515 >                    done.await();
516 >                }});
517 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
518 >            assertFalse(p.isTerminating());
519 >            done.countDown();
520 >        } finally {
521 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
522 >        }
523 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
524 >        assertTrue(p.isTerminated());
525 >        assertFalse(p.isTerminating());
526      }
527  
528      /**
529 <     *  isTerminating is not true when running or when terminated
529 >     * isTerminating is not true when running or when terminated
530       */
531      public void testIsTerminating() throws InterruptedException {
532 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
533 <        assertFalse(p1.isTerminating());
534 <        try {
535 <            p1.execute(new SmallRunnable());
536 <            assertFalse(p1.isTerminating());
537 <        } finally {
538 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
539 <        }
540 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
541 <        assertTrue(p1.isTerminated());
542 <        assertFalse(p1.isTerminating());
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       * getQueue returns the work queue, which contains queued tasks
559       */
560      public void testGetQueue() throws InterruptedException {
561 <        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
562 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, q);
563 <        FutureTask[] tasks = new FutureTask[5];
564 <        for (int i = 0; i < 5; i++) {
565 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
566 <            p1.execute(tasks[i]);
567 <        }
568 <        try {
569 <            Thread.sleep(SHORT_DELAY_MS);
570 <            BlockingQueue<Runnable> wq = p1.getQueue();
571 <            assertSame(q, wq);
572 <            assertFalse(wq.contains(tasks[0]));
573 <            assertTrue(wq.contains(tasks[4]));
574 <            for (int i = 1; i < 5; ++i)
575 <                tasks[i].cancel(true);
576 <            p1.shutdownNow();
561 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
562 >        final ThreadPoolExecutor p =
563 >            new CustomTPE(1, 1,
564 >                          LONG_DELAY_MS, MILLISECONDS,
565 >                          q);
566 >        final CountDownLatch threadStarted = new CountDownLatch(1);
567 >        final CountDownLatch done = new CountDownLatch(1);
568 >        try {
569 >            FutureTask[] tasks = new FutureTask[5];
570 >            for (int i = 0; i < tasks.length; i++) {
571 >                Callable task = new CheckedCallable<Boolean>() {
572 >                    public Boolean realCall() throws InterruptedException {
573 >                        threadStarted.countDown();
574 >                        assertSame(q, p.getQueue());
575 >                        done.await();
576 >                        return Boolean.TRUE;
577 >                    }};
578 >                tasks[i] = new FutureTask(task);
579 >                p.execute(tasks[i]);
580 >            }
581 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
582 >            assertSame(q, p.getQueue());
583 >            assertFalse(q.contains(tasks[0]));
584 >            assertTrue(q.contains(tasks[tasks.length - 1]));
585 >            assertEquals(tasks.length - 1, q.size());
586          } finally {
587 <            joinPool(p1);
587 >            done.countDown();
588 >            joinPool(p);
589          }
590      }
591  
# Line 475 | Line 594 | public class ThreadPoolExecutorSubclassT
594       */
595      public void testRemove() throws InterruptedException {
596          BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
597 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, q);
598 <        FutureTask[] tasks = new FutureTask[5];
599 <        for (int i = 0; i < 5; i++) {
600 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
601 <            p1.execute(tasks[i]);
602 <        }
603 <        try {
604 <            Thread.sleep(SHORT_DELAY_MS);
605 <            assertFalse(p1.remove(tasks[0]));
597 >        final ThreadPoolExecutor p =
598 >            new CustomTPE(1, 1,
599 >                          LONG_DELAY_MS, MILLISECONDS,
600 >                          q);
601 >        Runnable[] tasks = new Runnable[6];
602 >        final CountDownLatch threadStarted = new CountDownLatch(1);
603 >        final CountDownLatch done = new CountDownLatch(1);
604 >        try {
605 >            for (int i = 0; i < tasks.length; i++) {
606 >                tasks[i] = new CheckedRunnable() {
607 >                        public void realRun() throws InterruptedException {
608 >                            threadStarted.countDown();
609 >                            done.await();
610 >                        }};
611 >                p.execute(tasks[i]);
612 >            }
613 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
614 >            assertFalse(p.remove(tasks[0]));
615              assertTrue(q.contains(tasks[4]));
616              assertTrue(q.contains(tasks[3]));
617 <            assertTrue(p1.remove(tasks[4]));
618 <            assertFalse(p1.remove(tasks[4]));
617 >            assertTrue(p.remove(tasks[4]));
618 >            assertFalse(p.remove(tasks[4]));
619              assertFalse(q.contains(tasks[4]));
620              assertTrue(q.contains(tasks[3]));
621 <            assertTrue(p1.remove(tasks[3]));
621 >            assertTrue(p.remove(tasks[3]));
622              assertFalse(q.contains(tasks[3]));
623          } finally {
624 <            joinPool(p1);
624 >            done.countDown();
625 >            joinPool(p);
626          }
627      }
628  
629      /**
630 <     *   purge removes cancelled tasks from the queue
630 >     * purge removes cancelled tasks from the queue
631       */
632 <    public void testPurge() {
633 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
632 >    public void testPurge() throws InterruptedException {
633 >        final CountDownLatch threadStarted = new CountDownLatch(1);
634 >        final CountDownLatch done = new CountDownLatch(1);
635 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
636 >        final ThreadPoolExecutor p =
637 >            new CustomTPE(1, 1,
638 >                          LONG_DELAY_MS, MILLISECONDS,
639 >                          q);
640          FutureTask[] tasks = new FutureTask[5];
641 <        for (int i = 0; i < 5; i++) {
642 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
643 <            p1.execute(tasks[i]);
641 >        try {
642 >            for (int i = 0; i < tasks.length; i++) {
643 >                Callable task = new CheckedCallable<Boolean>() {
644 >                    public Boolean realCall() throws InterruptedException {
645 >                        threadStarted.countDown();
646 >                        done.await();
647 >                        return Boolean.TRUE;
648 >                    }};
649 >                tasks[i] = new FutureTask(task);
650 >                p.execute(tasks[i]);
651 >            }
652 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
653 >            assertEquals(tasks.length, p.getTaskCount());
654 >            assertEquals(tasks.length - 1, q.size());
655 >            assertEquals(1L, p.getActiveCount());
656 >            assertEquals(0L, p.getCompletedTaskCount());
657 >            tasks[4].cancel(true);
658 >            tasks[3].cancel(false);
659 >            p.purge();
660 >            assertEquals(tasks.length - 3, q.size());
661 >            assertEquals(tasks.length - 2, p.getTaskCount());
662 >            p.purge();         // Nothing to do
663 >            assertEquals(tasks.length - 3, q.size());
664 >            assertEquals(tasks.length - 2, p.getTaskCount());
665 >        } finally {
666 >            done.countDown();
667 >            joinPool(p);
668          }
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);
669      }
670  
671      /**
672 <     *  shutDownNow returns a list containing tasks that were not run
672 >     * shutdownNow returns a list containing tasks that were not run
673       */
674 <    public void testShutDownNow() {
675 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
674 >    public void testShutdownNow() {
675 >        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
676          List l;
677          try {
678              for (int i = 0; i < 5; i++)
679 <                p1.execute(new MediumPossiblyInterruptedRunnable());
679 >                p.execute(new MediumPossiblyInterruptedRunnable());
680          }
681          finally {
682              try {
683 <                l = p1.shutdownNow();
683 >                l = p.shutdownNow();
684              } catch (SecurityException ok) { return; }
532
685          }
686 <        assertTrue(p1.isShutdown());
686 >        assertTrue(p.isShutdown());
687          assertTrue(l.size() <= 4);
688      }
689  
# Line 795 | Line 947 | public class ThreadPoolExecutorSubclassT
947      }
948  
949      /**
950 <     * Constructor throws if workQueue is set to null
950 >     * Constructor throws if workQueue is null
951       */
952      public void testConstructorNullPointerException6() {
953          try {
# Line 805 | Line 957 | public class ThreadPoolExecutorSubclassT
957      }
958  
959      /**
960 <     * Constructor throws if handler is set to null
960 >     * Constructor throws if handler is null
961       */
962      public void testConstructorNullPointerException7() {
963          try {
# Line 816 | Line 968 | public class ThreadPoolExecutorSubclassT
968      }
969  
970      /**
971 <     * Constructor throws if ThreadFactory is set top null
971 >     * Constructor throws if ThreadFactory is null
972       */
973      public void testConstructorNullPointerException8() {
974          try {
975 <            ThreadFactory f = null;
976 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
975 >            new CustomTPE(1, 2,
976 >                          LONG_DELAY_MS, MILLISECONDS,
977 >                          new ArrayBlockingQueue<Runnable>(10),
978 >                          (ThreadFactory) null,
979 >                          new NoOpREHandler());
980              shouldThrow();
981          } catch (NullPointerException success) {}
982      }
983  
984  
985      /**
986 <     *  execute throws RejectedExecutionException
832 <     *  if saturated.
986 >     * execute throws RejectedExecutionException if saturated.
987       */
988      public void testSaturatedExecute() {
989 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
990 <        try {
991 <
992 <            for (int i = 0; i < 5; ++i) {
993 <                p.execute(new MediumRunnable());
989 >        ThreadPoolExecutor p =
990 >            new CustomTPE(1, 1,
991 >                          LONG_DELAY_MS, MILLISECONDS,
992 >                          new ArrayBlockingQueue<Runnable>(1));
993 >        final CountDownLatch done = new CountDownLatch(1);
994 >        try {
995 >            Runnable task = new CheckedRunnable() {
996 >                public void realRun() throws InterruptedException {
997 >                    done.await();
998 >                }};
999 >            for (int i = 0; i < 2; ++i)
1000 >                p.execute(task);
1001 >            for (int i = 0; i < 2; ++i) {
1002 >                try {
1003 >                    p.execute(task);
1004 >                    shouldThrow();
1005 >                } catch (RejectedExecutionException success) {}
1006 >                assertTrue(p.getTaskCount() <= 2);
1007              }
1008 <            shouldThrow();
1009 <        } catch (RejectedExecutionException success) {}
1010 <        joinPool(p);
1008 >        } finally {
1009 >            done.countDown();
1010 >            joinPool(p);
1011 >        }
1012      }
1013  
1014      /**
1015 <     *  executor using CallerRunsPolicy runs task if saturated.
1015 >     * executor using CallerRunsPolicy runs task if saturated.
1016       */
1017      public void testSaturatedExecute2() {
1018          RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1019 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1019 >        ThreadPoolExecutor p = new CustomTPE(1, 1,
1020 >                                             LONG_DELAY_MS, MILLISECONDS,
1021 >                                             new ArrayBlockingQueue<Runnable>(1),
1022 >                                             h);
1023          try {
853
1024              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1025 <            for (int i = 0; i < 5; ++i) {
1025 >            for (int i = 0; i < tasks.length; ++i)
1026                  tasks[i] = new TrackedNoOpRunnable();
857            }
1027              TrackedLongRunnable mr = new TrackedLongRunnable();
1028              p.execute(mr);
1029 <            for (int i = 0; i < 5; ++i) {
1029 >            for (int i = 0; i < tasks.length; ++i)
1030                  p.execute(tasks[i]);
1031 <            }
863 <            for (int i = 1; i < 5; ++i) {
1031 >            for (int i = 1; i < tasks.length; ++i)
1032                  assertTrue(tasks[i].done);
865            }
1033              try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1034          } finally {
1035              joinPool(p);
# Line 870 | Line 1037 | public class ThreadPoolExecutorSubclassT
1037      }
1038  
1039      /**
1040 <     *  executor using DiscardPolicy drops task if saturated.
1040 >     * executor using DiscardPolicy drops task if saturated.
1041       */
1042      public void testSaturatedExecute3() {
1043          RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1044 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1044 >        ThreadPoolExecutor p =
1045 >            new CustomTPE(1, 1,
1046 >                          LONG_DELAY_MS, MILLISECONDS,
1047 >                          new ArrayBlockingQueue<Runnable>(1),
1048 >                          h);
1049          try {
879
1050              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1051 <            for (int i = 0; i < 5; ++i) {
1051 >            for (int i = 0; i < tasks.length; ++i)
1052                  tasks[i] = new TrackedNoOpRunnable();
883            }
1053              p.execute(new TrackedLongRunnable());
1054 <            for (int i = 0; i < 5; ++i) {
1055 <                p.execute(tasks[i]);
1056 <            }
1057 <            for (int i = 0; i < 5; ++i) {
889 <                assertFalse(tasks[i].done);
890 <            }
1054 >            for (TrackedNoOpRunnable task : tasks)
1055 >                p.execute(task);
1056 >            for (TrackedNoOpRunnable task : tasks)
1057 >                assertFalse(task.done);
1058              try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1059          } finally {
1060              joinPool(p);
# Line 895 | Line 1062 | public class ThreadPoolExecutorSubclassT
1062      }
1063  
1064      /**
1065 <     *  executor using DiscardOldestPolicy drops oldest task if saturated.
1065 >     * executor using DiscardOldestPolicy drops oldest task if saturated.
1066       */
1067      public void testSaturatedExecute4() {
1068          RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
# Line 916 | Line 1083 | public class ThreadPoolExecutorSubclassT
1083      }
1084  
1085      /**
1086 <     *  execute throws RejectedExecutionException if shutdown
1086 >     * execute throws RejectedExecutionException if shutdown
1087       */
1088      public void testRejectedExecutionExceptionOnShutdown() {
1089 <        ThreadPoolExecutor tpe =
1089 >        ThreadPoolExecutor p =
1090              new CustomTPE(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
1091 <        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1091 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1092          try {
1093 <            tpe.execute(new NoOpRunnable());
1093 >            p.execute(new NoOpRunnable());
1094              shouldThrow();
1095          } catch (RejectedExecutionException success) {}
1096  
1097 <        joinPool(tpe);
1097 >        joinPool(p);
1098      }
1099  
1100      /**
1101 <     *  execute using CallerRunsPolicy drops task on shutdown
1101 >     * execute using CallerRunsPolicy drops task on shutdown
1102       */
1103      public void testCallerRunsOnShutdown() {
1104          RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
# Line 948 | Line 1115 | public class ThreadPoolExecutorSubclassT
1115      }
1116  
1117      /**
1118 <     *  execute using DiscardPolicy drops task on shutdown
1118 >     * execute using DiscardPolicy drops task on shutdown
1119       */
1120      public void testDiscardOnShutdown() {
1121          RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
# Line 966 | Line 1133 | public class ThreadPoolExecutorSubclassT
1133  
1134  
1135      /**
1136 <     *  execute using DiscardOldestPolicy drops task on shutdown
1136 >     * execute using DiscardOldestPolicy drops task on shutdown
1137       */
1138      public void testDiscardOldestOnShutdown() {
1139          RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
# Line 984 | Line 1151 | public class ThreadPoolExecutorSubclassT
1151  
1152  
1153      /**
1154 <     *  execute (null) throws NPE
1154 >     * execute(null) throws NPE
1155       */
1156      public void testExecuteNull() {
1157 <        ThreadPoolExecutor tpe = null;
1157 >        ThreadPoolExecutor p = null;
1158          try {
1159 <            tpe = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1160 <            tpe.execute(null);
1159 >            p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1160 >            p.execute(null);
1161              shouldThrow();
1162          } catch (NullPointerException success) {}
1163  
1164 <        joinPool(tpe);
1164 >        joinPool(p);
1165      }
1166  
1167      /**
1168 <     *  setCorePoolSize of negative value throws IllegalArgumentException
1168 >     * setCorePoolSize of negative value throws IllegalArgumentException
1169       */
1170      public void testCorePoolSizeIllegalArgumentException() {
1171 <        ThreadPoolExecutor tpe =
1171 >        ThreadPoolExecutor p =
1172              new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1173          try {
1174 <            tpe.setCorePoolSize(-1);
1174 >            p.setCorePoolSize(-1);
1175              shouldThrow();
1176          } catch (IllegalArgumentException success) {
1177          } finally {
1178 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1178 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1179          }
1180 <        joinPool(tpe);
1180 >        joinPool(p);
1181      }
1182  
1183      /**
1184 <     *  setMaximumPoolSize(int) throws IllegalArgumentException if
1185 <     *  given a value less the core pool size
1184 >     * setMaximumPoolSize(int) throws IllegalArgumentException
1185 >     * if given a value less the core pool size
1186       */
1187      public void testMaximumPoolSizeIllegalArgumentException() {
1188 <        ThreadPoolExecutor tpe =
1188 >        ThreadPoolExecutor p =
1189              new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1190          try {
1191 <            tpe.setMaximumPoolSize(1);
1191 >            p.setMaximumPoolSize(1);
1192              shouldThrow();
1193          } catch (IllegalArgumentException success) {
1194          } finally {
1195 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1195 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1196          }
1197 <        joinPool(tpe);
1197 >        joinPool(p);
1198      }
1199  
1200      /**
1201 <     *  setMaximumPoolSize throws IllegalArgumentException
1202 <     *  if given a negative value
1201 >     * setMaximumPoolSize throws IllegalArgumentException
1202 >     * if given a negative value
1203       */
1204      public void testMaximumPoolSizeIllegalArgumentException2() {
1205 <        ThreadPoolExecutor tpe =
1205 >        ThreadPoolExecutor p =
1206              new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1207          try {
1208 <            tpe.setMaximumPoolSize(-1);
1208 >            p.setMaximumPoolSize(-1);
1209              shouldThrow();
1210          } catch (IllegalArgumentException success) {
1211          } finally {
1212 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1212 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1213          }
1214 <        joinPool(tpe);
1214 >        joinPool(p);
1215      }
1216  
1217  
1218      /**
1219 <     *  setKeepAliveTime  throws IllegalArgumentException
1220 <     *  when given a negative value
1219 >     * setKeepAliveTime throws IllegalArgumentException
1220 >     * when given a negative value
1221       */
1222      public void testKeepAliveTimeIllegalArgumentException() {
1223 <        ThreadPoolExecutor tpe =
1223 >        ThreadPoolExecutor p =
1224              new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1225  
1226          try {
1227 <            tpe.setKeepAliveTime(-1,MILLISECONDS);
1227 >            p.setKeepAliveTime(-1,MILLISECONDS);
1228              shouldThrow();
1229          } catch (IllegalArgumentException success) {
1230          } finally {
1231 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1231 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1232          }
1233 <        joinPool(tpe);
1233 >        joinPool(p);
1234      }
1235  
1236      /**
1237       * terminated() is called on termination
1238       */
1239      public void testTerminated() {
1240 <        CustomTPE tpe = new CustomTPE();
1241 <        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1242 <        assertTrue(tpe.terminatedCalled);
1243 <        joinPool(tpe);
1240 >        CustomTPE p = new CustomTPE();
1241 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1242 >        assertTrue(p.terminatedCalled);
1243 >        joinPool(p);
1244      }
1245  
1246      /**
1247       * beforeExecute and afterExecute are called when executing task
1248       */
1249      public void testBeforeAfter() throws InterruptedException {
1250 <        CustomTPE tpe = new CustomTPE();
1250 >        CustomTPE p = new CustomTPE();
1251          try {
1252              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1253 <            tpe.execute(r);
1254 <            Thread.sleep(SHORT_DELAY_MS);
1253 >            p.execute(r);
1254 >            delay(SHORT_DELAY_MS);
1255              assertTrue(r.done);
1256 <            assertTrue(tpe.beforeCalled);
1257 <            assertTrue(tpe.afterCalled);
1258 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1256 >            assertTrue(p.beforeCalled);
1257 >            assertTrue(p.afterCalled);
1258 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1259          } finally {
1260 <            joinPool(tpe);
1260 >            joinPool(p);
1261          }
1262      }
1263  
# Line 1169 | Line 1336 | public class ThreadPoolExecutorSubclassT
1336       * invokeAny(c) throws NPE if c has null elements
1337       */
1338      public void testInvokeAny3() throws Exception {
1339 <        final CountDownLatch latch = new CountDownLatch(1);
1339 >        CountDownLatch latch = new CountDownLatch(1);
1340          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1341 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1342 +        l.add(latchAwaitingStringTask(latch));
1343 +        l.add(null);
1344          try {
1175            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1176            l.add(new Callable<String>() {
1177                      public String call() {
1178                          try {
1179                              latch.await();
1180                          } catch (InterruptedException ok) {}
1181                          return TEST_STRING;
1182                      }});
1183            l.add(null);
1345              e.invokeAny(l);
1346              shouldThrow();
1347          } catch (NullPointerException success) {
# Line 1195 | Line 1356 | public class ThreadPoolExecutorSubclassT
1356       */
1357      public void testInvokeAny4() throws Exception {
1358          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1359 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1360 +        l.add(new NPETask());
1361          try {
1199            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1200            l.add(new NPETask());
1362              e.invokeAny(l);
1363              shouldThrow();
1364          } catch (ExecutionException success) {
# Line 1213 | Line 1374 | public class ThreadPoolExecutorSubclassT
1374      public void testInvokeAny5() throws Exception {
1375          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1376          try {
1377 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1377 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1378              l.add(new StringTask());
1379              l.add(new StringTask());
1380              String result = e.invokeAny(l);
# Line 1255 | Line 1416 | public class ThreadPoolExecutorSubclassT
1416       */
1417      public void testInvokeAll3() throws Exception {
1418          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1419 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1420 +        l.add(new StringTask());
1421 +        l.add(null);
1422          try {
1259            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1260            l.add(new StringTask());
1261            l.add(null);
1423              e.invokeAll(l);
1424              shouldThrow();
1425          } catch (NullPointerException success) {
# Line 1272 | Line 1433 | public class ThreadPoolExecutorSubclassT
1433       */
1434      public void testInvokeAll4() throws Exception {
1435          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1436 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1437 +        l.add(new NPETask());
1438 +        List<Future<String>> futures = e.invokeAll(l);
1439 +        assertEquals(1, futures.size());
1440          try {
1441 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1277 <            l.add(new NPETask());
1278 <            List<Future<String>> result = e.invokeAll(l);
1279 <            assertEquals(1, result.size());
1280 <            for (Future<String> future : result)
1281 <                future.get();
1441 >            futures.get(0).get();
1442              shouldThrow();
1443          } catch (ExecutionException success) {
1444 +            assertTrue(success.getCause() instanceof NullPointerException);
1445          } finally {
1446              joinPool(e);
1447          }
# Line 1292 | Line 1453 | public class ThreadPoolExecutorSubclassT
1453      public void testInvokeAll5() throws Exception {
1454          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1455          try {
1456 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1456 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1457              l.add(new StringTask());
1458              l.add(new StringTask());
1459 <            List<Future<String>> result = e.invokeAll(l);
1460 <            assertEquals(2, result.size());
1461 <            for (Future<String> future : result)
1459 >            List<Future<String>> futures = e.invokeAll(l);
1460 >            assertEquals(2, futures.size());
1461 >            for (Future<String> future : futures)
1462                  assertSame(TEST_STRING, future.get());
1463          } finally {
1464              joinPool(e);
# Line 1325 | Line 1486 | public class ThreadPoolExecutorSubclassT
1486       */
1487      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1488          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1489 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1490 +        l.add(new StringTask());
1491          try {
1329            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1330            l.add(new StringTask());
1492              e.invokeAny(l, MEDIUM_DELAY_MS, null);
1493              shouldThrow();
1494          } catch (NullPointerException success) {
# Line 1354 | Line 1515 | public class ThreadPoolExecutorSubclassT
1515       * timed invokeAny(c) throws NPE if c has null elements
1516       */
1517      public void testTimedInvokeAny3() throws Exception {
1518 <        final CountDownLatch latch = new CountDownLatch(1);
1518 >        CountDownLatch latch = new CountDownLatch(1);
1519          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1520 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1521 +        l.add(latchAwaitingStringTask(latch));
1522 +        l.add(null);
1523          try {
1360            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1361            l.add(new Callable<String>() {
1362                      public String call() {
1363                          try {
1364                              latch.await();
1365                          } catch (InterruptedException ok) {}
1366                          return TEST_STRING;
1367                      }});
1368            l.add(null);
1524              e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1525              shouldThrow();
1526          } catch (NullPointerException success) {
# Line 1380 | Line 1535 | public class ThreadPoolExecutorSubclassT
1535       */
1536      public void testTimedInvokeAny4() throws Exception {
1537          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1538 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1539 +        l.add(new NPETask());
1540          try {
1384            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1385            l.add(new NPETask());
1541              e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1542              shouldThrow();
1543          } catch (ExecutionException success) {
# Line 1398 | Line 1553 | public class ThreadPoolExecutorSubclassT
1553      public void testTimedInvokeAny5() throws Exception {
1554          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1555          try {
1556 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1556 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1557              l.add(new StringTask());
1558              l.add(new StringTask());
1559              String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
# Line 1427 | Line 1582 | public class ThreadPoolExecutorSubclassT
1582       */
1583      public void testTimedInvokeAllNullTimeUnit() throws Exception {
1584          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1585 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1586 +        l.add(new StringTask());
1587          try {
1431            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1432            l.add(new StringTask());
1588              e.invokeAll(l, MEDIUM_DELAY_MS, null);
1589              shouldThrow();
1590          } catch (NullPointerException success) {
# Line 1456 | Line 1611 | public class ThreadPoolExecutorSubclassT
1611       */
1612      public void testTimedInvokeAll3() throws Exception {
1613          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1614 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1615 +        l.add(new StringTask());
1616 +        l.add(null);
1617          try {
1460            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1461            l.add(new StringTask());
1462            l.add(null);
1618              e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1619              shouldThrow();
1620          } catch (NullPointerException success) {
# Line 1473 | Line 1628 | public class ThreadPoolExecutorSubclassT
1628       */
1629      public void testTimedInvokeAll4() throws Exception {
1630          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1631 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1632 +        l.add(new NPETask());
1633 +        List<Future<String>> futures =
1634 +            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1635 +        assertEquals(1, futures.size());
1636          try {
1637 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1478 <            l.add(new NPETask());
1479 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1480 <            assertEquals(1, result.size());
1481 <            for (Future<String> future : result)
1482 <                future.get();
1637 >            futures.get(0).get();
1638              shouldThrow();
1639          } catch (ExecutionException success) {
1640              assertTrue(success.getCause() instanceof NullPointerException);
# Line 1494 | Line 1649 | public class ThreadPoolExecutorSubclassT
1649      public void testTimedInvokeAll5() throws Exception {
1650          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1651          try {
1652 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1652 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1653              l.add(new StringTask());
1654              l.add(new StringTask());
1655 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1656 <            assertEquals(2, result.size());
1657 <            for (Future<String> future : result)
1655 >            List<Future<String>> futures =
1656 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1657 >            assertEquals(2, futures.size());
1658 >            for (Future<String> future : futures)
1659                  assertSame(TEST_STRING, future.get());
1660          } finally {
1661              joinPool(e);
# Line 1512 | Line 1668 | public class ThreadPoolExecutorSubclassT
1668      public void testTimedInvokeAll6() throws Exception {
1669          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1670          try {
1671 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1671 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1672              l.add(new StringTask());
1673              l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1674              l.add(new StringTask());
1675 <            List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1676 <            assertEquals(3, result.size());
1677 <            Iterator<Future<String>> it = result.iterator();
1675 >            List<Future<String>> futures =
1676 >                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1677 >            assertEquals(3, futures.size());
1678 >            Iterator<Future<String>> it = futures.iterator();
1679              Future<String> f1 = it.next();
1680              Future<String> f2 = it.next();
1681              Future<String> f3 = it.next();
# Line 1537 | Line 1694 | public class ThreadPoolExecutorSubclassT
1694       * thread factory fails to create more
1695       */
1696      public void testFailingThreadFactory() throws InterruptedException {
1697 <        ExecutorService e = new CustomTPE(100, 100, LONG_DELAY_MS, MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory());
1698 <        try {
1699 <            for (int k = 0; k < 100; ++k) {
1700 <                e.execute(new NoOpRunnable());
1701 <            }
1702 <            Thread.sleep(LONG_DELAY_MS);
1697 >        final ExecutorService e =
1698 >            new CustomTPE(100, 100,
1699 >                          LONG_DELAY_MS, MILLISECONDS,
1700 >                          new LinkedBlockingQueue<Runnable>(),
1701 >                          new FailingThreadFactory());
1702 >        try {
1703 >            final int TASKS = 100;
1704 >            final CountDownLatch done = new CountDownLatch(TASKS);
1705 >            for (int k = 0; k < TASKS; ++k)
1706 >                e.execute(new CheckedRunnable() {
1707 >                    public void realRun() {
1708 >                        done.countDown();
1709 >                    }});
1710 >            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1711          } finally {
1712              joinPool(e);
1713          }
# Line 1552 | Line 1717 | public class ThreadPoolExecutorSubclassT
1717       * allowsCoreThreadTimeOut is by default false.
1718       */
1719      public void testAllowsCoreThreadTimeOut() {
1720 <        ThreadPoolExecutor tpe = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1721 <        assertFalse(tpe.allowsCoreThreadTimeOut());
1722 <        joinPool(tpe);
1720 >        ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1721 >        assertFalse(p.allowsCoreThreadTimeOut());
1722 >        joinPool(p);
1723      }
1724  
1725      /**
1726       * allowCoreThreadTimeOut(true) causes idle threads to time out
1727       */
1728 <    public void testAllowCoreThreadTimeOut_true() throws InterruptedException {
1729 <        ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1730 <        tpe.allowCoreThreadTimeOut(true);
1731 <        tpe.execute(new NoOpRunnable());
1732 <        try {
1733 <            Thread.sleep(MEDIUM_DELAY_MS);
1734 <            assertEquals(0, tpe.getPoolSize());
1728 >    public void testAllowCoreThreadTimeOut_true() throws Exception {
1729 >        final ThreadPoolExecutor p =
1730 >            new CustomTPE(2, 10,
1731 >                          SHORT_DELAY_MS, MILLISECONDS,
1732 >                          new ArrayBlockingQueue<Runnable>(10));
1733 >        final CountDownLatch threadStarted = new CountDownLatch(1);
1734 >        try {
1735 >            p.allowCoreThreadTimeOut(true);
1736 >            p.execute(new CheckedRunnable() {
1737 >                public void realRun() throws InterruptedException {
1738 >                    threadStarted.countDown();
1739 >                    assertEquals(1, p.getPoolSize());
1740 >                }});
1741 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
1742 >            for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) {
1743 >                if (p.getPoolSize() == 0)
1744 >                    break;
1745 >                delay(10);
1746 >            }
1747 >            assertEquals(0, p.getPoolSize());
1748          } finally {
1749 <            joinPool(tpe);
1749 >            joinPool(p);
1750          }
1751      }
1752  
1753      /**
1754       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1755       */
1756 <    public void testAllowCoreThreadTimeOut_false() throws InterruptedException {
1757 <        ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1758 <        tpe.allowCoreThreadTimeOut(false);
1759 <        tpe.execute(new NoOpRunnable());
1760 <        try {
1761 <            Thread.sleep(MEDIUM_DELAY_MS);
1762 <            assertTrue(tpe.getPoolSize() >= 1);
1756 >    public void testAllowCoreThreadTimeOut_false() throws Exception {
1757 >        final ThreadPoolExecutor p =
1758 >            new CustomTPE(2, 10,
1759 >                          SHORT_DELAY_MS, MILLISECONDS,
1760 >                          new ArrayBlockingQueue<Runnable>(10));
1761 >        final CountDownLatch threadStarted = new CountDownLatch(1);
1762 >        try {
1763 >            p.allowCoreThreadTimeOut(false);
1764 >            p.execute(new CheckedRunnable() {
1765 >                public void realRun() throws InterruptedException {
1766 >                    threadStarted.countDown();
1767 >                    assertTrue(p.getPoolSize() >= 1);
1768 >                }});
1769 >            delay(SMALL_DELAY_MS);
1770 >            assertTrue(p.getPoolSize() >= 1);
1771          } finally {
1772 <            joinPool(tpe);
1772 >            joinPool(p);
1773          }
1774      }
1775  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines