 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Importance of join() method in Java?
The join() Method
In Java, a join() is a final method of the Thread class, and it can be used to join the start of a thread's execution to the end of another thread's execution so that a thread will not start running until another thread has ended.
If the join() method is called on a thread instance, the currently running thread will be blocked until the thread instance has finished its execution. The join() method in Java is important in multithreading because it allows one thread to wait for the completion of another thread before continuing execution.
Syntax
Following is the syntax of the join() method in Java -
public final void join() throws InterruptedException
Note: This method does not accept any parameters.
Example 1
The following program uses the join() method to ensure that the main thread waits for thread t1 to finish before starting threads t2 and t3 -
public class JoinTest extends Thread { public void run() { for(int i=1; i <= 3; i++) { try { Thread.sleep(1000); } catch(Exception e) { System.out.println(e); } System.out.println("TutorialsPoint "+ i); } } public static void main(String args[]) { JoinTest t1 = new JoinTest(); JoinTest t2 = new JoinTest(); JoinTest t3 = new JoinTest(); t1.start(); try { t1.join(); // calling join() method } catch(Exception e) { System.out.println(e); } t2.start(); t3.start(); } }  Following is the output of the above program -
TutorialsPoint 1 TutorialsPoint 2 TutorialsPoint 3 TutorialsPoint 1 TutorialsPoint 1 TutorialsPoint 2 TutorialsPoint 2 TutorialsPoint 3 TutorialsPoint 3
Example 2
The following is another example of using the join() method in "multithreading". This program creates "three" threads: t1, t2, and t3. Each thread prints its name and step number twice with a "0.5" (500) second delay. The t1 thread starts first, and t1.join() is used to make the main thread wait until t1 finished -
public class JoinTest extends Thread { public void run() { for (int i = 1; i <= 2; i++) { try { Thread.sleep(500); } catch (Exception e) { System.out.println(e); } System.out.println(Thread.currentThread().getName() + " -step " + i); } } public static void main(String args[]) { JoinTest t1 = new JoinTest(); JoinTest t2 = new JoinTest(); JoinTest t3 = new JoinTest(); t1.setName("Thread-1"); t2.setName("Thread-2"); t3.setName("Thread-3"); t1.start(); try { t1.join(); // calling join() method } catch (Exception e) { System.out.println(e); } t2.start(); t3.start(); } }  Following is the output of the above program -
Thread-1 -step 1 Thread-1 -step 2 Thread-2 -step 1 Thread-3 -step 1 Thread-2 -step 2 Thread-3 -step 2
