0

I have two threads and one atomic integer. Both run a loop of 1000 iterations. One Thread increments by one the other decrements by one. The result is at the end 0.
Good so far.
But does a) one thread wait for next statement when it can't edit the variable because CAS failed or b= does each thread have second backround thread (created implicit) that edits the variable and waits until it can edit the variable?
Second assumtion (b) wouldn't make sense since i get a return value, but i better ask.

1 Answers1

0

AtomicInteger uses Hardware/CPU Level synchronization and threads will wait for a specific amount of time before a CAS attempt fails ( assumption a. is partially true).This wiki has more details https://en.wikipedia.org/wiki/Compare-and-swap