Search code examples
javamultithreadingvolatilejava-memory-model

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


... without additional synchronization ? The Tree class below is meant to be accessed by multiple threads (it is a singleton but not implemented via an enum)

class Tree {

    private volatile Node root;

    Tree() {
        root = new Node();
        // the threads are spawned _after_ the tree is constructed
    }

    private final class Node {
        short numOfKeys;
    }
}
  • Will updates to the numOfKeys field be visible to reader threads without any explicit synchronization (notice that both readers and writers have to acquire an instance of ReentrantReadWriteLock - same instance for each node - but barring that) ? If not would making numOfKeys volatile suffice ?
  • Is changing the root as simple as root = new Node() (only a writer thread would change the root, apart from the main thread which calls the Tree constructor)

Related:

EDIT: interested in post Java 5 semantics


Solution

  • 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.