Volatile Keyword

Volatile Keyword

A user uses volatile keyword in java when he wants to modify the value of a variable by using different threads. We use it to make classes thread safe as well. It simply means that multiple threads can use a java method and instance of the classes at the same time without facing any difficulty. Volatile keyword can be either with primitive type or for the objects. The volatile keyword does not cache the value of the variable and always read the variable from the main memory. The volatile keyword cannot be used with classes or methods. However, it works well with variables. It also guarantees visibility and ordering. It prevents the compiler from the reordering of code.

Uses of Volatile Keyword

  • A user can use a volatile variable if he wants to read and write long and double variable automatically. 
  • Volatile keyword comes into play as an alternative way of achieving synchronization in Java.
  • All reader threads will see the updated value of the volatile variable after completing the write operation. If you are not using the volatile keyword, different reader thread may see different values.
  • User can use it to inform the compiler that multiple threads will access a particular statement. This inturn prevents the compiler from doing any reordering or any optimization.
  • If a user do not wish to use volatile variable compiler can reorder the code, free to write in cache value of volatile variable instead of reading from the main memory.

Below is a program to understand how volatile keyword works in java

class SharedObject
   static int sharedVar = 10;

Here we assume that there are two threads are working on SharedObject. If these two threads run on different processors then each thread will have its own local copy of sharedVariable. If the user modifies value of one thread, then its value the change might not reflect in the original one in the main memory instantly. This depends on the write policy of cache. Now the other thread is not aware of the modified value which leads to data inconsistency. 

Below is an image to illustrate Volatile Keyword

Volatile Keyword

A place where the threads operate on non-volatile variables, each thread may copy variables from main memory into a CPU cache while working on them, for performance reasons. However, If your computer contains more than one CPU, each thread may run on a different CPU. That means, that each thread may copy the variables into the CPU cache of different CPUs.

Volatile vs Synchronised

Volatile Synchronized
Volatile comes into play where user only wants visibility and not scalability.Synchronized keyword supports both mutual exclusion as well as visibility.
This keyword is a field modifierThis keyword is a code modifier
This improves thread performanceIt doesnt improve thread performance, rather degrades it.
The values of volatile keyword can never be cachedThe values of synchronized keywords can be cached.
The use of volatile is limited to very restricted set of cases as most of the times atomicity is desired.The use of synchronized is not limited to some set of cases.