Thread join() method

method-void join()
class-java.lang.Thread
non-static
throws Interrupted Exception

The method join can be used to make one thread joined into the end of another thread. This is a guranteed method.

Examine the following code and see the result.

class Demo{
    public static void main(String args[]){

        A a=new A();
        B b=new B();

        a.start();
        b.start();

        }
}

class A extends Thread{
    public void run(){

        System.out.println("Thread A started");
        System.out.println("Thread A running");
        System.out.println("Thread A ending");
    }
}


class B extends Thread{
    public void run() {

        System.out.println("Thread B started");
        System.out.println("Thread B running");
        System.out.println("Thread B ending");

    }
}

Result can not be guessed, as two threads execute separately.


Imagine you want to execute Thread-B after the complete execution of Thread-A. (First Thread A, then Thread B) I am goin to show two comman ways to do that.

Method (1)
  • You can call join method of Thread-A from the Thread-B.
  • But make sure a thread A need to started, before calling join method.
  • Then start the Thread B.
class Demo{
    public static void main(String args[]){


        B b=new B();

        b.starting();

        }
}

class A extends Thread{
    public void run(){

        System.out.println("Thread A started");
        System.out.println("Thread A running");
        System.out.println("Thread A ending");
    }

    public void starting(){
            this.start();

        }

}


class B extends Thread{
    public void run() {

        System.out.println("Thread B started");
        System.out.println("Thread B running");
        System.out.println("Thread B ending");

    }

    public void starting(){
            A a=new A();
            a.starting();
            try{
                a.join();
            }catch(InterruptedException e){

                }

            this.start();


        }
}


Result can be gurantedd to be as follows.


Method (2) Join thread-A to another third thread , which executes both A, B threads. Here, Demo class starts A and B threads. Which means main thrad of Demo class. So first start the Thread A. Then join main thread or the current into thread thread A. Start Thread B.

class Demo{
    public static void main(String args[]){

        A a=new A();
        a.start();

        try{
            a.join();// Joins the main thread of class Demo which is the current thread into Thread A.
                       //The rest of the code below this point will continue after terminating the Thread A.
        }catch(InterruptedException e){

            }

        System.out.println(a.isAlive());//Thread A is terminated and returns false here

        B b=new B();

        b.start();

        }
}

class A extends Thread{
    public void run(){

        System.out.println("Thread A started");
        System.out.println("Thread A running");
        System.out.println("Thread A ending");
    }


}


class B extends Thread{
    public void run() {

        System.out.println("Thread B started");
        System.out.println("Thread B running");
        System.out.println("Thread B ending");

    }


}
Result can be gurantedd to be as follows,.



One important thing to know is that when we call,
a.join();
The current executing thread is joined onto the end of the the thread represented by 'a'. So the rest of the code beyond this point will execute only after 'a' is teminated (Finished executing its run method).

Comments

Threading is most confusing topic of Java and most asked topic also and confusion only increased with new concurrent package. thanks for your tips quite useful for beginners. I have also shared my experience as Top 10 multi-threading interview questions , do let me know how do you find it.
Anonymous said…
Great explanation. I always wondered what the join() as actually joining to, you help make it very clear that its the main thread in the Demo class which is doing the control of the flow.
Anonymous said…
even our trainer wasnt able to explain this topic...he sucks!!
i want to kill him...just a headshot!! :D
could you please borrow me a gun??? :P

Popular posts from this blog

Secure Spring Boot REST API using Basic Authentication

Building a RESTFul Service using Spring Boot

Integrating Swagger with Spring Boot REST API