The join() method is used to hold the execution of currently running thread until the specified thread is dead(finished execution). In this tutorial we will discuss the purpose and use of join() method with examples.
Why we use join() method?
In normal circumstances we generally have more than one thread, thread scheduler schedules the threads, which does not guarantee the order of execution of threads.
For example lets have a look at the following code:
Without using join()
Here we have three threads th1, th2 and th3. Even though we have started the threads in a sequential manner the thread scheduler does not start and end them in the specified order. Everytime you run this code, you may get a different result each time. So the question is: How can we make sure that the threads executes in a particular order. The Answer is: By using join() method appropriately.
public class JoinExample2 { public static void main(String[] args) { Thread th1 = new Thread(new MyClass2(), "th1"); Thread th2 = new Thread(new MyClass2(), "th2"); Thread th3 = new Thread(new MyClass2(), "th3"); th1.start(); th2.start(); th3.start(); } } class MyClass2 implements Runnable{ @Override public void run() { Thread t = Thread.currentThread(); System.out.println("Thread started: "+t.getName()); try { Thread.sleep(4000); } catch (InterruptedException ie) { ie.printStackTrace(); } System.out.println("Thread ended: "+t.getName()); } }
Output:
Thread started: th1 Thread started: th3 Thread started: th2 Thread ended: th1 Thread ended: th3 Thread ended: th2
Lets have a look at the another code where we are using the join() method.
The same example with join()
Lets say our requirement is to execute them in the order of first, second and third. We can do so by using join() method appropriately.
public class JoinExample { public static void main(String[] args) { Thread th1 = new Thread(new MyClass(), "th1"); Thread th2 = new Thread(new MyClass(), "th2"); Thread th3 = new Thread(new MyClass(), "th3"); // Start first thread immediately th1.start(); /* Start second thread(th2) once first thread(th1) * is dead */ try { th1.join(); } catch (InterruptedException ie) { ie.printStackTrace(); } th2.start(); /* Start third thread(th3) once second thread(th2) * is dead */ try { th2.join(); } catch (InterruptedException ie) { ie.printStackTrace(); } th3.start(); // Displaying a message once third thread is dead try { th3.join(); } catch (InterruptedException ie) { ie.printStackTrace(); } System.out.println("All three threads have finished execution"); } } class MyClass implements Runnable{ @Override public void run() { Thread t = Thread.currentThread(); System.out.println("Thread started: "+t.getName()); try { Thread.sleep(4000); } catch (InterruptedException ie) { ie.printStackTrace(); } System.out.println("Thread ended: "+t.getName()); } }
Output:
Thread started: th1 Thread ended: th1 Thread started: th2 Thread ended: th2 Thread started: th3 Thread ended: th3 All three threads have finished execution
In this example we have used the join() method in such a way that our threads execute in the specified order.
AKshay says
Thank you very much!!! It was very helpful…
nishant says
Nice example,very helpful in understanding concept about join() method.