AtomicReferenceFieldUpdater api详解

  • Post author:
  • Post category:其他


今天学习AtomicReferenceFieldUpdater类,该类是JUC原子包中的类,通过单元测试代码把所有public api方法跑了一遍,大致了解了底层实现

package test.java.util.concurrent.atomic;


import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import java.util.function.BinaryOperator;
import java.util.function.LongBinaryOperator;
import java.util.function.UnaryOperator;

import org.junit.Test;

/**
 * AtomicReferenceFieldUpdater的测试类
 *
 * @author zqw
 * @date 2020-06-20 22:34:19
 */
public class AtomicReferenceFieldUpdaterTest {
    //abstract
    private volatile Integer age;

    /**
     * 初始化采用静态方法+内部类,私有化构造函数
     * @throws
     * @date 2020/6/20 23:46
     */
    @Test
    public void testNewUpdater() {
        AtomicReferenceFieldUpdaterTest test=new AtomicReferenceFieldUpdaterTest();
        AtomicReferenceFieldUpdater updater = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class,
                Integer.class,"age");
        System.out.println(updater.get(test));
    }

    /**
     * 如果指定对象的目标属性值为expect的值,则更新成新值,返回true
     * 否则不更新,返回false
     *
     * @throws
     * @date 2020/6/20 23:46
     */
    @Test
    public void testCompareAndSet() {
        AtomicReferenceFieldUpdaterTest test = new AtomicReferenceFieldUpdaterTest();
        AtomicReferenceFieldUpdater updater = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpda



版权声明:本文为u011106263原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。