Skip to main content
Java

Java Thread States

2 mins

A traffic light with different states (red, yellow, green), symbolizing the various stages a thread can be in during its lifecycle (e.g., NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED).

Thread States #

In Java, a thread can be in one of several states during its lifecycle. Understanding these states is crucial for managing threads effectively, especially in a multithreaded environment. The Java Thread states are defined in the java.lang.Thread.State enum, and each state represents a different stage in the lifecycle of a thread.

The following diagram shows the different states a thread can be in during its lifecycle.

Thread States Diagram #

stateDiagram-v2 [*] --> New: create new thread New --> Runnable: call start() Runnable --> Blocked: acquire lock Blocked --> Runnable: release lock Runnable --> Waiting: wait() Waiting --> Runnable: notify() Runnable --> TimedWaiting: sleep() TimedWaiting --> Runnable: timeout/notify() Runnable --> Terminated: run() completes Waiting --> Terminated: run() completes TimedWaiting --> Terminated: run() completes Blocked --> Terminated: run() completes Terminated --> [*]

Thread States Descriptions #

State Description
New The thread is in this state after it has been created but before the start() method has been called on it. At this point, the thread is considered not alive.
Runnable Once the thread’s start() method is called, it moves to the Runnable state. In this state, a thread is eligible to run but isn’t necessarily running at the moment. It’s up to the thread scheduler to give the thread time to run. The Runnable state includes both running and ready-to-run threads. In other words, a thread in this state may be executing or simply ready for execution whenever it gets CPU time.
Blocked A thread enters this state when it’s waiting to acquire a monitor lock (typically, this occurs when entering a synchronized block or method) to enter or reenter a synchronized context. It remains in this state until it acquires the lock.
Waiting A thread is in this state when it’s waiting indefinitely for another thread to perform a particular action. This state is entered through methods like Object.wait(), Thread.join(), or LockSupport.park(). In this state, a thread waits for another thread to perform a specific action, such as notification (notify() or notifyAll()) or interruption.
Timed Waiting Similar to the Waiting state, but with a specified waiting time. A thread is in this state when it’s waiting for another thread to perform an action for up to a specified waiting time. This state is entered through methods like Thread.sleep(long millis), Object.wait(long timeout), Thread.join(long millis), or LockSupport.parkNanos()/parkUntil().
Terminated A thread is in this state when its run() method completes. It may have terminated normally, or it may have terminated due to an uncaught exception. Once a thread is in this state, it cannot be restarted.