The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, Formally, i
ID: 3698597 • Letter: T
Question
The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, Formally, it can be expressed as fib0 0 fibl- Write a multithreaded program that generates the Fibonacci sequence. This program should work as follows: On the command line, the user will enter the number of Fibonacci numbers that the program is to generate. The program will then create a separate thread that will generate the Fibonacci numbers, placing the sequence in data that can be shared by the threads (an array is probably the most convenient data structure). When the thread finishes execution, the parent thread will output the sequence generated by the child thread. Because the parent thread cannot begin outputting the Fibonacci sequence until the child thread finishes, the parent thread will have to wait for the child thread to finish. Use the techniques described in Section 4.4 to meet this requirement. This problem requires the parent thread to wait for the child thread to finish its execution before printing out the computed values. If we let the parent thread access the Fibonacci numbers as soon as they have been computed by the child thread-rather than waiting for the child thread to terminate- what changes would be necessary to the solution for this exercise? Implement your modified solutionExplanation / Answer
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class Fibonacci {
private static long[] value;
public static void main(String args[]) throws InterruptedException {
int n;
try {
n = Integer.parseInt(args[0]);
} catch (Exception e) {
throw new RuntimeException(
"Please enter in the form java n number ");
}
value = new long[n + 1];
long start = System.nanoTime();
System.out.print("Dynamic Programming = " + fibon(n));
long end = System.nanoTime();
System.out.println(" time = " + (end - start) + "ns");
start = System.nanoTime();
System.out.print("Sequence = " + Sequence(n));
end = System.nanoTime();
System.out.println(" time = " + (end - start) + "ns");
start = System.nanoTime();
//int nThreads = Runtime.getRuntime().availableProcessors();
int nThreads = 30;
ExecutorService executorService = Executors
.newFixedThreadPool(nThreads);
int result;
try {
result = fibonacciSum(n, executorService);
} catch (ExecutionException e) {
throw new RuntimeException("Thread Interuppted ");
}
System.out.print(" MultiThreading = " + result);
end = System.nanoTime();
System.out.println(" time = " + (end - start) + "ns");
}
public static long fibon(int n) {
value[0] = 1;
value[1] = 1;
if (n <= 2)
return 1;
else if (value[n - 1] != 0)
return value[n];
for (int j = 2; j <= n; j++) {
value[j] = fibon(j - 2) + fibon(j - 1);
}
return value[n];
}
public static long Sequence(int n) {
if (n <= 2)
return 1;
else
return (Sequence(n - 1) + Sequence(n - 2));
}
private static class FibonacciThread implements Runnable {
int index;
int result;
ExecutorService executorService;
public FibonacciThread(int index) {
this.index = index;
}
public void run() {
try {
this.result = fibonacciSum(index, executorService);
} catch (Exception e) {
throw new RuntimeException("Thread interupted");
}
}
}
private static int fibonacciSum(int index, ExecutorService executorService)
throws InterruptedException, ExecutionException {
if (index == 1 || index == 2) {
return 1;
} else {
FibonacciThread fibonacciThread1 = new FibonacciThread(index - 2);
fibonacciThread1.executorService=executorService;
Future future = executorService.submit(fibonacciThread1);
Object object = future.get();
int resultPart2 = fibonacciSum(index - 1, executorService);
int result = fibonacciThread1.result + resultPart2;
//executorService.shutdown();
return result;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.