Thread join() Method Example Java

It is very confusing for a novice, the difference between join(), yield(), sleep(), suspend() and wait() methods in Java thread management. Here we go with join() method. To understand easily, I take the example of parent and child threads.

Parent and Child Threads

One thread creates another thread. The thread which creates is known as parent thread and the one getting created is known as child thread. The question here is who should die first. Is it first parent then child or other way or who ever finish its job it may die? Any rule exists? Here, I give an example from our daily life.

A mother walks with her baby child of just 4 years age to nearby vegetable market by catching baby’s hand. While bargaining with the vendor, she forgot the child and lost her hand. The child moves while crying. Get the same child and leave at the same spot. Does she move in the same direction while crying or in other direction? She moves in a different direction. If the child does not leave mummy’s hand, the direction is fixed – from house to market and from market to house. Here, the child is called as orphan child. The orphan child moves eccentrically in different direction each time when left alone. So the child should be with mummy always. If mummy dies first, the child becomes orphan. So, the child should die first and then the parent.

This above example applies to our threads also. If parent thread dies first, the child thread becomes orphan thread. Orphan thread moves eccentrically when called different times. If a parent thread comes to die (as its assigned job is finished) before its child dies, it should not be allowed. It is good coding in multithreading. How to do the job? The answer is join() method. It is an instance method (should be called with a thread object) defined in Thread class.

Method signature of overloaded Thread join() defined in Thread class
  • public final void join() throws InterruptedException;
  • public final synchronized void join(long milliseconds) throws InterruptedException;
  • public final synchronized void join(long milliseconds, int nanoseconds) throws InterruptedException;

The Thread join() method is overloaded which sometimes takes time period also. Like sleep(), join() also throws a checked exception – InterruptedException.

Last two join() methods are not much used, where you specify the time period to wait, because join() method timing is OS dependent where join() will exactly may not wait the time period you specify.

Calling join() method on a thread, until the thread dies, its parent thread is not allowed to die by the JVM even it completes its job and ready for die – a communication between two threads.

Simple Thread join() Method Example to know which is parent and which is child.
class Test extends Thread
{
  public void run()
  {
    for(int i=0; i<3; i++)
    {
      System.out.println("Test: " + i);
    }
  }
}

public class Demo extends Thread
{
  public void run()
  {
    Test t1 = new Test();
    t1.start();

    for(int i=0; i<3; i++)
    {
      System.out.println("Demo: " + i);
    }
    try 
    {
      t1.join();
    }
    catch(InterruptedException e)  { e.printStackTrace(); }
  }
  public static void main(String args[])
  {
    Demo d1 = new Demo();
    d1.start();
  }
}

In the above code, two threads exist - d1 of Demo class and t1 of Test class. In the run() method of d1, t1 of Test is created. That is, t1 is created in the context of d1. d1 is called as parent thread and t1 is called as child thread. Calling of t1.join(), the JVM does not allow d1 to die before the death of t1. Calling join() method is always a good programming paradigm.

One parent thread can create number of child threads (in the above, only one t1 is created and started) and should wait until all the child threads die (complete their execution).

Thread join() - Some more ways of explaining join() method
  1. The join() method permits one thread to wait until the completion of another thread.
  2. The join() method waits (does not die) for the death of another thread.
  3. A thread can be in waiting state until other thread terminates its execution.

5 thoughts on “Thread join() Method Example Java”

  1. Hello sir, I have been following you since the last 2 years. Sir I have done my b. Tech this year (2015). Sir I have not placed anywhere and now I want to clear the ocjp exam. Sir please guide me how to clear the exam. Please reply sir.

      1. Hello sir

        Sir actually I don’t have 60% in my schooling that’s why no company prefers me but sir I am really good in my technical specially in java. So please guide me what to do in future n how can I implement my knowledge with a good company.

        1. Apply for small companies offering small salary of even below 10 thousand. Once you gain an experience of 3 years, you will be king and no body bothers of your school or even college marks. Apply anywhere after three years for a higher salary, you will be absorbed.

          1. Thank you sir. And one more question Sir, what would be the job opportunities if I could creak oca / ocp only….

Leave a Comment

Your email address will not be published.