java - 关于 Volatile 这篇文章的问题

标签 java multithreading volatile

我看到了这个:

Pattern #2: one-time safe publication
The visibility failures that
are possible in the absence of synchronization can get even trickier
to reason about when writing to object references instead of primitive
values. In the absence of synchronization, it is possible to see an
up-to-date value for an object reference that was written by another
thread and still see stale values for that object's state. (This
hazard is the root of the problem with the infamous
double-checked-locking idiom, where an object reference is read
without synchronization, and the risk is that you could see an
up-to-date reference but still observe a partially constructed object
through that reference.)*

来自 IBMdeveloperworks_volatile

我很困惑:

  1. 什么是“原始值
  2. 原因:“看到由另一个线程写入的对象引用的最新值,但仍然看到该对象状态的过时值

最佳答案

1) 原始值不是像 int、boolean、float 等对象。请记住,存在与这些对象等效的对象,例如 Integer、Boolean 等。

2) 该线程可能会看到更新的对象引用,但该对象可能未完全初始化。例如,如果当该线程获取对象的新更新引用时构造函数尚未完成,则可能会发生这种情况...这可能很复杂,但可能会在某些 JVM 实现中发生。

关于java - 关于 Volatile 这篇文章的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36002210/

相关文章:

java - 将字母映射到手机键盘上的数字

java - SWT - 显示忙碌的光标

java - System.out.println 与 java volatile

java - 如何理解 "The variable does not participate in invariants with other state variables when using volatile keyword"?

java - 在不安装 Eclipse 的情况下运行 RCP 应用程序

Java:JPA 类,从 Date 重构为 DateTime

multithreading - 从非 UI QThread 修改 QStandardItemModel?

java - 同步并行 API 请求以从长时间运行的查询返回相同结果而无需多次执行的最佳实践是什么?

c# - 如何获取 Timer Elapsed 事件中发生的异常?

java - 相对于其他字段的易变语义