Clicky

What is a “volative” variable in Java”

The “volatile” keyword indicates that a value may change between different accesses, even if it does not appear to be modified.

In Java volatile keyword is used to mark a Java variable so that every read is done from the computer’s main memory, and not from the CPU cache, and that every write to a volatile variable will be written to main memory, and not just to the CPU cache.

In case where only one thread reads and writes the value of a volatile variable and other threads only read the variable, then the reading threads are guaranteed to see the latest value written to the volatile variable.