Skip to content

Instantly share code, notes, and snippets.

@alcides
Created July 19, 2010 13:00
Show Gist options
  • Save alcides/481366 to your computer and use it in GitHub Desktop.
Save alcides/481366 to your computer and use it in GitHub Desktop.
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