--- jsr166/src/test/tck/CountedCompleterTest.java 2015/10/18 19:33:21 1.27 +++ jsr166/src/test/tck/CountedCompleterTest.java 2016/08/29 19:13:16 1.28 @@ -13,9 +13,12 @@ import java.util.concurrent.CountedCompl import java.util.concurrent.ExecutionException; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinTask; +import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; +import java.util.function.BiConsumer; +import java.util.function.Consumer; import junit.framework.Test; import junit.framework.TestSuite; @@ -1842,4 +1845,115 @@ public class CountedCompleterTest extend testInvokeOnPool(singletonPool(), a); } + /** CountedCompleter class javadoc code sample, version 1. */ + public static void forEach1(E[] array, Consumer action) { + class Task extends CountedCompleter { + final int lo, hi; + Task(Task parent, int lo, int hi) { + super(parent); this.lo = lo; this.hi = hi; + } + + public void compute() { + if (hi - lo >= 2) { + int mid = (lo + hi) >>> 1; + // must set pending count before fork + setPendingCount(2); + new Task(this, mid, hi).fork(); // right child + new Task(this, lo, mid).fork(); // left child + } + else if (hi > lo) + action.accept(array[lo]); + tryComplete(); + } + } + new Task(null, 0, array.length).invoke(); + } + + /** CountedCompleter class javadoc code sample, version 2. */ + public static void forEach2(E[] array, Consumer action) { + class Task extends CountedCompleter { + final int lo, hi; + Task(Task parent, int lo, int hi) { + super(parent); this.lo = lo; this.hi = hi; + } + + public void compute() { + if (hi - lo >= 2) { + int mid = (lo + hi) >>> 1; + setPendingCount(1); // not off by one ! + new Task(this, mid, hi).fork(); // right child + new Task(this, lo, mid).compute(); // direct invoke + } else { + if (hi > lo) + action.accept(array[lo]); + tryComplete(); + } + } + } + new Task(null, 0, array.length).invoke(); + } + + /** CountedCompleter class javadoc code sample, version 3. */ + public static void forEach3(E[] array, Consumer action) { + class Task extends CountedCompleter { + final int lo, hi; + Task(Task parent, int lo, int hi) { + super(parent); this.lo = lo; this.hi = hi; + } + + public void compute() { + int n = hi - lo; + for (; n >= 2; n /= 2) { + addToPendingCount(1); + new Task(this, lo + n/2, lo + n).fork(); + } + if (n > 0) + action.accept(array[lo]); + propagateCompletion(); + } + } + new Task(null, 0, array.length).invoke(); + } + + /** CountedCompleter class javadoc code sample, version 4. */ + public static void forEach4(E[] array, Consumer action) { + class Task extends CountedCompleter { + final int lo, hi; + Task(Task parent, int lo, int hi) { + super(parent, 31 - Integer.numberOfLeadingZeros(hi - lo)); + this.lo = lo; this.hi = hi; + } + + public void compute() { + for (int n = hi - lo; n >= 2; n /= 2) + new Task(this, lo + n/2, lo + n).fork(); + action.accept(array[lo]); + propagateCompletion(); + } + } + if (array.length > 0) + new Task(null, 0, array.length).invoke(); + } + + void testRecursiveDecomposition( + BiConsumer> action) { + int n = ThreadLocalRandom.current().nextInt(8); + Integer[] a = new Integer[n]; + for (int i = 0; i < n; i++) a[i] = i + 1; + AtomicInteger ai = new AtomicInteger(0); + action.accept(a, (x) -> ai.addAndGet(x)); + assertEquals(n * (n + 1) / 2, ai.get()); + } + + /** + * Variants of divide-by-two recursive decomposition into leaf tasks, + * as described in the CountedCompleter class javadoc code samples + */ + public void testRecursiveDecomposition() { + testRecursiveDecomposition(CountedCompleterTest::forEach1); + testRecursiveDecomposition(CountedCompleterTest::forEach2); + testRecursiveDecomposition(CountedCompleterTest::forEach3); + testRecursiveDecomposition(CountedCompleterTest::forEach4); + } + }