简短答案
您忘了声明
guard为易失性布尔值。
如果您将字段声明省略为
volatile,则不会告诉JVM该字段可以被多线程看到,例如您的示例。
在这种情况下,的值
guard将被读取一次,并且将导致无限循环。它将针对以下内容进行优化(无打印):
if(!guard){ while(true) { }}现在为什么要
System.out.println改变这种行为?因为
writes是同步的,这迫使线程不缓存读取。
这里粘贴了
println一种
PrintStream使用的方法的代码
System.out.println:
public void println(String x) { synchronized (this) { print(x); newline(); }}和
write方法:
private void write(String s) { try { synchronized (this) { ensureOpen(); textOut.write(s); textOut.flushBuffer(); charOut.flushBuffer(); if (autoFlush && (s.indexOf('n') >= 0)) out.flush(); } } catch (InterruptedIOException x) { Thread.currentThread().interrupt(); } catch (IOException x) { trouble = true; }}注意同步。



