Results 1 to 6 of 6
  1. #1
    pooshi is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Multiple Thread Fibonacci Code

    Hello,

    I am new to Java and this site. I have a program that creates two threads but it is not doing what I expect. This is the code I have so far.
    The out put in the console is: Usage: Fib <num> and not the Fibonacci numbers. I would use the Eclipse Debugger but I am not yet acquainted with it yet.
    Any thoughts?

    Java Code:
    package org.driver;
    /**
     * Generates the Fibonacci sequence in a separate thread. 
     */
    
    class FibThread implements Runnable
    {
    	private int[] fibNums;
    
    	public FibThread(int[] fibNums) 
    	{
    		this.fibNums = fibNums;
    	}
    	
    	public void run() 
    	{
    		int length = fibNums.length;
    
    		if (length == 0)
    			return;
    		else if (length == 1)
    			fibNums[0] = 0;
    		else if (length == 2) 
    		{
    			fibNums[0] = 0;
    			fibNums[1] = 1;
    		}
    		else 
    		{ // length > 2
    			fibNums[0] = 0;
    			fibNums[1] = 1;
    
    			for (int i = 2; i < length; i++)
    				fibNums[i] = fibNums[i-1] + fibNums[i-2];
    		}
    	}
    }
    
    public class Driver
    {
    	public static void main(String args[]) 
    	{
    		if (args.length == 0) 
    		{
    			System.out.println("Usage: Fib <num> ");
    			System.exit(0);
    		}
    		else if (Integer.parseInt(args[0]) < 0) 
    		{
    			System.out.println("Sequence size must be >= 0\n");
    			System.exit(0);
    		}
    		else 
    		{
    			int[] sequence = new int[Integer.parseInt(args[0])];
    			Thread worker = new Thread(new FibThread(sequence));
    			worker.start();
    
    			try 
    			{
    				worker.join();
    			} catch (InterruptedException ie) { }
    
    			for (int i = 0; i < sequence.length; i++)
    				System.out.println(sequence[i]);
    		}
    	}
    }
    Thanks for your help,
    Pooshi

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,235
    Rep Power
    19

    Default Re: Multiple Thread Fibonacci Code

    Quote Originally Posted by pooshi View Post
    I have a program that creates two threads but it is not doing what I expect.
    You need to tell us what you expect, and what the program is doing instead.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    pooshi is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Re: Multiple Thread Fibonacci Code

    Sorry,

    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. When the thread finishes execution, the parent thread will output the sequence generated by the child thread.

    Thanks,
    Pooshi

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,235
    Rep Power
    19

    Default Re: Multiple Thread Fibonacci Code

    Quote Originally Posted by pooshi View Post
    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. When the thread finishes execution, the parent thread will output the sequence generated by the child thread.
    Is that what you expect, or what the program is doing?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    pooshi is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Re: Multiple Thread Fibonacci Code

    This is what I expect:
    1) The user will enter the number of Fibonacci number, via the command line.
    What the program is doing:
    1) Creating a separate child thread that will generate the Fibonacci numbers that were entered by the user.
    2) The parent thread will output the sequence generated by the child thread.

    Thanks,
    Pooshi

  6. #6
    pooshi is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

Similar Threads

  1. Thread running multiple flashing buttons.
    By tib0lt in forum Threads and Synchronization
    Replies: 3
    Last Post: 09-26-2012, 07:05 PM
  2. Sftp thread for multiple site
    By verWire3619 in forum Threads and Synchronization
    Replies: 1
    Last Post: 08-06-2011, 06:04 AM
  3. question regarding thread code
    By afx111 in forum New To Java
    Replies: 1
    Last Post: 02-01-2011, 07:39 AM
  4. while running multiple thread repaint isnt being called....
    By raptor in forum Threads and Synchronization
    Replies: 4
    Last Post: 08-25-2010, 11:47 PM
  5. Code executes after thread
    By bubbless in forum New To Java
    Replies: 5
    Last Post: 11-27-2009, 05:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •