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

Comparing jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java (file contents):
Revision 1.20 by jsr166, Sat Oct 9 22:27:16 2010 UTC vs.
Revision 1.21 by jsr166, Mon Oct 11 07:21:32 2010 UTC

# Line 195 | Line 195 | public class ThreadPoolExecutorSubclassT
195       * execute successfully executes a runnable
196       */
197      public void testExecute() throws InterruptedException {
198 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
198 >        final ThreadPoolExecutor p =
199 >            new CustomTPE(1, 1,
200 >                          LONG_DELAY_MS, MILLISECONDS,
201 >                          new ArrayBlockingQueue<Runnable>(10));
202 >        final CountDownLatch done = new CountDownLatch(1);
203 >        final Runnable task = new CheckedRunnable() {
204 >            public void realRun() {
205 >                done.countDown();
206 >            }};
207          try {
208 <            p1.execute(new ShortRunnable());
209 <            Thread.sleep(SMALL_DELAY_MS);
208 >            p.execute(task);
209 >            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
210          } finally {
211 <            joinPool(p1);
211 >            joinPool(p);
212          }
213      }
214  
# Line 209 | Line 217 | public class ThreadPoolExecutorSubclassT
217       * thread becomes active
218       */
219      public void testGetActiveCount() throws InterruptedException {
220 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
221 <        assertEquals(0, p2.getActiveCount());
222 <        p2.execute(new MediumRunnable());
223 <        Thread.sleep(SHORT_DELAY_MS);
224 <        assertEquals(1, p2.getActiveCount());
225 <        joinPool(p2);
220 >        final ThreadPoolExecutor p =
221 >            new CustomTPE(2, 2,
222 >                          LONG_DELAY_MS, MILLISECONDS,
223 >                          new ArrayBlockingQueue<Runnable>(10));
224 >        final CountDownLatch threadStarted = new CountDownLatch(1);
225 >        final CountDownLatch done = new CountDownLatch(1);
226 >        try {
227 >            assertEquals(0, p.getActiveCount());
228 >            p.execute(new CheckedRunnable() {
229 >                public void realRun() throws InterruptedException {
230 >                    threadStarted.countDown();
231 >                    assertEquals(1, p.getActiveCount());
232 >                    done.await();
233 >                }});
234 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
235 >            assertEquals(1, p.getActiveCount());
236 >        } finally {
237 >            done.countDown();
238 >            joinPool(p);
239 >        }
240      }
241  
242      /**
243       * prestartCoreThread starts a thread if under corePoolSize, else doesn't
244       */
245      public void testPrestartCoreThread() {
246 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
247 <        assertEquals(0, p2.getPoolSize());
248 <        assertTrue(p2.prestartCoreThread());
249 <        assertEquals(1, p2.getPoolSize());
250 <        assertTrue(p2.prestartCoreThread());
251 <        assertEquals(2, p2.getPoolSize());
252 <        assertFalse(p2.prestartCoreThread());
253 <        assertEquals(2, p2.getPoolSize());
254 <        joinPool(p2);
246 >        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
247 >        assertEquals(0, p.getPoolSize());
248 >        assertTrue(p.prestartCoreThread());
249 >        assertEquals(1, p.getPoolSize());
250 >        assertTrue(p.prestartCoreThread());
251 >        assertEquals(2, p.getPoolSize());
252 >        assertFalse(p.prestartCoreThread());
253 >        assertEquals(2, p.getPoolSize());
254 >        joinPool(p);
255      }
256  
257      /**
258       * prestartAllCoreThreads starts all corePoolSize threads
259       */
260      public void testPrestartAllCoreThreads() {
261 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
262 <        assertEquals(0, p2.getPoolSize());
263 <        p2.prestartAllCoreThreads();
264 <        assertEquals(2, p2.getPoolSize());
265 <        p2.prestartAllCoreThreads();
266 <        assertEquals(2, p2.getPoolSize());
267 <        joinPool(p2);
261 >        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
262 >        assertEquals(0, p.getPoolSize());
263 >        p.prestartAllCoreThreads();
264 >        assertEquals(2, p.getPoolSize());
265 >        p.prestartAllCoreThreads();
266 >        assertEquals(2, p.getPoolSize());
267 >        joinPool(p);
268      }
269  
270      /**
# Line 250 | Line 272 | public class ThreadPoolExecutorSubclassT
272       * when tasks complete
273       */
274      public void testGetCompletedTaskCount() throws InterruptedException {
275 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
276 <        assertEquals(0, p2.getCompletedTaskCount());
277 <        p2.execute(new ShortRunnable());
278 <        Thread.sleep(SMALL_DELAY_MS);
279 <        assertEquals(1, p2.getCompletedTaskCount());
280 <        try { p2.shutdown(); } catch (SecurityException ok) { return; }
281 <        joinPool(p2);
275 >        final ThreadPoolExecutor p =
276 >            new CustomTPE(2, 2,
277 >                          LONG_DELAY_MS, MILLISECONDS,
278 >                          new ArrayBlockingQueue<Runnable>(10));
279 >        final CountDownLatch threadStarted = new CountDownLatch(1);
280 >        final CountDownLatch threadProceed = new CountDownLatch(1);
281 >        final CountDownLatch threadDone = new CountDownLatch(1);
282 >        try {
283 >            assertEquals(0, p.getCompletedTaskCount());
284 >            p.execute(new CheckedRunnable() {
285 >                public void realRun() throws InterruptedException {
286 >                    threadStarted.countDown();
287 >                    assertEquals(0, p.getCompletedTaskCount());
288 >                    threadProceed.await();
289 >                    threadDone.countDown();
290 >                }});
291 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
292 >            assertEquals(0, p.getCompletedTaskCount());
293 >            threadProceed.countDown();
294 >            threadDone.await();
295 >            Thread.sleep(SHORT_DELAY_MS);
296 >            assertEquals(1, p.getCompletedTaskCount());
297 >        } finally {
298 >            joinPool(p);
299 >        }
300      }
301  
302      /**
303       * getCorePoolSize returns size given in constructor if not otherwise set
304       */
305      public void testGetCorePoolSize() {
306 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
307 <        assertEquals(1, p1.getCorePoolSize());
308 <        joinPool(p1);
306 >        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
307 >        assertEquals(1, p.getCorePoolSize());
308 >        joinPool(p);
309      }
310  
311      /**
312       * getKeepAliveTime returns value given in constructor if not otherwise set
313       */
314      public void testGetKeepAliveTime() {
315 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
316 <        assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS));
317 <        joinPool(p2);
315 >        ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
316 >        assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS));
317 >        joinPool(p);
318      }
319  
320  
# Line 357 | Line 397 | public class ThreadPoolExecutorSubclassT
397       * multiple threads active
398       */
399      public void testGetLargestPoolSize() throws InterruptedException {
400 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
401 <        assertEquals(0, p2.getLargestPoolSize());
402 <        p2.execute(new MediumRunnable());
403 <        p2.execute(new MediumRunnable());
404 <        Thread.sleep(SHORT_DELAY_MS);
405 <        assertEquals(2, p2.getLargestPoolSize());
406 <        joinPool(p2);
400 >        final int THREADS = 3;
401 >        final ThreadPoolExecutor p =
402 >            new CustomTPE(THREADS, THREADS,
403 >                          LONG_DELAY_MS, MILLISECONDS,
404 >                          new ArrayBlockingQueue<Runnable>(10));
405 >        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
406 >        final CountDownLatch done = new CountDownLatch(1);
407 >        try {
408 >            assertEquals(0, p.getLargestPoolSize());
409 >            for (int i = 0; i < THREADS; i++)
410 >                p.execute(new CheckedRunnable() {
411 >                    public void realRun() throws InterruptedException {
412 >                        threadsStarted.countDown();
413 >                        done.await();
414 >                        assertEquals(THREADS, p.getLargestPoolSize());
415 >                    }});
416 >            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
417 >            assertEquals(THREADS, p.getLargestPoolSize());
418 >        } finally {
419 >            done.countDown();
420 >            joinPool(p);
421 >            assertEquals(THREADS, p.getLargestPoolSize());
422 >        }
423      }
424  
425      /**
# Line 371 | Line 427 | public class ThreadPoolExecutorSubclassT
427       * otherwise set
428       */
429      public void testGetMaximumPoolSize() {
430 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
431 <        assertEquals(2, p2.getMaximumPoolSize());
432 <        joinPool(p2);
430 >        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
431 >        assertEquals(2, p.getMaximumPoolSize());
432 >        joinPool(p);
433      }
434  
435      /**
436       * getPoolSize increases, but doesn't overestimate, when threads
437       * become active
438       */
439 <    public void testGetPoolSize() {
440 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
441 <        assertEquals(0, p1.getPoolSize());
442 <        p1.execute(new MediumRunnable());
443 <        assertEquals(1, p1.getPoolSize());
444 <        joinPool(p1);
439 >    public void testGetPoolSize() throws InterruptedException {
440 >        final ThreadPoolExecutor p =
441 >            new CustomTPE(1, 1,
442 >                          LONG_DELAY_MS, MILLISECONDS,
443 >                          new ArrayBlockingQueue<Runnable>(10));
444 >        final CountDownLatch threadStarted = new CountDownLatch(1);
445 >        final CountDownLatch done = new CountDownLatch(1);
446 >        try {
447 >            assertEquals(0, p.getPoolSize());
448 >            p.execute(new CheckedRunnable() {
449 >                public void realRun() throws InterruptedException {
450 >                    threadStarted.countDown();
451 >                    assertEquals(1, p.getPoolSize());
452 >                    done.await();
453 >                }});
454 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
455 >            assertEquals(1, p.getPoolSize());
456 >        } finally {
457 >            done.countDown();
458 >            joinPool(p);
459 >        }
460      }
461  
462      /**
463       * getTaskCount increases, but doesn't overestimate, when tasks submitted
464       */
465      public void testGetTaskCount() throws InterruptedException {
466 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
467 <        assertEquals(0, p1.getTaskCount());
468 <        p1.execute(new MediumRunnable());
469 <        Thread.sleep(SHORT_DELAY_MS);
470 <        assertEquals(1, p1.getTaskCount());
471 <        joinPool(p1);
466 >        final ThreadPoolExecutor p =
467 >            new CustomTPE(1, 1,
468 >                          LONG_DELAY_MS, MILLISECONDS,
469 >                          new ArrayBlockingQueue<Runnable>(10));
470 >        final CountDownLatch threadStarted = new CountDownLatch(1);
471 >        final CountDownLatch done = new CountDownLatch(1);
472 >        try {
473 >            assertEquals(0, p.getTaskCount());
474 >            p.execute(new CheckedRunnable() {
475 >                public void realRun() throws InterruptedException {
476 >                    threadStarted.countDown();
477 >                    assertEquals(1, p.getTaskCount());
478 >                    done.await();
479 >                }});
480 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
481 >            assertEquals(1, p.getTaskCount());
482 >        } finally {
483 >            done.countDown();
484 >            joinPool(p);
485 >        }
486      }
487  
488      /**
# Line 405 | Line 490 | public class ThreadPoolExecutorSubclassT
490       */
491      public void testIsShutdown() {
492  
493 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
494 <        assertFalse(p1.isShutdown());
495 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
496 <        assertTrue(p1.isShutdown());
497 <        joinPool(p1);
493 >        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
494 >        assertFalse(p.isShutdown());
495 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
496 >        assertTrue(p.isShutdown());
497 >        joinPool(p);
498      }
499  
500  
# Line 417 | Line 502 | public class ThreadPoolExecutorSubclassT
502       * isTerminated is false before termination, true after
503       */
504      public void testIsTerminated() throws InterruptedException {
505 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
506 <        assertFalse(p1.isTerminated());
507 <        try {
508 <            p1.execute(new MediumRunnable());
509 <        } finally {
510 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
511 <        }
512 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
513 <        assertTrue(p1.isTerminated());
505 >        final ThreadPoolExecutor p =
506 >            new CustomTPE(1, 1,
507 >                          LONG_DELAY_MS, MILLISECONDS,
508 >                          new ArrayBlockingQueue<Runnable>(10));
509 >        final CountDownLatch threadStarted = new CountDownLatch(1);
510 >        final CountDownLatch done = new CountDownLatch(1);
511 >        try {
512 >            assertFalse(p.isTerminating());
513 >            p.execute(new CheckedRunnable() {
514 >                public void realRun() throws InterruptedException {
515 >                    threadStarted.countDown();
516 >                    assertFalse(p.isTerminating());
517 >                    done.await();
518 >                }});
519 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
520 >            assertFalse(p.isTerminating());
521 >            done.countDown();
522 >        } finally {
523 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
524 >        }
525 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
526 >        assertTrue(p.isTerminated());
527 >        assertFalse(p.isTerminating());
528      }
529  
530      /**
531       * isTerminating is not true when running or when terminated
532       */
533      public void testIsTerminating() throws InterruptedException {
534 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
535 <        assertFalse(p1.isTerminating());
536 <        try {
537 <            p1.execute(new SmallRunnable());
538 <            assertFalse(p1.isTerminating());
539 <        } finally {
540 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
541 <        }
542 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
543 <        assertTrue(p1.isTerminated());
544 <        assertFalse(p1.isTerminating());
534 >        final ThreadPoolExecutor p =
535 >            new CustomTPE(1, 1,
536 >                          LONG_DELAY_MS, MILLISECONDS,
537 >                          new ArrayBlockingQueue<Runnable>(10));
538 >        final CountDownLatch threadStarted = new CountDownLatch(1);
539 >        final CountDownLatch done = new CountDownLatch(1);
540 >        try {
541 >            assertFalse(p.isTerminating());
542 >            p.execute(new CheckedRunnable() {
543 >                public void realRun() throws InterruptedException {
544 >                    threadStarted.countDown();
545 >                    assertFalse(p.isTerminating());
546 >                    done.await();
547 >                }});
548 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
549 >            assertFalse(p.isTerminating());
550 >            done.countDown();
551 >        } finally {
552 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
553 >        }
554 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
555 >        assertTrue(p.isTerminated());
556 >        assertFalse(p.isTerminating());
557      }
558  
559      /**
560       * getQueue returns the work queue, which contains queued tasks
561       */
562      public void testGetQueue() throws InterruptedException {
563 <        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
564 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, q);
565 <        FutureTask[] tasks = new FutureTask[5];
566 <        for (int i = 0; i < 5; i++) {
567 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
568 <            p1.execute(tasks[i]);
569 <        }
570 <        try {
571 <            Thread.sleep(SHORT_DELAY_MS);
572 <            BlockingQueue<Runnable> wq = p1.getQueue();
573 <            assertSame(q, wq);
574 <            assertFalse(wq.contains(tasks[0]));
575 <            assertTrue(wq.contains(tasks[4]));
576 <            for (int i = 1; i < 5; ++i)
577 <                tasks[i].cancel(true);
578 <            p1.shutdownNow();
563 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
564 >        final ThreadPoolExecutor p =
565 >            new CustomTPE(1, 1,
566 >                          LONG_DELAY_MS, MILLISECONDS,
567 >                          q);
568 >        final CountDownLatch threadStarted = new CountDownLatch(1);
569 >        final CountDownLatch done = new CountDownLatch(1);
570 >        try {
571 >            FutureTask[] tasks = new FutureTask[5];
572 >            for (int i = 0; i < tasks.length; i++) {
573 >                Callable task = new CheckedCallable<Boolean>() {
574 >                    public Boolean realCall() throws InterruptedException {
575 >                        threadStarted.countDown();
576 >                        assertSame(q, p.getQueue());
577 >                        done.await();
578 >                        return Boolean.TRUE;
579 >                    }};
580 >                tasks[i] = new FutureTask(task);
581 >                p.execute(tasks[i]);
582 >            }
583 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
584 >            assertSame(q, p.getQueue());
585 >            assertFalse(q.contains(tasks[0]));
586 >            assertTrue(q.contains(tasks[tasks.length - 1]));
587 >            assertEquals(tasks.length - 1, q.size());
588          } finally {
589 <            joinPool(p1);
589 >            done.countDown();
590 >            joinPool(p);
591          }
592      }
593  
# Line 475 | Line 596 | public class ThreadPoolExecutorSubclassT
596       */
597      public void testRemove() throws InterruptedException {
598          BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
599 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, q);
600 <        FutureTask[] tasks = new FutureTask[5];
601 <        for (int i = 0; i < 5; i++) {
602 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
603 <            p1.execute(tasks[i]);
604 <        }
605 <        try {
606 <            Thread.sleep(SHORT_DELAY_MS);
607 <            assertFalse(p1.remove(tasks[0]));
599 >        final ThreadPoolExecutor p =
600 >            new CustomTPE(1, 1,
601 >                          LONG_DELAY_MS, MILLISECONDS,
602 >                          q);
603 >        Runnable[] tasks = new Runnable[6];
604 >        final CountDownLatch threadStarted = new CountDownLatch(1);
605 >        final CountDownLatch done = new CountDownLatch(1);
606 >        try {
607 >            for (int i = 0; i < tasks.length; i++) {
608 >                tasks[i] = new CheckedRunnable() {
609 >                        public void realRun() throws InterruptedException {
610 >                            threadStarted.countDown();
611 >                            done.await();
612 >                        }};
613 >                p.execute(tasks[i]);
614 >            }
615 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
616 >            assertFalse(p.remove(tasks[0]));
617              assertTrue(q.contains(tasks[4]));
618              assertTrue(q.contains(tasks[3]));
619 <            assertTrue(p1.remove(tasks[4]));
620 <            assertFalse(p1.remove(tasks[4]));
619 >            assertTrue(p.remove(tasks[4]));
620 >            assertFalse(p.remove(tasks[4]));
621              assertFalse(q.contains(tasks[4]));
622              assertTrue(q.contains(tasks[3]));
623 <            assertTrue(p1.remove(tasks[3]));
623 >            assertTrue(p.remove(tasks[3]));
624              assertFalse(q.contains(tasks[3]));
625          } finally {
626 <            joinPool(p1);
626 >            done.countDown();
627 >            joinPool(p);
628          }
629      }
630  
631      /**
632       * purge removes cancelled tasks from the queue
633       */
634 <    public void testPurge() {
635 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
634 >    public void testPurge() throws InterruptedException {
635 >        final CountDownLatch threadStarted = new CountDownLatch(1);
636 >        final CountDownLatch done = new CountDownLatch(1);
637 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
638 >        final ThreadPoolExecutor p =
639 >            new CustomTPE(1, 1,
640 >                          LONG_DELAY_MS, MILLISECONDS,
641 >                          q);
642          FutureTask[] tasks = new FutureTask[5];
643 <        for (int i = 0; i < 5; i++) {
644 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
645 <            p1.execute(tasks[i]);
646 <        }
647 <        tasks[4].cancel(true);
648 <        tasks[3].cancel(true);
649 <        p1.purge();
650 <        long count = p1.getTaskCount();
651 <        assertTrue(count >= 2 && count < 5);
652 <        joinPool(p1);
643 >        try {
644 >            for (int i = 0; i < tasks.length; i++) {
645 >                Callable task = new CheckedCallable<Boolean>() {
646 >                    public Boolean realCall() throws InterruptedException {
647 >                        threadStarted.countDown();
648 >                        done.await();
649 >                        return Boolean.TRUE;
650 >                    }};
651 >                tasks[i] = new FutureTask(task);
652 >                p.execute(tasks[i]);
653 >            }
654 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
655 >            assertEquals(tasks.length, p.getTaskCount());
656 >            assertEquals(tasks.length - 1, q.size());
657 >            assertEquals(1L, p.getActiveCount());
658 >            assertEquals(0L, p.getCompletedTaskCount());
659 >            tasks[4].cancel(true);
660 >            tasks[3].cancel(false);
661 >            p.purge();
662 >            assertEquals(tasks.length - 3, q.size());
663 >            assertEquals(tasks.length - 2, p.getTaskCount());
664 >            p.purge();         // Nothing to do
665 >            assertEquals(tasks.length - 3, q.size());
666 >            assertEquals(tasks.length - 2, p.getTaskCount());
667 >        } finally {
668 >            done.countDown();
669 >            joinPool(p);
670 >        }
671      }
672  
673      /**
674       * shutDownNow returns a list containing tasks that were not run
675       */
676      public void testShutDownNow() {
677 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
677 >        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
678          List l;
679          try {
680              for (int i = 0; i < 5; i++)
681 <                p1.execute(new MediumPossiblyInterruptedRunnable());
681 >                p.execute(new MediumPossiblyInterruptedRunnable());
682          }
683          finally {
684              try {
685 <                l = p1.shutdownNow();
685 >                l = p.shutdownNow();
686              } catch (SecurityException ok) { return; }
687          }
688 <        assertTrue(p1.isShutdown());
688 >        assertTrue(p.isShutdown());
689          assertTrue(l.size() <= 4);
690      }
691  
# Line 819 | Line 974 | public class ThreadPoolExecutorSubclassT
974       */
975      public void testConstructorNullPointerException8() {
976          try {
977 <            ThreadFactory f = null;
978 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
977 >            new CustomTPE(1, 2,
978 >                          LONG_DELAY_MS, MILLISECONDS,
979 >                          new ArrayBlockingQueue<Runnable>(10),
980 >                          (ThreadFactory) null,
981 >                          new NoOpREHandler());
982              shouldThrow();
983          } catch (NullPointerException success) {}
984      }
# Line 830 | Line 988 | public class ThreadPoolExecutorSubclassT
988       * execute throws RejectedExecutionException if saturated.
989       */
990      public void testSaturatedExecute() {
991 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
992 <        try {
993 <
994 <            for (int i = 0; i < 5; ++i) {
995 <                p.execute(new MediumRunnable());
991 >        ThreadPoolExecutor p =
992 >            new CustomTPE(1, 1,
993 >                          LONG_DELAY_MS, MILLISECONDS,
994 >                          new ArrayBlockingQueue<Runnable>(1));
995 >        final CountDownLatch done = new CountDownLatch(1);
996 >        try {
997 >            Runnable task = new CheckedRunnable() {
998 >                public void realRun() throws InterruptedException {
999 >                    done.await();
1000 >                }};
1001 >            for (int i = 0; i < 2; ++i)
1002 >                p.execute(task);
1003 >            for (int i = 0; i < 2; ++i) {
1004 >                try {
1005 >                    p.execute(task);
1006 >                    shouldThrow();
1007 >                } catch (RejectedExecutionException success) {}
1008 >                assertTrue(p.getTaskCount() <= 2);
1009              }
1010 <            shouldThrow();
1011 <        } catch (RejectedExecutionException success) {}
1012 <        joinPool(p);
1010 >        } finally {
1011 >            done.countDown();
1012 >            joinPool(p);
1013 >        }
1014      }
1015  
1016      /**
# Line 846 | Line 1018 | public class ThreadPoolExecutorSubclassT
1018       */
1019      public void testSaturatedExecute2() {
1020          RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1021 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1021 >        ThreadPoolExecutor p = new CustomTPE(1, 1,
1022 >                                             LONG_DELAY_MS, MILLISECONDS,
1023 >                                             new ArrayBlockingQueue<Runnable>(1),
1024 >                                             h);
1025          try {
851
1026              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1027 <            for (int i = 0; i < 5; ++i) {
1027 >            for (int i = 0; i < tasks.length; ++i)
1028                  tasks[i] = new TrackedNoOpRunnable();
855            }
1029              TrackedLongRunnable mr = new TrackedLongRunnable();
1030              p.execute(mr);
1031 <            for (int i = 0; i < 5; ++i) {
1031 >            for (int i = 0; i < tasks.length; ++i)
1032                  p.execute(tasks[i]);
1033 <            }
861 <            for (int i = 1; i < 5; ++i) {
1033 >            for (int i = 1; i < tasks.length; ++i)
1034                  assertTrue(tasks[i].done);
863            }
1035              try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1036          } finally {
1037              joinPool(p);
# Line 872 | Line 1043 | public class ThreadPoolExecutorSubclassT
1043       */
1044      public void testSaturatedExecute3() {
1045          RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1046 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1046 >        ThreadPoolExecutor p =
1047 >            new CustomTPE(1, 1,
1048 >                          LONG_DELAY_MS, MILLISECONDS,
1049 >                          new ArrayBlockingQueue<Runnable>(1),
1050 >                          h);
1051          try {
877
1052              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1053 <            for (int i = 0; i < 5; ++i) {
1053 >            for (int i = 0; i < tasks.length; ++i)
1054                  tasks[i] = new TrackedNoOpRunnable();
881            }
1055              p.execute(new TrackedLongRunnable());
1056 <            for (int i = 0; i < 5; ++i) {
1057 <                p.execute(tasks[i]);
1058 <            }
1059 <            for (int i = 0; i < 5; ++i) {
887 <                assertFalse(tasks[i].done);
888 <            }
1056 >            for (TrackedNoOpRunnable task : tasks)
1057 >                p.execute(task);
1058 >            for (TrackedNoOpRunnable task : tasks)
1059 >                assertFalse(task.done);
1060              try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1061          } finally {
1062              joinPool(p);
# Line 917 | Line 1088 | public class ThreadPoolExecutorSubclassT
1088       * execute throws RejectedExecutionException if shutdown
1089       */
1090      public void testRejectedExecutionExceptionOnShutdown() {
1091 <        ThreadPoolExecutor tpe =
1091 >        ThreadPoolExecutor p =
1092              new CustomTPE(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
1093 <        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1093 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1094          try {
1095 <            tpe.execute(new NoOpRunnable());
1095 >            p.execute(new NoOpRunnable());
1096              shouldThrow();
1097          } catch (RejectedExecutionException success) {}
1098  
1099 <        joinPool(tpe);
1099 >        joinPool(p);
1100      }
1101  
1102      /**
# Line 985 | Line 1156 | public class ThreadPoolExecutorSubclassT
1156       * execute(null) throws NPE
1157       */
1158      public void testExecuteNull() {
1159 <        ThreadPoolExecutor tpe = null;
1159 >        ThreadPoolExecutor p = null;
1160          try {
1161 <            tpe = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1162 <            tpe.execute(null);
1161 >            p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1162 >            p.execute(null);
1163              shouldThrow();
1164          } catch (NullPointerException success) {}
1165  
1166 <        joinPool(tpe);
1166 >        joinPool(p);
1167      }
1168  
1169      /**
1170       * setCorePoolSize of negative value throws IllegalArgumentException
1171       */
1172      public void testCorePoolSizeIllegalArgumentException() {
1173 <        ThreadPoolExecutor tpe =
1173 >        ThreadPoolExecutor p =
1174              new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1175          try {
1176 <            tpe.setCorePoolSize(-1);
1176 >            p.setCorePoolSize(-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      /**
# Line 1016 | Line 1187 | public class ThreadPoolExecutorSubclassT
1187       * if given a value less the core pool size
1188       */
1189      public void testMaximumPoolSizeIllegalArgumentException() {
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  
1202      /**
# Line 1033 | Line 1204 | public class ThreadPoolExecutorSubclassT
1204       * if given a negative value
1205       */
1206      public void testMaximumPoolSizeIllegalArgumentException2() {
1207 <        ThreadPoolExecutor tpe =
1207 >        ThreadPoolExecutor p =
1208              new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1209          try {
1210 <            tpe.setMaximumPoolSize(-1);
1210 >            p.setMaximumPoolSize(-1);
1211              shouldThrow();
1212          } catch (IllegalArgumentException success) {
1213          } finally {
1214 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1214 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1215          }
1216 <        joinPool(tpe);
1216 >        joinPool(p);
1217      }
1218  
1219  
# Line 1051 | Line 1222 | public class ThreadPoolExecutorSubclassT
1222       * when given a negative value
1223       */
1224      public void testKeepAliveTimeIllegalArgumentException() {
1225 <        ThreadPoolExecutor tpe =
1225 >        ThreadPoolExecutor p =
1226              new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1227  
1228          try {
1229 <            tpe.setKeepAliveTime(-1,MILLISECONDS);
1229 >            p.setKeepAliveTime(-1,MILLISECONDS);
1230              shouldThrow();
1231          } catch (IllegalArgumentException success) {
1232          } finally {
1233 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1233 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1234          }
1235 <        joinPool(tpe);
1235 >        joinPool(p);
1236      }
1237  
1238      /**
1239       * terminated() is called on termination
1240       */
1241      public void testTerminated() {
1242 <        CustomTPE tpe = new CustomTPE();
1243 <        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1244 <        assertTrue(tpe.terminatedCalled);
1245 <        joinPool(tpe);
1242 >        CustomTPE p = new CustomTPE();
1243 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1244 >        assertTrue(p.terminatedCalled);
1245 >        joinPool(p);
1246      }
1247  
1248      /**
1249       * beforeExecute and afterExecute are called when executing task
1250       */
1251      public void testBeforeAfter() throws InterruptedException {
1252 <        CustomTPE tpe = new CustomTPE();
1252 >        CustomTPE p = new CustomTPE();
1253          try {
1254              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1255 <            tpe.execute(r);
1255 >            p.execute(r);
1256              Thread.sleep(SHORT_DELAY_MS);
1257              assertTrue(r.done);
1258 <            assertTrue(tpe.beforeCalled);
1259 <            assertTrue(tpe.afterCalled);
1260 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1258 >            assertTrue(p.beforeCalled);
1259 >            assertTrue(p.afterCalled);
1260 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1261          } finally {
1262 <            joinPool(tpe);
1262 >            joinPool(p);
1263          }
1264      }
1265  
# Line 1525 | Line 1696 | public class ThreadPoolExecutorSubclassT
1696       * thread factory fails to create more
1697       */
1698      public void testFailingThreadFactory() throws InterruptedException {
1699 <        ExecutorService e = new CustomTPE(100, 100, LONG_DELAY_MS, MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory());
1700 <        try {
1701 <            for (int k = 0; k < 100; ++k) {
1702 <                e.execute(new NoOpRunnable());
1703 <            }
1704 <            Thread.sleep(LONG_DELAY_MS);
1699 >        final ExecutorService e =
1700 >            new CustomTPE(100, 100,
1701 >                          LONG_DELAY_MS, MILLISECONDS,
1702 >                          new LinkedBlockingQueue<Runnable>(),
1703 >                          new FailingThreadFactory());
1704 >        try {
1705 >            final int TASKS = 100;
1706 >            final CountDownLatch done = new CountDownLatch(TASKS);
1707 >            for (int k = 0; k < TASKS; ++k)
1708 >                e.execute(new CheckedRunnable() {
1709 >                    public void realRun() {
1710 >                        done.countDown();
1711 >                    }});
1712 >            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1713          } finally {
1714              joinPool(e);
1715          }
# Line 1540 | Line 1719 | public class ThreadPoolExecutorSubclassT
1719       * allowsCoreThreadTimeOut is by default false.
1720       */
1721      public void testAllowsCoreThreadTimeOut() {
1722 <        ThreadPoolExecutor tpe = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1723 <        assertFalse(tpe.allowsCoreThreadTimeOut());
1724 <        joinPool(tpe);
1722 >        ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1723 >        assertFalse(p.allowsCoreThreadTimeOut());
1724 >        joinPool(p);
1725      }
1726  
1727      /**
1728       * allowCoreThreadTimeOut(true) causes idle threads to time out
1729       */
1730 <    public void testAllowCoreThreadTimeOut_true() throws InterruptedException {
1731 <        ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1732 <        tpe.allowCoreThreadTimeOut(true);
1733 <        tpe.execute(new NoOpRunnable());
1734 <        try {
1735 <            Thread.sleep(MEDIUM_DELAY_MS);
1736 <            assertEquals(0, tpe.getPoolSize());
1730 >    public void testAllowCoreThreadTimeOut_true() throws Exception {
1731 >        final ThreadPoolExecutor p =
1732 >            new CustomTPE(2, 10,
1733 >                          SHORT_DELAY_MS, MILLISECONDS,
1734 >                          new ArrayBlockingQueue<Runnable>(10));
1735 >        final CountDownLatch threadStarted = new CountDownLatch(1);
1736 >        try {
1737 >            p.allowCoreThreadTimeOut(true);
1738 >            p.execute(new CheckedRunnable() {
1739 >                public void realRun() throws InterruptedException {
1740 >                    threadStarted.countDown();
1741 >                    assertEquals(1, p.getPoolSize());
1742 >                }});
1743 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
1744 >            for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) {
1745 >                if (p.getPoolSize() == 0)
1746 >                    break;
1747 >                Thread.sleep(10);
1748 >            }
1749 >            assertEquals(0, p.getPoolSize());
1750          } finally {
1751 <            joinPool(tpe);
1751 >            joinPool(p);
1752          }
1753      }
1754  
1755      /**
1756       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1757       */
1758 <    public void testAllowCoreThreadTimeOut_false() throws InterruptedException {
1759 <        ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1760 <        tpe.allowCoreThreadTimeOut(false);
1761 <        tpe.execute(new NoOpRunnable());
1762 <        try {
1763 <            Thread.sleep(MEDIUM_DELAY_MS);
1764 <            assertTrue(tpe.getPoolSize() >= 1);
1758 >    public void testAllowCoreThreadTimeOut_false() throws Exception {
1759 >        final ThreadPoolExecutor p =
1760 >            new CustomTPE(2, 10,
1761 >                          SHORT_DELAY_MS, MILLISECONDS,
1762 >                          new ArrayBlockingQueue<Runnable>(10));
1763 >        final CountDownLatch threadStarted = new CountDownLatch(1);
1764 >        try {
1765 >            p.allowCoreThreadTimeOut(false);
1766 >            p.execute(new CheckedRunnable() {
1767 >                public void realRun() throws InterruptedException {
1768 >                    threadStarted.countDown();
1769 >                    assertTrue(p.getPoolSize() >= 1);
1770 >                }});
1771 >            Thread.sleep(SMALL_DELAY_MS);
1772 >            assertTrue(p.getPoolSize() >= 1);
1773          } finally {
1774 <            joinPool(tpe);
1774 >            joinPool(p);
1775          }
1776      }
1777  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines