A Java thread goes through multiple states from creation to termination.
These states are defined in the Thread.State
enum.
Lifecycle Diagram (Simplified)
NEW → RUNNABLE → RUNNING → (WAITING / BLOCKED / TIMED_WAITING) → TERMINATED
Thread States
State | Description |
---|---|
NEW | Thread is created but not started |
RUNNABLE | Thread is ready to run, waiting for CPU |
RUNNING | Thread is currently executing its run() method |
BLOCKED | Waiting for a lock held by another thread |
WAITING | Waiting indefinitely (e.g. wait() ) |
TIMED_WAITING | Waiting with a timeout (e.g. sleep() , join(timeout) ) |
TERMINATED | Thread has completed execution or was stopped |
Example
Thread t = new Thread(() -> {
try {
Thread.sleep(1000); // TIMED_WAITING
} catch (InterruptedException e) {}
});
System.out.println(t.getState()); // NEW
t.start();
System.out.println(t.getState()); // RUNNABLE → RUNNING
Transition Triggers
Action | State Change |
---|---|
start() | NEW → RUNNABLE |
Thread gets CPU | RUNNABLE → RUNNING |
sleep(), join(), etc | RUNNING → TIMED_WAITING |
wait() | RUNNING → WAITING |
Lock contention | RUNNING → BLOCKED |
Thread ends | Any → TERMINATED |
Tip
Be ready to:
- explain thread states with real examples,
- describe how sleep(), wait(), join() affect state,
- use getState() for debugging multithreaded apps.
Parent: _Multithreading