Java - volatile reference to mutable object - will updates to the object's fields be visible to all threads Java - volatile reference to mutable object - will updates to the object's fields be visible to all threads multithreading multithreading

Java - volatile reference to mutable object - will updates to the object's fields be visible to all threads


No.

Placing a reference to an object in a volatile field does not affect the object itself in any way.

Once you load the reference to the object from the volatile field, you have an object no different from any other object, and the volatility has no further effect.


The are two question. Let's start with the second.

Assigning newly constructed objects to volatile variables works nicely. Every thread, that reads the volatile variable, will see a fully constructed object. There is no need for further synchronization. This pattern is usually seen in combination with immutable types.

class Tree {    private volatile Node node;    public void update() {        node = new Node(...);    }    public Node get() {        return node;    }}

Regarding the first question. You can use volatile variables to synchronize access to non-volatile variable. The following listing shows an example. Imagine that the two variables are initialized as shown, and that the two methods are executed concurrently. It is guaranteed, that if the second thread sees the update to foo, it will also see the update to bar.

volatile int foo = 0;int bar = 0;void thread1() {    bar = 1;    foo = 1; // write to volatile variable}void thread2() {    if (foo == 1) { // read from volatile variable        int r = bar; // r == 1    }}

However, your example is different. The reading and writing might look as follows. In contrast to the above example, both threads read from the volatile variable. However, read operations on volatile variables do not synchronize with each other.

void thread1() {    Node temp = root; // read from volatile variable    temp.numOfKeys = 1;}void thread2() {    Node temp = root; // read from volatile variable    int r = temp.numOfKeys;}

In other words: If thread A writes to a volatile variable x and thread B reads the value written to x, then after the read operation, thread B will see all write operations of thread A, that occurred before the write to x. But without a write operation to a volatile variable, there is no effect on updates to other variables.


That sounds more complicated than it actually is. Actually, there is only one rule to consider, which you can find in JLS8 ยง17.4.5:

[..] If all sequentially consistent executions are free of data races, [..] then all executions of the program will appear to be sequentially consistent.

Simply put, a data race exists if two threads can access the same variable at the same time, at least one operation is a write operation, and the variable is non-volatile. Data races can be eliminated by declaring shared variables as volatile. Without data races, there is no problem with visibility of updates.