public class TestClass extends Thread{
String name = "";
public TestClass(String str){
name = str;
}
public void run(){
try{
Thread.sleep( (int) (Math.random()*5000) );
System.out.println(name);
}
catch(Exception e)
{
}
}
public static void main(String[] str) throws Exception{
Thread t1 = new TestClass("tom");
Thread t2 = new TestClass("dick");
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("harry");
}
}
The main thread waits for t1 and t2 to complete the execution , so only the last print statement is guaranteed to print last. What changes in the code is required if I need to ensure that tom, dick and harry is printed sequentially.Can thread priorities help ?.