Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – isAlive() Method

Java Programming Language Multithreading isAlive Method

This method is used to verify whether the thread is alive or dead. It is defined in thread class.

The syntax of isAlive() method

final boolean isAlive()

The return type of this method is boolean.
If the thread has completed its task and went into a dead state, it returns false on calling isAlive() method on it, and it returns true if it is alive.

Example

class ChildThread extends Thread
{
    String name;
      
    ChildThread(String n)
    {
        name = n;
    }
      
    public void run()
    {
        Thread.currentThread().setName(name);
        System.out.println(Thread.currentThread().getName()+" Executed");
    }
}

class IsAliveDemo
{
    public static void main(String args[])
    {
        ChildThread c1 = new ChildThread("C1");
        ChildThread c2 = new ChildThread("C2");
        ChildThread c3 = new ChildThread("C3");
        ChildThread c4 = new ChildThread("C4");
             
        c1.start();
        c2.start();
        c3.start();
        c4.start();
             
        System.out.println("Is Child Thread C1 Alive :: "+c1.isAlive());
        System.out.println("Is Child Thread C2 Alive :: "+c2.isAlive());
        System.out.println("Is Child Thread C3 Alive :: "+c3.isAlive());
        System.out.println("Is Child Thread C4 Alive :: "+c4.isAlive());
    }
}

Output

C2 Executed
Is Child Thread C1 Alive :: true
Is Child Thread C2 Alive :: false
Is Child Thread C3 Alive :: true
Is Child Thread C4 Alive :: true
C4 Executed
C1 Executed
C3 Executed

 

Leave a reply