I’ve declared a volatile array of node based data structures.
volatile Node[] name;
For the most part, I don’t need each individual node to be volatile because I update the entire array when it needs to be updated.
name = new array of nodes;
Rarely, I will need to update a field inside a specific node of the array, but no other thread will need to read this field for at least several minutes. Can I assume that at this point, whatever change I’ve made would be visible to other threads?
In the case you describe, the
volatilekeyword onnamedoesn’t come into play at all. Your use ofvolatilewill only be relevant on reads from and writes toname, neither of which are done when accessing some field of some element inname.If you need to read guarantees on some field in some element of
name, you really need to make that fieldvolatile.