ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java (file contents):
Revision 1.18 by jsr166, Wed Aug 25 00:07:03 2010 UTC vs.
Revision 1.29 by jsr166, Fri May 27 19:35:24 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 117 | Line 115 | public class ThreadPoolExecutorSubclassT
115          }
116      }
117  
120
118      static class CustomTPE extends ThreadPoolExecutor {
119          protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
120              return new CustomTask<V>(c);
# Line 190 | Line 187 | public class ThreadPoolExecutorSubclassT
187          }
188      }
189  
193
190      /**
191 <     *  execute successfully executes a runnable
191 >     * execute successfully executes a runnable
192       */
193      public void testExecute() throws InterruptedException {
194 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
194 >        final ThreadPoolExecutor p =
195 >            new CustomTPE(1, 1,
196 >                          LONG_DELAY_MS, MILLISECONDS,
197 >                          new ArrayBlockingQueue<Runnable>(10));
198 >        final CountDownLatch done = new CountDownLatch(1);
199 >        final Runnable task = new CheckedRunnable() {
200 >            public void realRun() {
201 >                done.countDown();
202 >            }};
203          try {
204 <            p1.execute(new ShortRunnable());
205 <            Thread.sleep(SMALL_DELAY_MS);
204 >            p.execute(task);
205 >            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
206          } finally {
207 <            joinPool(p1);
207 >            joinPool(p);
208          }
209      }
210  
211      /**
212 <     *  getActiveCount increases but doesn't overestimate, when a
213 <     *  thread becomes active
212 >     * getActiveCount increases but doesn't overestimate, when a
213 >     * thread becomes active
214       */
215      public void testGetActiveCount() throws InterruptedException {
216 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
217 <        assertEquals(0, p2.getActiveCount());
218 <        p2.execute(new MediumRunnable());
219 <        Thread.sleep(SHORT_DELAY_MS);
220 <        assertEquals(1, p2.getActiveCount());
221 <        joinPool(p2);
216 >        final ThreadPoolExecutor p =
217 >            new CustomTPE(2, 2,
218 >                          LONG_DELAY_MS, MILLISECONDS,
219 >                          new ArrayBlockingQueue<Runnable>(10));
220 >        final CountDownLatch threadStarted = new CountDownLatch(1);
221 >        final CountDownLatch done = new CountDownLatch(1);
222 >        try {
223 >            assertEquals(0, p.getActiveCount());
224 >            p.execute(new CheckedRunnable() {
225 >                public void realRun() throws InterruptedException {
226 >                    threadStarted.countDown();
227 >                    assertEquals(1, p.getActiveCount());
228 >                    done.await();
229 >                }});
230 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
231 >            assertEquals(1, p.getActiveCount());
232 >        } finally {
233 >            done.countDown();
234 >            joinPool(p);
235 >        }
236      }
237  
238      /**
239 <     *  prestartCoreThread starts a thread if under corePoolSize, else doesn't
239 >     * prestartCoreThread starts a thread if under corePoolSize, else doesn't
240       */
241      public void testPrestartCoreThread() {
242 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
243 <        assertEquals(0, p2.getPoolSize());
244 <        assertTrue(p2.prestartCoreThread());
245 <        assertEquals(1, p2.getPoolSize());
246 <        assertTrue(p2.prestartCoreThread());
247 <        assertEquals(2, p2.getPoolSize());
248 <        assertFalse(p2.prestartCoreThread());
249 <        assertEquals(2, p2.getPoolSize());
250 <        joinPool(p2);
242 >        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
243 >        assertEquals(0, p.getPoolSize());
244 >        assertTrue(p.prestartCoreThread());
245 >        assertEquals(1, p.getPoolSize());
246 >        assertTrue(p.prestartCoreThread());
247 >        assertEquals(2, p.getPoolSize());
248 >        assertFalse(p.prestartCoreThread());
249 >        assertEquals(2, p.getPoolSize());
250 >        joinPool(p);
251      }
252  
253      /**
254 <     *  prestartAllCoreThreads starts all corePoolSize threads
254 >     * prestartAllCoreThreads starts all corePoolSize threads
255       */
256      public void testPrestartAllCoreThreads() {
257 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
258 <        assertEquals(0, p2.getPoolSize());
259 <        p2.prestartAllCoreThreads();
260 <        assertEquals(2, p2.getPoolSize());
261 <        p2.prestartAllCoreThreads();
262 <        assertEquals(2, p2.getPoolSize());
263 <        joinPool(p2);
257 >        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
258 >        assertEquals(0, p.getPoolSize());
259 >        p.prestartAllCoreThreads();
260 >        assertEquals(2, p.getPoolSize());
261 >        p.prestartAllCoreThreads();
262 >        assertEquals(2, p.getPoolSize());
263 >        joinPool(p);
264      }
265  
266      /**
267 <     *   getCompletedTaskCount increases, but doesn't overestimate,
268 <     *   when tasks complete
267 >     * getCompletedTaskCount increases, but doesn't overestimate,
268 >     * when tasks complete
269       */
270      public void testGetCompletedTaskCount() throws InterruptedException {
271 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
272 <        assertEquals(0, p2.getCompletedTaskCount());
273 <        p2.execute(new ShortRunnable());
274 <        Thread.sleep(SMALL_DELAY_MS);
275 <        assertEquals(1, p2.getCompletedTaskCount());
276 <        try { p2.shutdown(); } catch (SecurityException ok) { return; }
277 <        joinPool(p2);
271 >        final ThreadPoolExecutor p =
272 >            new CustomTPE(2, 2,
273 >                          LONG_DELAY_MS, MILLISECONDS,
274 >                          new ArrayBlockingQueue<Runnable>(10));
275 >        final CountDownLatch threadStarted = new CountDownLatch(1);
276 >        final CountDownLatch threadProceed = new CountDownLatch(1);
277 >        final CountDownLatch threadDone = new CountDownLatch(1);
278 >        try {
279 >            assertEquals(0, p.getCompletedTaskCount());
280 >            p.execute(new CheckedRunnable() {
281 >                public void realRun() throws InterruptedException {
282 >                    threadStarted.countDown();
283 >                    assertEquals(0, p.getCompletedTaskCount());
284 >                    threadProceed.await();
285 >                    threadDone.countDown();
286 >                }});
287 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
288 >            assertEquals(0, p.getCompletedTaskCount());
289 >            threadProceed.countDown();
290 >            threadDone.await();
291 >            delay(SHORT_DELAY_MS);
292 >            assertEquals(1, p.getCompletedTaskCount());
293 >        } finally {
294 >            joinPool(p);
295 >        }
296      }
297  
298      /**
299 <     *   getCorePoolSize returns size given in constructor if not otherwise set
299 >     * getCorePoolSize returns size given in constructor if not otherwise set
300       */
301      public void testGetCorePoolSize() {
302 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
303 <        assertEquals(1, p1.getCorePoolSize());
304 <        joinPool(p1);
302 >        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
303 >        assertEquals(1, p.getCorePoolSize());
304 >        joinPool(p);
305      }
306  
307      /**
308 <     *   getKeepAliveTime returns value given in constructor if not otherwise set
308 >     * getKeepAliveTime returns value given in constructor if not otherwise set
309       */
310      public void testGetKeepAliveTime() {
311 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
312 <        assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS));
313 <        joinPool(p2);
311 >        ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
312 >        assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS));
313 >        joinPool(p);
314      }
315  
280
316      /**
317       * getThreadFactory returns factory in constructor if not set
318       */
# Line 299 | Line 334 | public class ThreadPoolExecutorSubclassT
334          joinPool(p);
335      }
336  
302
337      /**
338       * setThreadFactory(null) throws NPE
339       */
# Line 336 | Line 370 | public class ThreadPoolExecutorSubclassT
370          joinPool(p);
371      }
372  
339
373      /**
374       * setRejectedExecutionHandler(null) throws NPE
375       */
# Line 351 | Line 384 | public class ThreadPoolExecutorSubclassT
384          }
385      }
386  
354
387      /**
388 <     *   getLargestPoolSize increases, but doesn't overestimate, when
389 <     *   multiple threads active
388 >     * getLargestPoolSize increases, but doesn't overestimate, when
389 >     * multiple threads active
390       */
391      public void testGetLargestPoolSize() throws InterruptedException {
392 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
393 <        assertEquals(0, p2.getLargestPoolSize());
394 <        p2.execute(new MediumRunnable());
395 <        p2.execute(new MediumRunnable());
396 <        Thread.sleep(SHORT_DELAY_MS);
397 <        assertEquals(2, p2.getLargestPoolSize());
398 <        joinPool(p2);
392 >        final int THREADS = 3;
393 >        final ThreadPoolExecutor p =
394 >            new CustomTPE(THREADS, THREADS,
395 >                          LONG_DELAY_MS, MILLISECONDS,
396 >                          new ArrayBlockingQueue<Runnable>(10));
397 >        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
398 >        final CountDownLatch done = new CountDownLatch(1);
399 >        try {
400 >            assertEquals(0, p.getLargestPoolSize());
401 >            for (int i = 0; i < THREADS; i++)
402 >                p.execute(new CheckedRunnable() {
403 >                    public void realRun() throws InterruptedException {
404 >                        threadsStarted.countDown();
405 >                        done.await();
406 >                        assertEquals(THREADS, p.getLargestPoolSize());
407 >                    }});
408 >            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
409 >            assertEquals(THREADS, p.getLargestPoolSize());
410 >        } finally {
411 >            done.countDown();
412 >            joinPool(p);
413 >            assertEquals(THREADS, p.getLargestPoolSize());
414 >        }
415      }
416  
417      /**
418 <     *   getMaximumPoolSize returns value given in constructor if not
419 <     *   otherwise set
418 >     * getMaximumPoolSize returns value given in constructor if not
419 >     * otherwise set
420       */
421      public void testGetMaximumPoolSize() {
422 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
423 <        assertEquals(2, p2.getMaximumPoolSize());
424 <        joinPool(p2);
422 >        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
423 >        assertEquals(2, p.getMaximumPoolSize());
424 >        joinPool(p);
425      }
426  
427      /**
428 <     *   getPoolSize increases, but doesn't overestimate, when threads
429 <     *   become active
428 >     * getPoolSize increases, but doesn't overestimate, when threads
429 >     * become active
430       */
431 <    public void testGetPoolSize() {
432 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
433 <        assertEquals(0, p1.getPoolSize());
434 <        p1.execute(new MediumRunnable());
435 <        assertEquals(1, p1.getPoolSize());
436 <        joinPool(p1);
431 >    public void testGetPoolSize() throws InterruptedException {
432 >        final ThreadPoolExecutor p =
433 >            new CustomTPE(1, 1,
434 >                          LONG_DELAY_MS, MILLISECONDS,
435 >                          new ArrayBlockingQueue<Runnable>(10));
436 >        final CountDownLatch threadStarted = new CountDownLatch(1);
437 >        final CountDownLatch done = new CountDownLatch(1);
438 >        try {
439 >            assertEquals(0, p.getPoolSize());
440 >            p.execute(new CheckedRunnable() {
441 >                public void realRun() throws InterruptedException {
442 >                    threadStarted.countDown();
443 >                    assertEquals(1, p.getPoolSize());
444 >                    done.await();
445 >                }});
446 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
447 >            assertEquals(1, p.getPoolSize());
448 >        } finally {
449 >            done.countDown();
450 >            joinPool(p);
451 >        }
452      }
453  
454      /**
455 <     *  getTaskCount increases, but doesn't overestimate, when tasks submitted
455 >     * getTaskCount increases, but doesn't overestimate, when tasks submitted
456       */
457      public void testGetTaskCount() throws InterruptedException {
458 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
459 <        assertEquals(0, p1.getTaskCount());
460 <        p1.execute(new MediumRunnable());
461 <        Thread.sleep(SHORT_DELAY_MS);
462 <        assertEquals(1, p1.getTaskCount());
463 <        joinPool(p1);
458 >        final ThreadPoolExecutor p =
459 >            new CustomTPE(1, 1,
460 >                          LONG_DELAY_MS, MILLISECONDS,
461 >                          new ArrayBlockingQueue<Runnable>(10));
462 >        final CountDownLatch threadStarted = new CountDownLatch(1);
463 >        final CountDownLatch done = new CountDownLatch(1);
464 >        try {
465 >            assertEquals(0, p.getTaskCount());
466 >            p.execute(new CheckedRunnable() {
467 >                public void realRun() throws InterruptedException {
468 >                    threadStarted.countDown();
469 >                    assertEquals(1, p.getTaskCount());
470 >                    done.await();
471 >                }});
472 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
473 >            assertEquals(1, p.getTaskCount());
474 >        } finally {
475 >            done.countDown();
476 >            joinPool(p);
477 >        }
478      }
479  
480      /**
481 <     *   isShutDown is false before shutdown, true after
481 >     * isShutdown is false before shutdown, true after
482       */
483      public void testIsShutdown() {
484  
485 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
486 <        assertFalse(p1.isShutdown());
487 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
488 <        assertTrue(p1.isShutdown());
489 <        joinPool(p1);
485 >        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
486 >        assertFalse(p.isShutdown());
487 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
488 >        assertTrue(p.isShutdown());
489 >        joinPool(p);
490      }
491  
415
492      /**
493 <     *  isTerminated is false before termination, true after
493 >     * isTerminated is false before termination, true after
494       */
495      public void testIsTerminated() throws InterruptedException {
496 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
497 <        assertFalse(p1.isTerminated());
498 <        try {
499 <            p1.execute(new MediumRunnable());
500 <        } finally {
501 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
502 <        }
503 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
504 <        assertTrue(p1.isTerminated());
496 >        final ThreadPoolExecutor p =
497 >            new CustomTPE(1, 1,
498 >                          LONG_DELAY_MS, MILLISECONDS,
499 >                          new ArrayBlockingQueue<Runnable>(10));
500 >        final CountDownLatch threadStarted = new CountDownLatch(1);
501 >        final CountDownLatch done = new CountDownLatch(1);
502 >        try {
503 >            assertFalse(p.isTerminating());
504 >            p.execute(new CheckedRunnable() {
505 >                public void realRun() throws InterruptedException {
506 >                    assertFalse(p.isTerminating());
507 >                    threadStarted.countDown();
508 >                    done.await();
509 >                }});
510 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
511 >            assertFalse(p.isTerminating());
512 >            done.countDown();
513 >        } finally {
514 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
515 >        }
516 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
517 >        assertTrue(p.isTerminated());
518 >        assertFalse(p.isTerminating());
519      }
520  
521      /**
522 <     *  isTerminating is not true when running or when terminated
522 >     * isTerminating is not true when running or when terminated
523       */
524      public void testIsTerminating() throws InterruptedException {
525 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
526 <        assertFalse(p1.isTerminating());
527 <        try {
528 <            p1.execute(new SmallRunnable());
529 <            assertFalse(p1.isTerminating());
530 <        } finally {
531 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
532 <        }
533 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
534 <        assertTrue(p1.isTerminated());
535 <        assertFalse(p1.isTerminating());
525 >        final ThreadPoolExecutor p =
526 >            new CustomTPE(1, 1,
527 >                          LONG_DELAY_MS, MILLISECONDS,
528 >                          new ArrayBlockingQueue<Runnable>(10));
529 >        final CountDownLatch threadStarted = new CountDownLatch(1);
530 >        final CountDownLatch done = new CountDownLatch(1);
531 >        try {
532 >            assertFalse(p.isTerminating());
533 >            p.execute(new CheckedRunnable() {
534 >                public void realRun() throws InterruptedException {
535 >                    assertFalse(p.isTerminating());
536 >                    threadStarted.countDown();
537 >                    done.await();
538 >                }});
539 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
540 >            assertFalse(p.isTerminating());
541 >            done.countDown();
542 >        } finally {
543 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
544 >        }
545 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
546 >        assertTrue(p.isTerminated());
547 >        assertFalse(p.isTerminating());
548      }
549  
550      /**
551       * getQueue returns the work queue, which contains queued tasks
552       */
553      public void testGetQueue() throws InterruptedException {
554 <        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
555 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, q);
556 <        FutureTask[] tasks = new FutureTask[5];
557 <        for (int i = 0; i < 5; i++) {
558 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
559 <            p1.execute(tasks[i]);
560 <        }
561 <        try {
562 <            Thread.sleep(SHORT_DELAY_MS);
563 <            BlockingQueue<Runnable> wq = p1.getQueue();
564 <            assertSame(q, wq);
565 <            assertFalse(wq.contains(tasks[0]));
566 <            assertTrue(wq.contains(tasks[4]));
567 <            for (int i = 1; i < 5; ++i)
568 <                tasks[i].cancel(true);
569 <            p1.shutdownNow();
554 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
555 >        final ThreadPoolExecutor p =
556 >            new CustomTPE(1, 1,
557 >                          LONG_DELAY_MS, MILLISECONDS,
558 >                          q);
559 >        final CountDownLatch threadStarted = new CountDownLatch(1);
560 >        final CountDownLatch done = new CountDownLatch(1);
561 >        try {
562 >            FutureTask[] tasks = new FutureTask[5];
563 >            for (int i = 0; i < tasks.length; i++) {
564 >                Callable task = new CheckedCallable<Boolean>() {
565 >                    public Boolean realCall() throws InterruptedException {
566 >                        threadStarted.countDown();
567 >                        assertSame(q, p.getQueue());
568 >                        done.await();
569 >                        return Boolean.TRUE;
570 >                    }};
571 >                tasks[i] = new FutureTask(task);
572 >                p.execute(tasks[i]);
573 >            }
574 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
575 >            assertSame(q, p.getQueue());
576 >            assertFalse(q.contains(tasks[0]));
577 >            assertTrue(q.contains(tasks[tasks.length - 1]));
578 >            assertEquals(tasks.length - 1, q.size());
579          } finally {
580 <            joinPool(p1);
580 >            done.countDown();
581 >            joinPool(p);
582          }
583      }
584  
# Line 475 | Line 587 | public class ThreadPoolExecutorSubclassT
587       */
588      public void testRemove() throws InterruptedException {
589          BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
590 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, q);
591 <        FutureTask[] tasks = new FutureTask[5];
592 <        for (int i = 0; i < 5; i++) {
593 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
594 <            p1.execute(tasks[i]);
595 <        }
596 <        try {
597 <            Thread.sleep(SHORT_DELAY_MS);
598 <            assertFalse(p1.remove(tasks[0]));
590 >        final ThreadPoolExecutor p =
591 >            new CustomTPE(1, 1,
592 >                          LONG_DELAY_MS, MILLISECONDS,
593 >                          q);
594 >        Runnable[] tasks = new Runnable[6];
595 >        final CountDownLatch threadStarted = new CountDownLatch(1);
596 >        final CountDownLatch done = new CountDownLatch(1);
597 >        try {
598 >            for (int i = 0; i < tasks.length; i++) {
599 >                tasks[i] = new CheckedRunnable() {
600 >                        public void realRun() throws InterruptedException {
601 >                            threadStarted.countDown();
602 >                            done.await();
603 >                        }};
604 >                p.execute(tasks[i]);
605 >            }
606 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
607 >            assertFalse(p.remove(tasks[0]));
608              assertTrue(q.contains(tasks[4]));
609              assertTrue(q.contains(tasks[3]));
610 <            assertTrue(p1.remove(tasks[4]));
611 <            assertFalse(p1.remove(tasks[4]));
610 >            assertTrue(p.remove(tasks[4]));
611 >            assertFalse(p.remove(tasks[4]));
612              assertFalse(q.contains(tasks[4]));
613              assertTrue(q.contains(tasks[3]));
614 <            assertTrue(p1.remove(tasks[3]));
614 >            assertTrue(p.remove(tasks[3]));
615              assertFalse(q.contains(tasks[3]));
616          } finally {
617 <            joinPool(p1);
617 >            done.countDown();
618 >            joinPool(p);
619          }
620      }
621  
622      /**
623 <     *   purge removes cancelled tasks from the queue
623 >     * purge removes cancelled tasks from the queue
624       */
625 <    public void testPurge() {
626 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
625 >    public void testPurge() throws InterruptedException {
626 >        final CountDownLatch threadStarted = new CountDownLatch(1);
627 >        final CountDownLatch done = new CountDownLatch(1);
628 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
629 >        final ThreadPoolExecutor p =
630 >            new CustomTPE(1, 1,
631 >                          LONG_DELAY_MS, MILLISECONDS,
632 >                          q);
633          FutureTask[] tasks = new FutureTask[5];
634 <        for (int i = 0; i < 5; i++) {
635 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
636 <            p1.execute(tasks[i]);
634 >        try {
635 >            for (int i = 0; i < tasks.length; i++) {
636 >                Callable task = new CheckedCallable<Boolean>() {
637 >                    public Boolean realCall() throws InterruptedException {
638 >                        threadStarted.countDown();
639 >                        done.await();
640 >                        return Boolean.TRUE;
641 >                    }};
642 >                tasks[i] = new FutureTask(task);
643 >                p.execute(tasks[i]);
644 >            }
645 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
646 >            assertEquals(tasks.length, p.getTaskCount());
647 >            assertEquals(tasks.length - 1, q.size());
648 >            assertEquals(1L, p.getActiveCount());
649 >            assertEquals(0L, p.getCompletedTaskCount());
650 >            tasks[4].cancel(true);
651 >            tasks[3].cancel(false);
652 >            p.purge();
653 >            assertEquals(tasks.length - 3, q.size());
654 >            assertEquals(tasks.length - 2, p.getTaskCount());
655 >            p.purge();         // Nothing to do
656 >            assertEquals(tasks.length - 3, q.size());
657 >            assertEquals(tasks.length - 2, p.getTaskCount());
658 >        } finally {
659 >            done.countDown();
660 >            joinPool(p);
661          }
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);
662      }
663  
664      /**
665 <     *  shutDownNow returns a list containing tasks that were not run
665 >     * shutdownNow returns a list containing tasks that were not run
666       */
667 <    public void testShutDownNow() {
668 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
667 >    public void testShutdownNow() {
668 >        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
669          List l;
670          try {
671              for (int i = 0; i < 5; i++)
672 <                p1.execute(new MediumPossiblyInterruptedRunnable());
672 >                p.execute(new MediumPossiblyInterruptedRunnable());
673          }
674          finally {
675              try {
676 <                l = p1.shutdownNow();
676 >                l = p.shutdownNow();
677              } catch (SecurityException ok) { return; }
678          }
679 <        assertTrue(p1.isShutdown());
679 >        assertTrue(p.isShutdown());
680          assertTrue(l.size() <= 4);
681      }
682  
683      // Exception Tests
684  
539
685      /**
686       * Constructor throws if corePoolSize argument is less than zero
687       */
# Line 597 | Line 742 | public class ThreadPoolExecutorSubclassT
742          } catch (NullPointerException success) {}
743      }
744  
600
601
745      /**
746       * Constructor throws if corePoolSize argument is less than zero
747       */
# Line 670 | Line 813 | public class ThreadPoolExecutorSubclassT
813          } catch (NullPointerException success) {}
814      }
815  
673
816      /**
817       * Constructor throws if corePoolSize argument is less than zero
818       */
# Line 742 | Line 884 | public class ThreadPoolExecutorSubclassT
884          } catch (NullPointerException success) {}
885      }
886  
745
887      /**
888       * Constructor throws if corePoolSize argument is less than zero
889       */
# Line 794 | Line 935 | public class ThreadPoolExecutorSubclassT
935      }
936  
937      /**
938 <     * Constructor throws if workQueue is set to null
938 >     * Constructor throws if workQueue is null
939       */
940      public void testConstructorNullPointerException6() {
941          try {
# Line 804 | Line 945 | public class ThreadPoolExecutorSubclassT
945      }
946  
947      /**
948 <     * Constructor throws if handler is set to null
948 >     * Constructor throws if handler is null
949       */
950      public void testConstructorNullPointerException7() {
951          try {
# Line 815 | Line 956 | public class ThreadPoolExecutorSubclassT
956      }
957  
958      /**
959 <     * Constructor throws if ThreadFactory is set top null
959 >     * Constructor throws if ThreadFactory is null
960       */
961      public void testConstructorNullPointerException8() {
962          try {
963 <            ThreadFactory f = null;
964 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
963 >            new CustomTPE(1, 2,
964 >                          LONG_DELAY_MS, MILLISECONDS,
965 >                          new ArrayBlockingQueue<Runnable>(10),
966 >                          (ThreadFactory) null,
967 >                          new NoOpREHandler());
968              shouldThrow();
969          } catch (NullPointerException success) {}
970      }
971  
828
972      /**
973 <     *  execute throws RejectedExecutionException
831 <     *  if saturated.
973 >     * execute throws RejectedExecutionException if saturated.
974       */
975      public void testSaturatedExecute() {
976 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
977 <        try {
978 <
979 <            for (int i = 0; i < 5; ++i) {
980 <                p.execute(new MediumRunnable());
976 >        ThreadPoolExecutor p =
977 >            new CustomTPE(1, 1,
978 >                          LONG_DELAY_MS, MILLISECONDS,
979 >                          new ArrayBlockingQueue<Runnable>(1));
980 >        final CountDownLatch done = new CountDownLatch(1);
981 >        try {
982 >            Runnable task = new CheckedRunnable() {
983 >                public void realRun() throws InterruptedException {
984 >                    done.await();
985 >                }};
986 >            for (int i = 0; i < 2; ++i)
987 >                p.execute(task);
988 >            for (int i = 0; i < 2; ++i) {
989 >                try {
990 >                    p.execute(task);
991 >                    shouldThrow();
992 >                } catch (RejectedExecutionException success) {}
993 >                assertTrue(p.getTaskCount() <= 2);
994              }
995 <            shouldThrow();
996 <        } catch (RejectedExecutionException success) {}
997 <        joinPool(p);
995 >        } finally {
996 >            done.countDown();
997 >            joinPool(p);
998 >        }
999      }
1000  
1001      /**
1002 <     *  executor using CallerRunsPolicy runs task if saturated.
1002 >     * executor using CallerRunsPolicy runs task if saturated.
1003       */
1004      public void testSaturatedExecute2() {
1005          RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1006 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1006 >        ThreadPoolExecutor p = new CustomTPE(1, 1,
1007 >                                             LONG_DELAY_MS, MILLISECONDS,
1008 >                                             new ArrayBlockingQueue<Runnable>(1),
1009 >                                             h);
1010          try {
852
1011              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1012 <            for (int i = 0; i < 5; ++i) {
1012 >            for (int i = 0; i < tasks.length; ++i)
1013                  tasks[i] = new TrackedNoOpRunnable();
856            }
1014              TrackedLongRunnable mr = new TrackedLongRunnable();
1015              p.execute(mr);
1016 <            for (int i = 0; i < 5; ++i) {
1016 >            for (int i = 0; i < tasks.length; ++i)
1017                  p.execute(tasks[i]);
1018 <            }
862 <            for (int i = 1; i < 5; ++i) {
1018 >            for (int i = 1; i < tasks.length; ++i)
1019                  assertTrue(tasks[i].done);
864            }
1020              try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1021          } finally {
1022              joinPool(p);
# Line 869 | Line 1024 | public class ThreadPoolExecutorSubclassT
1024      }
1025  
1026      /**
1027 <     *  executor using DiscardPolicy drops task if saturated.
1027 >     * executor using DiscardPolicy drops task if saturated.
1028       */
1029      public void testSaturatedExecute3() {
1030          RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1031 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1031 >        ThreadPoolExecutor p =
1032 >            new CustomTPE(1, 1,
1033 >                          LONG_DELAY_MS, MILLISECONDS,
1034 >                          new ArrayBlockingQueue<Runnable>(1),
1035 >                          h);
1036          try {
878
1037              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1038 <            for (int i = 0; i < 5; ++i) {
1038 >            for (int i = 0; i < tasks.length; ++i)
1039                  tasks[i] = new TrackedNoOpRunnable();
882            }
1040              p.execute(new TrackedLongRunnable());
1041 <            for (int i = 0; i < 5; ++i) {
1042 <                p.execute(tasks[i]);
1043 <            }
1044 <            for (int i = 0; i < 5; ++i) {
888 <                assertFalse(tasks[i].done);
889 <            }
1041 >            for (TrackedNoOpRunnable task : tasks)
1042 >                p.execute(task);
1043 >            for (TrackedNoOpRunnable task : tasks)
1044 >                assertFalse(task.done);
1045              try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1046          } finally {
1047              joinPool(p);
# Line 894 | Line 1049 | public class ThreadPoolExecutorSubclassT
1049      }
1050  
1051      /**
1052 <     *  executor using DiscardOldestPolicy drops oldest task if saturated.
1052 >     * executor using DiscardOldestPolicy drops oldest task if saturated.
1053       */
1054      public void testSaturatedExecute4() {
1055          RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
# Line 915 | Line 1070 | public class ThreadPoolExecutorSubclassT
1070      }
1071  
1072      /**
1073 <     *  execute throws RejectedExecutionException if shutdown
1073 >     * execute throws RejectedExecutionException if shutdown
1074       */
1075      public void testRejectedExecutionExceptionOnShutdown() {
1076 <        ThreadPoolExecutor tpe =
1076 >        ThreadPoolExecutor p =
1077              new CustomTPE(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
1078 <        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1078 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1079          try {
1080 <            tpe.execute(new NoOpRunnable());
1080 >            p.execute(new NoOpRunnable());
1081              shouldThrow();
1082          } catch (RejectedExecutionException success) {}
1083  
1084 <        joinPool(tpe);
1084 >        joinPool(p);
1085      }
1086  
1087      /**
1088 <     *  execute using CallerRunsPolicy drops task on shutdown
1088 >     * execute using CallerRunsPolicy drops task on shutdown
1089       */
1090      public void testCallerRunsOnShutdown() {
1091          RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
# Line 947 | Line 1102 | public class ThreadPoolExecutorSubclassT
1102      }
1103  
1104      /**
1105 <     *  execute using DiscardPolicy drops task on shutdown
1105 >     * execute using DiscardPolicy drops task on shutdown
1106       */
1107      public void testDiscardOnShutdown() {
1108          RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
# Line 963 | Line 1118 | public class ThreadPoolExecutorSubclassT
1118          }
1119      }
1120  
966
1121      /**
1122 <     *  execute using DiscardOldestPolicy drops task on shutdown
1122 >     * execute using DiscardOldestPolicy drops task on shutdown
1123       */
1124      public void testDiscardOldestOnShutdown() {
1125          RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
# Line 981 | Line 1135 | public class ThreadPoolExecutorSubclassT
1135          }
1136      }
1137  
984
1138      /**
1139       * execute(null) throws NPE
1140       */
1141      public void testExecuteNull() {
1142 <        ThreadPoolExecutor tpe = null;
1142 >        ThreadPoolExecutor p = null;
1143          try {
1144 <            tpe = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1145 <            tpe.execute(null);
1144 >            p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1145 >            p.execute(null);
1146              shouldThrow();
1147          } catch (NullPointerException success) {}
1148  
1149 <        joinPool(tpe);
1149 >        joinPool(p);
1150      }
1151  
1152      /**
1153 <     *  setCorePoolSize of negative value throws IllegalArgumentException
1153 >     * setCorePoolSize of negative value throws IllegalArgumentException
1154       */
1155      public void testCorePoolSizeIllegalArgumentException() {
1156 <        ThreadPoolExecutor tpe =
1156 >        ThreadPoolExecutor p =
1157              new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1158          try {
1159 <            tpe.setCorePoolSize(-1);
1159 >            p.setCorePoolSize(-1);
1160              shouldThrow();
1161          } catch (IllegalArgumentException success) {
1162          } finally {
1163 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1163 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1164          }
1165 <        joinPool(tpe);
1165 >        joinPool(p);
1166      }
1167  
1168      /**
1169 <     *  setMaximumPoolSize(int) throws IllegalArgumentException if
1170 <     *  given a value less the core pool size
1169 >     * setMaximumPoolSize(int) throws IllegalArgumentException
1170 >     * if given a value less the core pool size
1171       */
1172      public void testMaximumPoolSizeIllegalArgumentException() {
1173 <        ThreadPoolExecutor tpe =
1173 >        ThreadPoolExecutor p =
1174              new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1175          try {
1176 <            tpe.setMaximumPoolSize(1);
1176 >            p.setMaximumPoolSize(1);
1177              shouldThrow();
1178          } catch (IllegalArgumentException success) {
1179          } finally {
1180 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1180 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1181          }
1182 <        joinPool(tpe);
1182 >        joinPool(p);
1183      }
1184  
1185      /**
1186 <     *  setMaximumPoolSize throws IllegalArgumentException
1187 <     *  if given a negative value
1186 >     * setMaximumPoolSize throws IllegalArgumentException
1187 >     * if given a negative value
1188       */
1189      public void testMaximumPoolSizeIllegalArgumentException2() {
1190 <        ThreadPoolExecutor tpe =
1190 >        ThreadPoolExecutor p =
1191              new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1192          try {
1193 <            tpe.setMaximumPoolSize(-1);
1193 >            p.setMaximumPoolSize(-1);
1194              shouldThrow();
1195          } catch (IllegalArgumentException success) {
1196          } finally {
1197 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1197 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1198          }
1199 <        joinPool(tpe);
1199 >        joinPool(p);
1200      }
1201  
1049
1202      /**
1203 <     *  setKeepAliveTime  throws IllegalArgumentException
1204 <     *  when given a negative value
1203 >     * setKeepAliveTime throws IllegalArgumentException
1204 >     * when given a negative value
1205       */
1206      public void testKeepAliveTimeIllegalArgumentException() {
1207 <        ThreadPoolExecutor tpe =
1207 >        ThreadPoolExecutor p =
1208              new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1209  
1210          try {
1211 <            tpe.setKeepAliveTime(-1,MILLISECONDS);
1211 >            p.setKeepAliveTime(-1,MILLISECONDS);
1212              shouldThrow();
1213          } catch (IllegalArgumentException success) {
1214          } finally {
1215 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1215 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1216          }
1217 <        joinPool(tpe);
1217 >        joinPool(p);
1218      }
1219  
1220      /**
1221       * terminated() is called on termination
1222       */
1223      public void testTerminated() {
1224 <        CustomTPE tpe = new CustomTPE();
1225 <        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1226 <        assertTrue(tpe.terminatedCalled);
1227 <        joinPool(tpe);
1224 >        CustomTPE p = new CustomTPE();
1225 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1226 >        assertTrue(p.terminatedCalled);
1227 >        joinPool(p);
1228      }
1229  
1230      /**
1231       * beforeExecute and afterExecute are called when executing task
1232       */
1233      public void testBeforeAfter() throws InterruptedException {
1234 <        CustomTPE tpe = new CustomTPE();
1234 >        CustomTPE p = new CustomTPE();
1235          try {
1236              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1237 <            tpe.execute(r);
1238 <            Thread.sleep(SHORT_DELAY_MS);
1237 >            p.execute(r);
1238 >            delay(SHORT_DELAY_MS);
1239              assertTrue(r.done);
1240 <            assertTrue(tpe.beforeCalled);
1241 <            assertTrue(tpe.afterCalled);
1242 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1240 >            assertTrue(p.beforeCalled);
1241 >            assertTrue(p.afterCalled);
1242 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1243          } finally {
1244 <            joinPool(tpe);
1244 >            joinPool(p);
1245          }
1246      }
1247  
# Line 1135 | Line 1287 | public class ThreadPoolExecutorSubclassT
1287          }
1288      }
1289  
1138
1290      /**
1291       * invokeAny(null) throws NPE
1292       */
# Line 1297 | Line 1448 | public class ThreadPoolExecutorSubclassT
1448          }
1449      }
1450  
1300
1301
1451      /**
1452       * timed invokeAny(null) throws NPE
1453       */
# Line 1526 | Line 1675 | public class ThreadPoolExecutorSubclassT
1675       * thread factory fails to create more
1676       */
1677      public void testFailingThreadFactory() throws InterruptedException {
1678 <        ExecutorService e = new CustomTPE(100, 100, LONG_DELAY_MS, MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory());
1679 <        try {
1680 <            for (int k = 0; k < 100; ++k) {
1681 <                e.execute(new NoOpRunnable());
1682 <            }
1683 <            Thread.sleep(LONG_DELAY_MS);
1678 >        final ExecutorService e =
1679 >            new CustomTPE(100, 100,
1680 >                          LONG_DELAY_MS, MILLISECONDS,
1681 >                          new LinkedBlockingQueue<Runnable>(),
1682 >                          new FailingThreadFactory());
1683 >        try {
1684 >            final int TASKS = 100;
1685 >            final CountDownLatch done = new CountDownLatch(TASKS);
1686 >            for (int k = 0; k < TASKS; ++k)
1687 >                e.execute(new CheckedRunnable() {
1688 >                    public void realRun() {
1689 >                        done.countDown();
1690 >                    }});
1691 >            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1692          } finally {
1693              joinPool(e);
1694          }
# Line 1541 | Line 1698 | public class ThreadPoolExecutorSubclassT
1698       * allowsCoreThreadTimeOut is by default false.
1699       */
1700      public void testAllowsCoreThreadTimeOut() {
1701 <        ThreadPoolExecutor tpe = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1702 <        assertFalse(tpe.allowsCoreThreadTimeOut());
1703 <        joinPool(tpe);
1701 >        ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1702 >        assertFalse(p.allowsCoreThreadTimeOut());
1703 >        joinPool(p);
1704      }
1705  
1706      /**
1707       * allowCoreThreadTimeOut(true) causes idle threads to time out
1708       */
1709 <    public void testAllowCoreThreadTimeOut_true() throws InterruptedException {
1710 <        ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1711 <        tpe.allowCoreThreadTimeOut(true);
1712 <        tpe.execute(new NoOpRunnable());
1713 <        try {
1714 <            Thread.sleep(MEDIUM_DELAY_MS);
1715 <            assertEquals(0, tpe.getPoolSize());
1709 >    public void testAllowCoreThreadTimeOut_true() throws Exception {
1710 >        long coreThreadTimeOut = SHORT_DELAY_MS;
1711 >        final ThreadPoolExecutor p =
1712 >            new CustomTPE(2, 10,
1713 >                          coreThreadTimeOut, MILLISECONDS,
1714 >                          new ArrayBlockingQueue<Runnable>(10));
1715 >        final CountDownLatch threadStarted = new CountDownLatch(1);
1716 >        try {
1717 >            p.allowCoreThreadTimeOut(true);
1718 >            p.execute(new CheckedRunnable() {
1719 >                public void realRun() throws InterruptedException {
1720 >                    threadStarted.countDown();
1721 >                    assertEquals(1, p.getPoolSize());
1722 >                }});
1723 >            await(threadStarted);
1724 >            delay(coreThreadTimeOut);
1725 >            long startTime = System.nanoTime();
1726 >            while (p.getPoolSize() > 0
1727 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1728 >                Thread.yield();
1729 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1730 >            assertEquals(0, p.getPoolSize());
1731          } finally {
1732 <            joinPool(tpe);
1732 >            joinPool(p);
1733          }
1734      }
1735  
1736      /**
1737       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1738       */
1739 <    public void testAllowCoreThreadTimeOut_false() throws InterruptedException {
1740 <        ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1741 <        tpe.allowCoreThreadTimeOut(false);
1742 <        tpe.execute(new NoOpRunnable());
1743 <        try {
1744 <            Thread.sleep(MEDIUM_DELAY_MS);
1745 <            assertTrue(tpe.getPoolSize() >= 1);
1739 >    public void testAllowCoreThreadTimeOut_false() throws Exception {
1740 >        long coreThreadTimeOut = SHORT_DELAY_MS;
1741 >        final ThreadPoolExecutor p =
1742 >            new CustomTPE(2, 10,
1743 >                          coreThreadTimeOut, MILLISECONDS,
1744 >                          new ArrayBlockingQueue<Runnable>(10));
1745 >        final CountDownLatch threadStarted = new CountDownLatch(1);
1746 >        try {
1747 >            p.allowCoreThreadTimeOut(false);
1748 >            p.execute(new CheckedRunnable() {
1749 >                public void realRun() throws InterruptedException {
1750 >                    threadStarted.countDown();
1751 >                    assertTrue(p.getPoolSize() >= 1);
1752 >                }});
1753 >            delay(2 * coreThreadTimeOut);
1754 >            assertTrue(p.getPoolSize() >= 1);
1755          } finally {
1756 <            joinPool(tpe);
1756 >            joinPool(p);
1757          }
1758      }
1759  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines