Thread States / Thread Life Cycle in Java

0
104

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:

[java]

public class Manager{

public static void main(String args[]){

Thread.State state[]=Thread.State.values() ; // define one state array

for(Thread.State state:states){

// keep for each loop iterating all indexes of array

System.out.println (state ) ;

}

}

}

[/java]

 

OUTPUT:

new

Runnable

Blocked

Waiting

Timed waiting

Terminated

 

Readers might read this also:

 

 

 

Facebook Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.