事前安全出版吗?无论如何,除了决赛? [英] Safe Publication without happens-before? Anyhow besides final?

查看:50
本文介绍了事前安全出版吗?无论如何,除了决赛?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据JCP(16.2.2.安全发布):

According to JCP (16.2.2. Safe Publication):

发生这种事-在保证之前,实际上是比由安全出版物做出的对可见性和排序的保证.当从A到B安全发布X时,安全发布可以保证X状态的可见性,但不能触及其他变量A的状态.但是,如果发生了A将X放入队列的情况-在B从该队列中获取X之前,B不仅会在A离开X的状态下看到X(假定X未被A或其他任何人随后修改),而且B会看到A在移交之前所做的一切(再次受同样的警告)

This happens-before guarantee is actually a stronger promise of visibility and ordering than made by safe publication. When X is safely published from A to B, the safe publication guarantees visibility of the state of X, but not of the state of other variables A may have touched. But if A putting X on a queue happens-before B fetches X from that queue, not only does B see X in the state that A left it (assuming that X has not been subsequently modified by A or anyone else), but B sees everything A did before the handoff (again, subject to the same caveat)

我想知道什么时候才能安全发布,即不使用volatile/原子或同步(或通过使用内部列出的任何框架的AQS之类的框架)?

I'm wondering when safe publication can be without happens-before, i.e. w/o using volatile/atomics or synchronization (or via frameworks such as AQS which use any of listed inside)?

一种情况是不可变对象中的final字段,您可以在不发布任何其他步骤的情况下将其发布.

One case is final fields in immutable object, where you can publish it as is w/o any additional steps.

还有其他情况吗?

UPD:重新阅读3.5.3.安全发布惯用语,另一种情况-从静态初始化程序初始化对象引用".现在看来这些都是选择.

UPD: re-read 3.5.3. Safe Publication Idioms, another case - "Initializing an object reference from a static initializer". Seems now these are all options.

推荐答案

final以外,我一无所知,一目了然

I'm not aware of anything besides final, and a glance at http://java.sun.com/docs/books/jls/third_edition/html/memory.html seems to confirm this. Chapter 17.4 deals with everything except final, which is explained separately in 17.5.

不过,应该注意,JVM内部的任何内容都必须始终可见,然后才可能导致Java代码中的数据争用.这主要影响数组长度,虚拟表指针和字符串内容.这些永远都不能被视为未初始化或不一致.

It should be noted, though, that anything internal to the JVM must always be made visible before it can possibly contribute to a data race in the Java code. This affects mostly array lengths, virtual table pointers and String contents. These can never be seen as uninitialized or otherwise inconsistent.

这篇关于事前安全出版吗?无论如何,除了决赛?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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