ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

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

Facebook Comments

Check Your Domain Ranking

Leave a Reply

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

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!