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

What is Volatile variable in Java
volatile variable in Java is a special variable which is used to signal threads, a compiler that this particular variables value are going to be updated by multiple threads inside Java application. By making a variable volatile using the volatile keyword in Java, application programmer ensures that its value should always be read from main memory and thread should not use cached value of that variable from their own stack. With the introduction of Java memory model from Java 5 onwards along with introduction of CountDownLatch, CyclicBarrier, Semaphore and ConcurrentHashMap, volatile variable also guarantees "happens-before" relationship, which means not only another thread has visibility of latest value of volatile variable but also all the variable is seen by the thread which has updated value of volatile variable before these threads sees it. What is volatile variable and when to use it is always a popular Java threading question.

Important point related to volatile keyword in Java
Since volatile keyword is used to make any variable volatile in 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 keyword along with class and method is a compiler error.
  2. A volatile is also referred as modifier in Java.

When to use Volatile variable in Java
What is volatile variable in Java and when to use itThis is the most important thing to learn while learning about the volatile variable in Java. When to use volatile variable in Java is also a famous multi-threading interview question in Java. here are some of the scenario where you can use 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 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.

That's all on What is volatile variable in Java When to use volatile variable in Java. a volatile variable is an important concept to understand and use. It's also very important in terms of  Java interview point of view.

Further Learning
Multithreading and Parallel Computing in Java
Applying Concurrency and Multi-threading to Common Java Patterns
Java Concurrency in Practice - The Book
Java Concurrency in Practice Bundle by Heinz Kabutz

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


  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.

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

    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.

  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"


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