Thread Life Cycle In Java

Introduction

 
In this article, we discuss the life cycle of a thread in Java.
 

What is Thread

 
A thread is a single sequential flow of control in a program. Sometimes called a lightweight process, so there are 2 different definitions for explaining this topic.
 

States of threads

 
A thread life cycle contains the following states:
  1. New State
  2. Runnable State
  3. Running State
  4. Blocked (Non-Runnable) State
  5. Terminated State
pic-1.jpg
 

1. Initializing Thread

 
When we start a thread it goes into the new state.
 
In this state, the thread is only created but not working.
 
We can move the thread to the running state by invoking the "start()" method & the thread can be killed by the "stop()" method.
 
Syntax
 
Thread thread = new Thread;
 
Example
 
In this example we create a simple thread; we use the "sleep()" method for delaying the thread for execution.
  1. public class ThreadEx {  
  2.     public static void main(String args[]) throws Throwable {  
  3.         new ThreadEx().threadfunctn();  
  4.     }  
  5.     void threadfunctn() throws Throwable {  
  6.         for (int i = 0; i < 10; i++) {  
  7.             System.out.println("thread in " + Thread.currentThread().getName() + " process " + i);  
  8.             Thread.sleep(500);  
  9.         }  
  10.     }  
  11. }  
Output
 
pic-2.jpg
 

2. Runnable state of Thread

 
In this state Thread is in the running mode & waiting for control input.
 
We can move control to another thread by the "yield()" method.
 
Example
 
In this example we create two parallel threads using the "runnable()" interface, in other words using a "run()" method.
  1. public class RunnableEx implements Runnable {  
  2.     public static void main(String args[]) throws Throwable {  
  3.         RunnableEx run1 = new RunnableEx();  
  4.         RunnableEx run2 = new RunnableEx();  
  5.         new Thread(run1).start();  
  6.         new Thread(run2).start();  
  7.         // Our main thread is stopped here,  
  8.         // new thread-0 and new thread-1 are continued....  
  9.     }  
  10.     public void run() {  
  11.         try {  
  12.             for (int i = 0; i < 10; i++) {  
  13.                 System.out.println("In thread " + Thread.currentThread().getName() + " processed " + i);  
  14.                 Thread.sleep(500);  
  15.             }  
  16.         } catch (Throwable tr) {}  
  17.     }  
  18. }  
Output
 
pic-3.jpg
 

3. Running state of Thread

 
In this state the thread is ready for execution since the control of the CPU is provided to that thread.
 
Syntax
 
thread.start()
 
Example
  1. public class RunEx extends Thread {  
  2.     public static void main(String args[]) throws Throwable {  
  3.         new RunEx().start();  
  4.         new RunEx().start();  
  5.         // Our main thread Stopped here,  
  6.         // in thread-0 and thread-1 continued..........  
  7.     }  
  8.     public void run() {  
  9.         try {  
  10.             for (int i = 0; i < 5; i++) {  
  11.                 System.out.println(" In thread " + Thread.currentThread().getName() + " process " + i);  
  12.                 Thread.sleep(520);  
  13.             }  
  14.         } catch (Throwable tr) {}  
  15.     }  
  16. }  
Output
 
pic-4.jpg
  

4. Blocked (Non-Runnable) State of Thread

 
When our thread is not permitted to enter the Running and Runnable states, then we say it is in the blocked state.
 
The various ways a thread can go into a blocked state are:
  1. By calling the "suspend()" method.
  2. By calling the "sleep()" method.
  3. When the "wait()" method is called for synchronization.
  4. When an I/O operation is performed in our program, the thread is implicitly blocked by the JVM.
Example
  1. class BlockedEx extends Thread {  
  2.     public void run() {  
  3.         for (int i = 1; i < 5; i++) {  
  4.             try {  
  5.                 Thread.sleep(4500);  
  6.             } catch (InterruptedException eb) {  
  7.                 System.out.println(eb);  
  8.             }  
  9.             System.out.println(i);  
  10.         }  
  11.     }  
  12.     public static void main(String args[]) {  
  13.         BlockedEx t1 = new BlockedEx();  
  14.         BlockedEx t2 = new BlockedEx();  
  15.         t1.start();  
  16.         t2.start();  
  17.     }  
  18. }  
Output
 
pic-5.jpg
 

5. Dead state of Thread

 
When the process of the "run()" method of the thread is completed, the process of the particular thread is completed, in other words ended.
 
We can kill that particular thread by using the "stop()" method; for that particular thread and move it to be in the dead state.
 
Here is the example of a dead state:
  1. BlockedEx t1=new BlockedEx();  
  2. t1.stop();