--- jsr166/src/test/tck/ThreadPoolExecutorTest.java 2010/10/11 07:21:32 1.37 +++ jsr166/src/test/tck/ThreadPoolExecutorTest.java 2011/05/27 19:35:24 1.44 @@ -1,7 +1,7 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ * Other contributors include Andrew Wright, Jeffrey Hayes, * Pat Fisher, Mike Judd. */ @@ -46,7 +46,6 @@ public class ThreadPoolExecutorTest exte } } - /** * execute successfully executes a runnable */ @@ -154,7 +153,7 @@ public class ThreadPoolExecutorTest exte assertEquals(0, p.getCompletedTaskCount()); threadProceed.countDown(); threadDone.await(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertEquals(1, p.getCompletedTaskCount()); } finally { joinPool(p); @@ -185,7 +184,6 @@ public class ThreadPoolExecutorTest exte joinPool(p); } - /** * getThreadFactory returns factory in constructor if not set */ @@ -215,7 +213,6 @@ public class ThreadPoolExecutorTest exte joinPool(p); } - /** * setThreadFactory(null) throws NPE */ @@ -262,7 +259,6 @@ public class ThreadPoolExecutorTest exte joinPool(p); } - /** * setRejectedExecutionHandler(null) throws NPE */ @@ -280,7 +276,6 @@ public class ThreadPoolExecutorTest exte } } - /** * getLargestPoolSize increases, but doesn't overestimate, when * multiple threads active @@ -378,7 +373,7 @@ public class ThreadPoolExecutorTest exte } /** - * isShutDown is false before shutdown, true after + * isShutdown is false before shutdown, true after */ public void testIsShutdown() { final ThreadPoolExecutor p = @@ -391,7 +386,6 @@ public class ThreadPoolExecutorTest exte joinPool(p); } - /** * isTerminated is false before termination, true after */ @@ -406,11 +400,12 @@ public class ThreadPoolExecutorTest exte try { p.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { - threadStarted.countDown(); assertFalse(p.isTerminated()); + threadStarted.countDown(); done.await(); }}); assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); + assertFalse(p.isTerminating()); done.countDown(); } finally { try { p.shutdown(); } catch (SecurityException ok) { return; } @@ -433,8 +428,8 @@ public class ThreadPoolExecutorTest exte assertFalse(p.isTerminating()); p.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { - threadStarted.countDown(); assertFalse(p.isTerminating()); + threadStarted.countDown(); done.await(); }}); assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); @@ -563,9 +558,9 @@ public class ThreadPoolExecutorTest exte } /** - * shutDownNow returns a list containing tasks that were not run + * shutdownNow returns a list containing tasks that were not run */ - public void testShutDownNow() { + public void testShutdownNow() { final ThreadPoolExecutor p = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, @@ -586,7 +581,6 @@ public class ThreadPoolExecutorTest exte // Exception Tests - /** * Constructor throws if corePoolSize argument is less than zero */ @@ -659,8 +653,6 @@ public class ThreadPoolExecutorTest exte } catch (NullPointerException success) {} } - - /** * Constructor throws if corePoolSize argument is less than zero */ @@ -752,7 +744,6 @@ public class ThreadPoolExecutorTest exte } catch (NullPointerException success) {} } - /** * Constructor throws if corePoolSize argument is less than zero */ @@ -844,7 +835,6 @@ public class ThreadPoolExecutorTest exte } catch (NullPointerException success) {} } - /** * Constructor throws if corePoolSize argument is less than zero */ @@ -1211,7 +1201,6 @@ public class ThreadPoolExecutorTest exte } } - /** * execute using DiscardOldestPolicy drops task on shutdown */ @@ -1233,7 +1222,6 @@ public class ThreadPoolExecutorTest exte } } - /** * execute(null) throws NPE */ @@ -1306,7 +1294,6 @@ public class ThreadPoolExecutorTest exte joinPool(p); } - /** * setKeepAliveTime throws IllegalArgumentException * when given a negative value @@ -1344,7 +1331,7 @@ public class ThreadPoolExecutorTest exte try { TrackedNoOpRunnable r = new TrackedNoOpRunnable(); p.execute(r); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertTrue(r.done); assertTrue(p.beforeCalled); assertTrue(p.afterCalled); @@ -1405,7 +1392,6 @@ public class ThreadPoolExecutorTest exte } } - /** * invokeAny(null) throws NPE */ @@ -1599,8 +1585,6 @@ public class ThreadPoolExecutorTest exte } } - - /** * timed invokeAny(null) throws NPE */ @@ -1902,24 +1886,26 @@ public class ThreadPoolExecutorTest exte * allowCoreThreadTimeOut(true) causes idle threads to time out */ public void testAllowCoreThreadTimeOut_true() throws Exception { + long coreThreadTimeOut = SHORT_DELAY_MS; final ThreadPoolExecutor p = new ThreadPoolExecutor(2, 10, - SHORT_DELAY_MS, MILLISECONDS, + coreThreadTimeOut, MILLISECONDS, new ArrayBlockingQueue(10)); final CountDownLatch threadStarted = new CountDownLatch(1); try { p.allowCoreThreadTimeOut(true); p.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { + public void realRun() { threadStarted.countDown(); assertEquals(1, p.getPoolSize()); }}); - assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); - for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) { - if (p.getPoolSize() == 0) - break; - Thread.sleep(10); - } + await(threadStarted); + delay(coreThreadTimeOut); + long startTime = System.nanoTime(); + while (p.getPoolSize() > 0 + && millisElapsedSince(startTime) < LONG_DELAY_MS) + Thread.yield(); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); assertEquals(0, p.getPoolSize()); } finally { joinPool(p); @@ -1930,9 +1916,10 @@ public class ThreadPoolExecutorTest exte * allowCoreThreadTimeOut(false) causes idle threads not to time out */ public void testAllowCoreThreadTimeOut_false() throws Exception { + long coreThreadTimeOut = SHORT_DELAY_MS; final ThreadPoolExecutor p = new ThreadPoolExecutor(2, 10, - SHORT_DELAY_MS, MILLISECONDS, + coreThreadTimeOut, MILLISECONDS, new ArrayBlockingQueue(10)); final CountDownLatch threadStarted = new CountDownLatch(1); try { @@ -1942,7 +1929,7 @@ public class ThreadPoolExecutorTest exte threadStarted.countDown(); assertTrue(p.getPoolSize() >= 1); }}); - Thread.sleep(SMALL_DELAY_MS); + delay(2 * coreThreadTimeOut); assertTrue(p.getPoolSize() >= 1); } finally { joinPool(p);