Pages

Program to implement join method

/* Program to implement join method */
class A extends Thread
{
  int sum;
  public void run()
  {
    for(int i=1;i<=10;i++)
          sum+=i;
    System.out.println(“Sum in A = “ + sum);
    try{
            Thread.sleep(100);
        }
    catch(Exception e){System.out.println(e);}
   System.out.println(“End of Thread A”);
  }
}// end of thread A
class B extends Thread
{
  public void run()
  {
    for(int j=1;j<=10;j++)
       System.out.println(“j = “ + j);
  }
} // end of thread B
class C extends Thread
{
  A a1;
  C(A a1)
  {
    this.a1 = a1;
  }
  public void run()
  {
    for(int i=1;i<=10;i++)
    {
       System.out.println(“j = “ + j);
       if(i == 5)
       { 
         a1.join();
         System.out.println(“Sum = “ + a1.sum);
      } // if
    } // for
  } run
 }// end class
class JoinDemo
{
  public static void main(String args[])
  {
    A a1 = new A();
    B b1 = new B();
    C c1 = new C(a1);
    a1.start();
    b1.start();
    c1.start();
  } // main

} // class