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:

1
2
3
4
 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-locally

An 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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.

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on