package Threads;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;
/**
* Created by Frank
*/
public class RecursiveActionDemo extends RecursiveAction {
static int[] raw = {19, 3, 0, -1, 57, 24, 65, Integer.MAX_VALUE, 42, 0, 3, 5};
static int[] sorted = null;
int[] source;
int[] dest;
int length;
int start;
final static int THRESHOLD = 4;
public static void main(String[] args) {
sorted = new int[raw.length];
ForkJoinPool pool = new ForkJoinPool();
pool.invoke(new RecursiveActionDemo(raw, 0, raw.length, sorted));
System.out.println('[');
for (int i : sorted) {
System.out.println(i + ",");
}
System.out.println(']');
}
public RecursiveActionDemo(int[] source, int start, int length, int[] dest) {
this.source = source;
this.dest = dest;
this.length = length;
this.start = start;
}
@Override
protected void compute() {
System.out.println("ForkJoinDemo.compute()");
if (length < THRESHOLD) { // 直接计算
for (int i = start; i < start + length; i++) {
dest[i] = source[i] * source[i];
}
} else { // 分而治之
int split = length / 2;
/**
* invokeAll反复调用fork和join直到完成。
*/
invokeAll(new RecursiveActionDemo(source, start, split, dest), new RecursiveActionDemo(source, start + split, length - split, dest));
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有