--- jsr166/src/test/tck/ExecutorsTest.java 2011/04/14 22:55:08 1.38 +++ jsr166/src/test/tck/ExecutorsTest.java 2011/05/29 06:54:23 1.41 @@ -6,12 +6,10 @@ * Pat Fisher, Mike Judd. */ - import junit.framework.*; import java.util.*; import java.util.concurrent.*; import static java.util.concurrent.TimeUnit.MILLISECONDS; -import java.math.BigInteger; import java.security.*; public class ExecutorsTest extends JSR166TestCase { @@ -54,7 +52,6 @@ public class ExecutorsTest extends JSR16 } catch (NullPointerException success) {} } - /** * A new SingleThreadExecutor can execute runnables */ @@ -101,7 +98,6 @@ public class ExecutorsTest extends JSR16 } } - /** * A new newFixedThreadPool can execute runnables */ @@ -144,7 +140,6 @@ public class ExecutorsTest extends JSR16 } catch (IllegalArgumentException success) {} } - /** * An unconfigurable newFixedThreadPool can execute runnables */ @@ -176,7 +171,6 @@ public class ExecutorsTest extends JSR16 } catch (NullPointerException success) {} } - /** * a newSingleThreadScheduledExecutor successfully runs delayed task */ @@ -252,34 +246,33 @@ public class ExecutorsTest extends JSR16 * Future.get on submitted tasks will time out if they compute too long. */ public void testTimedCallable() throws Exception { + final ExecutorService[] executors = { + Executors.newSingleThreadExecutor(), + Executors.newCachedThreadPool(), + Executors.newFixedThreadPool(2), + Executors.newScheduledThreadPool(2), + }; + final Runnable sleeper = new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { - Thread.sleep(LONG_DELAY_MS); + delay(LONG_DELAY_MS); }}; - for (ExecutorService executor : - new ExecutorService[] { - Executors.newSingleThreadExecutor(), - Executors.newCachedThreadPool(), - Executors.newFixedThreadPool(2), - Executors.newScheduledThreadPool(2), - }) { - try { - Future future = executor.submit(sleeper); - try { - future.get(SHORT_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (TimeoutException success) { - } finally { - future.cancel(true); - } - } - finally { - joinPool(executor); - } + + List threads = new ArrayList(); + for (final ExecutorService executor : executors) { + threads.add(newStartedThread(new CheckedRunnable() { + public void realRun() { + long startTime = System.nanoTime(); + Future future = executor.submit(sleeper); + assertFutureTimesOut(future); + }})); } + for (Thread thread : threads) + awaitTermination(thread); + for (ExecutorService executor : executors) + joinPool(executor); } - /** * ThreadPoolExecutor using defaultThreadFactory has * specified group, priority, daemon status, and name @@ -313,7 +306,7 @@ public class ExecutorsTest extends JSR16 } try { - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); } finally { joinPool(e); } @@ -349,7 +342,7 @@ public class ExecutorsTest extends JSR16 ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory()); e.execute(r); e.shutdown(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); joinPool(e); }}; @@ -387,7 +380,6 @@ public class ExecutorsTest extends JSR16 } } - /** * Without class loader permissions, creating * privilegedCallableUsingCurrentClassLoader throws ACE @@ -543,7 +535,6 @@ public class ExecutorsTest extends JSR16 assertSame(one, c.call()); } - /** * callable(null Runnable) throws NPE */ @@ -584,5 +575,4 @@ public class ExecutorsTest extends JSR16 } catch (NullPointerException success) {} } - }