Thread Lifecycle in Java


In Java, a thread goes through various stages during its lifetime, from creation to termination. Understanding the thread lifecycle is important to manage threads effectively and efficiently. Java's Thread class provides the framework for creating and managing threads, and the lifecycle defines the states through which a thread progresses during its execution.
Thread Lifecycle States
Java threads undergo the following states during their lifecycle:
New (Born) State
Runnable State
Blocked State
Waiting State
Timed Waiting State
Terminated State
Each of these states plays a crucial role in how threads are managed and executed by the Java Virtual Machine (JVM). Let’s dive into each state:
1. New (Born) State
Description: When a thread is created, but not yet started, it is in the "New" state. At this point, the thread has been instantiated but has not begun executing any code. This state is also referred to as the "born" state.
Example:
Thread myThread = new Thread(); // Thread is in New state
- Action: The thread is now ready to be started, but it has not yet started running.
2. Runnable State
Description: Once the
start()
method is called on the thread object, it moves to the Runnable state. In this state, the thread is eligible for execution, but the JVM scheduler decides when it will actually run based on system resources (like available CPU).- In Runnable, the thread may not always be actively running; it’s simply ready to run when the CPU is available.
Example:
Thread myThread = new Thread();
myThread.start(); // Now in Runnable state
- Action: The thread enters the runnable queue, and the thread scheduler picks it up for execution. However, its actual execution depends on the system’s scheduling.
3. Blocked State
Description: A thread enters the Blocked state when it is trying to access a resource that is currently being used by another thread. For example, if a thread is trying to access a synchronized method or block but the lock is already held by another thread, it will be blocked.
- The thread remains in the blocked state until the resource it is waiting for becomes available.
Example:
synchronized (this) {
// If another thread has the lock, this thread will be blocked.
}
- Action: The thread remains in the blocked state until it can obtain the necessary lock or resource.
4. Waiting State
Description: A thread enters the Waiting state when it is waiting indefinitely for another thread to perform a particular action or to notify it. This can happen if the thread is waiting for a condition to be met, or if it's waiting for another thread to finish executing.
Example:
// Using wait() method on object
synchronized (this) {
try {
wait(); // Thread enters waiting state indefinitely
} catch (InterruptedException e) {
e.printStackTrace();
}
}
- Action: The thread waits for another thread to notify it using
notify()
ornotifyAll()
, or for some other condition to change. This is often used in thread coordination.
5. Timed Waiting State
Description: A thread enters the Timed Waiting state when it is waiting for a specified period of time. This can be done using methods like
sleep()
,join()
, orwait()
. After the specified time period expires, the thread is moved back to the Runnable state.Example:
Thread.sleep(1000); // Thread is in Timed Waiting for 1 second
- Action: The thread remains in the timed waiting state for the specified period. Once the time is over, it returns to the runnable state to resume execution.
6. Terminated (Dead) State
Description: A thread enters the Terminated state once its
run()
method completes or it is otherwise stopped (either by an exception or by calling thestop()
method, which is deprecated).- When a thread is in this state, it can no longer be started again, and it has finished its execution.
Example:
// Once the run() method finishes, the thread moves to the Terminated state
public void run() {
// thread work completed
}
- Action: The thread has completed its task, and the resources associated with it are released. It can no longer be restarted.
Transition Between States
A thread can move between these states depending on actions like calling start()
, sleep()
, wait()
, notify()
, and join()
. Here’s a basic flow of how a thread transitions between different states:
New -> Runnable: A thread moves from the "New" state to the "Runnable" state when the
start()
method is called.Runnable -> Blocked: A thread moves to the "Blocked" state if it is waiting for a resource.
Runnable -> Waiting: A thread moves to the "Waiting" state when it calls the
wait()
method.Runnable -> Timed Waiting: A thread moves to the "Timed Waiting" state when it calls
sleep()
orjoin()
with a time period.Runnable -> Terminated: A thread moves to the "Terminated" state when its execution is complete.
Conclusion
Understanding the thread lifecycle is vital when working with multithreading in Java. By being aware of the different thread states and their transitions, developers can manage thread execution efficiently and avoid common pitfalls such as deadlocks and race conditions. Proper synchronization, thread management, and coordination are crucial for building robust multithreaded applications.
Subscribe to my newsletter
Read articles from Mohammed Shakeel directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Mohammed Shakeel
Mohammed Shakeel
I'm Mohammed Shakeel, an aspiring Android developer and software engineer with a keen interest in web development. I am passionate about creating innovative mobile applications and web solutions that are both functional and aesthetically pleasing.