Understanding Thread Life Cycle, Thread States in Java | Tutorial with Examples
This tutorial explains thread life cycle in Java with examples. This is the second article in the Java Concurrency Series, with the first articleClick to Read tutorial on Java MultiThreading Basics & How-to create/run Threads covering basics of multithreading in Java. In this tutorial we will start by looking at the Java thread lifecycle diagram. We will then look at individual thread states in detail to understand the state information they encapsulate and how transitions happen between these states. Lastly, we will take a look at a code example showing how a thread moves through its states, understand the logic of the program via a sequence diagram and then understand how the code works.
Java Thread Life Cycle
Let us start by getting a high level understanding of the 6 thread states in Java with the diagram shown next -
Above diagram shows how a typical thread moves through the different stages of its life cycle. Let us look at these states one-by-one and understand their place in the life of a Java thread -
In-built Java Enum Constants for thread states
To provide a standard naming and reference for individual thread states, Java language designers have defined an enum named
OUTPUT of the above code
Explanation of the code
To better understand how
- New - A newly created thread object instance on which the
start()method has not yet been invoked is in thenewstate. To learn how to instantiate threads in the proper way check out this tutorialClick to Read tutorial explaining how to create a Thread in Java. - Runnable - A thread in
newstate enters therunnablestate when theThread.start()method is invoked on it. There are 2 important points to note regarding therunnablestate -- Although the thread enters the
runnablestate immediately on invoking thestart()method, but it is not necessary that the thread immediately starts executing. A thread runs when the logic it holds in itsrun()method can be executed by the processor. In case the thread logic needs any resource which is not available then the thread waits for the resource to become available. Secondly, a thread in
runnablestate may run for some time and then get blocked for a monitor lock, or enter thewaiting/timed_waitingstates as it waits for the opportunity/time to enterrunnablestate again.
- Although the thread enters the
- Blocked - A running thread may enter the
blockedstate as it waits for a monitor lock to be freed. It may also beblockedas it waits to reenter a monitor lock after being asked to wait using theThread.wait()method. - Waiting - A thread enters the
waitingstate when it is made to wait for a go-ahead signal to proceed. The go-ahead in this case is given by another thread and can be given in the following 3 scenarios -- Thread waiting due to
Thread.wait()method being called on it: The other thread can useThread.notify()orThread.notifyAll()to give the go-ahead to thewaitingthread. - Thread waiting as it itself has asked for joining another thread using
Thread.join(): Thewaitingthread gets a go-ahead when the thread its waiting for ends. Thread waiting due to
The waiting thread resumes whenLockSupport.park()method being invoked on it:LockSupport.unPark()is called with the parked thread object as the parameter.
- Thread waiting due to
- Timed_Waiting - A thread which is waiting as it has been specifically 'instructed' to wait for a specified waiting time is in a
timed_waitingstate. A thread can be made to wait for a pre-determined amount of time in the following ways -- Thread made to wait using
Thread.sleep()method. - Threads being asked to wait for a permit for a specified amount of time using
LockSuport.parkNanos()andLockSupport.parkUntil()methods. Threads being made to wait for a fixed amount of time using
Thread.wait(long millis)orThread.join(long millis, int nanos).
- Thread made to wait using
- Terminated - A thread enters its 'final resting' state or
terminatedstate when it has finished executing the logic specified in itsrun()method.
java.lang.Thread.State which has the following constants defined (each one named after the thread state it refers to) -
ThreadState.BLOCKEDThreadState.NEWThreadState.RUNNABLEThreadState.TERMINATEDThreadState.TIMED_WAITINGThreadState.WAITING
Java example showing how a thread moves between different states
package com.javabrahman.corejava.threads;
public class ThreadStates {
public static void main(String args[]){
//Creating an instance of Basic Thread
Thread threadInstance=new Thread(new BasicThread());
threadInstance.start();
System.out.println("BasicThread State: "+threadInstance.getState());
try {
boolean keepRunning=true;
int count=1;
while(keepRunning) {
Thread.sleep(2000);
System.out.println(count*2+ " Seconds elapsed - BasicThread State: "+threadInstance.getState());
count++;
if(count==4){
//6 seconds elapsed
synchronized(threadInstance) {
threadInstance.notify();
}
}
if(Thread.State.TERMINATED == threadInstance.getState()){
keepRunning = false;
}
}
}catch(InterruptedException iException){
iException.printStackTrace();
}
}
}
//BasicThread.java
package com.javabrahman.corejava.threads;
public class BasicThread implements Runnable {
@Override
public void run() {
Thread thread = Thread.currentThread();
try{
//Making the thread sleep for 5 seconds
System.out.println("Basic thread to sleep for 5 seconds");
thread.sleep(5000);
synchronized (thread) {
thread.wait();
}
}catch(InterruptedException iException){
iException.printStackTrace();
}
}
}
BasicThread State: RUNNABLE Basic thread to sleep for 5 seconds 2 Seconds elapsed - BasicThread State: TIMED_WAITING 4 Seconds elapsed - BasicThread State: TIMED_WAITING 6 Seconds elapsed - BasicThread State: WAITING 8 Seconds elapsed - BasicThread State: TERMINATED
BasicThread.java is moving through different thread states, let us first take a look at a sequence diagram showing the interactions between above two programs -
ThreadStates.javais the class with themain()method in above example. It instantiates aBasicThread.javainstance, namedthreadInstance,and then callsstart()onthreadInstanceto start the execution of parallel thread. The state ofBasicThreadis printed at this point asRUNNABLE.ThreadStatesthen gets into an infinite while loop with a boolean flag variable namedkeepRunning. At the beginning of the loop the thread goes to sleep for2000millisecondsor 2 seconds. At the same timeBasicThreadtoo goes to sleep for5000milliseconds.- In every iteration of while loop, at a gap of every 2 seconds,
BasicThread’s state is printed. During 1st iteration(2 seconds) and 2nd iteration(4 seconds) elapsed times -BasicThreadis printed as being in stateTIMED_WAITINGas it is sleeping for 5 seconds. - When 5 seconds elapse
BasicThreadwakes up, and it immediately asks itself to wait usingThread.wait()method. The state ofBasicThreadis printed at this point asWAITING. - Then, in 3rd iteration i.e. when 6 seconds elapse,
ThreadStatesinvokesnotify()onBasicThreadwhich then starts executing. - In 4th iteration(8 seconds)
ThreadStateschecks forBasicThread’s state. The state is nowTERMINATED. The flagkeepRunningis now set to false.ThreadStatesexits the while loop and ends its processing.