深入理解并发之AtomicReference类

深入理解并发之AtomicReference类

​ 上篇文章主要分享了基本封装类型的原子类,例如 AtomicInteger,AtomicBoolean,AtomicLong等。那么对象的原子操作如何实现?那本文就将从AtomicReference 类讲述对象原子类如何操作。

1: AtomicReference 源码

public class AtomicReference<V> implements java.io.Serializable {
 
    private static final Unsafe unsafe = Unsafe.getUnsafe();
    private static final long valueOffset;

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

    private volatile V value;

    /**
     * 有参构造
     */
    public AtomicReference(V initialValue) {
        value = initialValue;
    }

    /**
     * 无参构造
     */
    public AtomicReference() {
    }

    /**
     *  获取AtomicReference的当前对象引用值
     */
    public final V get() {
        return value;
    }

    /**
     * 设置AtomicReference最新的对象引用值,该新值的更新对其他线程立即可见
     */
    public final void set(V newValue) {
        value = newValue;
    }


    /**
     * 原子性地更新AtomicReference内部的value值,其中expect代表当前AtomicReference的value值,update		*  则是需要设置的新引用值。该方法会返回一个boolean的结果,当expect和AtomicReference的当前值不相        *  等时,修改会失败,返回值为false,若修改成功则会返回true.
     */
    public final boolean compareAndSet(V expect, V update) {
        return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
    }


    /**
     * 原子性地更新AtomicReference内部的value值,并且返回AtomicReference的旧值
     */
    @SuppressWarnings("unchecked")
    public final V getAndSet(V newValue) {
        return (V)unsafe.getAndSetObject(this, valueOffset, newValue);
    }

    /**
     *原子性地更新value值,并且返回AtomicReference的旧值,该方法需要传入一个Function接口
     */
    public final V getAndUpdate(UnaryOperator<V> updateFunction) {
        V prev, next;
        do {
            prev = get();
            next = updateFunction.apply(prev);
        } while (!compareAndSet(prev, next));
        return prev;
    }

    /**
     * 原子性地更新value值,并且返回AtomicReference更新后的新值,该方法需要传入一个Function接口
     */
    public final V updateAndGet(UnaryOperator<V> updateFunction) {
        V prev, next;
        do {
            prev = get();
            next = updateFunction.apply(prev);
        } while (!compareAndSet(prev, next));
        return next;
    }

    /**
     * 原子性地更新value值,并且返回AtomicReference更新前的旧值。该方法需要传入两个参数,
     *	第一个是更新后的新值,
     *	第二个是BinaryOperator接口
     */
    public final V getAndAccumulate(V x,
                                    BinaryOperator<V> accumulatorFunction) {
        V prev, next;
        do {
            prev = get();
            next = accumulatorFunction.apply(prev, x);
        } while (!compareAndSet(prev, next));
        return prev;
    }

    /**
     * 原子性地更新value值,并且返回AtomicReference更新后的值。该方法需要传入两个参数,
     *	第一个是更新的新值,
     *	第二个是BinaryOperator接口。
     */
    public final V accumulateAndGet(V x,
                                    BinaryOperator<V> accumulatorFunction) {
        V prev, next;
        do {
            prev = get();
            next = accumulatorFunction.apply(prev, x);
        } while (!compareAndSet(prev, next));
        return next;
    }
}

2: 基本用法

假设,我们现在要对用户进行银行卡转账操作并保证转账的安全性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大伟攀高峰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值