java 安全发布对象_java – 可变对象的安全发布

我读了几个相关的问题,但没有一个解释安全发布持有人的方法.我仍然对

Java Concurrency in Practice中的示例感到困惑,第3.5节:

有班主持人:

public Holder {

private int n;

public Holder(int n) { this.n = n };

public void assertSanity() {

if(n != n)

throw new AssertionError("This statement is false.");

}

}

及其不安全的出版物:

//unsafe publication

public Holder holder;

public void initialize() {

holder = new Holder(42);

}

可能会抛出AssertionError,我同意.作者写道,这是因为不安全的出版物,但另一方面没有答案:

什么是正确的出版方式?

它们表示4种安全的出版习语,但我不明白,为什么它们会在上述情况下起作用:

To publish an object safely, both the reference to the object and the

object’s state must be made visible to other threads at the same time.

A properly constructed object can be safely published by:

Initializing an object reference from a static initializer;

Storing a reference to it into a volatile field or AtomicReference;

Storing a reference to it into a final field of a properly constructed object;

or Storing a reference to it into a field that is properly guarded by

a lock.

我同意1& 4,但怀疑为什么以下出版物会起作用:

//safe publication

public volatile Holder holder;

要么

//safe publication

public final Holder holder;

挥发性的final只对引用有影响,而不是引用的对象状态,所以我认为AssertionError仍然可能,对吧?

作者没有对出版物进行改进,而是通过以下方式展示如何使Holder免受不安全出版物的影响:

private final int n;

我很好奇以下是否也能奏效?它与(有效)不变性有何关联?

private volatile int n;

这是我的第一个问题,谢谢你的帮助!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值