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).

3 comments

Popular posts from this blog

Yield Price Sri Lanka - Android Application

Manage SOAPAction of the Out Message

Remove duplicate XML elements using XSLT