All About “Volatile” Keyword in Java

Vikas Taank
3 min readJul 11, 2023

Java “Volatile” keyword is there since the dawn of Java. In this article we will discuss the need and motivation for Volatile Keyword and what does it solve

What Do we know about Volatile from literature.

  1. Volatile keyword establishes that all the reads to the Volatile variable has happened before the write operation.
  2. Volatile guarantees that every thread would see the most updated value for a variable which has been declared as Volatile.
  3. Volatile guarantees that the value of a Volatile variable is directly reread from the main memory before we use it.
  4. Volatile makes sure that value written by a thread is always flushed to the main memory before the byte code instruction completes.
  5. Volatile is not equivalent to synchronizing a critical section as Volatile does not involve any Locking.
  6. The key point about the Volatile is that it allows for only one operation on the memory location which will be immediately flushed to the main memory.
  7. The Volatile is hardware access mode and produces a CPU instruction that says to ignore the cache hardware and instead read or write to the main memory.

What is the use case of Volatile and what does it solve.

A volatile varibale should be used to model a variable where writes to the variable don’t depend on the current state of the variable. This is consequence of volatile Guaranteeing only a single operation.

For example the ++ and. — — operations are not safe to use on a volatile becasue they are equivalent to v=v+1 and v=v-1. The increment example is a classis example of state dependeant update.

For all the use cases which are state dependent a mutual exclusion locks must be used.

Volatile doesn’t use any locks so it can’t create deadlocks.

Volatile makes it easier to program certain use cases which will see in a shortwhile but at the cost of flushing that value constantly to the memory.

public class TaskManager implements Runnable {

private volatile boolean shutdown=false;

public void shutdown()
{
shutdown=true;
}

@Override
public void run() {
while(!shutdown)
{
//processing
}
}
}

The above code would make sure following.

  1. All the time the shutdown flag is false , processing will continue to happen.
  2. If a thread flips the shutdown , the TaskManager will exit after it has completed the current work unit.
  3. The more subtle point is derived from the Java memory model: any write to a volatile variable Happens-Before all subsequent read of that variable.
  4. As soon as the other thread calls shutdown() on the TaskManager the flag is changed to true and the effect of that change is guaranteed to be visible in the next read of that variable i.e. before the next work unit is accepted.

I have tried to summarize some of the key points for Volatile . If you like this article please clap or leave a comment. Thanks a lot again for reading.

--

--

Vikas Taank

I am new to Medium, trying to articulate my learnings so far . Please Join medium to read my articles. Please support- https://ko-fi.com/vikastaank