我写这篇文章是为了深入理解 Java 中的 volatile
public class Main {
private int x;
private volatile int g;
public void actor1(){
x = 1;
g = 1;
}
public void actor2(){
put_on_screen_without_sync(g);
put_on_screen_without_sync(x);
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我正在分析 JIT 为上述代码生成的内容。从我们在上一篇文章中的讨论中我们知道输出1, 0是不可能的,因为:
写挥发v的原因,每一个动作a前述v原因,那a之前是可见的(将被刷新到内存)v将是可见的。
.................(I removed not important body of method).....
0x00007f42307d9d5e: c7460c01000000 (1) mov dword ptr [rsi+0ch],1h
;*putfield x
; - package.Main::actor1@2 (line 14)
0x00007f42307d9d65: bf01000000 (2) mov edi,1h
0x00007f42307d9d6a: 897e10 (3) mov …Run Code Online (Sandbox Code Playgroud)