SrcForge

Lesson 45

Thread join() Method

join() makes the current thread wait until another thread finishes.

Java Track

Save progress as you learn.

61 lessons

Lesson content

join() pauses the current thread until another thread terminates.

Definition List

  • join(): Waits for another thread to finish execution.
  • Waiting State: Thread suspended until another finishes.
  • InterruptedException: Thrown if waiting thread is interrupted.
  • Terminated State: Final state after thread completes.

join() Variants

// Wait indefinitely
thread.join();

// Wait up to 2 seconds
thread.join(2000);

// Wait with nanoseconds
thread.join(2000, 500);

Thread State During join()

Runnable

Waiting (join)

Runnable

Example 01: Java Thread join() Method

package com.java.Multi_threading;

import java.io.*;

class ThreadJoin extends Thread {

    public void run() {
        for (int j = 0; j < 2; j++) {
            try {
                Thread.sleep(300);
                System.out.println("The current thread name is: " + Thread.currentThread().getName());
            } catch (Exception e) {
                System.out.println("The exception has been caught: " + e);
            }
            System.out.println(j);
        }
    }
}

public class ThreadJoinExample {

    public static void main(String argvs[]) {

        ThreadJoin th1 = new ThreadJoin();
        ThreadJoin th2 = new ThreadJoin();
        ThreadJoin th3 = new ThreadJoin();

        th1.start();

        try {
            System.out.println("The current thread name is: " + Thread.currentThread().getName());
            th1.join();
        } catch (Exception e) {
            System.out.println("The exception has been caught " + e);
        }

        th2.start();

        try {
            System.out.println("The current thread name is: " + Thread.currentThread().getName());
            th2.join();
        } catch (Exception e) {
            System.out.println("The exception has been caught " + e);
        }

        th3.start();
    }
}

Output

The current thread name is: main
The current thread name is: Thread-0
0
The current thread name is: Thread-0
1

The current thread name is: main
The current thread name is: Thread-1
0
The current thread name is: Thread-1
1

The current thread name is: Thread-2
0
The current thread name is: Thread-2
1

Handling InterruptedException

try {
    thread.join();
} catch (InterruptedException e) {
    e.printStackTrace();
}

join() vs sleep()

  • join() — waits for thread completion.
  • sleep() — waits for a time duration.
  • join() coordinates threads; sleep() does not.
  • Both throw InterruptedException.

Best Practices

  • Use join() only when thread dependency exists.
  • Always handle InterruptedException.
  • Prefer timed join() to avoid indefinite blocking.
  • Don't overuse; it reduces concurrency.

FAQ

1. What does join() do in Java?

Waits for another thread to finish before continuing.

2. What happens when join() is called?

Current thread enters Waiting state until target terminates.

3. Does join() throw an exception?

Yes. Throws InterruptedException if interrupted while waiting.

join() waits for another thread to finish.
Calling thread enters Waiting state.
Three variants: join(), join(ms), join(ms, ns).
Always catch InterruptedException.
Enforces sequential execution between threads.
Use timed join() to prevent indefinite blocking.
join() vs sleep(): coordination vs delay.

Where Java is used

Java in real-world development

join()

Waits indefinitely for the target thread to terminate.

join(long ms)

Waits up to the given milliseconds for thread completion.

join(long ms, int ns)

Adds nanosecond precision to the wait timeout.

Waiting State

Thread pauses at join() and resumes after target finishes.

InterruptedException

Thrown if waiting thread is interrupted. Must be caught.

join() vs sleep()

join() waits for a thread; sleep() waits for time.