Last Updated: 28 May, 2022
In Multithreading, a thread executes independently and during the execution, it moves from one state to another state. A thread follows a complete life cycle for execution.
A thread always exists in any one of the following states:
Life cycle of a thread in Java
NEW State (Newly Created Thread)
When a new thread is created, it is in a new state. It remains in this state until the program starts the thread using its start() method. This state is also known as the Born state.
RUNNABLE State (Ready for Execution)
When we call the start( ) method, then the thread moves from the New state to the Runnable state. This state is also known as a Ready state.
In this state a thread is ready for execution, execution control is passed to Thread-scheduler to finish its execution but it's up to Thread-scheduler to decide whether this thread should be executed or should be put on hold to give chance to other runnable threads.
RUNNING State (Executing a Thread)
Thread-Scheduler picks up a thread from the runnable thread pool and changes the state from Runnable to Running state, then the CPU starts executing this thread. A running thread can change state to Runnable, Dead, or Blocked depending on time slicing, thread completion of the run() method, or waiting for some resources.
WAITING State (Thread will be Blocked)
A running thread may move into the waiting or blocked state due to various reasons like the sleep( ) method called, wait( ) method called, suspend( ) method called, and join( ) method called, etc.
When a thread is in the waiting or blocked state, it may move to a Runnable state due to reasons like sleep time completed, waiting time completed, notify( ) or notifyAll( ) method called, resume( ) method called, etc.
DEAD State (A Thread Execution Completed)
A thread in the Running state may move into the dead state due to either its execution completed or the stop( ) method called. The dead state is also known as the terminated state.
Thread-Scheduler is part of JVM and It is platform-dependent.
To get the current state of the thread, use Thread.getState() method. Java provides java.lang.Thread.State class that defines the ENUM constants for the state of a thread.
These constants are given below:
1. New
public static final Thread.State NEW
This state represents New state of a thread. This is the first state of any thread.
2. Runnable
public static final Thread.State RUNNABLE
This state represents Runnable state of a thread.
3. Blocked
public static final Thread.State BLOCKED
This state represents Blocked state of a thread.
4. Waiting
public static final Thread.State WAITING
This state represents Waiting state of a thread.
5. Timed Waiting
public static final Thread.State TIMED_WAITING
Thread state for a waiting thread with a specified waiting time.
6. Terminated
public static final Thread.State TERMINATED
This state represents Terminated state of a thread.
Example : A sample program to demonstrate thread states
Output
State of thread1 : NEW
State of thread1 : RUNNABLE
State of thread2 : NEW
State of thread2 : RUNNABLE
State of thread1 while it called join() method on thread2 -TIMED_WAITING
After calling sleep() method, State of thread2 - TIMED_WAITING
thread2 completed it's execution, State of thread2 - TERMINATED
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com