首页 > 解决方案 > 我们可以将 Java 中的最终重新排序规则视为发生在规则之前吗?

问题描述

Java中存在两条最终记录规则,如下:

  1. a write to final field in constructor并且the constructed instance reference is assigned to variable afterwards不能重新排序
  2. read a instance reference并且read the final field in the instance afterwards不能重新排序

我们可以将上述规则视为发生之前的规则吗?

  1. a write to final field in constructor发生之前the constructed instance reference is assigned to variable afterwards
  2. read a instance reference发生之前read the final field in the instance afterwards

我认为发生在规则之前的语义更强。

标签: javafinalhappens-before

解决方案


最后,在我的理解中,final关键字和happens-before规则的语义保证属于不同的类别。上述推导是错误的和不必要的。


推荐阅读