我正在使用Java SE 7中的RecursiveTask测试Fibonacci示例http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/RecursiveTask.html.
该计划如下:
import java.util.concurrent.*;
public class testfuture{
public static void main(String[] args) {
System.out.println("Hello, World");
Fibonacci fib = new Fibonacci(10);
int result = fib.compute();
System.out.println(result);
}
}
class Fibonacci extends RecursiveTask<Integer> {
final int n;
Fibonacci(int n) { this.n = n; }
public Integer compute() {
if (n <= 1)
return n;
Fibonacci f1 = new Fibonacci(n - 1);
f1.fork();
Fibonacci f2 = new Fibonacci(n - 2);
return f2.invoke() + f1.join();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,该程序会抛出运行时异常
Hello, World …Run Code Online (Sandbox Code Playgroud)