Checking is thread Alive

 class A extends Thread
  {

    public void run()
    {
          for (int i=1; i<=5;i++)
               {
                   if (i==2)
                       yield();
                   System.out.println(this.getName()+" : "+ i);

               }

     System.out.println("End of child thread : "+this.getName());


    }
}


class B extends Thread
 {

   public void run()
   {
         for (int j=1; j<=5;j++)
              {
                  System.out.println(this.getName()+" : "+ j);


              }
    System.out.println("End of child thread : "+this.getName());


   }
}


class C extends Thread
 {

   public void run()
   {
       for (int k=1; k<=5;k++)
       {
                  System.out.println(this.getName()+" : "+ k);
                  try
                  {
                    sleep(500);
                }
                catch(InterruptedException e)
                {

                }

          }
    System.out.println("End of child thread : "+this.getName());


   }
}

class IsaliveJoinDemo
{
public static void main(String args[])
{
    A threadA=new A();
    B threadB=new B();
    C threadC=new C();

    threadA.setName("Thread A");
    threadB.setName("Thread B");
    threadC.setName("Thread C");


    System.out.println("Start Thread A");
    threadA.start();
    System.out.println("Thread A is alive : "+threadA.isAlive());

    System.out.println("Start Thread B");
    threadB.start();
    System.out.println("Thread B is alive : "+threadB.isAlive());

    System.out.println("Start Thread C");
    threadC.start();
    System.out.println("Thread C is alive : "+threadC.isAlive());

    for (int k=1; k<=5;k++)
    {
        System.out.println("Main Thread  : "+ k);
    }

    try
    {

    threadA.join();
    threadB.join();
    threadC.join();
    }
    catch(InterruptedException e)
    {
    System.out.println("Main Thread  interrupted");
    }

    System.out.println("End of main thread!");

    System.out.println("Thread A is alive : "+threadA.isAlive());
    System.out.println("Thread B is alive : "+threadB.isAlive());
    System.out.println("Thread C is alive : "+threadC.isAlive());
}
}

0 comments:

Post a Comment

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|