Created
November 28, 2017 22:36
-
-
Save xhiroga/1039ffb343630993af68a3e23a76eea2 to your computer and use it in GitHub Desktop.
Java7で実装されたフォーク/ジョインFWを使ったフィボナッチ数列の計算プログラムです。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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