Java provides five distinct thread states, each representing the various stages a thread can go through during its lifecycle. These states are part of the Thread.State
enum, which is used to represent the state of a thread at any point in time.
1. NEW
- Description: A thread that has been created but not yet started. In this state, the thread has been instantiated but its
start()
method has not been called yet. - Behavior: The thread remains in the
NEW
state until thestart()
method is invoked. - Transition: When
start()
is called, the thread moves to theRUNNABLE
state.
Example:
Thread t = new Thread(() -> System.out.println("Running")); // Thread is in NEW state now.
2. RUNNABLE
- Description: A thread that is ready to run and may be executing code. In this state, the thread is either executing on the CPU or waiting for CPU time to be allocated.
- Behavior: The thread can move in and out of this state as it gets CPU time or gets interrupted by the scheduler.
- Transition: The thread enters the
RUNNABLE
state whenstart()
is called, and it may stay in this state until it completes its execution, moves toBLOCKED
,WAITING
, orTIMED_WAITING
.
Example:
t.start(); // The thread is now in the RUNNABLE state
3. BLOCKED
- Description: A thread that is trying to acquire a lock but is blocked because another thread is holding that lock.
- Behavior: The thread enters this state when it attempts to enter a synchronized block or method but another thread already holds the intrinsic lock for the object.
- Transition: The thread transitions to
BLOCKED
when it tries to access a synchronized resource held by another thread. It moves back toRUNNABLE
once the lock is released.
Example:
synchronized(lock) { // Thread that acquires the lock first can enter. // Other threads will be BLOCKED until the lock is released. }
4. WAITING
- Description: A thread that is waiting indefinitely for another thread to perform a particular action. This can occur when a thread calls
Object.wait()
orThread.join()
without a timeout. - Behavior: The thread waits until another thread signals it via
notify()
ornotifyAll()
, or until the thread it’s waiting on finishes. - Transition: The thread enters
WAITING
when calling methods likeObject.wait()
,Thread.join()
, orLockSupport.park()
. It moves back toRUNNABLE
when it is notified or when the thread it’s waiting for completes.
Example:
synchronized(lock) { lock.wait(); // Thread will enter WAITING state }
5. TIMED_WAITING
- Description: A thread that is waiting for another thread to perform an action for a specified amount of time. This state is similar to
WAITING
, but with a time limit after which the thread automatically moves toRUNNABLE
. - Behavior: The thread remains in
TIMED_WAITING
until the specified waiting period elapses, or if another thread interrupts it or performs the expected action (e.g.,notify()
). - Transition: The thread enters
TIMED_WAITING
when it calls methods likeThread.sleep()
,Object.wait(long)
,Thread.join(long)
, orLockSupport.parkNanos()
. It moves back toRUNNABLE
after the timeout expires or the waiting condition is met.
Example:
Thread.sleep(1000); // Thread will enter TIMED_WAITING state for 1 second
6. TERMINATED
- Description: A thread that has completed its execution. It may have finished either by completing its
run()
method or by throwing an uncaught exception. - Behavior: Once a thread reaches the
TERMINATED
state, it cannot be restarted. - Transition: The thread enters
TERMINATED
after therun()
method completes or if an exception causes the thread to stop.
Example:
t.join(); // Thread will enter TERMINATED state after completion
Thread Lifecycle Transitions
The transitions between these states can be summarized as:
- NEW →
start()
→ RUNNABLE - RUNNABLE → BLOCKED (if waiting for a lock)
- RUNNABLE → WAITING/TIMED_WAITING (waiting for an event or sleeping)
- BLOCKED/WAITING/TIMED_WAITING → RUNNABLE (after receiving a lock or notification)
- RUNNABLE → TERMINATED (after completion of the thread’s task)
Thread State Diagram
Here is a conceptual state diagram that visualizes the lifecycle of a thread:
NEW -------------------> RUNNABLE ------------------> TERMINATED
start() CPU time complete or (After thread run finishes)
encounter issues
| |
| |
BLOCKED ------------------- WAITING/TIMED_WAITING
(Waiting for a lock) (Waiting for condition/timeout)
Conclusion
Understanding Java thread states helps you to manage thread behavior in a multithreaded environment. This is crucial for diagnosing and resolving issues related to performance, concurrency, and deadlocks in Java applications.