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

Java Thread Life Cycle

Whenever we create a thread in Java, it goes through different states in its life cycle.
Java provides a Thread.State enum for this, which contains the main states.

1. New State
• When we create an object of Thread class, but the start() method is not called, then the thread is in New state.
• In this state thread exists only as object.
Example:
• Thread t = new Thread(); // New state

2. Runnable State
• When we call the start() method, the thread goes into Runnable state.
• It means that the thread is ready to be executed by the CPU, but the scheduler cannot give it CPU yet.
• Here thread stays in ready queue.
Example:
• t.start(); // Runnable state

3. Running State
• When the CPU scheduler selects a thread and gives it CPU, then the thread is in the Running state.
• At this time the thread executes its run() method.
• Note: Only one thread can be running at a time, all others remain in runnable state.

4. Waiting / Blocked / Timed Waiting State
Sometimes the thread has to wait for some time or needs some resource, then it goes into waiting state or blocked state.

(a) Waiting State

• When a thread waits for a signal from another thread (like the wait() method), it enters the waiting state.
• From this stage, the thread is not exited until the second thread notify() or notifyAll() is done to it.

(b) Timed Waiting State
• When we tell a thread to wait for some time (like sleep(1000), join(500), wait(2000)), then it goes into Timed waiting state.
• When the specified time is completed, the thread returns to its runnable state.

(c) Blocked State
• When a thread wants a resource that is already being used by another thread, it goes into the blocked state.
• Eg: Waiting for resource lock in synchronized block.

5. Terminated / Dead State
• When a thread completes its task or its execution ends, it goes into dead state.
• Once a thread is dead, it cannot be restarted.
• If we call start() on dead thread then IllegalThreadStateException error is now.

Example:

class MyThread extends Thread {
public void run() {
try {
System.out.println(“Thread is running…”);

// Timed Waiting state
Thread.sleep(2000);
System.out.println(“Thread is running again…”);
} catch (InterruptedException e) {
System.out.println(e);
}
}
public static void main(String args[]) {
MyThread t1 = new MyThread(); // New state
System.out.println(“Thread created (New State)”);
t1.start(); // Runnable state
System.out.println(“Thread started (Runnable State)”);
}
}

O/P:

Thread created (New State)
Thread started (Runnable State)
Thread is running
Thread is running again

Real Life Example

Imagine a student going to take an exam:
1. New → Student wrote the name (admission taken, but did not start the study).
2. Runnable → The student is ready to study, but has not taken the book yet.
3. Running → Student is studying (Using CPU).
4. Waiting → The student is waiting for the teacher’s notes.
5. Terminated → The exam is over, the work is done.

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 *