Atomic Operation
Atomic operation make sure that the operation can only be execute in a single thread at a time. The CPU will make sure this happen. So 2 thread or 2 process execute the same line at the same time, CPU will make sure to make it sequential and execute one line at a time.
For example
private final AtomicInteger capacity;
capacity.incrementAndGet(); // this can only be executed
// within 1 thread.
However if we have multiple atomic operations, it's not guaranteed to be all executed in a single thread .
For example
private void incrementEnd() {
capacity.incrementAndGet();
if (end.incrementAndGet() == storage.length) {
end.set(0);
}
}
This is not always guaranteed to be thread-safe