group-icon
Java Developers
Group for Java developers and programmers. Ask questions and get answers related to Java programming
263 Members
Join this group to post and comment.
DHIRAJ LOTLIKAR
DHIRAJ LOTLIKAR
Information Technology
02 Feb 2019

What is the life cycle of a thread?

Java is a multi-threaded programming language which means we can develop multi-threaded program using Java.

A thread goes through various stages in its life cycle. For example, a thread is born, started, runs, and then dies. The following diagram shows the complete life cycle of a thread.

Following are the stages of the life cycle −


1) New − A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born thread.

2)Runnable − After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task.

3)Waiting − Sometimes, a thread transitions to the waiting state while the thread waits for another thread to perform a task. A thread transitions back to the runnable state only when another thread signals the waiting thread to continue executing.

4)Timed Waiting − A runnable thread can enter the timed waiting state for a specified interval of time. A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs.

5)Terminated (Dead) − A runnable thread enters the terminated state when it completes its task or otherwise terminates

Steps for Creating a thread

Step 1: As a first step, you need to implement a run() method provided by a Runnable interface. This method provides an entry point for the thread and you will put your complete business logic inside this method. Following is a simple syntax of the run() method ,

public void run()

Step 2

As a second step, you will instantiate a Thread object using the following constructor

Where, threadObj is an instance of a class that implements the Runnable interface and threadName is the name given to the new thread.

Thread(Runnable threadObj , string threadName);

Step 3

Once a Thread object is created, you can start it by calling start() method, which executes a call to run( ) method. Following is a simple syntax of start() method 

void start();