Here is a code snippet from AtomicInteger:
public final int accumulateAndGet(int x,
IntBinaryOperator accumulatorFunction) {
int prev = get(), next = 0;
for (boolean haveNext = false;;) {
if (!haveNext)
next = accumulatorFunction.applyAsInt(prev, x);
if (weakCompareAndSetVolatile(prev, next))
return next;
haveNext = (prev == (prev = get()));
}
}
Is there any specific reason of using for(... instead of while(... or it's just a programming choice?
Is it because of the reordering done by the jvm?