关于这篇挥发性文章的问题

时间:2016-03-15 03:31:46

标签: 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 个答案:

答案 0 :(得分:2)

1)原始值不是像int,boolean,float等对象。只要记住有等价的对象,如Integer,Boolean等。

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

相关问题