gpt4 book ai didi

java - JMM 中最终字段的语义

转载 作者:搜寻专家 更新时间:2023-11-01 03:05:03 26 4
gpt4 key购买 nike

这里 [ http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.5][1]

它说:

Given a write w, a freeze f, an action a (that is not a read of a final field), a read r1 of the final field frozen by f, and a read r2 such that hb(w, f), hb(f, a), mc(a, r1), and dereferences(r1, r2), then when determining which values can be seen by r2, we consider hb(w, r2). (This happens-before ordering does not transitively close with other happens-before orderings.)

他们想在这里说什么?我知道 r2 只是读取了 r1 读取的最终字段值,所以很明显 hb(w, r2),因为 r1 读取了这个变量的正确版本,因为该值被 f 卡住了。或者它们的意思不同?多深?他们还想说这个 hb 排序不会与其他 hb 排序“传递关闭”吗?

最佳答案

r1 读取由 w 写入的值这一事实并不意味着 hb(w, r1),更不用说了hb(w, r2)。反之亦然:如果 wr1 之间存在happens-before 关系,则 JMM 保证将观察到什么值;否则无法保证。如果 r1 确实观察到来自 w 的值,那只是巧合,不会事后导致事前发生 关系。

想象一个重要示例的最佳方式是通过数据竞赛安全发布的用例。对 String 的引用被写入非 volatile 字段,没有同步,然后该字段从另一个线程取消引用。如果线程碰巧观察到String(不能保证一定会),则可以保证线程会看到String处于一致的状态。

如果没有额外的happens-before 的特殊规定(仅在您的报价中描述的精确情况下才考虑),则无法保证 String 将是观察完好。

what do they want to say that this hb ordersing does not "transitively close" with other hb ordersings?

通常,hb(a,b)hb(b,c) 暗示 hb(a,c),但是这种传递性不适用于由于字段卡住而存在的特殊 happens-before。这些特殊的happens-before 有它们自己的“命名空间”。

关于java - JMM 中最终字段的语义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25589821/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com