Life Cycle of a Thread in Java
A thread’s life cycle in Java has five states: New, Runnable, Waiting, Timed Waiting, and Terminated. There are several thread states in a lifecycle, as listed below:
In Java, a thread has five states: New, Runnable, Waiting, Timed Waiting, and Terminated. A thread’s lifecycle includes the following thread states:
A new thread enters the new state when it is created. The thread has not yet started running when it is in this state. When a thread is in the new state, its code has not yet been executed or run.
The runnable state is assigned to a thread that is ready to run. A thread in this state may be actively running or ready to start running at any time. It is the thread scheduler’s responsibility to allocate time for the thread to run.
Notes
A Java thread can be in any of the following states at any time. A thread can only be in one of the following states at any given time:
Blocked State Runnable State Waiting State Terminated State.
Implementation of Thread States in Java
In Java, use the Thread.getState() method to get the current state of a thread. Java includes the class java.lang.Thread.State is a class that defines the ENUM constants for a thread’s state, with a brief summary provided below:
1. New Thread status for a thread that has not yet started.
final public static Thread.State NEW
2. Thread State (Runnable) The state of a runnable thread. A thread in the runnable state is executing in the Java virtual machine, but it may be waiting for other operating system resources, such as a processor, to be available.
Thread.State Thread.State.RUNNABLE is a public static final Thread.State.
3. After calling Object, a thread in the blocked state awaits a monitor lock to enter or reenter a synchronized block/method.wait().
The public static final Thread has been BLOCKED.State
4. The state of a waiting thread is Waiting Thread. Because one of the following methods was called, a thread is in the waiting state:
Object.The thread is waiting with no timeout.without a break LockSupport.park public static final Thread.State WAITING
5. Timer for Waiting: A thread state with a time limit for a waiting thread. As a result of calling one of the following methods with a positive waiting time, a thread has entered the timed waiting state:
Thread.sleep Object.await a timeout thread join with timeout LockSupport.park for Nanos LockSupport.park for NanosUntil the final public static Thread.The goal has been accomplished. TIMED_WAITING
6. Terminated Thread state for a thread that has been terminated. The thread has finished its execution.
Final public static declaration Thread.State has been terminated.
Example:
class thread implements Runnable { public void run() { try { Thread.sleep(1500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println( "Thread 1 in FirstCode: " + FirstCode.thread1.getState()); try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } } } public class FirstCode implements Runnable { public static Thread thread1; public static FirstCode obj; public static void main(String[] args) { obj = new FirstCode (); thread1 = new Thread(obj); System.out.println( "Thread 1 in FirstCode: " + thread1.getState()); thread1.start(); System.out.println( "FirstCode's Thread 1 after the Start: " + thread1.getState()); } public void run() { thread myThread = new thread(); Thread thread2 = new Thread(myThread); System.out.println( "Thread 2 in FirstCode after creating: " + thread2.getState()); thread2.start(); System.out.println( "FirstCode's Thread 2 after the Start: " + thread2.getState()); try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println( "Thread 2 in FirstCode after Sleep:" + thread2.getState()); try { thread2.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println( "After execution, Thread 2 in FirstCode is: " + thread2.getState()); } }
Output:
Thread 1 in FirstCode: NEW
FirstCode’s Thread 1 after the Start: RUNNABLE
Thread 2 in FirstCode after creating: NEW
FirstCode’s Thread 2 after the Start: RUNNABLE
Thread 2 in FirstCode after Sleep: TIMED_WAITING
Thread 1 in FirstCode: WAITING
After execution, Thread 2 in FirstCode is: TERMINATED
Conclusion
In Java, The five states of a thread—new, runnable, running, blocked, and dead—are explained by the thread life cycle in Java. Throughout their lifetime, threads can be in one of four states: ready, running, waiting, or dead. Each state has unique properties. Every thread operates for a brief period of time before pausing and handing over control of the CPU to another thread to give other threads an opportunity.