Quiz: Thread.join() and isAlive() in Java

Total: 6 questions

1. 

What does Thread.join() do, and what happens to the thread that calls it?

join() is an instance (non-static) method of Thread: calling t.join() pauses the current thread until thread t terminates. The typical use case is starting background work and waiting for its result:

worker.start();
worker.join(); // main waits until worker terminates

The waiting thread goes into the WAITING state, or TIMED_WAITING with join(millis), and uses no CPU. It resumes when t terminates, when the timeout expires, or when another thread calls interrupt() on the waiting thread. That's why the method throws the checked InterruptedException.

Unlike sleep(), it doesn't wait for a fixed time but for an event - the termination of a specific thread - so it may return after a millisecond or after an hour.

2. 

What overloads does Thread.join() have, and what does join(0) mean?

There are four overloads, and all of them throw InterruptedException:

  • join() - waits for the thread to terminate with no time limit; same as join(0).
  • join(long millis) - waits at most millis ms. join(0) means "wait forever", not "don't wait"; a negative value throws IllegalArgumentException.
  • join(long millis, int nanos) - the same plus nanoseconds; nanos outside 0-999999 throws IllegalArgumentException.
  • boolean join(Duration duration) (Java 19+) - returns true if the thread terminated. For a thread that hasn't been started it throws IllegalThreadStateException; with a zero or negative duration it doesn't wait, it only checks whether the thread has terminated.

The timed overloads return early if the thread finishes, and when the timeout expires the thread may still be alive - check it with isAlive() or the result of join(Duration). The precision of the wait depends on the OS and the system timer.

3. 

What does Thread.isAlive() return, and when does it return false?

isAlive() returns true if the thread has been started with start() and hasn't finished yet. false means one of two things: the thread was created but not started, or it has already completed.

isAlive() can't tell these two cases apart. If you need the distinction, use getState(): it returns NEW for a thread that hasn't been started and TERMINATED for one that has finished.

The result is a snapshot at the moment of the call: a tiny task may finish before the code reaches a check right after start(). A typical use is finding out after join(millis) whether the thread terminated or the wait simply timed out.

4. 

How do you properly wait for one or several threads to finish: join() or polling isAlive() in a loop?

Use join(). A loop like while (t.isAlive()) {} spins idly and keeps a whole CPU core busy (busy-waiting), while join() puts the waiting thread to sleep until t terminates and also gives you the visibility guarantee (happens-before). Polling with Thread.sleep(100) loads the CPU less, but reacts up to 100 ms late and gains nothing. Polling isAlive() makes sense only where you can't block, for example in a UI render loop.

To wait for several threads, start all of them first, then call join() on each one in a loop: the total wait equals the time of the longest thread. If you call start() and join() in the same loop, the threads run strictly one after another.

When there are many threads, or you aren't working with Thread directly, CountDownLatch or ExecutorService.invokeAll() are more convenient.

5. 

What happens if a thread calls join() on itself, or two threads call join() on each other?

In both cases the threads hang forever.

System.out.println("Waiting for myself...");
Thread.currentThread().join(); // waits forever
System.out.println("Never gets here");

The main thread waits for the main thread to terminate, and it can't terminate because it's waiting. The program prints one line and hangs until you kill it; there is no exception and no compiler warning.

If two threads call join() on each other, each one waits for the other - a special case of a deadlock.

6. 

Why call join() on a virtual thread in Java 21?

Virtual threads (Java 21+) are always daemon threads, and the JVM doesn't wait for them to finish. Without join(), main may exit before the virtual thread gets to do anything:

Thread vt = Thread.ofVirtual().start(() -> System.out.println("virtual"));
vt.join(); // without this line the output may never appear

join() itself works the same way as with platform threads. When a virtual thread calls it, the virtual thread is unmounted from its carrier thread for the duration of the wait, and the carrier goes on running other virtual threads, so thousands of waiting virtual threads don't tie up thousands of OS threads.

Page 1 of 1