您忘了声明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;
}
}
注意同步。