Anybody can explain why this program is giving output 9? I guessed "unpredictable behavior" as its answer since main thread and thread-0 can execute in any sequence. What will join() do on main thread itself?
public class Starter extends Thread{
private int x = 2;
public static void main(String[] args) throws Exception{
new Starter().makeItSo();
}
public Starter(){
//Thread main
x=5;
start();
}
public void makeItSo() throws Exception{
//Thread main
join();
x = x-1;
System.out.println(x);
}
public void run(){
//Thread-0
x*= 2;
}
}
Main thread starts the thread-0 and calls the run method. but if you put SOP in makeItSo(), it says that main thread is waiting there after call to join()! why? What I thought there will be no sequence between makeItSo() or run() so value of X will be unpredictable.