Life Cycle of a Thread in Java: Understanding Thread States

Explore the life cycle of a thread in Java, including key thread states: New, Active, Blocked, Waiting, Timed Waiting, and Terminated. Learn how threads transition between these states in multithreaded applications to manage execution flow effectively.



Life Cycle of a Thread (Thread States)

In Java, a thread always exists in any one of the following states:

  • New
  • Active
  • Blocked / Waiting
  • Timed Waiting
  • Terminated

Explanation of Different Thread States

New: Whenever a new thread is created, it is in the new state. The code has not been run yet.

Active: When a thread invokes the start() method, it moves to the active state. This state includes:

  • Runnable: The thread is ready to run. It may be running or waiting to run.
  • Running: The thread gets the CPU and runs. It frequently switches between runnable and running states.

Blocked or Waiting: The thread is inactive temporarily, waiting for a resource or another thread.

Timed Waiting: The thread is waiting for a specific time. For example, when sleep() is invoked.

Terminated: The thread has finished execution or terminated abnormally.

Implementation of Thread States

Use the Thread.getState() method to get the current state of a thread. The java.lang.Thread.State class provides constants to represent thread states:

  • Thread.State.NEW: The thread is in the new state.
  • Thread.State.RUNNABLE: The thread is in the runnable state.
  • Thread.State.BLOCKED: The thread is in the blocked state.
  • Thread.State.WAITING: The thread is in the waiting state.
  • Thread.State.TIMED_WAITING: The thread is in the timed waiting state.
  • Thread.State.TERMINATED: The thread is in the terminated state.

Java Program for Demonstrating Thread States

Syntax

class ABC implements Runnable {
public void run() {
try {
    Thread.sleep(100);
} catch (InterruptedException ie) {
    ie.printStackTrace();
}
System.out.println("State of t1 while join() on t2: " + ThreadState.t1.getState());
try {
    Thread.sleep(200);
} catch (InterruptedException ie) {
    ie.printStackTrace();
}
}
}

public class ThreadState implements Runnable {
public static Thread t1;
public static ThreadState obj;

public static void main(String[] args) {
obj = new ThreadState();
t1 = new Thread(obj);

System.out.println("State of t1 after spawning: " + t1.getState());
t1.start();
System.out.println("State of t1 after start(): " + t1.getState());
}

public void run() {
ABC myObj = new ABC();
Thread t2 = new Thread(myObj);

System.out.println("State of t2 after spawning: " + t2.getState());
t2.start();
System.out.println("State of t2 after start(): " + t2.getState());

try {
    Thread.sleep(200);
} catch (InterruptedException ie) {
    ie.printStackTrace();
}
System.out.println("State of t2 after sleep(): " + t2.getState());

try {
    t2.join();
} catch (InterruptedException ie) {
    ie.printStackTrace();
}
System.out.println("State of t2 after completion: " + t2.getState());
}
}
Output

State of t1 after spawning: NEW
State of t1 after start(): RUNNABLE
State of t2 after spawning: NEW
State of t2 after start(): RUNNABLE
State of t1 while join() on t2: TIMED_WAITING
State of t2 after sleep(): TIMED_WAITING
State of t2 after completion: TERMINATED