Quantcast
Viewing latest article 3
Browse Latest Browse All 4

Answer by Vano for Use of infinite loop in AtomicInteger.addAndGet(int)

The motivation here is that you will eventuallyreturn next.Meaning that the condition :

if (compareAndSet(current, next))

Will be true at some iteration. It isn't known when exactly it will happen, so that is why you need to loop until it does. If you look at the code for compareAndSet you will see that it returns true when successful, and you can't know that it will be successful on the first try:

Returns true if successful. False return indicates that the actual value was not equal to the expected value.


Viewing latest article 3
Browse Latest Browse All 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>