Simplest and understandable example of volatile keyword in Java

Volatile –> Guarantees visibility and NOT atomicity

Synchronization (Locking) –> Guarantees visibility and atomicity (if done properly)

Volatile is not a substitute for synchronization

Use volatile only when you are updating the reference and not performing some other operations on it.

Example:

volatile int i = 0;

public void incrementI(){
   i++;
}

will not be thread safe without use of synchronization or AtomicInteger as incrementing is an compound operation.

Why program does not run indefinitely?

Well that depends on various circumstances. In most cases JVM is smart enough to flush the contents.

Correct use of volatile discusses various possible uses of volatile. Using volatile correctly is tricky, I would say “When in doubt, Leave it out”, use synchronized block instead.

Also:

synchronized block can be used in place of volatile but the inverse is not true.

Leave a Comment