Last active
December 14, 2015 06:59
-
-
Save cokeSchlumpf/5046887 to your computer and use it in GitHub Desktop.
ForkJoin API
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
package de.michaelwellner.edu.forkjoin; | |
import java.util.concurrent.ForkJoinPool; | |
import java.util.concurrent.RecursiveTask; | |
public class ForkJoin { | |
private static final boolean DEBUG = false; | |
private static class Fibonacci { | |
private final int n; | |
public Fibonacci(int n) { | |
this.n = n; | |
} | |
public long solve() { | |
return this.fibonacci(this.n); | |
} | |
private long fibonacci(int n) { | |
if (DEBUG) | |
System.out.println("Thread " + Thread.currentThread().getName() | |
+ " calculates " + "fibonacci for " + n); | |
if (n <= 1) { | |
return n; | |
} else { | |
return fibonacci(n - 1) + fibonacci(n - 2); | |
} | |
} | |
public int getN() { | |
return n; | |
} | |
} | |
private static class FibonacciTask extends RecursiveTask<Long> { | |
private static final long serialVersionUID = 5085586289180387451L; | |
private static final int THRESHOLD = 5; | |
private final Fibonacci fibonacci; | |
public FibonacciTask(Fibonacci fibonacci) { | |
this.fibonacci = fibonacci; | |
} | |
@Override | |
protected Long compute() { | |
if (this.fibonacci.getN() <= THRESHOLD) { | |
return this.fibonacci.solve(); | |
} else { | |
final FibonacciTask f1 = new FibonacciTask(new Fibonacci( | |
this.fibonacci.getN() - 1)); | |
final FibonacciTask f2 = new FibonacciTask(new Fibonacci( | |
this.fibonacci.getN() - 2)); | |
f1.fork(); | |
return f2.compute() + f1.join(); | |
} | |
} | |
} | |
public static void main(String... args) { | |
final int n = 10; | |
classic(n); | |
System.out.println("\n\n\n"); | |
forkjoin(n); | |
} | |
private static void forkjoin(final int n) { | |
final int processors = Runtime.getRuntime().availableProcessors(); | |
System.out.println("Available Processors: " + processors); | |
final Fibonacci f = new Fibonacci(n); | |
final ForkJoinPool pool = new ForkJoinPool(processors); | |
final long elapsedTime = stopWatch(new Runnable() { | |
@Override | |
public void run() { | |
System.out.println("Calculating fibonacci(" + n + ")"); | |
final FibonacciTask task = new FibonacciTask(f); | |
long result = pool.invoke(task); | |
System.out.println("Result: " + result); | |
} | |
}); | |
System.out.println("Elapsed Time: " + elapsedTime + "ms"); | |
} | |
private static void classic(final int n) { | |
final Fibonacci f = new Fibonacci(n); | |
final long elapsedTime = stopWatch(new Runnable() { | |
@Override | |
public void run() { | |
System.out.println("Calculating fibonacci(" + n + ")"); | |
System.out.println("Result: " + f.solve()); | |
} | |
}); | |
System.out.println("Elapsed Time: " + elapsedTime + "ms"); | |
} | |
private static long stopWatch(Runnable runnable) { | |
long start = System.currentTimeMillis(); | |
runnable.run(); | |
return (System.currentTimeMillis() - start); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment