+/EE360P

distributed systems

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

Wei-Lun Hungupdate name828c852

master
949 B29 linesraw
1import java.util.concurrent.*;
2class Fibonacci2 implements Callable<Integer> {
3  public static ExecutorService threadPool = Executors.newCachedThreadPool();
4  int n;
5  public Fibonacci2(int n) {
6    this.n = n;
7  }
8  public Integer call() {
9    try {
10       if ((n == 0)||(n == 1 )) return 1;
11       else {
12        Future<Integer> f1 = threadPool.submit(new Fibonacci2(n-1));
13        Future<Integer> f2 = threadPool.submit(new Fibonacci2(n-2));
14        return f1.get() + f2.get();
15      } 
16    } catch (Exception e) { System.err.println (e); return 1;}
17  }
18
19  public static void main(String[] args) {
20    try {
21	ExecutorService es = Executors.newSingleThreadExecutor();
22        Fibonacci2 f = new Fibonacci2(Integer.parseInt(args[0]));
23        Future<Integer> f1 = es.submit(f);
24        System.out.println("Answer is " + f1.get());
25        es.shutdown ();
26        f.threadPool.shutdown();
27    } catch (Exception e) { System.err.println (e); }
28  }
29}