Search code examples
javamultithreadingatomicrace-conditionvolatile

In which situations it's needed to use volatile keyword with Atomic field?


In which situations it's needed to use volatile keyword with Atomic field? Or if the field is Atomic*** keyword volatile is redundant?

Thank you


Solution

  • Difference is nicely mentioned in Java Concurrency In Practice book:

    • Volatile provides Memory visibility guarantee
    • Atomic variables provide both Memory visibility and Atomicity

    So there is no need to use volatile keyword with AtomicXXX variables.