Javatpoint Logo
Javatpoint Logo

Volatile Keyword in Java

Volatile keyword is used to modify the value of a variable by different threads. It is also used to make classes thread safe. It means that multiple threads can use a method and instance of the classes at the same time without any problem. The volatile keyword can be used either with primitive type or 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 is used with variables. It also guarantees visibility and ordering. It prevents the compiler from the reordering of code.

The contents of the particular device register could change at any time, so you need the volatile keyword to ensure that such accesses are not optimized away by the compiler.

Example

In the above example, assume that two threads are working on the same class. Both threads run on different processors where each thread has its local copy of var. If any thread modifies its value, the change will not reflect in the original one in the main memory. It leads to data inconsistency because the other thread is not aware of the modified value.

In the above example, static variables are class members that are shared among all objects. There is only one copy in the main memory. The value of a volatile variable will never be stored in the cache. All read and write will be done from and to the main memory.

When to use it?

  • You can use a volatile variable if you want to read and write long and double variable automatically.
  • It can be used 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.
  • It is used to inform the compiler that multiple threads will access a particular statement. It prevents the compiler from doing any reordering or any optimization.
  • If you do not use volatile variable compiler can reorder the code, free to write in cache value of volatile variable instead of reading from the main memory.

Important points

  • You can use the volatile keyword with variables. Using volatile keyword with classes and methods is illegal.
  • It guarantees that value of the volatile variable will always be read from the main memory, not from the local thread cache.
  • If you declared variable as volatile, Read and Writes are atomic
  • It reduces the risk of memory consistency error.
  • Any write to volatile variable in Java establishes a happen before the relationship with successive reads of that same variable.
  • The volatile variables are always visible to other threads.
  • The volatile variable that is an object reference may be null.
  • When a variable is not shared between multiple threads, you do not need to use the volatile keyword with that variable.

Difference between synchronization and volatile keyword

Volatile keyword is not a substitute of synchronized keyword, but it can be used as an alternative in certain cases. There are the following differences are as follows:

Volatile Keyword Synchronization Keyword
Volatile keyword is a field modifier. Synchronized keyword modifies code blocks and methods.
The thread cannot be blocked for waiting in case of volatile. Threads can be blocked for waiting in case of synchronized.
It improves thread performance. Synchronized methods degrade the thread performance.
It synchronizes the value of one variable at a time between thread memory and main memory. It synchronizes the value of all variables between thread memory and main memory.
Volatile fields are not subject to compiler optimization. Synchronize is subject to compiler optimization.

Example of Volatile Keyword

In the following example, we have defined a class which increases the counter value. The run () method in the VolatileThread.java gets the updated value and old value when the thread begins execution. In the main class, we define the array of thread.

VolatileData.java

VolatileThread.java

VolatileMain.java

Output:

[Thread 9]: Old value = 0
[Thread 9]: New value = 1
[Thread 10]: Old value = 1
[Thread 10]: New value = 2

Next Topic#




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA