什么使得不可变对象在没有安全发布技术的情况下被发布? [英] What makes immutable objects to be published without safe publication techniques?

查看:225
本文介绍了什么使得不可变对象在没有安全发布技术的情况下被发布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经阅读过 Java Concurrency in Practice

em> 第三章,

不能理解的语句:


通过任何机制发布。




  V / S 




有效的不可变对象应该安全发布。


编辑:我经历过类似的问题在SO 和答案,但仍然无法理解如何不可变的对象可以安全地发布,因为有一个机会,引用不可变对象的字段将被视为null或一些陈旧的价值从

解决方案

不是每个用例都需要在任何确切的时刻看到一个新的实例。考虑教科书的例子:lazily初始化单例,在每个线程重新初始化比安全共享更便宜。在这种情况下,您可能会不安全地共享一个不可变的实例,并且每个不能接收已经存在的副本的线程只会创建自己的。



至于术语: 不安全的发布意味着它发生在数据竞赛之下。 安全发布是相反的情况。



BTW java.lang.String 是一个有效的不可变仍然可以不安全地共享。


What does it mean to say that immutable objects can be published even without resorting to safe publication idioms?

I have read Java Concurrency in Practice (Chapter 3 , Sharing Objects) but still not able to comprehend the statement :

Immutable objects can be published through any mechanism.

               V/S 

Effectively immutable objects should be safely published.

Edit: I have been through a similar question on SO and the answers but still unable to understand how immutable objects can be published safely because there is a chance that the field referencing the immutable object will be seen as null or some stale value from earler invocations by an external thread.

解决方案

Not every use case needs to see a new instance at any precise moment. Consider the textbook example: lazily-initialized singletons which are cheaper to re-initialize in each thread than to share safely. In such a case you may unsafely share an immutable instance and each thread which doesn't manage to receive the already existent copy will just create its own.

As for terminology: unsafe publication means that it happens under a data race. Safe publication is the opposite case.

BTW java.lang.String is an example of an effectively immutable object which can nevertheless be shared unsafely.

这篇关于什么使得不可变对象在没有安全发布技术的情况下被发布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆