java final 可见性_java 如图,final关键字能不能保证对象的可见性?

能,如果声明holder为final,你的代码需要改一下,在Other ctor里面进行赋值。Final Fields

Loads and Stores of final fields act as "normal" accesses with respect to locks and volatiles, but impose two additional reordering rules:

A store of a final field (inside a constructor) and, if the field is a reference, any store that this final can reference, cannot be reordered with a subsequent store (outside that constructor) of the reference to the object holding that field into a variable accessible to other threads. For example, you cannot reorder

x.finalField = v; ... ; sharedRef = x;

This comes into play for example when inlining constructors, where "..." spans the logical end of the constructor. You cannot move stores of finals within constructors down below a store outside of the constructor that might make the object visible to other threads. (As seen below, this may also require issuing a barrier). Similarly, you cannot reorder either of the first two with the third assignment in:

v.afield = 1; x.finalField = v; ... ; sharedRef = x;

The initial load (i.e., the very first encounter by a thread) of a final field cannot be reordered with the initial load of the reference to the object containing the final field. This comes into play in:

x = sharedRef; ... ; i = x.finalField;

A compiler would never reorder these since they are dependent, but there can be consequences of this rule on some processors.

信息来源,jsr-133 cookbook,鉴于JSR 133早就是Java规范了,所以你在JLS里面也能找到。Chapter 17. Threads and Locks​docs.oracle.com

展开一点讲,不可变对象本来就是要求线程安全的(不可变大家看到的还不一样,那还玩什么呢?),而可见性又属于线程安全的一部分,另一部分是互斥性。finalfields also allow programmers to implement thread-safe immutable objects without synchronization. A thread-safe immutable object is seen as immutable by all threads, even if a data race is used to pass references to the immutable object between threads. This can provide safety guarantees against misuse of an immutable class by incorrect or malicious code.finalfields must be used correctly to provide a guarantee of immutability.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值