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.
Asked
Active
Viewed 84 times
0
1 Answers
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

chandra Kalita
- 37
- 2
-
Ok but what does java do when it fails after the cpu reaches time limit for fails? – PatternRecognition May 06 '19 at 11:42