+/EE360P

distributed systems

git clone https://git.julia.blue/+/EE360P

Wei-Lun Hungadd additional examples for chap 3fd8db29

master
771 B22 linesraw
1import java.util.concurrent.RecursiveTask;
2import java.util.concurrent.ForkJoinPool;
3
4class Fibonacci3 extends RecursiveTask<Integer> {
5  final int n;
6  Fibonacci3(int n) { this.n = n; }
7  protected Integer compute() {
8    if ((n == 0)||(n == 1 )) return 1;
9    Fibonacci3 f1 = new Fibonacci3(n - 1);
10    f1.fork();
11    Fibonacci3 f2 = new Fibonacci3(n - 2);
12    return f2.compute() + f1.join();
13  }
14  public static void main(String[] args) {
15    int processors = Runtime.getRuntime().availableProcessors();
16    System.out.println("Number of processors: " + processors);
17    Fibonacci3 f = new Fibonacci3(Integer.parseInt(args[0]));
18    ForkJoinPool pool = new ForkJoinPool(processors);
19    int result = pool.invoke(f);
20    System.out.println("Result: " + result);
21  }
22}