Search code examples
javamultithreadingsynchronizationvolatileatomicinteger

Volatile keyword on Atomic Variable


The following code is valid in Java

volatile AtomicInteger a = new AtomicInteger(123);

Do we require volatile keyword on Atomic variables such as AtomicInteger? Or is volatile superfluous?


Solution

  • It's superfluous for most sane use cases, but conceivably applicable to some weird cases -- not that I can think of any. When in doubt, use final.