Pinch Pinch - 7 months ago 75
Java Question

Properties of AtomicInteger

If I have an AtomicInteger that starts out with 0, and it is incremented N times, is the final value guaranteed to be N? I guess it should be as that is the point of having AtomicInteger as a thread-safe counter, but I just wanted to make sure.


You should have searched before asking this here. From this question,

There are two main uses of AtomicInteger:

  1. As an atomic counter (incrementAndGet(), etc) that can be used by many threads concurrently
  2. As a primitive that supports compare-and-swap instruction (compareAndSet()) to implement non-blocking algorithms.

So the answer to your question is Yes