Use of infinite loop in AtomicInteger.addAndGet(int)

Viewed 1041

In Java's package java.util.concurrent.atomic AtomicInteger class has a method addAndGet(int)

which is

public final int addAndGet(int delta) {
    for (;;) {
        int current = get();
        int next = current + delta;
        if (compareAndSet(current, next))
            return next;
    }
}

Why it uses infinite loop here to set a value?

3 Answers
Related