Mastering Java’s Six Thread States: A Visual Guide
This article explains Java’s six thread states—NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, and TERMINATED—using clear diagrams, concise descriptions, and practical code examples to help developers understand how threads transition during execution.
Six Thread States in Java
Java defines six distinct thread states that a Thread instance can occupy during its lifecycle: NEW , RUNNABLE , BLOCKED , WAITING , TIMED_WAITING , and TERMINATED . Understanding these states is essential for diagnosing concurrency issues and writing correct multithreaded code.
NEW – Initialization State
A thread is in the NEW state immediately after it is instantiated with new Thread(...) or a subclass of Thread. It has not yet been started.
class MyThread extends Thread {
public void run() {
// code to be executed by the thread
}
}
MyThread myThread = new MyThread(); // myThread is in NEW stateRUNNABLE – Ready or Running
When start() is called, the thread moves to RUNNABLE . The JVM schedules it for execution, which may involve waiting for CPU time or I/O resources. RUNNABLE can be further split into:
Ready : the thread is waiting for the JVM scheduler.
Running : the thread has been allocated CPU time and is executing.
Thread thread = new Thread("thread");
thread.start();BLOCKED – Waiting for Monitor Lock
A thread enters BLOCKED when it attempts to enter a synchronized block or method but cannot acquire the monitor lock because another thread holds it.
When the lock becomes available, the thread transitions back to the RUNNABLE (ready) state.
WAITING – Indefinite Wait
The WAITING state occurs when a thread calls Object.wait() (without a timeout), Thread.join(), or LockSupport.park(). The thread remains idle until another thread invokes notify(), notifyAll(), or otherwise unparks it.
Thread thread1 = new Thread("thread1") {
@Override
public void run() {
synchronized (WaitingState1.class) {
try {
// thread waits indefinitely
WaitingState1.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
thread1.start();
TimeUnit.SECONDS.sleep(1); // let thread reach wait()TIMED_WAITING – Wait with Timeout
A thread enters TIMED_WAITING when it calls a timed version of wait, sleep, join, or the timed park methods ( LockSupport.parkNanos, LockSupport.parkUntil). The thread will automatically return to RUNNABLE after the specified timeout expires.
TERMINATED – End of Life
When the run() method completes, the thread transitions to TERMINATED . The thread can no longer be started or used.
Understanding these states helps developers reason about thread behavior, avoid deadlocks, and write more reliable concurrent Java applications.
Architect Chen
Sharing over a decade of architecture experience from Baidu, Alibaba, and Tencent.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
