bhushan23

threadjoin

Jul 8th, 2012
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.37 KB | None | 0 0
  1. class thread implements Runnable
  2. {Thread t;
  3.     String name;
  4.     thread(String tname)
  5.     {
  6.         name =tname;
  7.         t=new Thread(this,name);
  8.         t.start();
  9.         System.out.println("thread "+name+": "+t);
  10.    
  11.     }
  12.     public void run(){
  13.         try{
  14.         for(int i=5;i>0;i--)
  15.         {
  16.         System.out.println("thread "+name+": "+i);
  17.         Thread.sleep(1000);
  18.         }
  19.         }catch(InterruptedException e)
  20.         {
  21.             System.out.println("error ");
  22.         }
  23.     }
  24. }
  25. public class join {
  26. public static void main(String argv[])
  27. {
  28.     thread ob1=new thread("one");
  29.     thread ob2=new thread("two");
  30.     thread ob3=new thread("three");
  31.   System.out.println("thread "+ob1.t +" is "+ob1.t.isAlive());
  32.   System.out.println("thread "+ob2.t +" is "+ob2.t.isAlive());
  33.   System.out.println("thread "+ob3.t +" is "+ob3.t.isAlive());
  34.     try{
  35.         System.out.println("waiting for thread to finish");
  36.         ob1.t.join();
  37.         ob2.t.join();
  38.         ob3.t.join();
  39.             }catch(InterruptedException e)
  40.             {
  41.                 System.out.println("error in main");
  42.             }
  43.              System.out.println("thread "+ob1.t +" is "+ob1.t.isAlive());
  44.               System.out.println("thread "+ob2.t +" is "+ob2.t.isAlive());
  45.               System.out.println("thread "+ob3.t +" is "+ob3.t.isAlive());
  46.            
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment