gpt4 book ai didi

java - Java 最终字段值的 ‘up to date’ 保证是否扩展到间接引用?

转载 作者:IT王子 更新时间:2023-10-28 23:33:33 25 4
gpt4 key购买 nike

Java 语言规范在 section 17.5 中定义了 final 字段的语义。 :

The usage model for final fields is a simple one. Set the final fields for an object in that object's constructor. Do not write a reference to the object being constructed in a place where another thread can see it before the object's constructor is finished. If this is followed, then when the object is seen by another thread, that thread will always see the correctly constructed version of that object's final fields. It will also see versions of any object or array referenced by those final fields that are at least as up-to-date as the final fields are.

我的问题是 - “最新”保证是否扩展到嵌套数组和嵌套对象的内容?

简而言之:如果一个线程将可变对象图分配给对象中的 final 字段,并且对象图永远不会更新,那么所有线程都可以通过 final 字段安全地读取该对象图吗?

一个示例场景:

  1. 线程 A 构造 ArrayLists 的 HashMap,然后将 HashMap 分配给“MyClass”类实例中的最终字段“myFinal”
  2. 线程 B 看到对 MyClass 实例的(非同步)引用并读取“myFinal”,然后访问并读取其中一个 ArrayList 的内容

在这种情况下,线程 B 看到的 ArrayList 的成员是否保证至少与 MyClass 的构造函数完成时一样最新?

我正在寻找对 Java 内存模型和语言规范语义的澄清,而不是像同步这样的替代解决方案。我梦寐以求的答案是肯定或否定,并引用相关文本。

更新:

  • 我对 Java 1.5 及更高版本的语义感兴趣,即通过 JSR 133 引入的更新的 Java 内存模型。此更新中引入了对最终字段的“最新”保证。

最佳答案

In this scenario, are the members of the ArrayList as seen by Thread B guaranteed to be at least as up to date as they were when MyClass's constructor completed?

是的,他们是。

线程第一次遇到引用时需要读取内存。因为 HashMap 是构造的,其中的所有条目都是全新的,所以对对象的引用是 最新 构造函数完成时的状态。

在初次相遇之后,将应用通常的可见性规则。因此,当其他线程更改最终引用中的非最终字段时,其他线程可能看不到该更改,但它仍然会看到来自构造函数的引用。

实际上,这意味着如果你在构造函数之后不修改最终的hash-map,它的内容对于所有线程都是常量。

编辑

我知道我以前在某处看到过这种保证。

这是 article 中的一段有趣的内容描述 JSR 133

Initialization safety

The new JMM also seeks to provide a new guarantee of initialization safety -- that as long as an object is properly constructed (meaning that a reference to the object is not published before the constructor has completed), then all threads will see the values for its final fields that were set in its constructor, regardless of whether or not synchronization is used to pass the reference from one thread to another. Further, any variables that can be reached through a final field of a properly constructed object, such as fields of an object referenced by a final field, are also guaranteed to be visible to other threads as well. This means that if a final field contains a reference to, say, a LinkedList, in addition to the correct value of the reference being visible to other threads, also the contents of that LinkedList at construction time would be visible to other threads without synchronization. The result is a significant strengthening of the meaning of final -- that final fields can be safely accessed without synchronization, and that compilers can assume that final fields will not change and can therefore optimize away multiple fetches.

关于java - Java 最终字段值的 ‘up to date’ 保证是否扩展到间接引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2830739/

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