01
What is the difference between volatile and synchronized?
Show answer
volatile guarantees visibility: a write by one thread is immediately visible to others and reordering around it is restricted, but it does not make compound operations like count++ atomic. synchronized gives both mutual exclusion and visibility by acquiring a monitor lock, so only one thread runs the block at a time. Use volatile for simple flags such as a shutdown signal, synchronized or locks for check-then-act logic, and AtomicInteger or LongAdder for counters.