今天学习AtomicLongFieldUpdater类,该类是JUC原子包中的类,通过单元测试代码把所有public api方法跑了一遍,大致了解了底层实现
package test.java.util.concurrent.atomic;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.function.LongBinaryOperator;
import java.util.function.LongUnaryOperator;
import org.junit.Test;
/**
* AtomicLongFieldUpdater的测试类
*
* @author zqw
* @date 2020-06-18 23:41:56
*/
public class AtomicLongFieldUpdaterTest {
//abstract
private volatile long age;
/**
* 初始化采用静态方法+内部类,私有化构造函数
* 否则不更新,返回false
*
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testNewUpdater() {
AtomicLongFieldUpdaterTest test=new AtomicLongFieldUpdaterTest();
AtomicLongFieldUpdater updater = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class,
"age");
System.out.println(updater.get(test));
}
/**
* 如果指定对象的目标属性值为expect的值,则更新成新值,返回true
* 否则不更新,返回false
*
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testCompareAndSet() {
AtomicLongFieldUpdaterTest test = new AtomicLongFieldUpdaterTest();
AtomicLongFieldUpdater updater = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class,
"age");
System.out.println(updater.compareAndSet(test, 0, 2));
System.out.println(updater.get(test));
}
/**
* 如果指定对象的目标属性值为expect的值,则更新成新值,返回true
* 否则不更新,返回false
* weakCompareAndSet和compareAndSet暂时未发现区别
*
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testWeakCompareAndSet() {
AtomicLongFieldUpdaterTest test = new AtomicLongFieldUpdaterTest();
AtomicLongFieldUpdater updater = AtomicLongFieldUpdater.newUpdater(AtomicLongField