Java Volatile Keyword

Volatile keyword is intended to address variable visibility problems. If variable is declared volatile, then all writes to this variable in any thread will be written back to main memory immediately. Also, all reads from variable will be read directly from main memory.

Let’s delve a bit deeper in visibility guarantee. The Visibility Guarantee is as follows :

  • If Thread A writes to a volatile variable and Thread B subsequently reads the same volatile variable, then all variables visible to Thread A before writing the volatile variable will also be visible to Thread B after it has read the volatile variable
  • If Thread A reads a volatile variable, then all variables visible to Thread A when reading the volatile variable will also be re-read from main memory

First rule can be illustrated in code example :

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;
    }
}

In the code snippet above the full volatile visibility guarantee means, that when a value is written to days, the values of years and months are also written to main memory. So, when another thread subsequently reads days variable, changed years and months variables values will also be visible to another thread.

Below is example illustrating the second rule :

public class MyClass {
    private int years;
    private int months
    private volatile int days;

    public int totalDays() {
        int total = this.days;
        total += months * 30;
        total += years * 365;
        return total;
    }
}

TotalDays() method starts by reading the value of days into total variable. When reading the value of days, values of months and years are also read into main memory. Therefore developer is guaranteed to see the latest values of days, months and years with the above read sequence.

Supplying Visibility Guarantee is tied with reordering instructions (code optimizations made by JVM). Java volatile keyword gives a “happens-before” guarantee. It means that :

  • Reads from and writes to other variables cannot be reordered to occur after a write to a volatile variable, if reads / writes originally occured before write to volatile variable . The reads / writes before a write to a volatile variable are guaranteed to “happen before” the write to the volatile variable. Notice that it is still possible for e.g. reads / writes of other variables located after a write to a volatile to be reordered to occur before that write to the volatile. Just not the other way around. From after to before is allowed, but from before to after is not allowed
  • Reads from and writes to other variables cannot be reordered before a read of a volatile variable, if reads / writes originally occured after the read of the volatile variable. Notice that it is possible for reads of other variables that occur before the read of a volatile variable can be reordered to occur after the read of the volatile. Just not the other way around. From before to after is allowed, but from after to before is not allowed

The above happens-before guarantee assures that the visibility guarantee of the volatile keyword are being enforced.

Previous Next

Leave a Reply

Your email address will not be published. Required fields are marked *