正如预期的那样,下面程序中的读取器线程将永远运行,因为它将停止标志(非易失性)缓存在其本地处理器的缓存中。然而,一旦我在阅读器线程上取消注释 println,标志就会得到更新的标志值并且程序停止。这是怎么可能的,因为编写器线程只将标志写入其自己的本地缓存并且尚未刷新到主内存?
注意:在 MacBook Pro x86 架构的机器上运行这个程序。
public class FieldVisibility {
boolean stop = false;
public static void main(String[] args) throws Exception {
FieldVisibility fv = new FieldVisibility();
Runnable writerThreadJob = () -> { fv.writer(); };
Runnable readerThreadJob = () -> { fv.reader(); };
Thread writerThread = new Thread(writerThreadJob);
Thread readerThread = new Thread(readerThreadJob);
readerThread.start();
try { Thread.sleep(2); } catch (InterruptedException e) {}
writerThread.start();
}
private void writer() {
stop = true;
}
private void reader() {
while (!stop) {
// System.out.println("stop is still false...");
}
}
}