Thread isAlive() method in java



Contents of page :
  • Salient features of isAlive() method >
  • When thread is alive?
  • Program to demonstrate usage of isAlive() method >
  • Output analyzation >
  • Thread state - Thread is alive in runnable state >

Salient features of isAlive() method >
  • Definition of isAlive() method
public final native boolean isAlive();

  • method is used to find out whether thread is alive or not.
  • Method returns true if this thread is alive else it returns false.
  • Method is native, it’s implementation is provided by JVM.
  • is a Thread class method.

When thread is alive?
A thread is alive if it has been started and has not yet die.
Thread starts when it enters run() method and dies when it exits run() method.


Program to demonstrate usage of isAlive() method >
class MyRunnable implements Runnable{
  
   @Override
   public void run(){
          System.out.println("in run() method > isAlive = "
                       +Thread.currentThread().isAlive());
   }
}

/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class IsAliveTest{
  
   public static void main(String[] args) throws InterruptedException {
         
          Thread thread1=new Thread(new MyRunnable(),"Thread-1");
          System.out.println("before starting thread > isAlive = "
                       +thread1.isAlive());
          thread1.start();
         
          Thread.sleep(1000);// optional delay to ensure thread-1 completes run() method and die.
         
          System.out.println("after completion of run() method > isAlive = "
                       +thread1.isAlive());
   }
  
}
/*OUTPUT
before starting thread > isAlive = false
in run() method > isAlive = true
after completion of run() method > isAlive = false
*/


Output analyzation >

before starting thread > isAlive = false
When start() method wasn’t call on thread it wasn’t alive.


in run() method > isAlive = true
in run() method thread it was alive.

after completion of run() method > isAlive = false
after completion of run() method thread wasn’t alive.



When start() method is called on thread it enters runnable state.
>As soon as Thread enters runnable state it is eligible to run, but not running. (Thread scheduler has not scheduled the Thread execution yet, Thread has not entered in run() method yet), thread will be scheduled at discretion of implementation.
>Thread is considered alive in runnable state.



RELATED LINKS>



Important Thread methods (salient features, usage with programs)>

Sleep() method in threads - 10 key features with programs

Wait() and notify() methods- Definition, 8 key features, solving consumer producer problem with & without these methods and consequences of not using wait() and notify() methods.

Daemon threads - 12 salient features of Daemon Thread

2 alternate ways to stop thread, as stop() method is deprecated

Join() method - 10 salient features of join

Thread isAlive() method

interrupt() method of thread in java

holdsLock(object) method to find whether current thread holds the lock on monitor of specified object.

Thread priorities - setPriority() and getPriority() methods

Using Suspend and resume method in threads



Methods for Adding ShutdownHook and handling uncaught runtime exception >

Threads addShutdownHook method in java

Handling uncaught runtime exception generated in run method using - setDefaultUncaughtExceptionHandler method




eEdit
Must read for you :