public class NoVisibility {
private static boolean ready;
private static int num;
public static void main(String[] args) throws InterruptedException {
new Thread(new Runnable() {
@Override
public void run() {
while (!ready) {
}
System.out.println("num:" + num);
}
}).start();
Thread.sleep(1000);
num = 20;
ready = true;
}
}
求助各位大牛,上面的代码在-server的jvm中 无限循环,一直没想通,主线程设置ready=true,循环的线程为什么会无限循环呢?