How to Return a Value from Thread

26 Feb 2011 by

Thread implementation in Java is never easier if you do following my 3 steps strictly. However, there might be a lot of question regarding return value from a thread since run() method returns void. So I will show you how to get return value from a thread.
Just to review how to implement java thread, the 3 steps are:

  1. Extends Thread class or Implements Runnable interface
  2. Override run() method
  3. In main thread, initialize thread class and then call start() method

Be notice that the run() method has void as a return value meaning that we cannot have flexibility in return a value. So the only way to get a return value from thread is:

  1. Create a private global variable in thread class level
  2. Add getXXX() method to get the value from the variable defined in step 1
  3. Assign the return value in the run() method
  4. In the main thread, after calling start() method, you must call join() method. join() method tells the main thread to wait until the child thread finishes
  5. Call getXXX() to get the return value

The example below is to tell a thread to calculate Fibonacci value and return its result to main thread.

public class FibonacciThread extends Thread{
  //Step 1: create a variable to hold return value
  private long _fibonacciValue = 0;

  //Step 2: add getFibonacci() method
  public long getFibonacci(){
    return _fibonacciValue;

  //Step 3: Calculate and assign the value to a variable
  public void run(){
    _fibonacciValue = fibonacci(20);

  private long fibonacci(int n){
    if(n==0) return 0L;
    if(n==1) return 1L;

    return (fibonacci(n-1) + fibonacci(n-2));
} (main thread)

public class TestFibonacciThread {
  public static void main(String[] args){
    //Initialize thread class
    FibonacciThread fib = new FibonacciThread();
    //Start thread

    //Step 4: Call join() to wait until fib thread finishes
    }catch(InterruptedException ie){}

    //Step 5: call getFibonacci() to get the return value
    long fibonacci = fib.getFibonacci();

    System.out.println("The fibonacci value = " + fibonacci);

Note: If you did not mention join() method in the main thread, then the result will be 0. Why???

The answer is that, FibonacciThread is running parallel with main thread. Then the fib.getFibonacci() can be invoked before it is actually finished. So we must use join() method in this scenario to tell the main thread wait for FibonacciThread finishes.

Clear? If not, post your comment.

5 responses so far

  • Travis Halvorson says:

    Had ever step except join. I was reading stackoverflow forum for hours. The solutions there were messy at best. This is excatly what I thought the setup to be after reading java docs, but I didnt know to use join. This helps a lot since now my server and client can talk in thread and return the needed file without bogging down the ui.

  • xamarin says:

    Excellent tutorial , thanks a lot

  • Dasari Sai Ganesh says:

    is there any possibility to run multiple threads at a time and return all thread values

  • Henry says:

    This waits for the entire thread to finish before possible to return a value. But how can I return a value at the end of every iteration?

    I am reading from a socket. I wish to continue reading from the socket, even after I start using the data received.

    Please help

  • Henry says:

    Sorry. Forgot to select “Notify”

Leave a Reply

Time limit is exhausted. Please reload the CAPTCHA.