- NEW – a newly created thread that has not yet started the execution
- RUNNABLE – either running or ready for execution but it’s waiting for resource allocation
- BLOCKED – waiting to acquire a monitor lock to enter or re-enter a synchronized block/method
- WAITING – waiting for some other thread to perform a particular action without any time limit
- TIMED_WAITING – waiting for some other thread to perform a specific action for a specified period
- TERMINATED – has completed its execution
NEW Thread (or a Born Thread) is a thread that’s been created but not yet started.
It remains in this state until we start it using the start() method
NewState.java
public class NewState implements Runnable{ public void run(){ System.out.println("I am in new State"); } }
Main.java
public class Main { public static void main(String[] args) throws InterruptedException { Thread objThread = new Thread(new NewState()); System.out.println(objThread.getState()); } }
Output
NEW
Runnable When we’ve created a new thread and called the start() method on that, it’s moved from NEW to RUNNABLE state. Threads in this state are either running or ready to run, but
they’re waiting for resource allocation from the system. In a multi-threaded environment, the Thread-Scheduler (which is part of JVM) allocates a fixed amount of time to each thread. So it runs for a particular amount of time, then leaves the control to other RUNNABLE threads.
RunnableState .java
public class RunnableState implements Runnable{ public void run(){ System.out.println("I would be in Runnable State"); } }
Main.java
public class Main { public static void main(String[] args) throws InterruptedException { Thread objRThread = new Thread(new RunnableState()); objRThread.start(); System.out.println(objRThread.getState()); } }
Output
RUNNABLE I would be in Runnable State
This is the state of a dead thread. It’s in the TERMINATED state when it has either finished execution or was terminated abnormally.
TerminatedState.java
public class TerminatedState implements Runnable{ public void run(){ Thread objNewState = new Thread(new NewState()); objNewState.start(); } }
Main.java
public class Main { public static void main(String[] args) throws InterruptedException { Thread objTState = new Thread(new TerminatedState()); objTState.start(); objTState.sleep(1000); System.out.println("T1 : "+ objTState.getState()); } }
Output
I am in new State T1 : TERMINATED
A thread is in the BLOCKED state when it’s currently not eligible to run. It enters this state when it is waiting for a monitor lock and is trying to access a section of code that is locked by some other thread.
BlockedState.java
public class BlockedState implements Runnable{ public void run(){ blockedResource(); } public static synchronized void blockedResource(){ while(true){ //Do Nothing } } }
Main.java
public class Main { public static void main(String[] args) throws InterruptedException { Thread objB1Thread = new Thread(new BlockedState()); Thread objB2Thread = new Thread(new BlockedState()); objB1Thread.start(); objB2Thread.start(); Thread.sleep(1000); System.out.println(objB1Thread.getState()); System.out.println(objB2Thread.getState()); System.exit(0); } }
Output
RUNNABLE BLOCKED
A thread is in WAITING state when it’s waiting for some other thread to perform a particular action. According to JavaDocs, any thread can enter this state by calling any one of the following
object.wait() (or) thread.join() (or) LockSupport.park()
WaitingState.java
public class WaitingState implements Runnable{ public void run(){ Thread objWaitState = new Thread(new SleepState()); objWaitState.start(); try { objWaitState.join(); } catch (InterruptedException e) { throw new RuntimeException(e); } } }
SleepState.java
public class SleepState implements Runnable{ @Override public void run() { try { Thread.sleep(5000); } catch (InterruptedException e) { throw new RuntimeException(e); } } }
Main.java
public class Main { public static void main(String[] args) throws InterruptedException { Thread objWaitingThread = new Thread(new WaitingState()); objWaitingThread.start(); objWaitingThread.sleep(1000); System.out.println("T1 : "+ objWaitingThread.getState()); System.out.println("Main : "+Thread.currentThread().getState()); } }
Output
T1 : WAITING Main : RUNNABLE
A thread is in TIMED_WAITING state when it’s waiting for another thread to perform a particular action within a stipulated amount of time. According to JavaDocs, there are five ways to put a thread on TIMED_WAITING state:
thread.sleep(long millis) (or) wait(int timeout) (or) wait(int timeout, int nanos) thread.join(long millis) (or) LockSupport.parkNanos (or) LockSupport.parkUntil
TimedWaitState.java
public class TimedWaitState implements Runnable{ @Override public void run() { try { Thread.sleep(5000); } catch (InterruptedException e) { throw new RuntimeException(e); } } }
Main.java
public class Main { public static void main(String[] args) throws InterruptedException { Thread objTWState = new Thread(new TimedWaitState()); objTWState.start(); Thread.sleep(2000); System.out.println("T1 : "+ objTWState.getState()); } }
Output
T1 : TIMED_WAITING