The volatile keyword in Java is a modifier that marks a variable that’s being stored in the main memory. Modifiers are specific keywords present in Java that are used to make changes to the characteristics of a variable, method or class and limit its scope.
Volatile Keyword in Java Definition
The volatile keyword in Java is a non-access modifier that signals to a thread that a variable is stored in the main memory, not the CPU cache. This ensures that all threads see the same value for the volatile keyword.
Modifiers in Java are divided into two types — access modifiers and non-access modifiers. Volatile is a non-access modifier.
What Is the Volatile Keyword in Java?
The volatile keyword in Java is used to mark a Java variable as “being stored in the main memory.”
Every thread that accesses a volatile variable will read it from main memory and not from the CPU cache. This way, all threads see the same value for the volatile variable.
The volatile keyword can be used with variables of any kind, including primitive types and object references. When used with objects, all threads will see the most up-to-date values for all fields in the object.
The volatile keyword is often used with flags that indicate that a thread needs to stop running. For example, a thread might have a boolean flag called “done”
, and when another thread sets this flag to “true”
, the first thread will know to stop running. Without the volatile keyword, the first thread might keep running indefinitely, because it would never see the updated value of the “done”
flag.
There are other uses for the volatile keyword as well, but this is one of the most common. In general, if you need to make sure that all threads see the same value for a certain variable, then you should mark that variable as being volatile.
How to Use the Volatile Keyword
public class Singleton {
private static volatile Singleton _instance; // volatile variable
public static Singleton getInstance() {
if (_instance == null) {
synchronized (Singleton.class) {
if (_instance == null)
_instance = new Singleton();
}
}
return _instance;
}
}
In the code above, we’re creating an instance lazily at the time the first request comes.
If we don’t make the _instance
variable volatile
, then the thread, which is creating the instance of Singleton
, isn’t able to communicate to the other thread. So, if Thread A
is creating a Singleton
instance, and just after creation, the CPU corrupts, all other threads will not be able to see the value of _instance
as not null
, and they will believe it is still assigned null.
Why does this happen? Because reader threads aren’t doing any locking, and until the writer thread comes out of a synchronized block, the memory will not be synchronized and the value of _instance
will not be updated in main memory. This is handled by Java itself with the volatile keyword, and such updates will be visible by all reader threads.
Knowing why, how and when to use the volatile keyword, as well as other modifiers, comes with experience, and experience comes with time.
Frequently Asked Questions
What is the volatile keyword in Java?
The volatile keyword in Java signals that a variable is being stored in memory. Every thread that accesses a volatile variable will read it from main memory ensuring all threads see the same value for the volatile variable.
What are non-access modifiers in Java?
Non-access modifiers in Java provide information about the characteristics of a class, method, or variable to the JVM. There are seven non-access modifiers, including:
- final
- static
- abstract
- synchronized
- volatile
- transient
- native