Java
7的概念
ForkJoinPool允许任务通过将任务提交给同一执行者来“分叉”另一个任务。然后为它提供一个选项,供以后尝试通过尝试运行“帮助加入”该任务(如果尚未运行)的选项提供。
我相信,通过简单地将
Executorwith与组合,可以在Java 6中完成相同的操作
FutureTask。像这样:
public class Fib implements Callable<Integer> { int n; Executor exec; Fib(final int n, final Executor exec) { this.n = n; this.exec = exec; } @Override public Integer call() throws Exception { if (n == 0 || n == 1) { return n; } //Divide the problem final Fib n1 = new Fib(n - 1, exec); final Fib n2 = new Fib(n - 2, exec); //FutureTask only allows run to complete once final FutureTask<Integer> n2Task = new FutureTask<Integer>(n2); //Ask the Executor for help exec.execute(n2Task); //Do half the work ourselves final int partialResult = n1.call(); //Do the other half of the work if the Executor hasn't n2Task.run(); //Return the combined result return partialResult + n2Task.get(); }}


