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.21 by jsr166, Mon Oct 11 07:21:32 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
192       */
# Line 292 | Line 288 | public class ThreadPoolExecutorSubclassT
288              assertEquals(0, p.getCompletedTaskCount());
289              threadProceed.countDown();
290              threadDone.await();
291 <            Thread.sleep(SHORT_DELAY_MS);
291 >            delay(SHORT_DELAY_MS);
292              assertEquals(1, p.getCompletedTaskCount());
293          } finally {
294              joinPool(p);
# Line 317 | Line 313 | public class ThreadPoolExecutorSubclassT
313          joinPool(p);
314      }
315  
320
316      /**
317       * getThreadFactory returns factory in constructor if not set
318       */
# Line 339 | Line 334 | public class ThreadPoolExecutorSubclassT
334          joinPool(p);
335      }
336  
342
337      /**
338       * setThreadFactory(null) throws NPE
339       */
# Line 376 | Line 370 | public class ThreadPoolExecutorSubclassT
370          joinPool(p);
371      }
372  
379
373      /**
374       * setRejectedExecutionHandler(null) throws NPE
375       */
# Line 391 | Line 384 | public class ThreadPoolExecutorSubclassT
384          }
385      }
386  
394
387      /**
388       * getLargestPoolSize increases, but doesn't overestimate, when
389       * multiple threads active
# Line 486 | Line 478 | public class ThreadPoolExecutorSubclassT
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  
# Line 497 | Line 489 | public class ThreadPoolExecutorSubclassT
489          joinPool(p);
490      }
491  
500
492      /**
493       * isTerminated is false before termination, true after
494       */
# Line 512 | Line 503 | public class ThreadPoolExecutorSubclassT
503              assertFalse(p.isTerminating());
504              p.execute(new CheckedRunnable() {
505                  public void realRun() throws InterruptedException {
515                    threadStarted.countDown();
506                      assertFalse(p.isTerminating());
507 +                    threadStarted.countDown();
508                      done.await();
509                  }});
510              assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
# Line 541 | Line 532 | public class ThreadPoolExecutorSubclassT
532              assertFalse(p.isTerminating());
533              p.execute(new CheckedRunnable() {
534                  public void realRun() throws InterruptedException {
544                    threadStarted.countDown();
535                      assertFalse(p.isTerminating());
536 +                    threadStarted.countDown();
537                      done.await();
538                  }});
539              assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
# Line 671 | Line 662 | public class ThreadPoolExecutorSubclassT
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() {
667 >    public void testShutdownNow() {
668          ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
669          List l;
670          try {
# Line 691 | Line 682 | public class ThreadPoolExecutorSubclassT
682  
683      // Exception Tests
684  
694
685      /**
686       * Constructor throws if corePoolSize argument is less than zero
687       */
# Line 752 | Line 742 | public class ThreadPoolExecutorSubclassT
742          } catch (NullPointerException success) {}
743      }
744  
755
756
745      /**
746       * Constructor throws if corePoolSize argument is less than zero
747       */
# Line 825 | Line 813 | public class ThreadPoolExecutorSubclassT
813          } catch (NullPointerException success) {}
814      }
815  
828
816      /**
817       * Constructor throws if corePoolSize argument is less than zero
818       */
# Line 897 | Line 884 | public class ThreadPoolExecutorSubclassT
884          } catch (NullPointerException success) {}
885      }
886  
900
887      /**
888       * Constructor throws if corePoolSize argument is less than zero
889       */
# Line 983 | Line 969 | public class ThreadPoolExecutorSubclassT
969          } catch (NullPointerException success) {}
970      }
971  
986
972      /**
973       * execute throws RejectedExecutionException if saturated.
974       */
# Line 1133 | Line 1118 | public class ThreadPoolExecutorSubclassT
1118          }
1119      }
1120  
1136
1121      /**
1122       * execute using DiscardOldestPolicy drops task on shutdown
1123       */
# Line 1151 | Line 1135 | public class ThreadPoolExecutorSubclassT
1135          }
1136      }
1137  
1154
1138      /**
1139       * execute(null) throws NPE
1140       */
# Line 1216 | Line 1199 | public class ThreadPoolExecutorSubclassT
1199          joinPool(p);
1200      }
1201  
1219
1202      /**
1203       * setKeepAliveTime throws IllegalArgumentException
1204       * when given a negative value
# Line 1253 | Line 1235 | public class ThreadPoolExecutorSubclassT
1235          try {
1236              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1237              p.execute(r);
1238 <            Thread.sleep(SHORT_DELAY_MS);
1238 >            delay(SHORT_DELAY_MS);
1239              assertTrue(r.done);
1240              assertTrue(p.beforeCalled);
1241              assertTrue(p.afterCalled);
# Line 1305 | Line 1287 | public class ThreadPoolExecutorSubclassT
1287          }
1288      }
1289  
1308
1290      /**
1291       * invokeAny(null) throws NPE
1292       */
# Line 1467 | Line 1448 | public class ThreadPoolExecutorSubclassT
1448          }
1449      }
1450  
1470
1471
1451      /**
1452       * timed invokeAny(null) throws NPE
1453       */
# Line 1728 | Line 1707 | public class ThreadPoolExecutorSubclassT
1707       * allowCoreThreadTimeOut(true) causes idle threads to time out
1708       */
1709      public void testAllowCoreThreadTimeOut_true() throws Exception {
1710 +        long coreThreadTimeOut = SHORT_DELAY_MS;
1711          final ThreadPoolExecutor p =
1712              new CustomTPE(2, 10,
1713 <                          SHORT_DELAY_MS, MILLISECONDS,
1713 >                          coreThreadTimeOut, MILLISECONDS,
1714                            new ArrayBlockingQueue<Runnable>(10));
1715          final CountDownLatch threadStarted = new CountDownLatch(1);
1716          try {
# Line 1740 | Line 1720 | public class ThreadPoolExecutorSubclassT
1720                      threadStarted.countDown();
1721                      assertEquals(1, p.getPoolSize());
1722                  }});
1723 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
1724 <            for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) {
1725 <                if (p.getPoolSize() == 0)
1726 <                    break;
1727 <                Thread.sleep(10);
1728 <            }
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(p);
# Line 1756 | Line 1737 | public class ThreadPoolExecutorSubclassT
1737       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1738       */
1739      public void testAllowCoreThreadTimeOut_false() throws Exception {
1740 +        long coreThreadTimeOut = SHORT_DELAY_MS;
1741          final ThreadPoolExecutor p =
1742              new CustomTPE(2, 10,
1743 <                          SHORT_DELAY_MS, MILLISECONDS,
1743 >                          coreThreadTimeOut, MILLISECONDS,
1744                            new ArrayBlockingQueue<Runnable>(10));
1745          final CountDownLatch threadStarted = new CountDownLatch(1);
1746          try {
# Line 1768 | Line 1750 | public class ThreadPoolExecutorSubclassT
1750                      threadStarted.countDown();
1751                      assertTrue(p.getPoolSize() >= 1);
1752                  }});
1753 <            Thread.sleep(SMALL_DELAY_MS);
1753 >            delay(2 * coreThreadTimeOut);
1754              assertTrue(p.getPoolSize() >= 1);
1755          } finally {
1756              joinPool(p);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines