浅析CompareAndSet(CAS)

CAS:Compare and Swap,比较并交换。

java.util.concurrent包中借助CAS实现了区别于synchronouse同步锁的一种乐观锁。

CAS有3个操作数,内存值V,旧的预期值A,要修改的新值B。当且仅当预期值A和内存值V相同时,将内存值V修改为B,否则什么都不做。

在AtomicInteger.java里,CAS是这么被实际应用的,就拿里头的自增函数来说:

    /**
     * Atomically increments by one the current value.
     * @return the updated value
     */
    public final int incrementAndGet() {
        for (;;) {
            int current = get();
            int next = current + 1;
            if (compareAndSet(current, next))
                return next;
        }
    }
    /**
     * Atomically sets the value to the given updated value
     * if the current value {@code ==} the expected value.
     * @param expect the expected value
     * @param update the new value
     * @return true if successful. False return indicates that
     * the actual value was not equal to the expected value.
     */
    public final boolean compareAndSet(int expect, int update) {
        return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
    }

从注释来看,就是在native函数里,判断当前对象和期望的expect(也就是前面的current)是否一致,如果一致则返回true,且还做了一步自增操作;

从this和valueOffset来看,this是具体的内存地址,而valueOffset是变量的在该对象里的内存地址,这是为了后续汇编代码的操作,所以这里的this结合valueOffset则就是AtomicInteger的实际值,而current是线程修改时用到的初值,如果实际值和初值一致,则说明当前并没有人修改AtomicInteger,故当前修改有效,那么就可以继续赋值为update

    private static final long valueOffset;
    static {
        try {
            valueOffset = unsafe.objectFieldOffset
               (AtomicInteger.class.getDeclaredField("value"));
        } catch (Exception ex) { throw new Error(ex); }
    }

如图的value就是当前AtomicInteger的值。

总的可以理解为如下操作:

if (this == expect) {
    this = update
    return true;
} else {
    return false;
}

再进一步的底层,使用的汇编语言上的锁 LOCK_IF_MP(mp),这里就不再深入说明了。

原文链接:https://www.jianshu.com/p/e13a46e866ae

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值