Thread example in java

 class A implements Runnable
  {
    String name;
    Thread ThreadA;
    A(String Threadname)
    {
        name=Threadname;
        ThreadA=new Thread(this,name);
        System.out.println("New Thread : "+ThreadA);
        ThreadA.start();
    }
    public void run()
    {
          for (int i=1; i<=5;i++)
               {

                   System.out.println(ThreadA.getName()+" : "+ i);
                try
                  {
                    ThreadA.sleep(500);
                }
                catch(InterruptedException e)
                {

                }
               }

     System.out.println("End of child Thread : "+ThreadA.getName());


    }
}


class B implements Runnable
 {
    String name;
    Thread ThreadB;
    B(String Threadname)
    {
        name=Threadname;
        ThreadB=new Thread(this,name);
        System.out.println("New Thread : "+ThreadB);
        ThreadB.start();
    }
   public void run()
   {
         for (int j=1; j<=5;j++)
              {
                  System.out.println(ThreadB.getName()+" : "+ j);
                try
                  {
                    ThreadB.sleep(500);
                }
                catch(InterruptedException e)
                {

                }

              }
    System.out.println("End of child Thread : "+ThreadB.getName());


   }
}


class C implements Runnable
 {

    String name;
    Thread ThreadC;
    C(String Threadname)
    {
        name=Threadname;
        ThreadC=new Thread(this,name);
        System.out.println("New Thread : "+ThreadC);
        ThreadC.start();
    }
   public void run()
   {
       for (int k=1; k<=5;k++)
       {
                  System.out.println(ThreadC.getName()+" : "+ k);
                  try
                  {
                    ThreadC.sleep(500);
                }
                catch(InterruptedException e)
                {

                }

          }
    System.out.println("End of child Thread : "+ThreadC.getName());


   }
}

class IsaliveJoinRunnable
{
public static void main(String args[])
{
    A a=new A("A");
    B b=new B("B");
    C c=new C("C");


    System.out.println("Thread A is alive : "+a.ThreadA.isAlive());

    System.out.println("Thread B is alive : "+b.ThreadB.isAlive());

    System.out.println("Thread C is alive : "+c.ThreadC.isAlive());

    for (int k=1; k<=5;k++)
    {
        System.out.println("Main Thread  : "+ k);
                try
                  {
                    Thread.sleep(500);
                }
                catch(InterruptedException e)
                {

                }
    }

    try
    {

    a.ThreadA.join();
    b.ThreadB.join();
    c.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 : "+a.ThreadA.isAlive());
    System.out.println("Thread B is alive : "+b.ThreadB.isAlive());
    System.out.println("Thread C is alive : "+c.ThreadC.isAlive());
}
}

0 comments:

Post a Comment

 
 
 
 


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