--- jsr166/src/test/tck/RecursiveActionTest.java 2011/03/15 19:47:07 1.30 +++ jsr166/src/test/tck/RecursiveActionTest.java 2011/06/27 02:47:32 1.37 @@ -12,9 +12,11 @@ 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 { @@ -267,7 +269,7 @@ public class RecursiveActionTest extends assertEquals(21, f.result); checkCompletedNormally(f); - f.reinitialize(); + f = new FibAction(8); f.cancel(true); assertSame(f, f.fork()); myself.interrupt(); @@ -280,7 +282,7 @@ public class RecursiveActionTest extends checkCancelled(f); } - f.reinitialize(); + f = new FibAction(8); f.completeExceptionally(new FJException()); assertSame(f, f.fork()); myself.interrupt(); @@ -294,7 +296,7 @@ public class RecursiveActionTest extends } // test quietlyJoin() - f.reinitialize(); + f = new FibAction(8); assertSame(f, f.fork()); myself.interrupt(); assertTrue(myself.isInterrupted()); @@ -303,7 +305,7 @@ public class RecursiveActionTest extends assertEquals(21, f.result); checkCompletedNormally(f); - f.reinitialize(); + f = new FibAction(8); f.cancel(true); assertSame(f, f.fork()); myself.interrupt(); @@ -312,7 +314,7 @@ public class RecursiveActionTest extends Thread.interrupted(); checkCancelled(f); - f.reinitialize(); + f = new FibAction(8); f.completeExceptionally(new FJException()); assertSame(f, f.fork()); myself.interrupt(); @@ -490,7 +492,6 @@ public class RecursiveActionTest extends testInvokeOnPool(mainPool(), a); } - /** * helpQuiesce returns when tasks are complete. * getQueuedTaskCount returns 0 when quiescent @@ -500,7 +501,7 @@ public class RecursiveActionTest extends public void realCompute() { FibAction f = new FibAction(8); assertSame(f, f.fork()); - f.helpQuiesce(); + helpQuiesce(); assertEquals(21, f.result); assertEquals(0, getQueuedTaskCount()); checkCompletedNormally(f); @@ -508,7 +509,6 @@ public class RecursiveActionTest extends testInvokeOnPool(mainPool(), a); } - /** * invoke task throws exception when task completes abnormally */ @@ -778,7 +778,6 @@ public class RecursiveActionTest extends testInvokeOnPool(mainPool, a); } - /** * setRawResult(null) succeeds */ @@ -792,7 +791,7 @@ public class RecursiveActionTest extends } /** - * A reinitialized task may be re-invoked + * A reinitialized normally completed task may be re-invoked */ public void testReinitialize() { RecursiveAction a = new CheckedRecursiveAction() { @@ -812,6 +811,29 @@ public class RecursiveActionTest extends } /** + * 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); + } + + /** * invoke task throws exception after invoking completeExceptionally */ public void testCompleteExceptionally() { @@ -925,7 +947,6 @@ public class RecursiveActionTest extends testInvokeOnPool(mainPool(), a); } - /** * invokeAll(tasks) with any null task throws NPE */ @@ -1176,4 +1197,47 @@ public class RecursiveActionTest extends 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)); + } }