Using join() method 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:
Output
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);
georgeWill.start();
// let the speakers speak
try {
georgeWill.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Town Hall closing.");
}
Java Concurrency
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() {
try{
Thread.sleep( (int) (Math.random()*1000) );
System.out.println(name);
}
catch(Exception e){
}
}
public static void main(String[] str) throws Exception{
//1
Thread t1 = new TestClass("tom");
//2
Thread t2 = new TestClass("dick");
//3
t1.start();
//4
t2.start();
//5
System.out.println("harry");
//6
}
}
Select 1 option:
- Insert t1.join(); and t2.join(); at //4 and //5 respectively.
- Insert t2.join() at //5
- Insert t1.join(); t2.join(); at //6
- Insert t1.join(); t2.join(); at //3
- Insert t1.join() at //5
Answer: a
Explanation:
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".