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.