Prior to Java 8, the code for CAS in the AtomicLong class was:
public final long incrementAndGet() {
for (;;) {
long current = get();
long next = current + 1;
if (compareAndSet(current, next))
return next;
}
}
But now it has been changed to one inner line:
public final long incrementAndGet() {
return unsafe.getAndAddLong(this, valueOffset, 1L) + 1L;
}
What advantage does this code have over the first? How does this new code work?
source
share