Created
July 19, 2010 13:00
-
-
Save alcides/481366 to your computer and use it in GitHub Desktop.
This file contains 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 jsr166y.*; | |
@SuppressWarnings("serial") | |
class Fib extends RecursiveAction { | |
public volatile int number; | |
private int THREASHOLD = 12; //(12 works) | |
private static int TARGET = 12; | |
Fib(int n) { number = n; } | |
private int seqFib(int n) { | |
if (n <= 1) return 1; | |
else return seqFib(n-1) + seqFib(n-2); | |
} | |
@Override | |
protected void compute() { | |
int n = number; | |
if (n <= 1) { /* do nothing */ } | |
else if (n <= THREASHOLD) | |
number = seqFib(n); | |
else { | |
Fib f1 = new Fib(n - 1); | |
Fib f2 = new Fib(n - 2); | |
invokeAll(f1,f2); | |
number = f1.number + f2.number; // compose | |
} | |
} | |
public static void main(String[] args) { | |
ForkJoinPool pool = new ForkJoinPool(); | |
Fib t = new Fib(TARGET); | |
pool.invoke(t); | |
System.out.println("Final result = " + t.number); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment