In this article Life Cycle of Thread in Java we give the information about There are 5 states in the life cycle of a thread. A thread passes through these five states throughout its lifetime.

Life Cycle of Thread in Java:

There are 5 states in the life cycle of a thread. A thread passes through these five states throughout its lifetime.

In Java, the life cycle of a thread is controlled by JVM.

Following are the states of java thread:-

  1. New
  2. Runnable
  3. Running
  4. Blocked / Sleeping
  5. Terminated

Life Cycle of Thread in Java

New

When a new thread is created it is in New state. In this state the thread is not alive and it is not ready for execution i.e. its code is not yet ready to run.

Once the start() method is called, the thread leaves the new state and moves to the next state. Once a thread leaves the new state, it can never return to the new state.

Runnable

When the start() method is called in a thread, the thread comes into runnable state. This means that it is ready to run but it is not running yet because it has not been selected to run by the thread scheduler.

A thread is considered alive in the runnable state. When a thread leaves the runnable state, it can come back to the runnable state.

Running

A thread comes into running state when the thread scheduler selects it to run. In this state, the thread executes the run() method.

From this state, a thread can go into runnable, blocked or terminated state.

Blocked / Sleeping

This state is called blocked or sleeping or waiting state. A thread comes into this state due to three situations.

  • wait() method is called in a thread.
  • sleep() method is called in a thread.
  • When one thread calls the join() method on another thread.

Due to which the first thread has to wait for the execution of the second thread to finish.

In this state the thread is alive but it cannot be run. When the thread comes out of this state, it goes back to the runnable state.

Terminated

This state is also called dead state. This is the last state of the thread. When a thread successfully completes the run() method, it goes into dead state.

In this state the thread is not alive. If you call start() method in dead thread then it will show you the error of IllegalThreadStateException.

Source Code:

public class CheckState extends Thread

{

public void run()

{

System.out.println(“run method”);

}

public static void main(String args[])

{

CheckState t1 = new CheckState();

CheckState t2= new CheckState();

System.out.println(“t1 State : ” + t1.getState());

System.out.println(“t2 State : ” + t2.getState());

t1.start();

System.out.println(“t1 State : ” + t1.getState());

System.out.println(“t2 State : ” + t2.getState());

t2.start();

System.out.println(“t1 State : ” + t1.getState());

System.out.println(“t2 State : ” + t2.getState());

}

}

Output:

t1 State : NEW

t2 State : NEW

t1 State : RUNNABLE

run method

t2 State : NEW

t1 State : TERMINATED

run method

t2 State : RUNNABLE

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

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