Q&A
Q&AWhat is the difference between volatile and synchronized?
Volatile provides visibility; synchronized provides both visibility and mutual exclusion.
Read the concise answer
A volatile field guarantees that a write is visible to other threads, but it does not make a compound operation atomic. synchronized locks a monitor and protects a block of code so only one thread executes it at a time. In practice, use volatile for state flags and synchronized for critical sections that need atomicity and ordering guarantees.