public final int incrementAndGet() {
for (;;) {
int current = get();
int next = current + 1;
if (compareAndSet(current, next))
return next;
}
}
這是自增操作,定義: CAS有3個操作數,內存值V,舊的預期值A,要修改的新值B。當且僅當預期值A和內存值V相同時,將內存值V修改為B,否則什么都不做預期值是next嗎? 內存值是current?假如1個線程自增時沒有其他線程競爭,那么預期值應該比內存值大1啊,怎么會預期值和內存值相同?
AtomicInteger 的CAS 問題
慕桂英3389331
2019-03-01 10:53:54