Last Updated : 16 Apr, 2025
A thread in Java can exist in any one of the following states at any given time. A thread lies only in one of the shown states at any instant:
The diagram below represents various states of a thread at any instant:
Life Cycle of a ThreadThere are multiple states of the thread in a lifecycle as mentioned below:
In Java, to get the current state of the thread, use Thread.getState() method to get the current state of the thread. Java provides java.lang.Thread.State enum that defines the ENUM constants for the state of a thread, as a summary of which is given below:
1. NewThread state for a thread that has not yet started.
2. Runnablepublic static final Thread.State NEW
Thread state for a runnable thread. A thread in the runnable state is executing in the Java virtual machine but it may be waiting for other resources from the operating system such as a processor.
3. Blockedpublic static final Thread.State RUNNABLE
Thread state for a thread blocked waiting for a monitor lock. A thread in the blocked state is waiting for a monitor lock to enter a synchronized block/method or reenter a synchronized block/method after calling Object.wait().
4. Waitingpublic static final Thread.State BLOCKED
Thread state for a waiting thread. A thread is in the waiting state due to calling one of the following methods:
5. Timed Waitingpublic static final Thread.State WAITING
Thread state for a waiting thread with a specified waiting time. A thread is in the timed waiting state due to calling one of the following methods with a specified positive waiting time:
6. Terminatedpublic static final Thread.State TIMED_WAITING
Thread state for a terminated thread. The thread has completed execution.
Example of Demonstrating Thread Statespublic static final Thread.State TERMINATED
Below is a real-world example of a ticket booking system that demonstrates different thread states:
Example:
Java
// Java program to demonstrate thread states
// using a ticket booking scenario
class TicketBooking implements Runnable {
@Override
public void run() {
try {
// Timed waiting
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("State of bookingThread while mainThread is waiting: " +
TicketSystem.mainThread.getState());
try {
// Another timed waiting
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class TicketSystem implements Runnable {
public static Thread mainThread;
public static TicketSystem ticketSystem;
@Override
public void run() {
TicketBooking booking = new TicketBooking();
Thread bookingThread = new Thread(booking);
System.out.println("State after creating bookingThread: " + bookingThread.getState());
bookingThread.start();
System.out.println("State after starting bookingThread: " + bookingThread.getState());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("State after sleeping bookingThread: " + bookingThread.getState());
try {
// Moves mainThread to waiting state
bookingThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("State after bookingThread finishes: " + bookingThread.getState());
}
public static void main(String[] args) {
ticketSystem = new TicketSystem();
mainThread = new Thread(ticketSystem);
System.out.println("State after creating mainThread: " + mainThread.getState());
mainThread.start();
System.out.println("State after starting mainThread: " + mainThread.getState());
}
}
Output:
Explanation:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4