Skip to content

Instantly share code, notes, and snippets.

@xhiroga
Created November 28, 2017 22:36
Show Gist options
  • Save xhiroga/1039ffb343630993af68a3e23a76eea2 to your computer and use it in GitHub Desktop.
Save xhiroga/1039ffb343630993af68a3e23a76eea2 to your computer and use it in GitHub Desktop.
Java7で実装されたフォーク/ジョインFWを使ったフィボナッチ数列の計算プログラムです。
import java.util.concurrent.RecursiveTask;
import java.util.concurrent.ForkJoinPool;
public class Fib7 extends RecursiveTask<Integer>{
private final int n;
Fib7(int n){
this.n = n;
}
@Override
protected Integer compute(){
if(n <= 1){
return n;
}
Fib7 f1 = new Fib7(n - 1);
f1.fork();
Fib7 f2 = new Fib7(n - 2);
return f2.compute() + f1.join();
}
public static void main(String args[]){
ForkJoinPool pool = new ForkJoinPool();
System.out.println(pool.invoke(new Fib7(45)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment