Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Generating Fibonacci Series Using a Multithreaded Java Program

807580Oct 19 2009 — edited Oct 21 2009
Well...as the title says, I need to generate a Fibonacci series using Java's thread library (including Runnable, I assume). I really don't know where to begin. Here's all I have so far...
import java.util.Scanner;  

public class FibonacciThread {     
     public static void main (String[] args) { 	
     int userInput, fibonacciOutput; 	
     Scanner scan = new Scanner(System.in);
 System.out.print("Please enter a non-negative number:"); 	
     userInput = scan.nextInt(); 	 	
     fibonacciOutput= fib(userInput);  	 
     System.out.println("The " + userInput+ "th Fibonacci number = " + fibonacciOutput+ "."); }      

public static int fib(int k) { 	
   if (k <= 2) { 	    
      return 1; 	
   }  	
   else { 	    
      return fib(k-1) + fib(k-2); }}}
It's not much...I know. But I don't know where to begin. Here are more instructions on what I'm to do...
"Write a multithreaded program that generated a Fib series...User should enter number to generate to...the program will then generate a separate thread that will generate the Fib numbers, placing the sequence in data that is shared by the threads (an array is probably most convenient)..."

Any help will be greatly appreciated! :)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 18 2009
Added on Oct 19 2009
16 comments
3,548 views