Fork-join in Java 7

[url=http://www.ibm.com/developerworks/java/library/j-jtp11137.html]Java theory and practice: Stick a fork in it[/url]

[quote]One of the additions to the java.util.concurrent packages coming in Java™ 7 is a framework for fork-join style parallel decomposition. The fork-join abstraction provides a natural mechanism for decomposing many algorithms to effectively exploit hardware parallelism.[/quote]

汗...
Java 7不愧是kitchen-sink...不过这东西看起来又挺实用的,有些时候虽然觉得(在多处理器/多核心环境下)有些地方应该开多线程来做,不过要自己管理那么多东西也确实怪麻烦的。看一例子:

[quote]Listing 3. Solving the select-max problem with the fork-join framework
public class MaxWithFJ extends RecursiveAction {
private final int threshold;
private final SelectMaxProblem problem;
public int result;

public MaxWithFJ(SelectMaxProblem problem, int threshold) {
this.problem = problem;
this.threshold = threshold;
}

protected void compute() {
if (problem.size < threshold)
result = problem.solveSequentially();
else {
int midpoint = problem.size / 2;
MaxWithFJ left = new MaxWithFJ(problem.subproblem(0, midpoint), threshold);
MaxWithFJ right = new MaxWithFJ(problem.subproblem(midpoint +
1, problem.size), threshold);
coInvoke(left, right);
result = Math.max(left.result, right.result);
}
}

public static void main(String[] args) {
SelectMaxProblem problem = ...
int threshold = ...
int nThreads = ...
MaxWithFJ mfj = new MaxWithFJ(problem, threshold);
ForkJoinExecutor fjPool = new ForkJoinPool(nThreads);

fjPool.invoke(mfj);
int result = mfj.result;
}
}
[/quote]

不过这东西要是能配合匿名方法/闭包来做就更简洁了。
话说,[url=http://www.third-bit.com/~gvwilson]Greg Wilson[/url]很明显对这东西[url=http://pyre.third-bit.com/blog/archives/1222.html]不感冒[/url]。一提到调试器,这并行计算的痛处就又来了一个……
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值