1
2
3
4
5
6
7
public final boolean getAndSet(boolean newValue) {
    for (;;) {
        boolean current = get(); // line 3
        if (compareAndSet(current, newValue)) //line 4
            return current;
    }
}

if current value was changed between line 3 and line 4
The new value won’t be set and go loop until it’s done.

AtomicInteger use compare-and-set