Thread Lifecycle
The Six Thread States
State | Meaning |
|---|---|
NEW | Thread object created, but start() hasn't been called yet |
RUNNABLE | Eligible to run — either currently executing or waiting for CPU time from the scheduler |
BLOCKED | Waiting to acquire a lock (synchronized block/method) held by another thread |
WAITING | Waiting indefinitely for another thread's action, e.g. after calling wait() or join() with no timeout |
TIMED_WAITING | Waiting for a bounded amount of time, e.g. Thread.sleep(ms) or join(ms) |
TERMINATED | run() has completed — the thread has finished executing and cannot be restarted |
Observing state transitions
public class LifecycleDemo {
public static void main(String[] args) throws InterruptedException {
Thread worker = new Thread(() -> {
try {
Thread.sleep(500);
} catch (InterruptedException ignored) { }
});
System.out.println(worker.getState()); // NEW
worker.start();
System.out.println(worker.getState()); // RUNNABLE
Thread.sleep(100);
System.out.println(worker.getState()); // TIMED_WAITING (inside sleep)
worker.join(); // wait for it to finish
System.out.println(worker.getState()); // TERMINATED
}
}NEW RUNNABLE TIMED_WAITING TERMINATED
Thread.sleep() — Pausing Execution
join() — Waiting for Another Thread to Finish
join() ensures work finishes before continuing
Thread worker = new Thread(() -> {
System.out.println("Worker starting");
try {
Thread.sleep(300);
} catch (InterruptedException ignored) { }
System.out.println("Worker done");
});
worker.start();
worker.join(); // main thread waits here until worker terminates
System.out.println("Main thread sees worker has finished");Worker starting Worker done Main thread sees worker has finished
Virtual Threads — Java 21
Thread.State defines six states: NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED
Thread.sleep() pauses the current thread for a bounded time without releasing locks
join() blocks the calling thread until another thread terminates
Java 21's virtual threads follow the same lifecycle but scale to vastly more concurrent threads