Home » Java programming language

Java Thread Class final void join() method with Example

Java Thread Class final void join() method: Here, we are going to learn about the final void join() method of Thread class with its syntax and example.
Submitted by Preeti Jain, on July 26, 2019

Thread Class final void join()

  • This method is available in package java.lang.Thread.join().
  • join() method is applicable when a thread wants to wait until completing some other thread then we should go for join() method of Thread class.
  • This method is not static so we cannot access this method with the class name too.
  • This method is final we can't override this method in child class.
  • The return type of this method is void so it does not return anything.
  • This method throws an InterruptedException so it is needed to handle exception either by try-catch or throws otherwise we will get a compile-time error.

For example: We have three threads [t1 – PreparedExamPaper ] ,[t2 – PrintingExamPaper],[t3- DistributingExamPaper] so will see what will happen.

Let suppose if a thread t1 executes, t2.join(), then thread t1 will entered into waiting for the state until t2 completes once t2 completes then t1 will continue its execution.

Similarly, If a thread t2 executes, t3.join() then thread t2 will entered into waiting for the state until t3 completes once t3 completes, then t2 will continue its execution.

Syntax:

    final void join(){
    }

Parameter(s):

When we write t2.join(), so this line means currently executing thread will stop its execution and that thread will wait() for t2 completion.

Return value:

The return type of this method is void, it does not return anything.

Java program to demonstrate example of join() method

/*  We will use Thread class methods so we are importing 
    the package but it is not mandated because 
    it is imported by default
*/

import java.lang.Thread;

class MyThread extends Thread {
    //Override run() method of Thread class 
    public void run() {
        for (int i = 0; i < 5; ++i)
            System.out.println("We are in MyThread");
        try {
            Thread.sleep(500);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
}

class MainThread {
    public static void main(String[] args) throws Exception {
        MyThread mt = new MyThread();
        mt.start();

        /* Note -1*/
        mt.join();

        for (int j = 0; j < 5; ++j)
            System.out.println("We are in MainThread");
    }
}

Note 1: If we comment /*mt.join()*/ in the above program then both threads will execute simultaneously we can't expect exact execution order and then we can't expect exact output.

Output

E:\Programs>javac MainThread.java

E:\Programs>java MainThread
We are in MyThread
We are in MyThread
We are in MyThread
We are in MyThread
We are in MyThread
We are in MainThread
We are in MainThread
We are in MainThread
We are in MainThread
We are in MainThread



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.