You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.0 KiB
Java
51 lines
1.0 KiB
Java
package _12._1;
|
|
|
|
import provided._12._1.Fibonacci;
|
|
|
|
public class FibonacciParallel extends Fibonacci {
|
|
|
|
@Override
|
|
public long calculate(int n) {
|
|
if (n == 0) {
|
|
return 0;
|
|
} else if (n == 1) {
|
|
return 1;
|
|
} else {
|
|
FibonacciThread thread1 = new FibonacciThread(n-1);
|
|
FibonacciThread thread2 = new FibonacciThread(n-2);
|
|
thread1.start();
|
|
thread2.start();
|
|
|
|
try {
|
|
thread1.join();
|
|
thread2.join();
|
|
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return thread1.getResult() + thread2.getResult();
|
|
}
|
|
}
|
|
|
|
|
|
class FibonacciThread extends Thread {
|
|
private int n;
|
|
private long result;
|
|
|
|
public FibonacciThread(int n) {
|
|
this.n = n;
|
|
}
|
|
|
|
@Override
|
|
public void run() {
|
|
result = calculate(n);
|
|
}
|
|
|
|
public long getResult() {
|
|
return result;
|
|
}
|
|
}
|
|
}
|
|
|
|
|