安全发布和不可变与有效不可变的优势 [英] safe publication and the advantage of being immutable vs. effectively immutable

查看:99
本文介绍了安全发布和不可变与有效不可变的优势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重读实践中的Java并发,我不确定我是否完全理解有关不变性和安全发布的章节。

I'm re-reading Java Concurrency In Practice, and I'm not sure I fully understand the chapter about immutability and safe publication.

本书的内容是什么是:


任何线程都可以安全地使用不可变对象,而无需额外的
同步,即使不使用同步来发布
他们。

Immutable objects can be used safely by any thread without additional synchronization, even when synchronization is not used to publish them.

我不明白的是,为什么会有人(有兴趣使他的代码正确)发布了一些不安全的引用?

What I don't understand is, why would anyone (interested in making his code correct) publish some reference unsafely?

如果对象是不可变的,并且它发布得不安全,我理解获取对象引用的任何其他线程都会看到它正确的状态,因为适当的不变性提供的保证(最终字段等)。

If the object is immutable, and it's published unsafely, I understand that any other thread obtaining a reference to the object would see its correct state, because of the guarantees offered by proper immutability (with final fields, etc.).

但是如果发布不安全,另一个线程可能仍会在发布后看到 null 或之前的引用,而是对不可变对象的引用,在我看来似乎没有人想要的东西。

But if the publication is unsafe, another thread might still see null or the previous reference after the publication, instead of the reference to the immutable object, which seems to me like something no-one would like.

如果使用安全发布来确保新的引用被看到所有线程,那么即使对象只是有效不可变(没有最终字段,但无法将它们静音),那么一切都是安全的再次。正如书中所说:

And if safe publication is used to make sure the new reference is seen by all the threads, then even if the object is just effectively immutable (no final fields, but no way to mute them), then everything is safe again. As the book says :


安全发布有效不可变的对象可以安全地使用
任何线程没有额外的同步。

Safely published effectively immutable objects can be used safely by any thread without additional synchronization.

那么,为什么不变性(与有效不变性相比)如此重要?在什么情况下需要不安全的出版物?

So, why is immutability (vs. effective immutability) so important? In what case would an unsafe publication be wanted?

推荐答案

设计不需要同步的对象有两个原因:

It is desirable to design objects that don't need synchronization for two reasons:


  1. 对象的用户可能忘记同步。

  2. 即使开销很大很少,同步不是免费的,特别是如果您的对象不经常使用并且由许多不同的线程使用。

因为上述原因非常重要的是,最好学习有时很难的规则,作为编写者,制作不需要同步的安全对象,而不是希望代码的所有用户都记得正确使用它。

Because the above reasons are very important, it is better to learn the sometimes difficult rules and as a writer, make safe objects that don't require synchronization rather than hoping all the users of your code will remember to use it correctly.

还要记住,作者并没有说该对象是不安全发布的,它是在没有同步的情况下安全发布的。

Also remember that the author is not saying the object is unsafely published, it is safely published without synchronization.

至于你的第二个问题,我刚刚检查过,本书并没有向你承诺另一个线程将始终看到对更新对象的引用,只要它确实如此,它将看到一个完整的对象。但我可以想象,如果它是通过另一个( Runnable ?)对象的构造函数发布的,那将是很好的。这确实有助于解释所有情况。

As for your second question, I just checked, and the book does not promise you that another thread will always see the reference to the updated object, just that if it does, it will see a complete object. But I can imagine that if it is published through the constructor of another (Runnable?) object, it will be sweet. That does help with explaining all cases though.

编辑:

有效不可变和不可变
有效不可变和不可变之间的区别在于,在第一种情况下,您仍需要以安全的方式发布对象。对于真正不可变的对象,这不是必需的。所以真正的不可变对象是首选,因为它们更容易发布,原因如上所述。

effectively immutable and immutable The difference between effectively immutable and immutable is that in the first case you still need to publish the objects in a safe way. For the truly immutable objects this isn't needed. So truly immutable objects are preferred because they are easier to publish for the reasons I stated above.

这篇关于安全发布和不可变与有效不可变的优势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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