The Java Thread Lifecycle refers to the various states a thread undergoes during its existence in a program. A thread can be in one of several states as it progresses through its lifecycle, from creation to completion. These states define the status of the thread and the actions that can be performed on it.
There are 5 state in the Java Thread Lifecycle
- New State
- Runnable State
- Blocked/Waiting State
- Timed Waiting State
- Terminated (Dead) State
Diagram:
1. New State
A thread is in the New state as soon as it is created but before it has started executing. The thread is instantiated but has not yet started.
What Happens: It will remain in this state until the start() method is invoked. The thread moves to the Runnable state once start() is called.
Example:
Thread thread = new Thread(); // Thread is in New state
2. Runnable State
In the Runnable state, the thread is ready to run and waiting for the CPU time to be allocated. It is actively waiting for the thread scheduler to give it a chance to execute.
What Happens: The thread is either running or waiting for CPU time. It is not necessarily actively executing, as it might be waiting for other threads to finish or for a certain condition to occur.
Note: In the Runnable state, the thread is ready to be picked by the CPU scheduler to run, but the operating system might assign the CPU to another thread first.
Example:
//Main.java file
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running");
}
}
public class Main {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start(); // The thread moves to the Runnable state after start() is called
}
}
3. Blocked/Waiting State
A thread can enter the Blocked or Waiting state when it is waiting for a resource, lock, or condition. These states occur when a thread is waiting for some external event (such as I/O or acquiring a lock) before it can proceed with its execution.
Blocked State: A thread enters this state when it is waiting to acquire a lock or resource that is being held by another thread.
Waiting State: A thread enters this state when it is waiting indefinitely for some other thread to perform a specific action (e.g., a thread calling join() on another thread).
What Happens: A thread may enter this state when it is performing operations that involve synchronization (e.g., accessing shared resources) or waiting for another thread to finish.
Example:
class MyThread extends Thread {
synchronized public void run() {
System.out.println("Thread is running");
}
}
public class Main {
public static void main(String[] args) {
MyThread thread1 = new MyThread();
MyThread thread2 = new MyThread();
thread1.start(); // Starts thread1
thread2.start(); // thread2 will be blocked if thread1 holds the lock
}
}
4. Timed Waiting State
A thread enters the Timed Waiting state when it is waiting for a specified amount of time before resuming execution. The waiting time is defined in milliseconds or nanoseconds.
Examples of methods that cause a thread to enter Timed Waiting:
- Thread.sleep(milliseconds)
- Thread.join(milliseconds)
- Object.wait(milliseconds)
- Thread.yield()
What Happens: The thread enters this state for a fixed amount of time or until an interrupt occurs. After the specified time, it will return to the Runnable state and compete for CPU time.
//Main.java file
class MyThread extends Thread {
public void run() {
try {
Thread.sleep(2000); // Thread sleeps for 2 seconds
System.out.println("Thread is awake after 2 seconds");
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
public class Main {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start(); // The thread will go to Timed Waiting state due to sleep()
}
}
5. Terminated (Dead) State
A thread enters the Terminated or Dead state when it has completed its execution or has been terminated due to an exception or an error. In this state, the thread can no longer be started or executed.
What Happens: The thread has finished executing its run() method or was interrupted or forcibly terminated. Once a thread is in the Dead state, it cannot be restarted.
Example:
//Main.java file
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running");
}
}
public class Main {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start(); // After thread finishes, it enters the Terminated state
}
}