Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Thread.join — Wait for a thread to terminate

Use `Thread.join` to wait for a thread to terminate. Learn which overloads confirm termination and when the waiting thread can safely read the worker’s result.

Version: Java SE 21

Syntax

public final void join() throws InterruptedException
public final void join(long millis) throws InterruptedException
public final void join(long millis, int nanos) throws InterruptedException
public final boolean join(java.time.Duration duration) throws InterruptedException

Thread.join is an instance method of java.lang.Thread. In worker.join(), the calling thread waits for worker to terminate. A join does not start or stop the worker. Call start() first; the no-argument join() has no timeout.

join(long millis) takes a timeout in milliseconds, and join(long millis, int nanos) lets you add nanoseconds. Negative millis or nanos outside 0 through 999,999 causes IllegalArgumentException. When all supplied arguments are zero, there is no timeout. Both overloads return void: a timed return alone does not confirm termination. For a thread you have started, isAlive() == false after the call confirms that it has terminated by that check. All three void overloads return immediately for a thread that has not been started.

join(java.time.Duration), available since Java 19, returns true if the thread has terminated or false if it has not. A zero or negative duration checks without waiting. On a thread that has not been started, this overload throws IllegalThreadStateException.

The worker’s actions happen-before another thread detects its termination. Once termination is confirmed, the caller can read the worker’s result even from a non-volatile field, provided there are no conflicting writes. If several workers contribute to a result, join each before reading it. A join does not fix races between workers or prove that a task ran without errors.

When a join throws InterruptedException, it clears the interrupt status of the calling thread, not the worker. Propagate the exception or, if you catch it to exit the current operation, restore the status with Thread.currentThread().interrupt(). Do not treat interruption as confirmation of termination.

Examples

import java.time.Duration;
import java.util.concurrent.CountDownLatch;

public class JoinDemo {
    static final class Result {
        int value;
    }

    public static void main(String[] args) throws InterruptedException {
        Result result = new Result();
        CountDownLatch release = new CountDownLatch(1);
        Thread worker = new Thread(() -> {
            try {
                release.await();
                result.value = 42;
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        });
        Duration timeout = Duration.ofMillis(10);
        worker.start();

        boolean before;
        try {
            before = worker.join(timeout);
        } finally {
            release.countDown();
        }
        worker.join();
        boolean after = worker.join(timeout);

        System.out.println("before = " + before);
        System.out.println("after = " + after);
        System.out.println("value = " + result.value);
    }
}

Result

before = false
after = true
value = 42

The latch prevents the worker from terminating during the first timed attempt, so it returns `false`. After the latch is released, `join()` confirms termination: the second timed attempt returns `true`, and `main` reads `42`.

Related lessons

  • Java · Lesson 49 · Intermediate · Version: 2
  • Java · Lesson 52 · Intermediate · Version: 2
  • Java · Lesson 53 · Intermediate · Version: 2
  • Java · Lesson 58 · Intermediate · Version: 2

Sources