In Java, threads play a crucial role in enabling concurrent execution of tasks, which improves the performance and efficiency of applications. A thread’s lifecycle in Java involves a series of states, from its creation to its completion. These states define what a thread is doing at any given time and allow developers to manage threads effectively.
Thread States
Java defines the following thread states through the Thread.State
enum:
- New (Born state)
- Runnable
- Blocked
- Waiting
- Timed Waiting
- Terminated (Dead state)
New (Born state)
A thread enters the New state as soon as it is created but before it is started. In this state, the thread has been instantiated but hasn’t begun execution yet.
1 |
Thread thread = new Thread(); |
- Transition: The thread will move to the Runnable state once the
start()
method is invoked.
Runnable
In the Runnable state, the thread is eligible for execution by the Java Virtual Machine (JVM). However, just because a thread is in this state doesn’t mean it is currently executing. The thread could be waiting for CPU time to execute, or it may be waiting for a resource.
- Transition: The thread enters this state after the
start()
method is called. - The thread can transition back to this state from other states (like Blocked, Waiting, or Timed Waiting) once the required conditions are met.
Blocked
A thread enters the Blocked state when it is waiting to acquire a lock (mutex) to enter a synchronized block or method. If another thread holds the lock, the current thread remains in the Blocked state until the lock becomes available.
- Transition: The thread enters this state when it tries to access a synchronized block or method and the lock is already held by another thread.
- Transition back: Once the thread acquires the lock, it moves to the Runnable state.
Waiting
A thread enters the Waiting state when it is waiting indefinitely for another thread to perform a particular action. For example, a thread will enter this state when it calls methods like wait()
or join()
without a specified timeout.
1 2 3 |
synchronized (object) { object.wait(); } |
- Transition: A thread enters the Waiting state after calling methods like
wait()
orjoin()
(with no timeout). - Transition back: The thread returns to the Runnable state once it is notified by another thread (via
notify()
ornotifyAll()
) or the join operation completes.
Timed Waiting
In the Timed Waiting state, a thread is waiting for a specified amount of time. This happens when methods such as sleep()
, join(long millis)
, or wait(long millis)
are called.
1 |
Thread.sleep(1000); |
- Transition: A thread enters the Timed Waiting state after calling methods like
sleep(long millis)
orjoin(long millis)
. - Transition back: After the specified time elapses, the thread moves back to the Runnable state.
Terminated (Dead state)
A thread enters the Terminated state when it has completed its execution or has been terminated due to an exception or a manual intervention (such as calling stop()
on a thread).
- Transition: The thread enters the Terminated state automatically once its
run()
method has completed execution or if it is interrupted.
Thread State Transitions Summary
State | Action |
---|---|
New | Thread is created but not yet started. |
Runnable | Thread is ready to be executed by the JVM, but it may not be running yet. |
Blocked | Thread is waiting to acquire a lock. |
Waiting | Thread is waiting indefinitely for another thread to perform a specific action. |
Timed Waiting | Thread is waiting for a specified period of time. |
Terminated | Thread has finished its execution or has been stopped. |