Thread Lifecycle & State Transitions
1Concept
A Java thread transitions across 6 states defined in `java.lang.Thread.State`: 1. `NEW` (Created, not yet started); 2. `RUNNABLE` (Executing in JVM or waiting for OS CPU scheduling); 3. `BLOCKED` (Waiting to acquire an intrinsic monitor lock); 4. `WAITING` (Waiting indefinitely via `wait()` or `join()`); 5. `TIMED_WAITING` (Waiting with timeout via `sleep(ms)`); 6. `TERMINATED` (Completed).
2Architecture Diagram
NEW --(.start())--> RUNNABLE <-----------------------------------+
| |
+-------------+-------------+ |
v v |
BLOCKED WAITING / TIMED_WAITING |
(Waiting for monitor) (wait(), sleep(), join()) |
| | |
+---------------------------+-----------------------------+
|
v
TERMINATED3Code Example
Core Java
public class ThreadStateInspectionDemo {
public static void main(String[] args) throws InterruptedException {
Thread worker = new Thread(() -> {
try {
Thread.sleep(100);
} catch (InterruptedException ignored) {}
});
System.out.println("State after instantiation: " + worker.getState());
worker.start();
System.out.println("State immediately after start: " + worker.getState());
Thread.sleep(20);
System.out.println("State while sleeping: " + worker.getState());
worker.join();
System.out.println("State after completion: " + worker.getState());
}
}4Expected Output
State after instantiation: NEW State immediately after start: RUNNABLE State while sleeping: TIMED_WAITING State after completion: TERMINATED
5Key Takeaways
- ✓A terminated thread cannot be restarted; calling `.start()` on a dead thread throws `IllegalThreadStateException`.
- ✓`Thread.sleep()` does NOT release acquired monitor locks.
- ✓Thread dumps (`jstack <pid>`) report exact states of all threads for deadlock diagnostics.