0
0
JavaHow-ToBeginner · 3 min read

How to Use join() Method in Java Threads

In Java, use the join() method on a thread object to pause the current thread until the target thread finishes execution. This ensures that the current thread waits for the other thread to complete before continuing.
📐

Syntax

The join() method is called on a Thread object to make the current thread wait until that thread finishes.

  • thread.join(); - Waits indefinitely until thread completes.
  • thread.join(milliseconds); - Waits up to the specified milliseconds.
java
Thread thread = new Thread(() -> {
    // thread task
});
thread.start();
try {
    thread.join(); // current thread waits here until 'thread' finishes
} catch (InterruptedException e) {
    e.printStackTrace();
}
💻

Example

This example shows two threads: the main thread waits for a worker thread to finish using join(). The worker thread prints messages with delays, and the main thread waits before printing its final message.

java
public class JoinExample {
    public static void main(String[] args) {
        Thread worker = new Thread(() -> {
            try {
                for (int i = 1; i <= 3; i++) {
                    System.out.println("Worker thread: step " + i);
                    Thread.sleep(500); // simulate work
                }
            } catch (InterruptedException e) {
                System.out.println("Worker interrupted");
            }
        });

        worker.start();

        try {
            worker.join(); // main waits for worker to finish
        } catch (InterruptedException e) {
            System.out.println("Main thread interrupted while waiting");
        }

        System.out.println("Main thread resumes after worker finishes");
    }
}
Output
Worker thread: step 1 Worker thread: step 2 Worker thread: step 3 Main thread resumes after worker finishes
⚠️

Common Pitfalls

Common mistakes when using join() include:

  • Not handling InterruptedException, which can cause your program to crash.
  • Calling join() on a thread that was never started, which causes the current thread to wait forever.
  • Using join() without understanding it blocks the current thread, potentially freezing your program if misused.
java
Thread t = new Thread(() -> System.out.println("Running"));
// t.join(); // Wrong: join called before start, will wait forever

t.start();
try {
    t.join(); // Correct: start first, then join
} catch (InterruptedException e) {
    e.printStackTrace();
}
📊

Quick Reference

MethodDescription
join()Waits indefinitely for the thread to finish
join(long millis)Waits up to millis milliseconds for the thread to finish
join(long millis, int nanos)Waits up to millis milliseconds plus nanos nanoseconds
Throws InterruptedExceptionMust handle or declare this exception when calling join()

Key Takeaways

Use thread.join() to pause the current thread until another thread finishes.
Always start a thread before calling join() to avoid waiting forever.
Handle InterruptedException when using join() to keep your program stable.
join() blocks the current thread, so use it carefully to avoid freezing your app.
You can specify a timeout with join(milliseconds) to wait only a limited time.