Do the ‘up to date’ guarantees for values of Java#39;s final fields extend to indirect references?(Java 的 final 字段值的“最新保证是否扩展到间接引用?)
问题描述
Java 语言规范在 中定义了 final 字段的语义第 17.5 节:
The Java language spec defines semantics of final fields in 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.
我的问题是 - 最新"保证是否扩展到嵌套数组和嵌套对象的内容?
My question is - does the 'up-to-date' guarantee extend to the contents of nested arrays, and nested objects?
简而言之:如果一个线程将可变对象图分配给对象中的 final 字段,并且对象图永远不会更新,那么所有线程都可以通过 final 字段安全地读取该对象图吗?
In a nutshell: If one thread assigns a mutable object graph to a final field in an object, and the object graph is never updated, can all threads safely read that object graph via the final field?
一个示例场景:
- 线程 A 构造 ArrayLists 的 HashMap,然后将 HashMap 分配给MyClass"类实例中的最终字段myFinal"
- 线程 B 看到对 MyClass 实例的(非同步)引用并读取myFinal",然后访问并读取其中一个 ArrayList 的内容
在这种情况下,线程 B 看到的 ArrayList 的成员是否保证至少与 MyClass 的构造函数完成时一样最新?
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?
我正在寻找对 Java 内存模型和语言规范语义的澄清,而不是像同步这样的替代解决方案.我梦寐以求的答案是肯定或否定,并参考相关文本.
I'm looking for clarification of the semantics of the Java Memory Model and language spec, rather than alternative solutions like synchronization. My dream answer would be a yes or no, with a reference to the relevant text.
更新:
- 我对 Java 1.5 及更高版本的语义感兴趣,即通过 JSR 133 引入的更新的 Java 内存模型.此更新中引入了对最终字段的最新"保证.
推荐答案
在这种情况下,是线程 B 看到的 ArrayList保证至少达到MyClass 的日期构造函数完成了吗?
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?
是的,他们是.
线程第一次遇到引用时需要读取内存.因为哈希映射是构造的,其中的所有条目都是全新的,所以对对象的引用是 最新
构造函数完成时的状态.
A thread is required to read memory when it encounters reference for the first time. Because hash map is constructed, all entries in it are brand new, then the references to objects are up-to-date
to what they were when the constructor has finished.
在初次相遇之后,将应用通常的可见性规则.因此,当其他线程更改最终引用中的非最终字段时,其他线程可能看不到该更改,但它仍然会看到来自构造函数的引用.
After that initial encounter, the usual visibility rules apply. So, when other thread changes non-final field in the final references, the other thread may not see that change, but it still will see the reference that came out of constructor.
实际上,这意味着如果你在构造函数之后不修改最终的hash-map,它的内容对于所有线程都是常量.
In reality, it means that if you do not modify final hash-map after the constructor, its contents are constants for all threads.
编辑
我知道我以前在某处看到过这种保证.
I knew that I've seen this guarantee somewhere before.
这是 文章 中描述 JSR 133 的有趣段落
Here is a paragraph of interest from this article that describes JSR 133
初始化安全
新的 JMM 还寻求提供一个初始化安全的新保障——只要一个对象被正确构造(意味着一个对象的引用不是在构造函数之前发布完成),然后所有线程都会看到其最终字段的值在其构造函数中设置,不管有没有同步用于传递从一个线程到另一个线程的引用.此外,任何变量可以通过 a 的最后一个字段到达正确构造的对象,例如a 引用的对象的字段final 字段,也保证是其他线程也可见.这意味着如果最终字段包含引用,比如说,一个 LinkedList,在除了正确的值参考对其他人可见线程,也是那个的内容LinkedList 在构建时会其他线程可见同步.结果是一个显着加强final的意思——那个final字段可以安全访问而无需同步,以及编译器可以假设最终字段不会改变,因此可以优化掉多次获取.
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 的 final 字段值的“最新"保证是否扩展到间接引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java 的 final 字段值的“最新"保证是否扩展到间接引用?
基础教程推荐
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01