--- jsr166/src/test/tck/RecursiveActionTest.java 2010/09/16 00:52:49 1.18 +++ jsr166/src/test/tck/RecursiveActionTest.java 2011/06/27 02:47:32 1.37 @@ -1,16 +1,22 @@ /* * 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/ */ import junit.framework.*; import java.util.concurrent.CancellationException; +import java.util.concurrent.SynchronousQueue; import java.util.concurrent.ExecutionException; import java.util.concurrent.ForkJoinPool; +import java.util.concurrent.ForkJoinTask; import java.util.concurrent.ForkJoinWorkerThread; import java.util.concurrent.RecursiveAction; +import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import static java.util.concurrent.TimeUnit.SECONDS; +import java.util.Arrays; import java.util.HashSet; public class RecursiveActionTest extends JSR166TestCase { @@ -39,26 +45,127 @@ public class RecursiveActionTest extends private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { try { - assertFalse(a.isDone()); - assertFalse(a.isCompletedNormally()); - assertFalse(a.isCompletedAbnormally()); - assertFalse(a.isCancelled()); - assertNull(a.getException()); + checkNotDone(a); assertNull(pool.invoke(a)); - assertTrue(a.isDone()); - assertTrue(a.isCompletedNormally()); - assertFalse(a.isCompletedAbnormally()); - assertFalse(a.isCancelled()); - assertNull(a.getException()); + checkCompletedNormally(a); } finally { joinPool(pool); } } - static final class FJException extends RuntimeException { - FJException() { super(); } + void checkNotDone(RecursiveAction a) { + assertFalse(a.isDone()); + assertFalse(a.isCompletedNormally()); + assertFalse(a.isCompletedAbnormally()); + assertFalse(a.isCancelled()); + assertNull(a.getException()); + assertNull(a.getRawResult()); + + if (! ForkJoinTask.inForkJoinPool()) { + Thread.currentThread().interrupt(); + try { + a.get(); + shouldThrow(); + } catch (InterruptedException success) { + } catch (Throwable fail) { threadUnexpectedException(fail); } + + Thread.currentThread().interrupt(); + try { + a.get(5L, SECONDS); + shouldThrow(); + } catch (InterruptedException success) { + } catch (Throwable fail) { threadUnexpectedException(fail); } + } + + try { + a.get(0L, SECONDS); + shouldThrow(); + } catch (TimeoutException success) { + } catch (Throwable fail) { threadUnexpectedException(fail); } + } + + void checkCompletedNormally(RecursiveAction a) { + assertTrue(a.isDone()); + assertFalse(a.isCancelled()); + assertTrue(a.isCompletedNormally()); + assertFalse(a.isCompletedAbnormally()); + assertNull(a.getException()); + assertNull(a.getRawResult()); + assertNull(a.join()); + assertFalse(a.cancel(false)); + assertFalse(a.cancel(true)); + try { + assertNull(a.get()); + } catch (Throwable fail) { threadUnexpectedException(fail); } + try { + assertNull(a.get(5L, SECONDS)); + } catch (Throwable fail) { threadUnexpectedException(fail); } + } + + void checkCancelled(RecursiveAction a) { + assertTrue(a.isDone()); + assertTrue(a.isCancelled()); + assertFalse(a.isCompletedNormally()); + assertTrue(a.isCompletedAbnormally()); + assertTrue(a.getException() instanceof CancellationException); + assertNull(a.getRawResult()); + + try { + a.join(); + shouldThrow(); + } catch (CancellationException success) { + } catch (Throwable fail) { threadUnexpectedException(fail); } + + try { + a.get(); + shouldThrow(); + } catch (CancellationException success) { + } catch (Throwable fail) { threadUnexpectedException(fail); } + + try { + a.get(5L, SECONDS); + shouldThrow(); + } catch (CancellationException success) { + } catch (Throwable fail) { threadUnexpectedException(fail); } + } + + void checkCompletedAbnormally(RecursiveAction a, Throwable t) { + assertTrue(a.isDone()); + assertFalse(a.isCancelled()); + assertFalse(a.isCompletedNormally()); + assertTrue(a.isCompletedAbnormally()); + assertSame(t.getClass(), a.getException().getClass()); + assertNull(a.getRawResult()); + assertFalse(a.cancel(false)); + assertFalse(a.cancel(true)); + + try { + a.join(); + shouldThrow(); + } catch (Throwable expected) { + assertSame(expected.getClass(), t.getClass()); + } + + try { + a.get(); + shouldThrow(); + } catch (ExecutionException success) { + assertSame(t.getClass(), success.getCause().getClass()); + } catch (Throwable fail) { threadUnexpectedException(fail); } + + try { + a.get(5L, SECONDS); + shouldThrow(); + } catch (ExecutionException success) { + assertSame(t.getClass(), success.getCause().getClass()); + } catch (Throwable fail) { threadUnexpectedException(fail); } + } + + public static final class FJException extends RuntimeException { + public FJException() { super(); } + public FJException(Throwable cause) { super(cause); } } // A simple recursive action for testing @@ -108,10 +215,7 @@ public class RecursiveActionTest extends FibAction f = new FibAction(8); assertNull(f.invoke()); assertEquals(21, f.result); - assertTrue(f.isDone()); - assertFalse(f.isCancelled()); - assertFalse(f.isCompletedAbnormally()); - assertNull(f.getRawResult()); + checkCompletedNormally(f); }}; testInvokeOnPool(mainPool(), a); } @@ -127,10 +231,7 @@ public class RecursiveActionTest extends FibAction f = new FibAction(8); f.quietlyInvoke(); assertEquals(21, f.result); - assertTrue(f.isDone()); - assertFalse(f.isCancelled()); - assertFalse(f.isCompletedAbnormally()); - assertNull(f.getRawResult()); + checkCompletedNormally(f); }}; testInvokeOnPool(mainPool(), a); } @@ -145,10 +246,189 @@ public class RecursiveActionTest extends assertSame(f, f.fork()); assertNull(f.join()); assertEquals(21, f.result); - assertTrue(f.isDone()); - assertNull(f.getRawResult()); + checkCompletedNormally(f); + }}; + testInvokeOnPool(mainPool(), a); + } + + /** + * join/quietlyJoin of a forked task succeeds in the presence of interrupts + */ + public void testJoinIgnoresInterrupts() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + FibAction f = new FibAction(8); + final Thread myself = Thread.currentThread(); + + // test join() + assertSame(f, f.fork()); + myself.interrupt(); + assertTrue(myself.isInterrupted()); + assertNull(f.join()); + Thread.interrupted(); + assertEquals(21, f.result); + checkCompletedNormally(f); + + f = new FibAction(8); + f.cancel(true); + assertSame(f, f.fork()); + myself.interrupt(); + assertTrue(myself.isInterrupted()); + try { + f.join(); + shouldThrow(); + } catch (CancellationException success) { + Thread.interrupted(); + checkCancelled(f); + } + + f = new FibAction(8); + f.completeExceptionally(new FJException()); + assertSame(f, f.fork()); + myself.interrupt(); + assertTrue(myself.isInterrupted()); + try { + f.join(); + shouldThrow(); + } catch (FJException success) { + Thread.interrupted(); + checkCompletedAbnormally(f, success); + } + + // test quietlyJoin() + f = new FibAction(8); + assertSame(f, f.fork()); + myself.interrupt(); + assertTrue(myself.isInterrupted()); + f.quietlyJoin(); + Thread.interrupted(); + assertEquals(21, f.result); + checkCompletedNormally(f); + + f = new FibAction(8); + f.cancel(true); + assertSame(f, f.fork()); + myself.interrupt(); + assertTrue(myself.isInterrupted()); + f.quietlyJoin(); + Thread.interrupted(); + checkCancelled(f); + + f = new FibAction(8); + f.completeExceptionally(new FJException()); + assertSame(f, f.fork()); + myself.interrupt(); + assertTrue(myself.isInterrupted()); + f.quietlyJoin(); + Thread.interrupted(); + checkCompletedAbnormally(f, f.getException()); + }}; + testInvokeOnPool(mainPool(), a); + a.reinitialize(); + testInvokeOnPool(singletonPool(), a); + } + + /** + * join/quietlyJoin of a forked task when not in ForkJoinPool + * succeeds in the presence of interrupts + */ + public void testJoinIgnoresInterruptsOutsideForkJoinPool() { + final SynchronousQueue sq = + new SynchronousQueue(); + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() throws InterruptedException { + FibAction[] fibActions = new FibAction[6]; + for (int i = 0; i < fibActions.length; i++) + fibActions[i] = new FibAction(8); + + fibActions[1].cancel(false); + fibActions[2].completeExceptionally(new FJException()); + fibActions[4].cancel(true); + fibActions[5].completeExceptionally(new FJException()); + + for (int i = 0; i < fibActions.length; i++) + fibActions[i].fork(); + + sq.put(fibActions); + + helpQuiesce(); + }}; + + Runnable r = new CheckedRunnable() { + public void realRun() throws InterruptedException { + FibAction[] fibActions = sq.take(); + FibAction f; + final Thread myself = Thread.currentThread(); + + // test join() ------------ + + f = fibActions[0]; + assertFalse(ForkJoinTask.inForkJoinPool()); + myself.interrupt(); + assertTrue(myself.isInterrupted()); + assertNull(f.join()); + assertTrue(Thread.interrupted()); + assertEquals(21, f.result); + checkCompletedNormally(f); + + f = fibActions[1]; + myself.interrupt(); + assertTrue(myself.isInterrupted()); + try { + f.join(); + shouldThrow(); + } catch (CancellationException success) { + assertTrue(Thread.interrupted()); + checkCancelled(f); + } + + f = fibActions[2]; + myself.interrupt(); + assertTrue(myself.isInterrupted()); + try { + f.join(); + shouldThrow(); + } catch (FJException success) { + assertTrue(Thread.interrupted()); + checkCompletedAbnormally(f, success); + } + + // test quietlyJoin() --------- + + f = fibActions[3]; + myself.interrupt(); + assertTrue(myself.isInterrupted()); + f.quietlyJoin(); + assertTrue(Thread.interrupted()); + assertEquals(21, f.result); + checkCompletedNormally(f); + + f = fibActions[4]; + myself.interrupt(); + assertTrue(myself.isInterrupted()); + f.quietlyJoin(); + assertTrue(Thread.interrupted()); + checkCancelled(f); + + f = fibActions[5]; + myself.interrupt(); + assertTrue(myself.isInterrupted()); + f.quietlyJoin(); + assertTrue(Thread.interrupted()); + assertTrue(f.getException() instanceof FJException); + checkCompletedAbnormally(f, f.getException()); }}; + + Thread t; + + t = newStartedThread(r); testInvokeOnPool(mainPool(), a); + awaitTermination(t, LONG_DELAY_MS); + + a.reinitialize(); + t = newStartedThread(r); + testInvokeOnPool(singletonPool(), a); + awaitTermination(t, LONG_DELAY_MS); } /** @@ -161,7 +441,7 @@ public class RecursiveActionTest extends assertSame(f, f.fork()); assertNull(f.get()); assertEquals(21, f.result); - assertTrue(f.isDone()); + checkCompletedNormally(f); }}; testInvokeOnPool(mainPool(), a); } @@ -174,9 +454,9 @@ public class RecursiveActionTest extends public void realCompute() throws Exception { FibAction f = new FibAction(8); assertSame(f, f.fork()); - assertNull(f.get(5L, TimeUnit.SECONDS)); + assertNull(f.get(5L, SECONDS)); assertEquals(21, f.result); - assertTrue(f.isDone()); + checkCompletedNormally(f); }}; testInvokeOnPool(mainPool(), a); } @@ -207,12 +487,11 @@ public class RecursiveActionTest extends assertSame(f, f.fork()); f.quietlyJoin(); assertEquals(21, f.result); - assertTrue(f.isDone()); + checkCompletedNormally(f); }}; testInvokeOnPool(mainPool(), a); } - /** * helpQuiesce returns when tasks are complete. * getQueuedTaskCount returns 0 when quiescent @@ -222,15 +501,14 @@ public class RecursiveActionTest extends public void realCompute() { FibAction f = new FibAction(8); assertSame(f, f.fork()); - f.helpQuiesce(); + helpQuiesce(); assertEquals(21, f.result); - assertTrue(f.isDone()); assertEquals(0, getQueuedTaskCount()); + checkCompletedNormally(f); }}; testInvokeOnPool(mainPool(), a); } - /** * invoke task throws exception when task completes abnormally */ @@ -241,7 +519,9 @@ public class RecursiveActionTest extends try { f.invoke(); shouldThrow(); - } catch (FJException success) {} + } catch (FJException success) { + checkCompletedAbnormally(f, success); + } }}; testInvokeOnPool(mainPool(), a); } @@ -254,7 +534,8 @@ public class RecursiveActionTest extends public void realCompute() { FailingFibAction f = new FailingFibAction(8); f.quietlyInvoke(); - assertTrue(f.isDone()); + assertTrue(f.getException() instanceof FJException); + checkCompletedAbnormally(f, f.getException()); }}; testInvokeOnPool(mainPool(), a); } @@ -270,7 +551,9 @@ public class RecursiveActionTest extends try { f.join(); shouldThrow(); - } catch (FJException success) {} + } catch (FJException success) { + checkCompletedAbnormally(f, success); + } }}; testInvokeOnPool(mainPool(), a); } @@ -286,7 +569,11 @@ public class RecursiveActionTest extends try { f.get(); shouldThrow(); - } catch (ExecutionException success) {} + } catch (ExecutionException success) { + Throwable cause = success.getCause(); + assertTrue(cause instanceof FJException); + checkCompletedAbnormally(f, cause); + } }}; testInvokeOnPool(mainPool(), a); } @@ -302,7 +589,11 @@ public class RecursiveActionTest extends try { f.get(5L, TimeUnit.SECONDS); shouldThrow(); - } catch (ExecutionException success) {} + } catch (ExecutionException success) { + Throwable cause = success.getCause(); + assertTrue(cause instanceof FJException); + checkCompletedAbnormally(f, cause); + } }}; testInvokeOnPool(mainPool(), a); } @@ -316,9 +607,8 @@ public class RecursiveActionTest extends FailingFibAction f = new FailingFibAction(8); assertSame(f, f.fork()); f.quietlyJoin(); - assertTrue(f.isDone()); - assertTrue(f.isCompletedAbnormally()); assertTrue(f.getException() instanceof FJException); + checkCompletedAbnormally(f, f.getException()); }}; testInvokeOnPool(mainPool(), a); } @@ -334,7 +624,9 @@ public class RecursiveActionTest extends try { f.invoke(); shouldThrow(); - } catch (CancellationException success) {} + } catch (CancellationException success) { + checkCancelled(f); + } }}; testInvokeOnPool(mainPool(), a); } @@ -351,7 +643,9 @@ public class RecursiveActionTest extends try { f.join(); shouldThrow(); - } catch (CancellationException success) {} + } catch (CancellationException success) { + checkCancelled(f); + } }}; testInvokeOnPool(mainPool(), a); } @@ -368,7 +662,9 @@ public class RecursiveActionTest extends try { f.get(); shouldThrow(); - } catch (CancellationException success) {} + } catch (CancellationException success) { + checkCancelled(f); + } }}; testInvokeOnPool(mainPool(), a); } @@ -383,9 +679,11 @@ public class RecursiveActionTest extends assertTrue(f.cancel(true)); assertSame(f, f.fork()); try { - f.get(5L, TimeUnit.SECONDS); + f.get(5L, SECONDS); shouldThrow(); - } catch (CancellationException success) {} + } catch (CancellationException success) { + checkCancelled(f); + } }}; testInvokeOnPool(mainPool(), a); } @@ -400,10 +698,7 @@ public class RecursiveActionTest extends assertTrue(f.cancel(true)); assertSame(f, f.fork()); f.quietlyJoin(); - assertTrue(f.isDone()); - assertTrue(f.isCompletedAbnormally()); - assertTrue(f.isCancelled()); - assertTrue(f.getException() instanceof CancellationException); + checkCancelled(f); }}; testInvokeOnPool(mainPool(), a); } @@ -475,15 +770,14 @@ public class RecursiveActionTest extends RecursiveAction a = new CheckedRecursiveAction() { public void realCompute() { ForkJoinWorkerThread w = - (ForkJoinWorkerThread)(Thread.currentThread()); - int idx = w.getPoolIndex(); - assertTrue(idx >= 0); - assertTrue(idx < mainPool.getPoolSize()); + (ForkJoinWorkerThread) Thread.currentThread(); + assertTrue(w.getPoolIndex() >= 0); + // pool size can shrink after assigning index, so cannot check + // assertTrue(w.getPoolIndex() < mainPool.getPoolSize()); }}; testInvokeOnPool(mainPool, a); } - /** * setRawResult(null) succeeds */ @@ -491,25 +785,50 @@ public class RecursiveActionTest extends RecursiveAction a = new CheckedRecursiveAction() { public void realCompute() { setRawResult(null); + assertNull(getRawResult()); }}; assertNull(a.invoke()); } /** - * A reinitialized task may be re-invoked + * A reinitialized normally completed task may be re-invoked */ public void testReinitialize() { RecursiveAction a = new CheckedRecursiveAction() { public void realCompute() { FibAction f = new FibAction(8); - assertNull(f.invoke()); - assertEquals(21, f.result); - assertTrue(f.isDone()); - assertFalse(f.isCancelled()); - assertFalse(f.isCompletedAbnormally()); - f.reinitialize(); - assertNull(f.invoke()); - assertEquals(21, f.result); + checkNotDone(f); + + for (int i = 0; i < 3; i++) { + assertNull(f.invoke()); + assertEquals(21, f.result); + checkCompletedNormally(f); + f.reinitialize(); + checkNotDone(f); + } + }}; + testInvokeOnPool(mainPool(), a); + } + + /** + * A reinitialized abnormally completed task may be re-invoked + */ + public void testReinitializeAbnormal() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + FailingFibAction f = new FailingFibAction(8); + checkNotDone(f); + + for (int i = 0; i < 3; i++) { + try { + f.invoke(); + shouldThrow(); + } catch (FJException success) { + checkCompletedAbnormally(f, success); + } + f.reinitialize(); + checkNotDone(f); + } }}; testInvokeOnPool(mainPool(), a); } @@ -525,7 +844,9 @@ public class RecursiveActionTest extends try { f.invoke(); shouldThrow(); - } catch (FJException success) {} + } catch (FJException success) { + checkCompletedAbnormally(f, success); + } }}; testInvokeOnPool(mainPool(), a); } @@ -539,8 +860,8 @@ public class RecursiveActionTest extends FibAction f = new FibAction(8); f.complete(null); assertNull(f.invoke()); - assertTrue(f.isDone()); assertEquals(0, f.result); + checkCompletedNormally(f); }}; testInvokeOnPool(mainPool(), a); } @@ -554,9 +875,9 @@ public class RecursiveActionTest extends FibAction f = new FibAction(8); FibAction g = new FibAction(9); invokeAll(f, g); - assertTrue(f.isDone()); + checkCompletedNormally(f); assertEquals(21, f.result); - assertTrue(g.isDone()); + checkCompletedNormally(g); assertEquals(34, g.result); }}; testInvokeOnPool(mainPool(), a); @@ -570,7 +891,7 @@ public class RecursiveActionTest extends public void realCompute() { FibAction f = new FibAction(8); invokeAll(f); - assertTrue(f.isDone()); + checkCompletedNormally(f); assertEquals(21, f.result); }}; testInvokeOnPool(mainPool(), a); @@ -587,10 +908,13 @@ public class RecursiveActionTest extends FibAction h = new FibAction(7); invokeAll(f, g, h); assertTrue(f.isDone()); - assertEquals(21, f.result); assertTrue(g.isDone()); - assertEquals(34, g.result); assertTrue(h.isDone()); + checkCompletedNormally(f); + assertEquals(21, f.result); + checkCompletedNormally(g); + assertEquals(34, g.result); + checkCompletedNormally(g); assertEquals(13, h.result); }}; testInvokeOnPool(mainPool(), a); @@ -611,16 +935,18 @@ public class RecursiveActionTest extends set.add(h); invokeAll(set); assertTrue(f.isDone()); - assertEquals(21, f.result); assertTrue(g.isDone()); - assertEquals(34, g.result); assertTrue(h.isDone()); + checkCompletedNormally(f); + assertEquals(21, f.result); + checkCompletedNormally(g); + assertEquals(34, g.result); + checkCompletedNormally(g); assertEquals(13, h.result); }}; testInvokeOnPool(mainPool(), a); } - /** * invokeAll(tasks) with any null task throws NPE */ @@ -649,7 +975,9 @@ public class RecursiveActionTest extends try { invokeAll(f, g); shouldThrow(); - } catch (FJException success) {} + } catch (FJException success) { + checkCompletedAbnormally(g, success); + } }}; testInvokeOnPool(mainPool(), a); } @@ -664,7 +992,9 @@ public class RecursiveActionTest extends try { invokeAll(g); shouldThrow(); - } catch (FJException success) {} + } catch (FJException success) { + checkCompletedAbnormally(g, success); + } }}; testInvokeOnPool(mainPool(), a); } @@ -681,7 +1011,9 @@ public class RecursiveActionTest extends try { invokeAll(f, g, h); shouldThrow(); - } catch (FJException success) {} + } catch (FJException success) { + checkCompletedAbnormally(g, success); + } }}; testInvokeOnPool(mainPool(), a); } @@ -702,7 +1034,9 @@ public class RecursiveActionTest extends try { invokeAll(set); shouldThrow(); - } catch (FJException success) {} + } catch (FJException success) { + checkCompletedAbnormally(f, success); + } }}; testInvokeOnPool(mainPool(), a); } @@ -720,8 +1054,8 @@ public class RecursiveActionTest extends assertSame(f, f.fork()); assertTrue(f.tryUnfork()); helpQuiesce(); - assertFalse(f.isDone()); - assertTrue(g.isDone()); + checkNotDone(f); + checkCompletedNormally(g); }}; testInvokeOnPool(singletonPool(), a); } @@ -741,6 +1075,10 @@ public class RecursiveActionTest extends assertSame(f, f.fork()); assertTrue(getSurplusQueuedTaskCount() > 0); helpQuiesce(); + assertEquals(0, getSurplusQueuedTaskCount()); + checkCompletedNormally(f); + checkCompletedNormally(g); + checkCompletedNormally(h); }}; testInvokeOnPool(singletonPool(), a); } @@ -757,8 +1095,10 @@ public class RecursiveActionTest extends assertSame(f, f.fork()); assertSame(f, peekNextLocalTask()); assertNull(f.join()); - assertTrue(f.isDone()); + checkCompletedNormally(f); helpQuiesce(); + checkCompletedNormally(f); + checkCompletedNormally(g); }}; testInvokeOnPool(singletonPool(), a); } @@ -776,14 +1116,14 @@ public class RecursiveActionTest extends assertSame(f, f.fork()); assertSame(f, pollNextLocalTask()); helpQuiesce(); - assertFalse(f.isDone()); + checkNotDone(f); + checkCompletedNormally(g); }}; testInvokeOnPool(singletonPool(), a); } /** - * pollTask returns an unexecuted task - * without executing it + * pollTask returns an unexecuted task without executing it */ public void testPollTask() { RecursiveAction a = new CheckedRecursiveAction() { @@ -794,8 +1134,8 @@ public class RecursiveActionTest extends assertSame(f, f.fork()); assertSame(f, pollTask()); helpQuiesce(); - assertFalse(f.isDone()); - assertTrue(g.isDone()); + checkNotDone(f); + checkCompletedNormally(g); }}; testInvokeOnPool(singletonPool(), a); } @@ -813,14 +1153,15 @@ public class RecursiveActionTest extends assertSame(g, peekNextLocalTask()); assertNull(f.join()); helpQuiesce(); - assertTrue(f.isDone()); + checkCompletedNormally(f); + checkCompletedNormally(g); }}; testInvokeOnPool(asyncSingletonPool(), a); } /** - * pollNextLocalTask returns least recent unexecuted task - * without executing it, in async mode + * pollNextLocalTask returns least recent unexecuted task without + * executing it, in async mode */ public void testPollNextLocalTaskAsync() { RecursiveAction a = new CheckedRecursiveAction() { @@ -831,15 +1172,15 @@ public class RecursiveActionTest extends assertSame(f, f.fork()); assertSame(g, pollNextLocalTask()); helpQuiesce(); - assertTrue(f.isDone()); - assertFalse(g.isDone()); + checkCompletedNormally(f); + checkNotDone(g); }}; testInvokeOnPool(asyncSingletonPool(), a); } /** - * pollTask returns an unexecuted task - * without executing it, in async mode + * pollTask returns an unexecuted task without executing it, in + * async mode */ public void testPollTaskAsync() { RecursiveAction a = new CheckedRecursiveAction() { @@ -850,10 +1191,53 @@ public class RecursiveActionTest extends assertSame(f, f.fork()); assertSame(g, pollTask()); helpQuiesce(); - assertTrue(f.isDone()); - assertFalse(g.isDone()); + checkCompletedNormally(f); + checkNotDone(g); }}; testInvokeOnPool(asyncSingletonPool(), a); } + /** Demo from RecursiveAction javadoc */ + static class SortTask extends RecursiveAction { + final long[] array; final int lo, hi; + SortTask(long[] array, int lo, int hi) { + this.array = array; this.lo = lo; this.hi = hi; + } + SortTask(long[] array) { this(array, 0, array.length); } + protected void compute() { + if (hi - lo < THRESHOLD) + sortSequentially(lo, hi); + else { + int mid = (lo + hi) >>> 1; + invokeAll(new SortTask(array, lo, mid), + new SortTask(array, mid, hi)); + merge(lo, mid, hi); + } + } + // implementation details follow: + final static int THRESHOLD = 100; + void sortSequentially(int lo, int hi) { + Arrays.sort(array, lo, hi); + } + void merge(int lo, int mid, int hi) { + long[] buf = Arrays.copyOfRange(array, lo, mid); + for (int i = 0, j = lo, k = mid; i < buf.length; j++) + array[j] = (k == hi || buf[i] < array[k]) ? + buf[i++] : array[k++]; + } + } + + /** + * SortTask demo works as advertised + */ + public void testSortTaskDemo() { + ThreadLocalRandom rnd = ThreadLocalRandom.current(); + long[] array = new long[1007]; + for (int i = 0; i < array.length; i++) + array[i] = rnd.nextLong(); + long[] arrayClone = array.clone(); + testInvokeOnPool(mainPool(), new SortTask(array)); + Arrays.sort(arrayClone); + assertTrue(Arrays.equals(array, arrayClone)); + } }