AlgoMaster Logo

volatile Keyword

Last Updated: June 6, 2026

High Priority
20 min read

volatile is a small word that solves one specific problem in multithreaded code: making sure that when one thread writes to a field, every other thread sees the new value. Without volatile (or synchronized, or some other coordination), Java threads are free to keep stale copies of a field in CPU registers or in a private cache. This lesson covers what volatile actually guarantees, the visibility problem it fixes, the classic "stop flag" pattern, the happens-before rule for volatile reads and writes, and the critical limitation: volatile does not make compound operations atomic, so volatile int counter; counter++; is still broken.

volatile is the lighter-weight tool for the cases where visibility is needed but not mutual exclusion.

Premium Content

Subscribe to unlock full access to this content and more premium articles.