update at 2020-12-30 18:28:53 by ehlxr

This commit is contained in:
ehlxr 2020-12-30 18:28:53 +08:00
parent 5c751d5a09
commit e2f510420c

View File

@ -25,7 +25,8 @@
package io.github.ehlxr.forkjoin; package io.github.ehlxr.forkjoin;
import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction; import java.util.concurrent.RecursiveTask;
import java.util.stream.IntStream;
/** /**
* @author ehlxr * @author ehlxr
@ -34,59 +35,57 @@ import java.util.concurrent.RecursiveAction;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println(sumOfSquares(ForkJoinPool.commonPool(), new double[]{1.2, 2.4, 3.5, 4.2, 5.0, 6, 7.6})); ForkJoinPool forkJoinPool = new ForkJoinPool(Runtime.getRuntime().availableProcessors());
// Future<Integer> future = forkJoinPool.submit(new RecursiveTaskDemo(arr, 0, arr.length));
// System.out.println("计算出来的总和="+future.get());
int[] arr = IntStream.range(0, 1000).toArray();
Integer integer = forkJoinPool.invoke(new RecursiveTaskDemo(arr, 0, arr.length));
System.out.println("计算出来的总和=" + integer);
// 关闭线程池
forkJoinPool.shutdown();
} }
static double sumOfSquares(ForkJoinPool pool, double[] array) { static class RecursiveTaskDemo extends RecursiveTask<Integer> {
int n = array.length; /**
Applyer a = new Applyer(array, 0, n, null); * 每个"小任务"最多只打印70个数
pool.invoke(a); */
return a.result; private static final int MAX = 100;
} private final int[] arr;
private final int start;
private final int end;
static class Applyer extends RecursiveAction { public RecursiveTaskDemo(int[] arr, int start, int end) {
final double[] array; this.arr = arr;
final int lo, hi; this.start = start;
double result; this.end = end;
Applyer next; // keeps track of right-hand-side tasks
Applyer(double[] array, int lo, int hi, Applyer next) {
this.array = array;
this.lo = lo;
this.hi = hi;
this.next = next;
}
double atLeaf(int l, int h) {
double sum = 0;
for (int i = l; i < h; ++i) {// perform leftmost base step}
sum += array[i] * array[i];
}
return sum;
} }
@Override @Override
protected void compute() { protected Integer compute() {
int l = lo; int sum = 0;
int h = hi; // 当end-start的值小于MAX时候开始打印
Applyer right = null; if ((end - start) < MAX) {
while (h - l > 1 && getSurplusQueuedTaskCount() <= 3) { for (int i = start; i < end; i++) {
int mid = (l + h) >>> 1; sum += arr[i];
right = new Applyer(array, mid, h, right);
right.fork();
h = mid;
}
double sum = atLeaf(l, h);
while (right != null) {
if (right.tryUnfork()) {// directly calculate if not stolen}
sum += right.atLeaf(right.lo, right.hi);
} else {
right.join();
sum += right.result;
} }
right = right.next; return sum;
} else {
System.err.println("=====任务分解======");
// 将大任务分解成两个小任务
int middle = (start + end) / 2;
RecursiveTaskDemo left = new RecursiveTaskDemo(arr, start, middle);
RecursiveTaskDemo right = new RecursiveTaskDemo(arr, middle, end);
// 并行执行两个小任务
left.fork();
right.fork();
// 把两个小任务累加的结果合并起来
return left.join() + right.join();
} }
result = sum;
} }
} }
} }