Java字节码指令相对于GC始终是原子的(即,执行单个指令时不会发生周期)。
GC只能运行在两个字节码指令之间。
查看javac为代码中的if指令生成的字节码,我们可以简单地检查一下GC是否会产生作用:
// a GC here wouldn't change anythingALOAD 1// a GC cycle here would update all references accordingly, even the one on the stackALOAD 2// same here. A GC cycle will update all references to the object on the stackIF_ACMPNE L3// this is the comparison of the two references. no cycle can happen while this comparison// "is running" so there won't be any problems with this either
另外,即使GC能够在执行字节码指令期间运行,该对象的引用也不会更改。在循环之前和之后,它仍然是同一对象。
因此,简而言之,您的问题的答案是“否”,它将始终输出true。



