1/* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 2 along with this work; if not, write to the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 */ 22 23/* 24 * This file is available under and governed by the GNU General Public 25 * License version 2 only, as published by the Free Software Foundation. 26 * However, the following notice accompanied the original version of this 27 * file: 28 * 29 * Written by Doug Lea and Martin Buchholz with assistance from 30 * members of JCP JSR-166 Expert Group and released to the public 31 * domain, as explained at 32 * http://creativecommons.org/publicdomain/zero/1.0/ 33 */ 34 35import static java.util.concurrent.TimeUnit.MILLISECONDS; 36import static java.util.concurrent.TimeUnit.SECONDS; 37 38import java.util.concurrent.CountedCompleter; 39import java.util.concurrent.ThreadLocalRandom; 40import java.util.concurrent.atomic.AtomicInteger; 41import java.util.function.BiConsumer; 42import java.util.function.Consumer; 43 44import junit.framework.Test; 45import junit.framework.TestSuite; 46 47public class CountedCompleter8Test extends JSR166TestCase { 48 49 public static void main(String[] args) { 50 main(suite(), args); 51 } 52 53 public static Test suite() { 54 return new TestSuite(CountedCompleter8Test.class); 55 } 56 57 /** CountedCompleter class javadoc code sample, version 1. */ 58 public static <E> void forEach1(E[] array, Consumer<E> action) { 59 class Task extends CountedCompleter<Void> { 60 final int lo, hi; 61 Task(Task parent, int lo, int hi) { 62 super(parent); this.lo = lo; this.hi = hi; 63 } 64 65 public void compute() { 66 if (hi - lo >= 2) { 67 int mid = (lo + hi) >>> 1; 68 // must set pending count before fork 69 setPendingCount(2); 70 new Task(this, mid, hi).fork(); // right child 71 new Task(this, lo, mid).fork(); // left child 72 } 73 else if (hi > lo) 74 action.accept(array[lo]); 75 tryComplete(); 76 } 77 } 78 new Task(null, 0, array.length).invoke(); 79 } 80 81 /** CountedCompleter class javadoc code sample, version 2. */ 82 public static <E> void forEach2(E[] array, Consumer<E> action) { 83 class Task extends CountedCompleter<Void> { 84 final int lo, hi; 85 Task(Task parent, int lo, int hi) { 86 super(parent); this.lo = lo; this.hi = hi; 87 } 88 89 public void compute() { 90 if (hi - lo >= 2) { 91 int mid = (lo + hi) >>> 1; 92 setPendingCount(1); // looks off by one, but correct! 93 new Task(this, mid, hi).fork(); // right child 94 new Task(this, lo, mid).compute(); // direct invoke 95 } else { 96 if (hi > lo) 97 action.accept(array[lo]); 98 tryComplete(); 99 } 100 } 101 } 102 new Task(null, 0, array.length).invoke(); 103 } 104 105 /** CountedCompleter class javadoc code sample, version 3. */ 106 public static <E> void forEach3(E[] array, Consumer<E> action) { 107 class Task extends CountedCompleter<Void> { 108 final int lo, hi; 109 Task(Task parent, int lo, int hi) { 110 super(parent); this.lo = lo; this.hi = hi; 111 } 112 113 public void compute() { 114 int n = hi - lo; 115 for (; n >= 2; n /= 2) { 116 addToPendingCount(1); 117 new Task(this, lo + n/2, lo + n).fork(); 118 } 119 if (n > 0) 120 action.accept(array[lo]); 121 propagateCompletion(); 122 } 123 } 124 new Task(null, 0, array.length).invoke(); 125 } 126 127 /** CountedCompleter class javadoc code sample, version 4. */ 128 public static <E> void forEach4(E[] array, Consumer<E> action) { 129 class Task extends CountedCompleter<Void> { 130 final int lo, hi; 131 Task(Task parent, int lo, int hi) { 132 super(parent, 31 - Integer.numberOfLeadingZeros(hi - lo)); 133 this.lo = lo; this.hi = hi; 134 } 135 136 public void compute() { 137 for (int n = hi - lo; n >= 2; n /= 2) 138 new Task(this, lo + n/2, lo + n).fork(); 139 action.accept(array[lo]); 140 propagateCompletion(); 141 } 142 } 143 if (array.length > 0) 144 new Task(null, 0, array.length).invoke(); 145 } 146 147 void testRecursiveDecomposition( 148 BiConsumer<Integer[], Consumer<Integer>> action) { 149 int n = ThreadLocalRandom.current().nextInt(8); 150 Integer[] a = new Integer[n]; 151 for (int i = 0; i < n; i++) a[i] = i + 1; 152 AtomicInteger ai = new AtomicInteger(0); 153 action.accept(a, ai::addAndGet); 154 assertEquals(n * (n + 1) / 2, ai.get()); 155 } 156 157 /** 158 * Variants of divide-by-two recursive decomposition into leaf tasks, 159 * as described in the CountedCompleter class javadoc code samples 160 */ 161 public void testRecursiveDecomposition() { 162 testRecursiveDecomposition(CountedCompleter8Test::forEach1); 163 testRecursiveDecomposition(CountedCompleter8Test::forEach2); 164 testRecursiveDecomposition(CountedCompleter8Test::forEach3); 165 testRecursiveDecomposition(CountedCompleter8Test::forEach4); 166 } 167 168} 169