Difference between transient vs volatile variable in Java
By: Minh Pham
Tags: Transient, Java, Volatile

Volatile

What

The volatile keyword in Java is used as an indicator to Java compiler and Thread that do not cache the value of this variable and always read it from main memory.

From Java 5 along with major changes like Autoboxing, Enum, Generics and Variable arguments, Java introduces some change in Java Memory Model (JMM), Which guarantees visibility of changes made from one thread to another also as “happens-before” which solves the problem of memory writes that happen in one thread can “leak through” and be seen by another thread.

How

Cannot be used with method or class and it can only be used with a variable.

Volatile keyword also guarantees visibility and ordering, after Java 5 write to any volatile variable happens before any read into the volatile variable

The use of volatile keyword also prevents compiler or JVM from the reordering of code or moving away from the synchronization barrier.

When

Important points on Volatile keyword in Java

The volatile keyword in Java is the only applied to a variable and using a volatile keyword with class and method is illegal.

volatile keyword in Java guarantees that the value of the volatile variable will always be read from the main memory and not from Thread’s local cache.

Since volatile keyword in Java only synchronizes the value of one variable between Thread memory and "main" memory while synchronized synchronizes the value of all variable between thread memory and "main" memory and locks and releases a monitor to boot. Due to this reason synchronized keyword in Java is likely to have more overhead than volatile.

In Java reads and writes are atomic for all variables declared using Java volatile keyword (including long and double variables).

Using the volatile keyword in Java on variables reduces the risk of memory consistency errors because any write to a volatile variable in Java establishes a happens-before relationship with subsequent reads of that same variable.

From Java 5 changes to a volatile variable are always visible to other threads. It means that when a thread reads a volatile variable in Java, it sees not just the latest change to the volatile variable but also the side effects of the code that led up the change.

Also from Java 5 writing into a volatile field has the same memory effect as a monitor release, and reading from a volatile field has the same memory effect as a monitor acquire

Reads and writes are atomic for reference variables are for most primitive variables (all types except long and double) even without the use of volatile keyword in Java.

Access to a volatile variable in Java never has a chance to block, since we are only doing a simple read or write, so unlike a synchronized block we will never hold on to any lock or wait for any lock.

You can not synchronize on the null object but your volatile variable in Java could be null .

object but your volatile variable in Java could be . Java volatile keyword doesn’t mean atomic, its common misconception that after declaring volatile ++ will be atomic, to make the operation atomic you still need to ensure exclusive access using synchronized method or block in Java.

Synchronized obtains and releases the lock on monitor’s Java volatile keyword doesn’t require that => Synchronized method affects performance more than a volatile keyword in Java.