Thread Life Cycle in Java

 Posted On  | Yashwant Chavan 

When Java Thread executing , it goes from different states called Life Cycle states. In this article we will see the details about each thread states. From Thread creation to Thread Completion.

New State

When Thread instance has been created, but nothing is called on thread instance that means start() method is not invoked yet. This state is Called New State. At this state thread is not alive.

Runnable State

When you invoke the start() method on thread instance, It is eligible to run. But First thread enter in Runnable State , At this time You can say that Thread is Alive state.

Running State

In this state all the action is performed, Scheduler select the eligible threads from Runnable pool and start the execution.

Waiting, Blocking State

In this state thread is still alive and not eligible to run, Also not in runnable state, Might be return to a runnable state. Thread goes to in this state because of long I/O blocking, or some other thread acquired lock on desired object or may be in sleep state.

Dead State

When run method is completed at this time you can say that Thread is in Dead state. Once the thread is dead you can not make it alive. If you try to invoke start() method again on thread instance you will get the Run time exception as java.lang.IllegalThreadStateException

package com.technicalkeeda.app;

public class ThreadExample extends Thread {

    public void run() {
     // Running State
        System.out.println("My Thread...");

 }
 //Dead state after method completion

    public static void main(String[] args) {
        // New State
        ThreadExample myThread = new ThreadExample();
        myThread.start();
        // Runnable State 
    }

}

Output

My Thread...


© technicalkeeda.com 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0330 seconds.