Java thread join() Example

Java thread join

join() method of Thread class is little bit confusing for a novice to understand straight away. I give in my own style with the aim "Java Made Simple". One thread creates another. See the following code.

public class Demo extends Thread
{
  public static void main(String args[])
  {
    Demo d1 = new Demo();
  }
}

Here, we call d1 as thread of Demo class as Demo extends Thread. d1 is created in main() method. For us, main() is known as a method, but it is internally a thread. That is, d1 thread is created in the context of main thread. "d1" is known as "child thread" and "main" is known as "parent thread". Here, parent thread happens to be main, but it can be any other thread also. See the next code.

See also this code.

class Test extends Thread {   }

public class Demo extends Thread
{
  static Test t1;
  public void run()
  {
    t1 = new Test();
  }      
  public static void main(String args[])
  {
     Demo d1 = new Demo();
     d1.start();
  }
}

In the above code, run() is implicitly called by d1. That is, d1 is executing run() method. In run(), t1 thread of Test is created. Now, "d1" is called as "parent thread" and "t1" is known as "child thread".

After understanding, parent and child threads, question is who has to die first?

Three options are given.

  1. Who ever finish its job, it can die without waiting for the other.
  2. Parent should not die until child dies even though parent finishes its job earlier.
  3. Child should not die until parent dies even though child finishes its job earlier.

A learner having OS knowledge and daemon processes etc., will definitely say the option b – that is, parent should not die until child dies. Even the parent finishes its job long before, should wait until its child dies. Child first and then only parent should die. This is correct way of using threads. Then, how to accomplish this in Java. Very simple to prove Java is simple, the answer is join() method defined in Thread class.

Program on Java thread join
class Test extends Thread {   }

public class Demo extends Thread
{
  static Test t1;	         // declare static to call from main() method
  public void run()              // or else, not required
  {
    t1 = new Test();
  }      
  public static void main(String args[])
  {
     Demo d1 = new Demo();
     d1.start();

     try
     {
       t1.join();
     }
     catch(InterruptedException e)
     {
       System.out.println(e.getMessage());
     }  
  }
}

t1.join() calling from main() method, instructs JVM not to allow d1 to die before t1 dies.

Now, tell me what is Java thread join () method? Let us generalize our concept. join() will not allow one thread to die before another. It is a communication between two threads like between a parent and child.

Still, you require more explanation with a good example where output difference comes if join() is not used, write in the comments box down.

e.getMessage()

The exception object message can be given in three ways. Explained in getMessage() & printStackTrace().

3 thoughts on “Java thread join() Example”

  1. import java.util.Date;
    class RunnableJob implements Runnable
    {
    public void run()
    {
    Thread thread=Thread.currentThread();
    System.out.println(“Runnable job is being run by”+thread.getName()+”at”+new Date());
    try
    {
    Thread.sleep(1000);
    }
    catch(InterruptedException e)
    {
    e.printStackTrace();
    }
    }
    }
    public class ThreadJoinExample3
    {
    public static void main(String[] args) throws InterruptedException
    {
    RunnableJob runnablejob=new RunnableJob();
    Thread thread1=new Thread(runnablejob,”T1″);
    Thread thread2=new Thread(runnablejob,”T2″);
    Thread thread3=new Thread(runnablejob,”T3″);
    Thread thread4=new Thread(runnablejob,”T4″);
    thread1.start();
    thread1.join();
    thread2.start();
    thread2.join();
    thread3.start();
    thread3.join();
    thread4.start();
    thread4.join();

    Thread thread5=new Thread(runnablejob,”T5″);
    Thread thread6=new Thread(runnablejob,”T6″);
    Thread thread7=new Thread(runnablejob,”T7″);
    Thread thread8=new Thread(runnablejob,”T8”);
    thread5.start();
    thread6.start();
    thread7.start();
    thread8.start();

    }
    }

    o/P:first four threads execute one by one after interval of 1 second. at fifth second last four threads executed at the same second.why is it so as there is sleep method used,so they should also hv 1 second gap?

  2. in the following code output 6 and 7 lines values 1and2 printed.why it was printed?
    class TestJoinMethod2 extends Thread{
    public void run(){
    for(int i=1;i<=5;i++){
    try{
    Thread.sleep(500);
    }catch(Exception e){System.out.println(e);}
    System.out.println(i);
    }
    }
    public static void main(String args[]){
    TestJoinMethod2 t1=new TestJoinMethod2();
    TestJoinMethod2 t2=new TestJoinMethod2();
    TestJoinMethod2 t3=new TestJoinMethod2();
    t1.start();
    try{
    t1.join(1500);
    }catch(Exception e){System.out.println(e);}

    t2.start();
    t3.start();
    }
    }

    Output:
    1
    2
    3
    1
    4
    1
    2
    5
    2
    3
    3
    4
    4
    5
    5

Leave a Comment

Your email address will not be published.