Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 185501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:28:51+00:00 2026-05-11T15:28:51+00:00

I’m relatively new in the Java world and I have a problem which I

  • 0

I’m relatively new in the Java world and I have a problem which I don’t understand.

I have a Class (to get the fibonacci row):

class Fib {     public static int f(int x){         if ( x < 2 )             return 1;                else              return f(x-1)+ f(x-2);           } } 

The task now is to start f(x-1) and f(x-2) each in a separate Thread. One time with implementing the Thread class and the other with implementing Runnable. As you probably know, it’s an exercise from my prof.

I know how to start a Thread in Java and I know how this whole Thread thing theoretically works, but I can’t find a solution for starting separate Threads in this recursive function.

What has to be done in the run function?

Probably

public void run(){ //int foo=start f(this.x-1)     //int bar=start f(this.x-2)       //return foo+bar? } 

And how can I paste x in my runnable function? Is x passed into the object at creation?

Class Fib ...{   int x;   public ... run ...    public ... f(x)....  } 

in the main method

(new Fib(x)).start(); 

Or am I on a totally wrong path?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T15:28:52+00:00Added an answer on May 11, 2026 at 3:28 pm

    For this to work, you need 1) a way to pass the number into the new thread, 2) to start the thread, 3) to wait for the thread to finish, and 4) a way to get the result back from the thread.

    You can pass in the number through the constructor. You can have a public data member called ‘answer’ to contain the result of the computation. Starting the thread can be done with the start() method, and the join() method waits for the thread to complete.

    The following example demonstrates this. That should be a good starting point; from here you can abstract away some of the messiness to get a better API as desired.

    public class Fib extends Thread {     private int x;     public int answer;      public Fib(int x) {         this.x = x;     }      public void run() {         if( x <= 2 )             answer = 1;         else {             try {                 Fib f1 = new Fib(x-1);                 Fib f2 = new Fib(x-2);                 f1.start();                 f2.start();                 f1.join();                 f2.join();                 answer = f1.answer + f2.answer;             }             catch(InterruptedException ex) { }         }     }      public static void main(String[] args)         throws Exception     {         try {             Fib f = new Fib( Integer.parseInt(args[0]) );             f.start();             f.join();             System.out.println(f.answer);         }         catch(Exception e) {             System.out.println('usage: java Fib NUMBER');         }     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 225k
  • Answers 225k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Can you use the official Source Distribution of Python? It… May 13, 2026 at 12:55 am
  • Editorial Team
    Editorial Team added an answer On my IIS7.5 config, found at C:\Windows\System32\inetsrv\config\applicationHost.config there is this… May 13, 2026 at 12:55 am
  • Editorial Team
    Editorial Team added an answer Use [^A-Za-z0-9]. Note: removed the space since that is not… May 13, 2026 at 12:55 am

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
In order to apply a triggered animation to all ToolTip s in my app,
I have a French site that I want to parse, but am running into

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.