Thread life cycle in java

In this tutorial you will learn about the thread life cycle of java.

Below is the block diagram of thread life cycle. We will take a close look of all the stages of thread life cycle.

thread-life-cycle-in-java_files001

 

New born stage: in this stage the thread is newly created and has not been started yet. You can see by diagram that this is first stage of all the thread, before start any thread; it has to crate the instance of thread. You can directly go to either dead or active stage from new born stage.

Active stage:  this is the stage where thread executes. A thread can be come in active stage only by new born stage.

Any thread can be come in active stage by calling start () method of that thread

Being in the active stage does not mean that thread is running, because there may be chance that

There is more thread which has the high priority than yours thread.

In active stage there is two more stages called as Running and Runnable stage.

Running stage: This is the stage where thread executes actually. It is thread shoulder responsibility to decide which thread should execute first. At a time only one thread can be in this stage, all other active thread will be in Runnable stage waiting for their execution time.

Runnable stage: this is the active stage where the threads are waiting for their term for execution.

You can move any thread from Running stage to Runnable stage by using yaield() method of thread class.

Blocked stage: This is the stage where the thread has to block for execution, from this stage a thread can be come back to active stage and can be directly send to dead stage.

There are some methods in thread which can be cause thread to go into blocked thread.

Wait(), suspend() and sleep() are method which can be used to bring thread into blocked stage.

To come back into active stage you have to use notify() or notifyAll() if thread came by wait() method in blocked stage.

To come back into active stage you have to use resume() method if thread came by suspend() method of thread.

The thread will come back automatically into active stage if time over given in milliseconds if thread came by sleep() method.

Dead stage: this is the last stage of thread life cycle. A thread has to come in dead stage if it has finished execution, you can explicitly bring a thread to dead stage from any other stage by using stop() method of thread class.