In this article we will learn about different thread states along with an example program that demonstrates thread life cycle.
Life cycle of a thread refers to all the actions or activities done by a thread from its creation to termination. A thread can be in any one of the following five states during its life cycle:
- New: A thread is created but didn’t begin its execution.
- Runnable: A thread that either is executing at present or that will execute when it gets the access to CPU.
- Terminated: A thread that has completed its execution.
- Waiting: A thread that is suspended because it is waiting for some action to occur. For example, it is waiting because of a call to non-timeout version of wait() method or join() method or sleep() method.
- Blocked: A thread that has suspended execution because it is waiting to acquire a lock or waiting for some I/O event to occur.
The life cycle of a thread is illustrated in the following diagram:
The state of a thread can be retrieved using the getState() method of the Thread class. The syntax of this method is as follows:
Thread.State getState()
The getState() method returns one of the values in State enumeration. The constants in State enumerations are:
- NEW
- RUNNABLE
- TERMINATED
- BLOCKED
- WAITING
- TIMED_WAITING
Below example demonstrates the use of getState() method:
class MyThread implements Runnable
{
Thread t;
MyThread(String name)
{
t = new Thread(this, name);
System.out.println("Child thread: " + t);
System.out.println("State: " + t.getState());
t.start();
}
@Override
public void run()
{
System.out.println("State: " + t.getState());
try
{
for(int i = 1; i <= 3; i++)
{
System.out.println(t.getName() + ": " + i);
Thread.sleep(2000);
}
}
catch(InterruptedException e)
{
System.out.println(t.getName() + " is interrupted!");
}
System.out.println(t.getName() + " is terminated");
}
}
public class Driver
{
public static void main(String[] args)
{
MyThread thread1 = new MyThread("First Thread");
try
{
System.out.println("Main thread is waiting...");
thread1.t.sleep(1000);
System.out.println("State: " + thread1.t.getState());
thread1.t.join();
System.out.println("State: " + thread1.t.getState());
}
catch(InterruptedException e)
{
System.out.println("Main thread is interrupted!");
}
System.out.println("Main thread terminated");
}
}
Output for the above program is:
Child thread: Thread[First Thread,5,main]
State: NEW
Main thread is waiting…
State: RUNNABLE
First Thread: 1
State: TIMED_WAITING
First Thread: 2
First Thread: 3
First Thread is terminated
State: TERMINATED
Main thread terminated
Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.
He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.
He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.
In the given Java thread life cycle example, the thread transitions through various states like ‘NEW,’ ‘RUNNABLE,’ and ‘TERMINATED.’ Could you explain how the thread state changes from ‘RUNNABLE’ to ‘TIMED_WAITING’ in the context of the provided code? What causes this transition and how does it affect the execution flow of the program?
The main thread enters from ‘Runnable” to ‘Timed_Waiting’ because of the sleep method and we are using join method to make thread “wait” for the completion of the child thread (Thread 1).