流不重要.只需要两个简单的线程就可以观察到相同的效果,例如
this answer.
当a不是volatile时,JIT编译器可以优化(实际上是!)连续赋值.
a = 1;
a = 2;
a = 3;
变成了
a = 3;
此外,JIT编译器还优化if(a == 1& a = a = 2&& a == 3)到if(false),然后安全地将整个testValue()调用作为死代码删除.
让我们看一下为lambda生成的程序集.
要打印已编译的代码,我使用-XX:CompileCommand = print,Race :: lambda $main $0.
# {method} {0x000000001e142de0} 'lambda$main$0' '(I)V' in 'Race'
# parm0: rdx = int
# [sp+0x20] (sp of caller)
0x00000000052eb740: sub rsp,18h
0x00000000052eb747: mov qword ptr [rsp+10h],rbp ;*synchronization entry
; - Race::lambda$main$0@-1 (line 8)
0x00000000052eb74c: mov r10,76b8940c0h ; {oop(a 'java/lang/Class' = 'Race')}
0x00000000052eb756: mov dword ptr [r10+68h],3h ;*putstatic a
; - Race::lambda$main$0@9 (line 10)
0x00000000052eb75e: add rsp,10h
0x00000000052eb762: pop rbp
0x00000000052eb763: test dword ptr [3470000h],eax
; {poll_return}
0x00000000052eb769: ret
除了方法序言和eplilogue之外,只有一条指令存储值3:
mov dword ptr [r10+68h],3h ;*putstatic a
因此,一旦编译该方法,System.out.println就不会发生.当您看到“成功”时,极少数情况发生在解释期间,当代码尚未进行JIT编译时.