Write a multithreaded program that generates the Fibonacci sequence using the Ja
ID: 3542859 • Letter: W
Question
Write a multithreaded program that generates the Fibonacci sequence
using the Java library. This program
should work as follows: The user will enter on the command line
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 ?nishes 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 ?nishes,
this will require having the parent thread wait for the child thread to
?nish.
*Ive seen examples of Pthreads here, but I need Java to help me with an assignment.
Explanation / Answer
import java.util.*;
class X extends Thread{
public void run()
{ test5.arr[0]=0;
test5.arr[1]=1;
for(int i=2;i<test5.n;i++)
{
test5.arr[i]=test5.arr[i-1]+test5.arr[i-2];
}
}
}
public class test5 {
static int n;
static int arr[]=new int[100];
public static void main(String args[])
{
System.out.println("enter the number of fibbonacci series length :");
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
n=Integer.parseInt(str);
X thread1=new X();
thread1.start();
try{
thread1.join();
}
catch(Exception e)
{
}
System.out.println("the fibbonacci series numbers are:");
for(int i=0;i<n;i++)
System.out.println(arr[i]);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.