AtomicLongArray和AtomicLongFieldUpdater

216 阅读1分钟
  • AtomicLongArray的作用则是对"长整形数组"进行原子操作,incrementAndGet()的作用是以原子方式将long数组的索引 i 的元素加1,并返回加1之后的值。
    incrementAndGet()源码如下:
public final long incrementAndGet(int i) {
    return addAndGet(i, 1);
}
public long addAndGet(int i, long delta) {
    // 检查数组是否越界
    long offset = checkedByteOffset(i);
    while (true) {
        // 获取long型数组的索引 offset 的原始值
        long current = getRaw(offset);
        // 修改long型值
        long next = current + delta;
        // 通过CAS更新long型数组的索引 offset的值。
        if (compareAndSetRaw(offset, current, next))
            return next;
    }
}

private long getRaw(long offset) {
    return unsafe.getLongVolatile(array, offset);
}

private boolean compareAndSetRaw(long offset, long expect, long update) {
    return unsafe.compareAndSwapLong(array, offset, expect, update);
}
  • AtomicLongFieldUpdater可以对指定"类的 'volatile long'类型的成员"进行原子更新。基于反射原理实现的。
//为对象创建并返回一个具有给定字段的更新器
static <U> AtomicLongFieldUpdater<U> newUpdater(Class<U> tclass, String fieldName)

public class LongFieldTest {
    
    public static void main(String[] args) {
        // 获取Person的class对象
        Class cls = Person.class; 
        // 新建AtomicLongFieldUpdater对象,传递参数是“class对象”和“long类型在类中对应的名称”
        AtomicLongFieldUpdater mAtoLong = AtomicLongFieldUpdater.newUpdater(cls, "id");
        Person person = new Person(12345678L);

        // 比较person的"id"属性,如果id的值为12345678L,则设置为1000。
        mAtoLong.compareAndSet(person, 12345678L, 1000);
        System.out.println("id="+person.getId());
    }
}

class Person {
    volatile long id;
    public Person(long id) {
        this.id = id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public long getId() {
        return id;
    }
}