Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Fibonacci Series Using a Multi-threaded Java Program

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Fibonacci Series Using a Multi-threaded Java Program

    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!


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Fibonacci Series Using a Multi-threaded Java Program

    Something like this? Here's the Fibonacci generating part. I haven't quite figured out a good way to keep main from accessing the array too soon, so I put in a little sleep statement in the constructor. I also implemented a dynamic programming solution to finding fibonacci numbers rather than the simple recursive solution (many times faster). I've left the stuff out that you had for what number to calculate the fibonacci number of, but I'm sure you can figure that stuff out (hint: you have it right in your code).

    public class Fibonacci implements Runnable
    {
    	int[] fib;
     
    	public static void main(String[] args)
    	{
    		int[] answers = new Fibonacci(10).getFib();
    		System.out.println(answer[9]);
    	}
     
    	public Fibonacci(int num)
    	{
    		this.fib = new int[num];
    		new Thread(this).start();
    		try
    		{
    			Thread.sleep(1);
    		}
    		catch (InterruptedException exception)
    		{}
    	}
     
    	public void run()
    	{
    		synchronized (this.fib)
    		{
    			this.fib[0] = 1;
    			this.fib[1] = 1;
    			for (int i = 2; i < this.fib.length; i++)
    			{
    				this.fib[i] = this.fib[i - 1] + this.fib[i - 2];
    			}
    		}
    	}
     
    	public int[] getFib()
    	{
    		return this.fib;
    	}
    }

Similar Threads

  1. Multi-dimension ArrayList example
    By helloworld922 in forum Java Programming Tutorials
    Replies: 1
    Last Post: February 3rd, 2010, 11:01 AM
  2. Convert Java Program to Java ME code
    By rinchan11 in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: October 5th, 2009, 11:18 PM
  3. Java Program Help
    By javakid93 in forum Java Theory & Questions
    Replies: 6
    Last Post: July 27th, 2009, 12:03 PM
  4. [SOLVED] displaying sum of harmonic series
    By sriraj.kundan in forum Algorithms & Recursion
    Replies: 2
    Last Post: June 16th, 2009, 12:42 AM
  5. [SOLVED] Problem in generating Fibonacci sequence in java
    By big_c in forum Algorithms & Recursion
    Replies: 2
    Last Post: April 24th, 2009, 09:52 AM