-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
  • 506
  • 4
  • 13
Pinch
  • 2,768
  • 3
  • 28
  • 44

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,462
  • 1
  • 23
  • 34