The volatile keyword in Java indicates that different threads may modify a variable value. It ensures that changes to a variable are always visible to other threads. This behavior prevents issues caused by thread local caching. When I was working with Java multithreading in my projects, I used the volatile keyword to solve visibility problems.
In this blog, I will explain what the volatile keyword means with some examples.
Let’s get started!
It is used to show that a variable can be changed by multiple threads at different times. It ensures that whenever a variable is updated, all other threads immediately see the latest value and no issues occur due to cached copies.
In a multithreaded Java program, multiple threads may access the same variable at the same time. Without proper control, some threads might read an old value instead of the updated one. The volatile keyword in Java helps prevent this issue by ensuring that all threads always work with the most recent value of a variable. This improves the reliability and consistency of multithreaded applications.
Key reasons for using the volatile keyword:
The volatile keyword is used when a variable is shared between multiple threads and its value may change during program execution. It helps ensure that each thread always reads the most recent value of that variable. Some common usages are:
Read Also: Java Tutorial for Beginners
Following examples show how the volatile keyword is used in Java and how it behaves in a multithreaded environment:
A variable is declared volatile. When one thread updates the value, other threads can immediately see the updated value.
Note: Even if counter is declared volatile, the operation counter++ is not atomic and may lead to race conditions when multiple threads update it simultaneously.
|

How it works:
Two threads access the same volatile variable. One thread updates it and the other reads the updated value.
|

How it works:
A worker thread continuously performs a task in a loop. A volatile flag is used to stop the thread from another thread. As the variable is volatile, the worker thread immediately detects the change.
|

How This Works:
A volatile variable can also represent the status of a process that multiple threads check.
|

How It Works:
Both are used in multithreading to manage shared data between threads. Volatile ensures visibility of variable updates across threads and synchronized provides mutual exclusion and thread safe access to critical sections. Here is a differentiation between them:
| Parameters | Volatile | Synchronized |
| Purpose | Ensures visibility of variable changes across threads. | Ensures mutual exclusion and synchronization between threads |
| Locking | Does not use locks | Uses intrinsic locks. |
| Thread Safety | Provides visibility only, not full thread safety for complex operations. | Provides both visibility and atomicity that makes the operations thread safe. |
| Performance | Faster because no locking overhead. | Slower compared to volatile due to lock acquisition and release. |
| Use Case | Best for simple flags or status variables shared between threads. | Best for critical sections where multiple threads modify shared data. |
| Atomic Operations | Does not guarantee atomicity. | Guarantees atomic execution of the synchronized block. |
| Code Scope | Applied to variables only. | Applied to methods or blocks of code. |
Read Also: Java Interview Questions and Answers
The volatile keyword guarantees that a variable’s latest value is visible to all threads. When one thread updates a volatile variable, the updated value is immediately visible to other threads. Volatile does not provide full thread safety for complex operations.
volatile works best for variables used as control flags or status indicators that multiple threads read and update. For example:
|
Operations such as count++, value += 1 or check-then-act logic involve multiple steps. The volatile keyword does not guarantee atomicity for these operations which may lead to race conditions when multiple threads modify the same variable.
volatile should not be used as a replacement for synchronized. While volatile ensures visibility of variable updates across threads, it does not provide mutual exclusion. If multiple threads modify shared data, synchronization or locking mechanisms are required.
The Java Memory Model prevents certain read and write operations on volatile variables from being reordered by the compiler or CPU. This helps maintain predictable behavior in multithreaded programs.
Using volatile may introduce a small performance overhead because it prevents some CPU caching and optimization techniques. The overhead is usually minimal, volatile should only be used when visibility guarantees are required.
In some cases, volatile may be used together with synchronized blocks or other concurrency mechanisms. This helps achieve both visibility and atomicity when multiple operations must remain consistent across threads.
In this blog, I have explained to you volatile keyword in Java that includes why it is used in multithreaded programs, how it ensures visibility of shared variables and how it is different from synchronized.
After reading this you should start practicing it to get a hands-on practice and confidence for your future projects.
Volatile makes sure that when one thread changes a variable, other threads immediately see the updated value. It prevents threads from using outdated values stored in their local memory.
No, volatile only ensures visibility of changes between threads. It does not make operations atomic. It cannot fully guarantee thread safety when multiple threads modify data.
Volatile can only be used with variables. It cannot be applied to methods, classes or local variables in Java.
Explore Our Trending Articles-