-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacci.java
More file actions
34 lines (25 loc) · 776 Bytes
/
Fibonacci.java
File metadata and controls
34 lines (25 loc) · 776 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package kb.concurrent.forkjoin;
import java.util.List;
import java.util.concurrent.RecursiveTask;
@SuppressWarnings("serial")
public class Fibonacci extends RecursiveTask<Integer> {
int n;
public Fibonacci(int n) {
this.n = n;
}
@Override
protected Integer compute() {
if (n <= 1)
return 1;
Fibonacci fn_1 = new Fibonacci(n - 1);
Fibonacci fn_2 = new Fibonacci(n - 2);
fn_1.fork();
fn_2.fork();
return fn_1.join() + fn_2.join();
}
public static void main(String[] args) {
List<Integer> inputs = List.of(1, 2, 3, 4, 5);
for (Integer input : inputs)
System.out.println(String.format("N%s -> %s", input, new Fibonacci(input).compute()));
}
}