Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Keywords and Statements

The volatile keyword

Reference for Java volatile: visibility and ordering guarantees, what it does not guarantee (atomic compound actions), typical uses and alternatives.

volatile is a field modifier that tells the JVM that other threads may read and write the variable at any time. Every write to a volatile field is visible to every later read of it from any thread, and the compiler and CPU are not allowed to reorder surrounding operations across it in ways that would break that.

Without volatile (or another synchronization action) a thread may keep a stale copy of a field in a register or cache and never notice that another thread changed it. volatile fixes visibility and ordering, but it does not make compound operations such as count++ atomic.

Java
private volatile boolean running = true;

Try it

Java A stop flag

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java volatile does not make count++ atomic

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
volatile Type name;
Rule Description
Allowed on Fields (instance and static) only; not on local variables, parameters or methods.
Not allowed with final (a final field never changes, so there is nothing to publish).
Applies to The variable itself. For a reference, the reference is volatile, not the object's fields.

Usage notes

  • Visibility and ordering, not atomicity. Reads and writes of a volatile variable are atomic (also for long and double), but x++, x += 1 and "check then act" sequences are not. Use AtomicInteger, LongAdder, synchronized or a lock for those.
  • Good uses: stop or status flags written by one thread and read by others, publishing an immutable object by writing it to a volatile reference, and the double-checked locking idiom (the field must be volatile).
  • Happens-before: a write to a volatile variable happens-before every subsequent read of the same variable, which also makes earlier writes by the writing thread visible to the reader.
  • Arrays: volatile int[] a makes the reference volatile; writes to elements are not volatile. Use AtomicIntegerArray or VarHandle for element access.
  • Cost: cheaper than a lock, but slower than a plain field because it limits optimizations and may need memory barriers.
  • Compound state: if two fields must change together, volatile cannot protect the invariant; use synchronized or an immutable holder object.
  • Not related to C's volatile: in Java it is about thread visibility, not about hardware registers.
  • Single-threaded code gains nothing from it.