I am reading an article about the Java Volatile keyword, got some questions. click here
public class MyClass {
private int years;
private int months
private volatile int days;
public void update(int years, int months, int days){
this.years = years;
this.months = months;
this.days = days;
}
}
The udpate() method writes three variables, of which only days is volatile.
The full volatile visibility guarantee means, that when a value is written to days, then all variables visible to the thread are also written to main memory. That means, that when a value is written to days, the values of years and months are also written to main memory.
So, what does "all variables visible to the thread" mean? Does it means all variables in thread's stacks? And what does "visible to the thread" mean? How can I know does months and years visible to the thread?
update()
in the meantime you and you might see the result of that call in one or more of your fields. For complete consistency you need to use proper synchronization/locking. – Aires