|
| 1 | +package main.me.zzw.app.concurrent; |
| 2 | + |
| 3 | +import java.util.concurrent.CountedCompleter; |
| 4 | +import java.util.concurrent.atomic.AtomicReference; |
| 5 | + |
| 6 | +/** |
| 7 | + * Created by infosea on 2016-09-09. |
| 8 | + */ |
| 9 | +class MyOperation<E> { |
| 10 | + void apply(E e) { |
| 11 | + System.out.println(e); |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +class ForEach<E> extends CountedCompleter<Void> { |
| 16 | + |
| 17 | + public static <E> void forEach(E[] array, MyOperation<E> op) { |
| 18 | + new ForEach<E>(null, array, op, 0, array.length).invoke(); |
| 19 | + } |
| 20 | + |
| 21 | + final E[] array; final MyOperation<E> op; final int lo, hi; |
| 22 | + ForEach(CountedCompleter<?> p, E[] array, MyOperation<E> op, int lo, int hi){ |
| 23 | + super(p); |
| 24 | + this.array = array; |
| 25 | + this.op = op; |
| 26 | + this.lo = lo; |
| 27 | + this.hi = hi; |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public void compute() { |
| 32 | + if(hi -lo >=2 ){ |
| 33 | + int mid = (lo + hi) >>> 1; |
| 34 | + setPendingCount(2); // must set pending count before fork |
| 35 | + new ForEach(this, array, op, mid, hi).fork(); // right child; |
| 36 | + new ForEach(this, array, op, lo, mid).fork(); // left child |
| 37 | + }else if (hi > lo) |
| 38 | + op.apply(array[lo]); |
| 39 | + tryComplete(); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +class ForEach2<E> extends CountedCompleter<Void>{ |
| 44 | + |
| 45 | + public static <E> void forEach(E[] array, MyOperation<E> op) { |
| 46 | + new ForEach<E>(null, array, op, 0, array.length).invoke(); |
| 47 | + } |
| 48 | + |
| 49 | + final E[] array; final MyOperation<E> op; final int lo, hi; |
| 50 | + ForEach2(CountedCompleter<?> p, E[] array, MyOperation<E> op, int lo, int hi){ |
| 51 | + super(p); |
| 52 | + this.array = array; |
| 53 | + this.op = op; |
| 54 | + this.lo = lo; |
| 55 | + this.hi = hi; |
| 56 | + } |
| 57 | + |
| 58 | + public void compute() { // version 2 |
| 59 | + if (hi - lo >= 2) { |
| 60 | + int mid = (lo + hi) >>> 1; |
| 61 | + setPendingCount(1); // only one pending |
| 62 | + new ForEach2(this, array, op, mid, hi).fork(); // right child |
| 63 | + new ForEach2(this, array, op, lo, mid).compute(); // direct invoke |
| 64 | + } else { |
| 65 | + if (hi > lo) |
| 66 | + op.apply(array[lo]); |
| 67 | + tryComplete(); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +class ForEach3<E> extends CountedCompleter<Void> { |
| 73 | + |
| 74 | + public static <E> void forEach(E[] array, MyOperation<E> op) { |
| 75 | + new ForEach3<E>(null, array, op, 0, array.length).invoke(); |
| 76 | + } |
| 77 | + |
| 78 | + final E[] array; |
| 79 | + final MyOperation<E> op; |
| 80 | + final int lo, hi; |
| 81 | + |
| 82 | + ForEach3(CountedCompleter<?> p, E[] array, MyOperation<E> op, int lo, int hi) { |
| 83 | + super(p); |
| 84 | + this.array = array; |
| 85 | + this.op = op; |
| 86 | + this.lo = lo; |
| 87 | + this.hi = hi; |
| 88 | + } |
| 89 | + |
| 90 | + public void compute() { // version 3 |
| 91 | + int l = lo, h = hi; |
| 92 | + while (h - l >= 2) { |
| 93 | + int mid = (l + h) >>> 1; |
| 94 | + addToPendingCount(1); |
| 95 | + new ForEach3(this, array, op, mid, h).fork(); // right child |
| 96 | + h = mid; |
| 97 | + } |
| 98 | + if (h > l) |
| 99 | + op.apply(array[l]); |
| 100 | + propagateCompletion(); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +class Searcher<E> extends CountedCompleter<E> { |
| 105 | + |
| 106 | + final E[] array; final AtomicReference<E> result; final int lo, hi; |
| 107 | + |
| 108 | + Searcher(CountedCompleter<?> p, E[] array, AtomicReference<E> result, int lo, int hi) { |
| 109 | + super(p); |
| 110 | + this.array = array; this.result = result; this.lo = lo; this.hi = hi; |
| 111 | + } |
| 112 | + |
| 113 | + public E getRawResult() { return result.get(); } |
| 114 | + |
| 115 | + public void compute() { // similar to ForEach version 3 |
| 116 | + int l = lo, h = hi; |
| 117 | + while (result.get() == null && h >= l) { |
| 118 | + if (h - l >= 2) { |
| 119 | + int mid = (l + h) >>> 1; |
| 120 | + addToPendingCount(1); |
| 121 | + new Searcher(this, array, result, mid, h).fork(); |
| 122 | + h = mid; |
| 123 | + } |
| 124 | + else { |
| 125 | + E x = array[l]; |
| 126 | + if (matches(x) && result.compareAndSet(null, x)) |
| 127 | + quietlyCompleteRoot(); // root task is now joinable |
| 128 | + break; |
| 129 | + } |
| 130 | + } |
| 131 | + tryComplete(); // normally complete whether or not found |
| 132 | + } |
| 133 | + |
| 134 | + boolean matches(E e) { |
| 135 | + return true; |
| 136 | + } // return true if found |
| 137 | + |
| 138 | + |
| 139 | + public static <E> E search(E[] array) { |
| 140 | + return new Searcher<E>(null, array, new AtomicReference<E>(), 0, array.length).invoke(); |
| 141 | + } |
| 142 | + |
| 143 | +} |
| 144 | + |
| 145 | +class MyMapper<E> { E apply(E v) { ... } } |
| 146 | +class MyReducer<E> { E apply(E x, E y) { ... } } |
| 147 | +class MapReducer<E> extends CountedCompleter<E> { |
| 148 | + final E[] array; final MyMapper<E> mapper; |
| 149 | + final MyReducer<E> reducer; final int lo, hi; |
| 150 | + MapReducer<E> sibling; |
| 151 | + E result; |
| 152 | + MapReducer(CountedCompleter<?> p, E[] array, MyMapper<E> mapper, |
| 153 | + MyReducer<E> reducer, int lo, int hi) { |
| 154 | + super(p); |
| 155 | + this.array = array; this.mapper = mapper; |
| 156 | + this.reducer = reducer; this.lo = lo; this.hi = hi; |
| 157 | + } |
| 158 | + public void compute() { |
| 159 | + if (hi - lo >= 2) { |
| 160 | + int mid = (lo + hi) >>> 1; |
| 161 | + MapReducer<E> left = new MapReducer(this, array, mapper, reducer, lo, mid); |
| 162 | + MapReducer<E> right = new MapReducer(this, array, mapper, reducer, mid, hi); |
| 163 | + left.sibling = right; |
| 164 | + right.sibling = left; |
| 165 | + setPendingCount(1); // only right is pending |
| 166 | + right.fork(); |
| 167 | + left.compute(); // directly execute left |
| 168 | + } |
| 169 | + else { |
| 170 | + if (hi > lo) |
| 171 | + result = mapper.apply(array[lo]); |
| 172 | + tryComplete(); |
| 173 | + } |
| 174 | + } |
| 175 | + public void onCompletion(CountedCompleter<?> caller) { |
| 176 | + if (caller != this) { |
| 177 | + MapReducer<E> child = (MapReducer<E>)caller; |
| 178 | + MapReducer<E> sib = child.sibling; |
| 179 | + if (sib == null || sib.result == null) |
| 180 | + result = child.result; |
| 181 | + else |
| 182 | + result = reducer.apply(child.result, sib.result); |
| 183 | + } |
| 184 | + } |
| 185 | + public E getRawResult() { return result; } |
| 186 | + |
| 187 | + public static <E> E mapReduce(E[] array, MyMapper<E> mapper, MyReducer<E> reducer) { |
| 188 | + return new MapReducer<E>(null, array, mapper, reducer, |
| 189 | + 0, array.length).invoke(); |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | + |
| 194 | + |
| 195 | +public class TCountedCompleter { |
| 196 | + public static void main(String[] args) { |
| 197 | + ForEach.forEach(new Integer[]{2,6,4,3,6,7,8,4,3,6,8,9,0,5,4,3,2}, new MyOperation<Integer>()); |
| 198 | + System.out.println("-----------"); |
| 199 | + ForEach2.forEach(new Integer[]{2,6,4,3,6,7,8,4,3,6,8,9,0,5,4,3,2}, new MyOperation<Integer>()); |
| 200 | + System.out.println("-----------"); |
| 201 | + ForEach3.forEach(new Integer[]{2,6,4,3,6,7,8,4,3,6,8,9,0,5,4,3,2}, new MyOperation<Integer>()); |
| 202 | + |
| 203 | + } |
| 204 | +} |
0 commit comments