20210817
AtomicInteger的incrementAndGet最终调Unsafe.getAndAddInt实现的
Unsafe.getAndAddInt:
public final int getAndAddInt(Object var1, long var2, int var4) {
int var5;
do {
var5 = this.getIntVolatile(var1, var2);
} while(!this.compareAndSwapInt(var1, var2, var5, var5 + var4));
return var5;
}
所以答案是通过自旋锁和CAS实现的
网友评论