The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →volatile makes updates to a field visible to other threads and establishes ordering, but it does not lock or make compound operations atomic. synchronized uses an object’s monitor to provide mutual exclusion as well as visibility and ordering. Use volatile for a simple shared value such as a stop flag; use synchronized when an operation or shared-state invariant must be protected as a unit.
How `volatile` and `synchronized` differ
| Property | volatile |
synchronized |
|---|---|---|
| Main guarantee | Visibility and ordering for access to one declared field. | Mutual exclusion for code using the same monitor, plus visibility and ordering across monitor release and acquisition. |
| Locking | Does not acquire a monitor. | Acquires and releases an object monitor. |
| Compound operation | Does not make read-modify-write or check-then-act logic atomic. | Can protect a complete operation when all participating threads use the same monitor. |
| Scope | The field marked volatile. |
A synchronized method or block. |
| Typical use | A stop flag or independently updated state value. | Counters, check-then-act logic, multi-field invariants, and other critical sections. |
| Waiting | Volatile field reads and writes do not wait to acquire a monitor. | A thread that cannot acquire the monitor waits until it becomes available. |
The Java Language Specification says each Java object is associated with a monitor, and only one thread at a time may hold a lock on a given monitor. A synchronized statement does not proceed until it has acquired its monitor; it releases that monitor automatically when the body completes. See the Java Language Specification, Chapter 17.
What visibility means—and what it does not mean
If one thread writes to a volatile field, a later read of that same field by another thread happens-after that write. This provides a defined visibility and ordering relationship for that field. The Java concurrency documentation describes the effect as similar to monitor entry and exit for memory consistency, but explicitly notes that volatile accesses do not entail mutual-exclusion locking. See Oracle’s java.util.concurrent package documentation.
Visibility is not the same as atomicity for a sequence of actions. Marking an integer volatile does not turn count++ into one indivisible operation. Incrementing involves reading the current value, adding one, and writing the result. Two threads can read the same starting value and overwrite one another’s updates.
Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Repair Windows errors before they cause bigger problemsFix Now →Scan for outdated or missing drivers - takes under a minuteDriver Scan →When a volatile field is appropriate
A shared stop flag
A volatile boolean is suitable when one thread sets a stop flag and another repeatedly checks it, provided that the flag is the whole state being communicated and no multi-step invariant must be protected:
class Worker implements Runnable {
private volatile boolean stop;
public void requestStop() {
stop = true;
}
@Override
public void run() {
while (!stop) {
doWork();
}
}
private void doWork() {
// Work that periodically checks the flag.
}
}
The volatile declaration ensures that the reading thread can observe a write to that field under the Java Memory Model. It does not make other fields or operations automatically safe.
Rank #2
When the state involves more than the field
If correctness depends on reading or changing multiple values together—or on checking a condition and then acting on it—volatile alone is not enough. Use a shared lock or a suitable atomic or concurrent utility designed for the operation.
When synchronized is the right choice
Protect a compound update
Use a synchronized block or method to keep an operation such as a counter increment together. Every thread accessing the protected state must use the same monitor:
Free tools Windows power users keep installed
One-click scans. No signup required.
class Counter {
private int count;
public synchronized void increment() {
count++;
}
public synchronized int value() {
return count;
}
}
Here, both methods synchronize on the same Counter instance. A thread holding that monitor excludes another thread from entering a synchronized method or block guarded by that same monitor. If some access to count bypasses the monitor, the synchronization does not protect that access.
Choose the monitor that actually coordinates the threads
- A synchronized instance method locks the receiver object.
- A synchronized static method locks the
Classobject for that class. - An explicit synchronized block locks the object named in the block.
- Locking different objects does not coordinate access to the same data; participating threads must agree on the same lock.
The concurrency documentation specifies that an unlock—such as exiting a synchronized block or method—happens-before every later lock of that same monitor. That relationship provides visibility as well as the exclusion enforced while the lock is held.
Rank #4
Does synchronized make an operation atomic?
It can make a compound operation indivisible with respect to other threads that use the same monitor. The monitor does not magically protect code that uses a different lock or no lock. For a correct critical section, place the full check-and-update sequence inside the synchronized region and ensure all relevant accesses follow that locking discipline.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Which is faster?
There is no universal performance figure that establishes volatile as faster or synchronized as slower across Java programs. Their guarantees differ, and performance depends on the JVM, runtime conditions, contention, and workload. Choose the construct that satisfies the correctness requirement first; benchmark a representative workload on the target JVM if performance is a concern. The cited specifications describe behavior, not a single comparative benchmark.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

