The following program hangs but if I uncomment the System.out.printf
it exits normally. I wonder why this happens. Thanks in advance.
public class MainClass{
public static class Node{
public Integer val;
Node() {
val = new Integer(0);
}
public void hang() {
int i = 0;
while(this.val != 1) {
// the program quit normally if I uncomment the following line
//System.out.printf("");
i++;
}
System.out.println(i);
System.out.println("quit");
}
private int func(int i) {
return i+1;
}
}
public static void main(String[] args) throws InterruptedException{
Node n = new Node();
Thread t = new Thread(new Runnable(){
@Override
public void run() {
n.hang();
}
});
t.start();
// wait for t to start
Thread.sleep(500);
n.val = new Integer(1);
Thread.sleep(500);
t.join();
}
}
output of java -version
openjdk version "1.8.0_191"
OpenJDK Runtime Environment (build 1.8.0_191-8u191-b12-0ubuntu0.16.04.1-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)