Thread States / Thread Life Cycle in Java

0
9

This tutorial is all about Thread States / Thread Life Cycle in Java.
In this post we will about Thread States in Java or in other terms Thread Life Cycle.
Below are the various stages in a Thread Life :

NEW : Before start()  method

RUNNABLE : After start() method

BLOCKED : Whenever thread is going under deadlock

WAITING: While calling wait() method and while calling join() method

TIMED WAITING: while sleeping

TERMINATED: once execution get over or while calling stop() method

 

We can also verify above states by writing a simple line of code as below:

  1. public class Manager{
  2.  
  3. public static void main(String args[]){
  4.  
  5. Thread.State state[]=Thread.State.values() ; // define one state array
  6.  
  7. for(Thread.State state:states){
  8.  
  9. // keep for each loop iterating all indexes of array
  10.  
  11. System.out.println (state ) ;
  12.  
  13. }
  14.  
  15. }
  16.  
  17. }

 

OUTPUT:

new

Runnable

Blocked

Waiting

Timed waiting

Terminated

 

Readers might read this also:

 

 

 

Facebook Comments
(Visited 7 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here