Lifecycle of a thread in Java

A thread is a lightweight process in Java. A Java program always has at least one thread that is running called the main() thread. It can initiate many more threads from the main() thread.

States of a thread

A thread in Java could have the following states throughout its life cycle:

  1. New

  2. Active

  3. Waiting/blocked

  4. Timed waiting

  5. Terminated / dead

Lifecycle of a Java thread
Lifecycle of a Java thread

The java.lang.Thread.State class

We can get the current state of the thread in Java by calling Thread.getState().

The java.lang.Thread.State class has an enum to represent the state of a thread that has the following values:

State

Declaration

New

public static final Thread.State NEW  


Runnable

public static final Thread.State RUNNABLE


Blocked

public static final Thread.State BLOCKED


Waiting

public static final Thread.State WAITING


Timed Waiting

public static final Thread.State TIMED_WAITING


Let's learn about these states.

The new state

When a thread is created, it is in the new state. In this state, the thread has not yet started the execution of the code.

Main.java
Thread1.java
public class Main {
public static void main(String[] args) {
Thread1 obj = new Thread1();
Thread thread = new Thread(obj);
//State of thread before starting it
System.out.println("State of thread before calling start: " + thread.getState());
thread.start();
}
}

The active state

A thread goes into an active state when it is invoked by the start() method. In the active state, it can have further the following sub-states:

  1. Runnable: A thread that is ready to run at a given time is moved to a runnable state, and it waits in the queue for its turn and time.

  2. Running: When an active thread is executing and occupying the CPU, it is in the running state.

Main.java
Thread1.java
// ABC class implements the interface Runnable
class Thread1 implements Runnable
{
public void run()
{
}
}

The waiting state

When a thread needs a resource that is occupied by another thread, it has to go into the waiting or blocked state. If a thread invokes the join() method, it goes into a waiting state and waits for the child thread to complete its execution.

Main.java
Thread2.java
Thread1.java
public class Main {
public static void main(String[] args) {
try {
Thread2.thread2 = new Thread(new Thread2());
//starting thread2
Thread2.thread2.start();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

The timed waiting state

In case a thread needs a resource that is being held by another thread or a process for a very long period of time, then the execution of this thread will remain blocked, and it will starve. To avoid this situation, sometimes timed waiting is used, for example, the thread is given a specific time to wait for that resource, and when that time limit reaches, it will stop waiting and continue its execution. One examples is using the sleep() method, which puts on a timed wait state to the current thread, and after the time is over, it continues its execution.

Main.java
Thread1.java
public class Main {
public static void main(String[] args) {
try{
Thread thread = new Thread(new Thread1());
//Start a thread and check its state
thread.start();
//wait for thread for a while
thread.join(20);
System.out.println("Thread state after calling sleep inside it: " + thread.getState());
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

The terminated state

A thread reaches the terminated state when it completes its execution or due to an unhandled exception for some external event. A terminated thread is also called dead because it can not be revoked or made alive.

Main.java
Thread1.java
public class Main {
public static void main(String[] args) {
try {
Thread1 obj = new Thread1();
Thread thread = new Thread(obj);
//State of thread before starting it
thread.start();
//wait for thread to complete its execution
thread.join();
System.out.println("State of thread after calling start: " + thread.getState());
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved