volatile keyword in Java
why, when, and how to use it
Modifiers are specific keywords present in Java using that we can make changes to the characteristics of a variable, method, or class and limit its scope. Java programming language has a rich set of Modifiers.
Modifiers in Java are divided into two types — Access Modifiers and Non-Access modifiers.
Non-Access modifiers
Non-access modifiers provide information about the characteristics of a class, method, or variable to the JVM. Seven types of Non-Access modifiers are present in Java. They are –
- final
- static
- abstract
- synchronized
- volatile
- transient
- native
This article will go into the details of the volatile keyword. Let’s explore first, why do we use it, when should we use it if we decide to, and last but not least, how to use it.
the WHY:
The volatile keyword in Java is used to mark a Java variable as “being stored in 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.
the WHEN and HOW:
1. volatile field
In the above example, we are creating instance lazily at the time the first request comes.
If we do not make the _instance
variable volatile
then the Thread which is creating the instance of Singleton
is not able to communicate to the other thread. So if Thread A is creating Singleton instance and just after creation, the CPU corrupts etc, 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 are not doing any locking and until the writer thread comes out of a synchronized block, the memory will not be synchronized and value of _instance
will not be updated in main memory. With the Volatile keyword in Java, this is handled by Java itself and such updates will be visible by all reader threads.
Knowing best why, how and when to use volatile, as well as other modifiers, comes with experience, and experience comes with time.
Enjoy the journey and happy coding!
Thank you for your time, dear reader!
Some of my articles:
10 best practices in Java
Hashing in JAVA
final Keyword in Java
The struggle of getting my first job as a software developer
Will Java still be worth in 2023? ☕
The BLUB Paradox
Things I wish I knew as a beginner programmer
Spring Boot: The GOOD and the BAD
Why do we love coding? 💻
Stay motivated
AI is bullshit
100 followers done. What’s next?
Is work from home going to end?