-3

Does a volatile Double have cross-thread visibility the way volatile double has?

From the Java Tutorials at Oracle is the following:

Using volatile variables reduces the risk of memory consistency errors, because any write to a volatile variable establishes a happens-before relationship with subsequent reads of that same variable. This means that changes to a volatile variable are always visible to other threads. What's more, it also means that when a thread reads a volatile variable, it sees not just the latest change to the volatile, but also the side effects of the code that led up the change.

It would seem to depend upon whether a Double is a special kind of object or an object that has nothing special about it.

H2ONaCl
  • 10,644
  • 14
  • 70
  • 114
  • What's the reason for the downvote, please? – H2ONaCl Oct 25 '16 at 23:39
  • It's unclear to me what distinction you're trying to make between `Double` and `double` (or whatever other type). What do you mean by _special_? – Sotirios Delimanolis Oct 25 '16 at 23:51
  • Is an assignment to an existing Double a change to an object reference or a change to an existing object. If it's a change to an object, then the cross-thread visibility of a reference is of no benefit. Right? – H2ONaCl Oct 25 '16 at 23:56
  • `volatile` is a field _modifier_. It modifies a _variable_. If you assign a value to a variable, you're modifying the variable, not any object. – Sotirios Delimanolis Oct 25 '16 at 23:58
  • there is nothing special about a Double. it is a normal Object the same as any other Object in java. so yes, assigning a volatile Double reference will be correctly visible across threads. – jtahlborn Oct 26 '16 at 00:02
  • Sotirios Delimanolis: `Double bigD = new Double(1); bigD = 2.0;` You're saying the object known as bigD did not get modified? – H2ONaCl Oct 26 '16 at 00:04
  • 1
    bigD is a variable, not an Object. that example includes two Double instances. one is created in each assignment. (FYI, Double objects are immutable). – jtahlborn Oct 26 '16 at 00:04
  • 1
    Read: [What is the difference between a variable, object, and reference?](http://stackoverflow.com/questions/32010172/what-is-the-difference-between-a-variable-object-and-reference) – Sotirios Delimanolis Oct 26 '16 at 00:09
  • 1
    jtahlborn: Interesting. If a Double is immutable, then `bigD = 2.0;` is a reference assignment and we know reference assignments have cross-thread visibility when the declaration is volatile. Thanks. – H2ONaCl Oct 26 '16 at 00:14

2 Answers2

0
volatile Double bigD = new Double(1.0); 
bigD = 2.0;

Double is an immutable class, as pointed out by jtahlborn. There is no change to the original object instance because it is immutable. The change to bigD is necessarily a reference change. That change benefits from the cross-thread visibility caused by the volatile declaration.

A list of Java immutables.

Examples of Java immutables.

Community
  • 1
  • 1
H2ONaCl
  • 10,644
  • 14
  • 70
  • 114
-1

Yes, changes to a volatile variable in one thread are guaranteed to be seen when any thread subsequently accesses that variable, regardless of whether the variable is a primitive type or a reference type. However, this does not prevent thread synchronization issues - you must still ensure that two threads are not trying to change the variable simultaneously.

Daniel O
  • 336
  • 2
  • 3
  • 8
  • But if an assignment to an existing Double is a change to it's content then the reference hasn't changed in which case there's no change to be made visible across threads. Right? – H2ONaCl Oct 25 '16 at 23:52
  • A thread might be accessing a cached copy of the Double for increased speed. When the thread needs that Double, the current value isn't always retrieved directly from memory - it may be from a cached copy of the Double, which could be out of date. – Daniel O Oct 26 '16 at 00:01
  • 1
    What do you mean by _you must still ensure that two threads are not trying to change the variable simultaneously_? The `volatile` insures atomicity of the variable modification. – Sotirios Delimanolis Oct 26 '16 at 00:03
  • What does "cached copy of the Double" mean? The whole point of `volatile` is the memory barrier. – chrylis -cautiouslyoptimistic- Oct 26 '16 at 00:07
  • @chrylis I mean if the variable were not declared as `volatile`, not changing the reference does not ensure accessing the reference will reflect the true current state of the variable. – Daniel O Oct 26 '16 at 00:10