--- jsr166/src/test/tck/ForkJoinPoolTest.java 2015/10/06 23:16:51 1.66 +++ jsr166/src/test/tck/ForkJoinPoolTest.java 2017/01/04 06:09:58 1.72 @@ -24,6 +24,7 @@ import java.util.concurrent.Future; import java.util.concurrent.RecursiveTask; import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.ReentrantLock; import junit.framework.AssertionFailedError; @@ -57,13 +58,6 @@ public class ForkJoinPoolTest extends JS // Some classes to test extension and factory methods - static class MyHandler implements Thread.UncaughtExceptionHandler { - volatile int catches = 0; - public void uncaughtException(Thread t, Throwable e) { - ++catches; - } - } - static class MyError extends Error {} // to test handlers @@ -74,9 +68,9 @@ public class ForkJoinPoolTest extends JS static class FailingThreadFactory implements ForkJoinPool.ForkJoinWorkerThreadFactory { - volatile int calls = 0; + final AtomicInteger calls = new AtomicInteger(0); public ForkJoinWorkerThread newThread(ForkJoinPool p) { - if (++calls > 1) return null; + if (calls.incrementAndGet() > 1) return null; return new FailingFJWSubclass(p); } } @@ -213,12 +207,26 @@ public class ForkJoinPoolTest extends JS * getPoolSize returns number of started workers. */ public void testGetPoolSize() { - ForkJoinPool p = new ForkJoinPool(1); + final CountDownLatch taskStarted = new CountDownLatch(1); + final CountDownLatch done = new CountDownLatch(1); + final ForkJoinPool p = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(p)) { assertEquals(0, p.getActiveThreadCount()); - Future future = p.submit(new StringTask()); + final Runnable task = new CheckedRunnable() { + public void realRun() throws InterruptedException { + taskStarted.countDown(); + assertEquals(1, p.getPoolSize()); + assertEquals(1, p.getActiveThreadCount()); + done.await(); + }}; + Future future = p.submit(task); + await(taskStarted); assertEquals(1, p.getPoolSize()); + assertEquals(1, p.getActiveThreadCount()); + done.countDown(); } + assertEquals(0, p.getPoolSize()); + assertEquals(0, p.getActiveThreadCount()); } /** @@ -305,15 +313,18 @@ public class ForkJoinPoolTest extends JS assertTrue(p.isQuiescent()); assertFalse(p.getAsyncMode()); - assertEquals(0, p.getActiveThreadCount()); assertEquals(0, p.getQueuedTaskCount()); assertEquals(0, p.getQueuedSubmissionCount()); assertFalse(p.hasQueuedSubmissions()); + while (p.getActiveThreadCount() != 0 + && millisElapsedSince(startTime) < LONG_DELAY_MS) + Thread.yield(); assertFalse(p.isShutdown()); assertFalse(p.isTerminating()); assertFalse(p.isTerminated()); assertTrue(f.isDone()); assertEquals(6765, (int) f.get()); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } } @@ -620,7 +631,7 @@ public class ForkJoinPoolTest extends JS public void testInvokeAny3() throws Throwable { ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(null); try { e.invokeAny(l); @@ -636,7 +647,7 @@ public class ForkJoinPoolTest extends JS CountDownLatch latch = new CountDownLatch(1); ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(latchAwaitingStringTask(latch)); l.add(null); try { @@ -653,7 +664,7 @@ public class ForkJoinPoolTest extends JS public void testInvokeAny5() throws Throwable { ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new NPETask()); try { e.invokeAny(l); @@ -670,7 +681,7 @@ public class ForkJoinPoolTest extends JS public void testInvokeAny6() throws Throwable { ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); l.add(new StringTask()); String result = e.invokeAny(l); @@ -709,7 +720,7 @@ public class ForkJoinPoolTest extends JS public void testInvokeAll3() throws InterruptedException { ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); l.add(null); try { @@ -726,7 +737,7 @@ public class ForkJoinPoolTest extends JS public void testInvokeAll4() throws Throwable { ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new NPETask()); List> futures = e.invokeAll(l); assertEquals(1, futures.size()); @@ -745,7 +756,7 @@ public class ForkJoinPoolTest extends JS public void testInvokeAll5() throws Throwable { ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); l.add(new StringTask()); List> futures = e.invokeAll(l); @@ -774,7 +785,7 @@ public class ForkJoinPoolTest extends JS public void testTimedInvokeAnyNullTimeUnit() throws Throwable { ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); try { e.invokeAny(l, MEDIUM_DELAY_MS, null); @@ -804,7 +815,7 @@ public class ForkJoinPoolTest extends JS CountDownLatch latch = new CountDownLatch(1); ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(latchAwaitingStringTask(latch)); l.add(null); try { @@ -821,14 +832,16 @@ public class ForkJoinPoolTest extends JS public void testTimedInvokeAny4() throws Throwable { ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + long startTime = System.nanoTime(); + List> l = new ArrayList<>(); l.add(new NPETask()); try { - e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); + e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (ExecutionException success) { assertTrue(success.getCause() instanceof NullPointerException); } + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } } @@ -838,11 +851,13 @@ public class ForkJoinPoolTest extends JS public void testTimedInvokeAny5() throws Throwable { ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + long startTime = System.nanoTime(); + List> l = new ArrayList<>(); l.add(new StringTask()); l.add(new StringTask()); - String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); + String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); assertSame(TEST_STRING, result); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } } @@ -865,7 +880,7 @@ public class ForkJoinPoolTest extends JS public void testTimedInvokeAllNullTimeUnit() throws Throwable { ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); try { e.invokeAll(l, MEDIUM_DELAY_MS, null); @@ -893,7 +908,7 @@ public class ForkJoinPoolTest extends JS public void testTimedInvokeAll3() throws InterruptedException { ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); l.add(null); try { @@ -909,7 +924,7 @@ public class ForkJoinPoolTest extends JS public void testTimedInvokeAll4() throws Throwable { ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new NPETask()); List> futures = e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); @@ -929,7 +944,7 @@ public class ForkJoinPoolTest extends JS public void testTimedInvokeAll5() throws Throwable { ForkJoinPool e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); l.add(new StringTask()); List> futures