-4

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.

Sdgh17
  • 148
  • 1
  • 12
Pinch
  • 2,628
  • 3
  • 25
  • 40

1 Answers1

1

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

Community
  • 1
  • 1
Imesha Sudasingha
  • 3,162
  • 1
  • 22
  • 32