Monday, February 20, 2023

What is Volatile Variable in Java? When to Use it? Example

What is a Volatile variable in Java?
The volatile variable in Java is a special variable that is used to signal threads and compilers and runtime optimizers like JIT that the value of this particular variable is going to be updated by multiple threads inside the Java application. By making a variable volatile using the volatile keyword in Java, the application programmer ensures that its value should always be read from the main memory and the thread should not use the cached value of that variable from their own stack. With the introduction of the Java memory model from Java 5 onwards along with the introduction of CountDownLatch, CyclicBarrier, Semaphore, and ConcurrentHashMap.

The volatile variable also guarantees "happens-before" relationship, which means not only another thread has visibility of the latest value of the volatile variable but also all the variable is seen by the thread which has updated value of the volatile variable before these threads see it. What is a volatile variable and when to use it is always a popular Java threading question?




When to use Volatile variable in Java? Answer

This is the most important thing to learn while learning about the volatile variable in Java. When to use a volatile variable in Java is also a famous multi-threading interview question in Java. here are some of the scenarios where you can use a volatile variable in Java :

1. Any variable which is shared between multiple threads should be made variable, in order to ensure that all thread must see the latest value of the volatile variable.

2. A signal to compiler and JIT (Just in time compiler) to ensure that compiler does not change ordering or volatile variable and moves them out of synchronized context.

3. You want to save the cost of synchronization as volatile variables are less expensive than synchronization. If you want to learn more then you can also check out these Multithreading and currency courses to learn more about volatile modifiers in Java. 


What is Volatile Variable in Java - When to use in Thread? Example




Important points about the volatile keyword in Java

Since a volatile keyword is used to make any variable volatile in the Java environment, it's good to know more about What is a volatile keyword, what is its limitation, and How to use the volatile keyword in Java.

1. The volatile keyword can only be applied to a variable, it can not be applied to class or method. using volatile keywords along with class and method is a compiler error.

2. A volatile is also referred to as a modifier in Java.

3. The volatile modifier provides visibility and ordering guarantee to ensure that each thread has a consistent view of volatile variable in Java application.

4. When a volatile variable's value is ready a thread, memory barrier is refreshed on JVM which means all the changes made happened before will also be visible to the thread which read the value of volatile variable. 


That's all on what is a volatile variable in Java and when to use a volatile variable in Java. A volatile variable is an important concept to understand and use. It's also very important in terms of the Java interview point of view. It's also important from performance perspective because if you only need visibility and ordering guarantee then you can use volatile instead of synchronized and save a lot of performance by avoiding locking and un-locking. 


Other tutorials on Java from this blog
Top thread interview question for Java programmer - Beginners
Top Java coding interview question for Senior Java programmer

P. S. - If you are new to Java multithreading world and want to learn Java threads and concurrency in depth but looking for free resources then you can also checkout this list of free Java Concurrency courses to start with. I have shared many great free Java concurrency courses form sites like Udemy and Coursera on that article which are useful for both beginners and experienced Java developers. 

4 comments:

  1. Hello there, Can you please write about difference between volatile variable in Java 1.4 and Java 5? I heard there are some changes made on How volatile variable works to fix the double checked locking problem, love to see an english explanation.

    ReplyDelete
  2. What is meaning of happens-before guarantee provided by volatile variable in Java? How they achieve synchronization and how strong that is?

    ReplyDelete
    Replies
    1. A happens-before relationship is a guarantee that memory written to by statement A is visible to statement B, that is, that statement A completes its write before statement B starts its read.

      Delete
  3. Thanks for a good writeup!! A small note in point 1 under when to use volatile,
    I think it should be :
    Any variable which is shared between multiple threads should be made "VOLATILE"

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.