+/EE360P

distributed systems

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

Wei-Lun Hungupdate name828c852

master
908 B33 linesraw
1public class Fibonacci extends Thread {
2    int n;
3    int result;
4    public Fibonacci(int n) {
5        this.n = n;
6    }
7    public void run() {
8        if ((n == 0)||(n == 1 )) result = 1;
9        else {
10            Fibonacci f1 = new Fibonacci(n-1);
11            Fibonacci f2 = new Fibonacci(n-2);
12            f1.start();
13            f2.start();
14            try {
15                f1.join();
16                f2.join();
17            } catch (InterruptedException e){};
18            result = f1.getResult() + f2.getResult();
19        }
20    }
21    public int getResult(){
22        return result;
23    }
24    public static void main(String[] args) {
25        Fibonacci f1 = new Fibonacci(Integer.parseInt(args[0]));
26        f1.start();
27        try {
28            f1.join();
29        } catch (InterruptedException e){};
30        System.out.println("Answer is " + f1.getResult());
31    }
32}
33