J2EEOnline J2EE 

Java Concepts  «Prev 

Using join() with Java Threads

In addition to ensuring that only one chunk of code can be accessed at once, you can synchronize by splicing processes back together.
Here is the pseudocode for the TownHall class' main() method:
 create the MC  
 create the speakers  
 join the MC's thread with the thread that initiated it  
 close the town hall  
What would happen if we did not attempt to join the MC's thread with the thread that initiated it (which is the thread running the main() method)?
The town hall would close while the MC and the speakers were still inside hashing out the issues. In other words, we would get output that looked something like this:

 MC here: good morning.  
 speaker 4 stepping onto soapbox  
 speaker 1 stepping onto soapbox  
 speaker 3 stepping onto soapbox  
 speaker 0 stepping onto soapbox  
 speaker 0: music should be bought  
 speaker 2 stepping onto soapbox  
 Town Hall closing.  
 speaker 2: dogs should be mandatory  

To join a thread with the thread that initiated it, you simply call the thread's join() method.
The MC for the town hall meeting does this as follows:
 public static void main(String args[]) {  
   //  create the speakers   
   MC georgeWill = new MC(podium);  
   // let the speakers speak  
   try {  
   } catch (InterruptedException e) {  
   System.out.println("Town Hall closing.");  
Note that the join() method might throw an exception, so we have to wrap this call to join() in a try statement.
At the time of this writing, the implementation of the join() method in the Netscape JVM (up to version 4.04) causes the thread to block forever.

Thread Question discussing the join() method

What can be done so that the following program prints "tom", "dick" and "harry" in that order?
public class TestClass extends Thread{
 String name = "";
 public TestClass(String str)  {
   name = str;
 public void run() {
    Thread.sleep( (int) (Math.random()*1000) );
  catch(Exception e){

 public static void main(String[] str) throws Exception{
   Thread t1 = new TestClass("tom");
   Thread t2 = new TestClass("dick");

Select 1 option:
  1. Insert t1.join(); and t2.join(); at //4 and //5 respectively.
  2. Insert t2.join() at //5
  3. Insert t1.join(); t2.join(); at //6
  4. Insert t1.join(); t2.join(); at //3
  5. Insert t1.join() at //5

Answer: a
Here, you have 3 threads in action. The main thread and the 2 threads that are created in main.
The concept is when a thread calls join() on another thread, the calling thread waits till the other thread dies.
If we insert t1.join() at //4, the main thread will not call t2.start() till t1 dies. So, in affect, "tom" will be printed first and then t2 is started. Now, if we put t2.join() at //5, the main thread will not print "harry" till t2 dies. So, "dick" is printed and then the main thread prints "harry".