by Dinesh Thakur

To determine whether a thread has finished. we can call isAlive() on the thread. This method is defined by Thread and its syntax is :

final boolean isAlive()

This method returns true if the thread upon which it is called is still running otherwise it returns false.

 

class isAliveThread implements Runnable

            {
               Thread t;
               isAliveThread(String n)
              {
                 t=new Thread(this,n);
                 System.out.println("Thread is :"+t);
              }
                     public void run()
                    {
                        try
                          {
                          for(int i=1;i<=5;i++)
                               {
                                  System.out.println(t.getName()+":"+i);
                                  t.sleep(200);
                               }
                          }
                                         catch(InterruptedException e)
                                            {
                                    System.out.println(t.getName()+" is Interrupted");
                                    }
                   }
            }
                           class isAliveThread2 implements Runnable
                              {
                                  Thread t;
                                  isAliveThread2(String n)
                                    {
                                       t=new Thread(this,n);
                               System.out.println("Thread is : "+t);
                            }
                                       public void run()
                                 {
                                     try
                                       {
                                           for(int i=1;i<=10;i++)
                                          {
                                             System.out.println(t.getName()+":"+i);
                                             t.sleep(200);
                                          }
                                       }  
                                               catch(InterruptedException e)                             
                                                             {
                                                    System.out.println(t.getName()+" is interrupted");
                                                 }
                                }             
                           }
                                    class isAliveJavaExample
                                     {
                                        public static void main(String args[])
                                        {
                                           isAliveThread k1=new isAliveThread("AWT Thread");
                                           isAliveThread2 k2=new isAliveThread2("Swing Thread");
                                           k1.t.start();
                                           k2.t.start();
                                           while(k1.t.isAlive() && k2.t.isAlive())
                                              {
                                                   try
                                                   {
                                                    Thread.sleep(400);
                                                     }
                                                    catch(InterruptedException e)
                                                           {
                                                            }
                                                 }
                                                                    if(!k1.t.isAlive())
                                                    System.out.println("AWT Thread is Over");
                                                    if(!k2.t.isAlive())
                                                    System.out.println("Swing Thread is Over");
                                        }
                                            }

isAlive() in Java Example