Search code examples
javamultithreadingatomic

Atomic operation in multi threaded workld


Are atomic operations safe enough to use it in multi threaded app without causing race conditions and other concurrency issue ? Let say that we don't worry about visibility (we read/write everything from CPU).


Solution

  • Are atomic operations safe enough to use it in multi threaded app without causing race conditions and other concurrency issue?

    Yes, java has strictly defined memory model (also known as JSR 133).

    There are also out-of-box atomic wrappers for primitive types in java.util.concurrent package, like AtomicInteger.

    Atomicity is implemented using compare-and-swap.