πŸŽ‰ Special Offer !    Code: GET300OFF    Flat β‚Ή300 OFF on every Java Course
Grab Deal πŸš€

join() Method in Java  


Introduction
  • The join() method in Java is used to pause the execution of the current thread until the thread on which join() is called completes its execution.
  • When a thread calls thread.join(), it waits for the specified thread to finish before continuing its own execution. This ensures sequential execution among threads when needed.
  • Real-World Examples where join() is used:
    • Task dependencies: When a task must complete before starting another task (e.g., loading data before processing it).
    • Sequential processing: Ensuring that multiple threads execute in a specific order, such as step-by-step computations.
    • Batch operations: Waiting for all threads in a batch to finish before aggregating results or moving to the next stage.
  • Java has provided the below join() methods in the Thread class:
    1. public final void join() throws InterruptedException
      • Waits indefinitely for the thread on which it is called to complete its execution.
      • It is an instance method, so it should be called on a specific thread object, e.g., t.join().
      • Throws InterruptedException if the waiting thread is interrupted while waiting.
    2. public final void join(long millis) throws InterruptedException
      • Waits for the thread to die for up to the specified number of milliseconds.
      • Useful for waiting a limited time instead of indefinitely.
    3. public final void join(long millis, int nanos) throws InterruptedException
      • Waits for the thread to die for the specified milliseconds plus nanoseconds.
      • Provides more precise waiting than the join(long millis) method.
  • Program:
    class MyThread extends Thread
    {
        @Override
        public void run()
        {
            // Child thread prints numbers from 1 to 5
            for (int i = 1; i <= 5; i++)
            {
                System.out.println(getName() + " is running: " + i);
            }
        }
    }
    
    public class JoinDemo
    {
        public static void main(String[] args) throws InterruptedException
        {
            System.out.println("Main thread started execution.");
    
            // Main thread creates another thread (child thread)
            MyThread t1 = new MyThread();
            t1.setName("Child-Thread");
            t1.start();
    
            // Main thread waits for child thread to finish
            // πŸ‘‰ Here the MAIN THREAD pauses until Child-Thread completes
            t1.join();
    
            // Main thread continues its execution (printing 1 to 5)
            for (int i = 1; i <= 5; i++)
            {
                System.out.println("Main thread is running: " + i);
            }
    
            System.out.println("Main thread finished execution.");
        }
    }
  • Important Key Points about join() method:
    • join() is an instance method of the Thread class.
    • It is used to pause the current thread until the thread on which join() is called completes its execution.
    • The join() method ensures sequential execution between threads when needed.
    • If the thread on which join() is called is already finished, the current thread continues immediately.
    • It can throw InterruptedException if the waiting thread is interrupted while waiting.
    • Variants of join() allow waiting for a specified time using join(long millis) or join(long millis, int nanos).
    • The current thread moves from Running state to Waiting state while waiting for the target thread to finish.
    • Unlike yield(), join() guarantees that the current thread will wait for the target thread to complete before continuing.