Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Multithreading. 2 Comments on Thread States
5
(1)

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:

 

thread-states


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


 

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:

  1. NEW
  2. RUNNABLE
  3. TERMINATED
  4. BLOCKED
  5. WAITING
  6. 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

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

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.

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

2 Comments

You can follow any responses to this entry through the RSS 2.0 feed.

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).

Leave a Reply

Your email address will not be published. Required fields are marked *