--- jsr166/src/test/tck/ForkJoinTask8Test.java 2015/10/04 18:40:57 1.18 +++ jsr166/src/test/tck/ForkJoinTask8Test.java 2015/10/11 15:34:06 1.24 @@ -126,17 +126,17 @@ public class ForkJoinTask8Test extends J { Thread.currentThread().interrupt(); - long t0 = System.nanoTime(); + long startTime = System.nanoTime(); assertSame(expected, a.join()); - assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); + assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); Thread.interrupted(); } { Thread.currentThread().interrupt(); - long t0 = System.nanoTime(); + long startTime = System.nanoTime(); a.quietlyJoin(); // should be no-op - assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); + assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); Thread.interrupted(); } @@ -172,9 +172,9 @@ public class ForkJoinTask8Test extends J Thread.interrupted(); { - long t0 = System.nanoTime(); + long startTime = System.nanoTime(); a.quietlyJoin(); // should be no-op - assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); + assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); } try { @@ -198,9 +198,9 @@ public class ForkJoinTask8Test extends J abstract static class BinaryAsyncAction extends ForkJoinTask { - private BinaryAsyncAction parent; + private volatile BinaryAsyncAction parent; - private BinaryAsyncAction sibling; + private volatile BinaryAsyncAction sibling; protected BinaryAsyncAction() { setForkJoinTaskTag(INITIAL_STATE); @@ -243,6 +243,14 @@ public class ForkJoinTask8Test extends J super.completeExceptionally(ex); } + public boolean cancel(boolean mayInterruptIfRunning) { + if (super.cancel(mayInterruptIfRunning)) { + completeExceptionally(new FJException()); + return true; + } + return false; + } + public final void complete() { BinaryAsyncAction a = this; for (;;) { @@ -265,13 +273,12 @@ public class ForkJoinTask8Test extends J } public final void completeExceptionally(Throwable ex) { - BinaryAsyncAction a = this; - while (!a.isCompletedAbnormally()) { + for (BinaryAsyncAction a = this;;) { a.completeThisExceptionally(ex); BinaryAsyncAction s = a.sibling; - if (s != null) - s.cancel(false); - if (!a.onException() || (a = a.parent) == null) + if (s != null && !s.isDone()) + s.completeExceptionally(ex); + if ((a = a.parent) == null) break; } } @@ -318,6 +325,8 @@ public class ForkJoinTask8Test extends J catch (Throwable ex) { compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE); } + if (getForkJoinTaskTag() == EXCEPTION_STATE) + throw new FJException(); return false; } @@ -339,19 +348,26 @@ public class ForkJoinTask8Test extends J } public final boolean exec() { - FailingAsyncFib f = this; - int n = f.number; - if (n > 1) { - while (n > 1) { - FailingAsyncFib p = f; - FailingAsyncFib r = new FailingAsyncFib(n - 2); - f = new FailingAsyncFib(--n); - p.linkSubtasks(r, f); - r.fork(); + try { + FailingAsyncFib f = this; + int n = f.number; + if (n > 1) { + while (n > 1) { + FailingAsyncFib p = f; + FailingAsyncFib r = new FailingAsyncFib(n - 2); + f = new FailingAsyncFib(--n); + p.linkSubtasks(r, f); + r.fork(); + } + f.number = n; } - f.number = n; + f.complete(); + } + catch (Throwable ex) { + compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE); } - f.complete(); + if (getForkJoinTaskTag() == EXCEPTION_STATE) + throw new FJException(); return false; } @@ -1163,7 +1179,7 @@ public class ForkJoinTask8Test extends J final ForkJoinTask b = ForkJoinTask.adapt(awaiter(done)); final ForkJoinTask c = ForkJoinTask.adapt(awaiter(done)); final ForkJoinPool p = singletonPool(); - try (PoolCleaner cleaner = cleaner(p)) { + try (PoolCleaner cleaner = cleaner(p, done)) { Thread external = new Thread(new CheckedRunnable() { public void realRun() { p.execute(a); @@ -1185,7 +1201,6 @@ public class ForkJoinTask8Test extends J assertFalse(r.isDone()); }}; p.invoke(s); - done.countDown(); } }