Saturday, January 11, 2014

How to check a thread is alive or not?

public class Thread8 extends Thread
{
    public void run()
    {
        for(int i=0;i<10;i++)
        {
            printMsg();
        }
    }
    public void printMsg()
    {
        Thread t=Thread.currentThread();
        String name=t.getName();
        System.out.println("   Name = "+name);
    }
    public static void main(String[] surat)
    {
        Thread8 t8 = new Thread8();
        t8.setName("MaHi");
        System.out.println("before start method, t8.isAlive() : "+t8.isAlive());
        t8.start();
        System.out.println("just after start method, t8.isAlive() : "+t8.isAlive());
        for(int i=0;i<10;i++)
        {
            t8.printMsg();
        }
        System.out.println("At the end of main method, t8.isAlive()=" + t8.isAlive());
        }
}

Output:

No comments:

Post a Comment