Joining a thread

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

The join() method

The join () method waits for a thread to die. In other words, it causes the currently running threads to stop executing until the thread it joins with completes its task.

Syntax:

public void join()throws InterruptedException
public void join(long milliseconds)throws InterruptedException

Example of join() method

 
  1. class TestJoinMethod1 extends Thread{  
  2.  public void run(){  
  3.   for(int i=1;i<=5;i++){  
  4.    try{  
  5.     Thread.sleep(500);  
  6.    }catch(Exception e){System.out.println(e);}  
  7.   System.out.println(i);  
  8.   }  
  9.  }  
  10. public static void main(String args[]){  
  11.  TestJoinMethod1 t1=new TestJoinMethod1();  
  12.  TestJoinMethod1 t2=new TestJoinMethod1();  
  13.  TestJoinMethod1 t3=new TestJoinMethod1();  
  14.  t1.start();  
  15.  try{  
  16.   t1.join();  
  17.  }catch(Exception e){System.out.println(e);}  
  18.   
  19.  t2.start();  
  20.  t3.start();  
  21.  }  
  22. }  

Test it Now

Output:1
       2
       3
       4
       5
       1
       1
       2
       2
       3
       3
       4
       4
       5
       5

As you can see in the above example,when t1 completes its task then t2 and t3 starts executing.

Example of join (long milliseconds) method

  1. class TestJoinMethod2 extends Thread{  
  2.  public void run(){  
  3.   for(int i=1;i<=5;i++){  
  4.    try{  
  5.     Thread.sleep(500);  
  6.    }catch(Exception e){System.out.println(e);}  
  7.   System.out.println(i);  
  8.   }  
  9.  }  
  10. public static void main(String args[]){  
  11.  TestJoinMethod2 t1=new TestJoinMethod2();  
  12.  TestJoinMethod2 t2=new TestJoinMethod2();  
  13.  TestJoinMethod2 t3=new TestJoinMethod2();  
  14.  t1.start();  
  15.  try{  
  16.   t1.join(1500);  
  17.  }catch(Exception e){System.out.println(e);}  
  18.   
  19.  t2.start();  
  20.  t3.start();  
  21.  }  
  22. }  

Test it Now

Output:1
       2
       3
       1
       4
       1
       2
       5
       2
       3
       3
       4
       4
       5
       5
  1. class TestJoinMethod2 extends Thread{  
  2.  public void run(){  
  3.   for(int i=1;i<=5;i++){  
  4.    try{  
  5.     Thread.sleep(500);  
  6.    }catch(Exception e){System.out.println(e);}  
  7.   System.out.println(i);  
  8.   }  
  9.  }  
  10. public static void main(String args[]){  
  11.  TestJoinMethod2 t1=new TestJoinMethod2();  
  12.  TestJoinMethod2 t2=new TestJoinMethod2();  
  13.  TestJoinMethod2 t3=new TestJoinMethod2();  
  14.  t1.start();  
  15.  try{  
  16.   t1.join(1500);  
  17.  }catch(Exception e){System.out.println(e);}  
  18.   
  19.  t2.start();  
  20.  t3.start();  
  21.  }  
  22. }  

Test it Now

Output:1
       2
       3
       1
       4
       1
       2
       5
       2
       3
       3
       4
       4
       5
       5
  1. class TestJoinMethod2 extends Thread{  
  2.  public void run(){  
  3.   for(int i=1;i<=5;i++){  
  4.    try{  
  5.     Thread.sleep(500);  
  6.    }catch(Exception e){System.out.println(e);}  
  7.   System.out.println(i);  
  8.   }  
  9.  }  
  10. public static void main(String args[]){  
  11.  TestJoinMethod2 t1=new TestJoinMethod2();  
  12.  TestJoinMethod2 t2=new TestJoinMethod2();  
  13.  TestJoinMethod2 t3=new TestJoinMethod2();  
  14.  t1.start();  
  15.  try{  
  16.   t1.join(1500);  
  17.  }catch(Exception e){System.out.println(e);}  
  18.   
  19.  t2.start();  
  20.  t3.start();  
  21.  }  
  22. }  

Test it Now

Output:1
       2
       3
       1
       4
       1
       2
       5
       2
       3
       3
       4
       4
       5
       5

 

In the above example,when t1 is completes its task for 1500 miliseconds(3 times) then t2 and t3 starts executing.

get Name(),set Name(String) and getId() method:

public String getName()
public void setName(String name)
public long getId()
 
  1. class TestJoinMethod3 extends Thread{  
  2.   public void run(){  
  3.    System.out.println("running...");  
  4.   }  
  5.  public static void main(String args[]){  
  6.   TestJoinMethod3 t1=new TestJoinMethod3();  
  7.   TestJoinMethod3 t2=new TestJoinMethod3();  
  8.   System.out.println("Name of t1:"+t1.getName());  
  9.   System.out.println("Name of t2:"+t2.getName());  
  10.   System.out.println("id of t1:"+t1.getId());  
  11.   
  12.   t1.start();  
  13.   t2.start();  
  14.   
  15.   t1.setName("Sonoo Jaiswal");  
  16.   System.out.println("After changing name of t1:"+t1.getName());  
  17.  }  
  18. }  

Test it Now

Output:Name of t1:Thread-0
       Name of t2:Thread-1
       id of t1:8
       running...
       After changling name of t1:Sonoo Jaiswal
       running...

The currentThread() method:

The currentThread() method returns a reference to the currently executing thread object.

Syntax:

public static Thread currentThread()

Example of currentThread() method

 
  1. class TestJoinMethod4 extends Thread{  
  2.  public void run(){  
  3.   System.out.println(Thread.currentThread().getName());  
  4.  }  
  5.  }  
  6.  public static void main(String args[]){  
  7.   TestJoinMethod4 t1=new TestJoinMethod4();  
  8.   TestJoinMethod4 t2=new TestJoinMethod4();  
  9.   
  10.   t1.start();  
  11.   t2.start();  
  12.  }  
  13. }  

Test it Now

Output:Thread-0
       Thread-1