volatile keyword in java
The "volatile"
keyword can be used to indicate that, a member variable that can be modified asynchronously by more than one thread.
In other words,
When a volatile variable is accessed concurrently by multiple threads, its value is updated consistently among all the threads and hence all the threads share the same value for volatile variable.
Example:
- public class VolatileExample
- {
- volatile int sharedValue;
- }
public class VolatileExample { volatile int sharedValue; }
Note :
The
volatile
keyword can not be applied for class, method and local variables.A final variable can not be declared as
volatile
.The value of
'this'
variable will never be cached thread-locallyAn access to a volatile variable never has the potential to block, We are only ever doing a simple read or write, so unlike a synchronized block we will never hold on to any lock
Since accessing a volatile variable never holds a lock, it is not suitable for cases where we want to perform read-update-write as an atomic operation.
Its always read-write from main memory
In summary, volatile is intended to guarantee that all threads see the same value of the specified variable
When to use volatile in Java ?
In some cases,volatile
variable can be used as an alternative way of achieving synchronization like Visibility. Using volatile variable, it’s guaranteed that all the threads will see an updated value of the volatile variable once write operation is completed
But, Without volatile
keyword different threads will see different values.
without the volatile modifier, it’s not guaranteed that one Thread sees the updated value of volatile variable from other thread.
One major use case is singleton design pattern as below
- public class LazySingletonDoubleLockCheck {
- private static volatile LazySingletonDoubleLockCheck singletonInstance = null;
- //making constructor as private to prevent access to outsiders
- private LazySingletonDoubleLockCheck() {
- }
- public static LazySingletonDoubleLockCheck getInstance(){
- if(singletonInstance==null){
- synchronized (LazySingleton.class) {
- if(singletonInstance ==null){
- singletonInstance = new LazySingletonDoubleLockCheck();
- }
- }
- }
- return singletonInstance;
- }
- }
public class LazySingletonDoubleLockCheck { private static volatile LazySingletonDoubleLockCheck singletonInstance = null; //making constructor as private to prevent access to outsiders private LazySingletonDoubleLockCheck() { } public static LazySingletonDoubleLockCheck getInstance(){ if(singletonInstance==null){ synchronized (LazySingleton.class) { if(singletonInstance ==null){ singletonInstance = new LazySingletonDoubleLockCheck(); } } } return singletonInstance; } }
In the above code, We have declared singletonInstance as volatile
and hence if anyone thread creates the object and assign it to singletonInstance then other threads can see its value.
This helps in not creating another instance of the class and thereby helps to achieve singleton class.
For more details on Singleton design pattern, read Singleton design pattern article.
If a variable is not shared between multiple threads, we don’t need to use volatile
keyword with that variable.
excellent explanation about volatile and transient variables with explanation,i gone through hybris and now started recaping the java.They way u given the concept with examples makes everybody undersdtand very easily.Thanks alot for helping us.