Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Concurrency

Java thread lifecycle and states

Reference for the Java thread lifecycle: the six Thread.State values, what moves a thread between them, join() with and without timeouts, and interrupts.

A Java thread is always in exactly one of six states, reported by Thread.getState() as a value of the nested enum Thread.State. The states describe what the thread is doing from the JVM's point of view: not started yet, runnable, waiting for a monitor lock, waiting for another thread, waiting with a timeout, or finished.

Knowing the states explains many symptoms: a program that never exits (a non-daemon thread is still alive), a thread that seems frozen (BLOCKED on a lock, or WAITING for a signal that never comes), and why start() can only be called once. The state is a snapshot for monitoring and debugging; never base program logic on it, because it can change the moment you read it.

Java
Thread t = new Thread(() -> { });
Thread.State before = t.getState();   // NEW
t.start();
t.join();                             // wait until the thread has finished
Thread.State after = t.getState();    // TERMINATED

Try it

The helper in this example polls until the worker reaches the state we want to look at, so the output is the same on every run.

Java Walking through the six states

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java join(), timeouts and interrupts

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public enum Thread.State { NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED }

public Thread.State getState()
public final void join() throws InterruptedException
public final void join(long millis) throws InterruptedException
public final boolean join(java.time.Duration duration) throws InterruptedException
public final boolean isAlive()
State Meaning
NEW Created, start() not called yet.
RUNNABLE Running, or ready to run and waiting for a CPU. Threads blocked in native I/O also report RUNNABLE.
BLOCKED Waiting to acquire a monitor to enter or re-enter a synchronized block.
WAITING Waiting without a timeout: Object.wait(), Thread.join(), LockSupport.park(), and therefore most java.util.concurrent blocking calls.
TIMED_WAITING Waiting with a timeout: sleep, wait(ms), join(ms), parkNanos, tryLock(timeout).
TERMINATED run() has finished, normally or by an exception.
Method Description
join() Waits until the thread terminates. join(0) also means "forever".
join(Duration) Waits at most the given time and returns true if the thread has terminated (since Java 19).
isAlive() true from start() until termination.

Throws: join throws InterruptedException if the calling thread is interrupted while waiting. start() throws IllegalThreadStateException if the thread was already started.

Usage notes

  • Transitions. NEW goes to RUNNABLE through start(). From RUNNABLE a thread can enter BLOCKED, WAITING or TIMED_WAITING and come back, and it ends in TERMINATED. There is no way back to NEW.
  • BLOCKED is only about monitors. Waiting for a ReentrantLock shows up as WAITING, because explicit locks park the thread.
  • A state is a snapshot. Use it for monitoring, logging and tests, not for coordination. To wait for completion, use join(), a latch or a Future.
  • join() gives you visibility. Everything the joined thread did happens-before join() returning, so you can read its results without extra synchronization.
  • Interrupts are cooperative. A blocked sleep, wait or join throws InterruptedException and clears the flag. If you catch it and cannot rethrow, call Thread.currentThread().interrupt() to restore the flag.
  • The JVM exits when the last non-daemon thread ends. A forgotten non-daemon thread in WAITING keeps the program running forever.
  • Virtual threads report the same states. A virtual thread parked while waiting for I/O or a lock shows WAITING or TIMED_WAITING.