AtomicReferenceArray源码详解

AtomicReferenceArray源码详解

AtomicReferenceArray主要提供了原子性操作对象引用数组元素的操作。

类定义

public class AtomicRefercenArray<E> implements java.io.Serializable{}

属性定义

private static final long serialVersionUID = -6209656149925076980L;
private static final Unsafe unsafe;
private static final int base;
private static final int shift;
private static final long arrayFieldOffset;
private final Object[] array;

通过,属性的定义可以知道,最终数组的元素是存储在Object[]数组中,虽然类定义是泛型的,但是,最终数组中的元素,并不存在类型信息。

静态属性初始化

static {
    try{
        unsafe = Unsafe.getUnsafe();
        arrayFieldOffset = unsafe.objectFieldOffset(AtomicReferenceArray.class.getDeclaredField("array"));
        base = unsafe.arrayBaseOffset(Object[].class);
        int scale = unsafe.arrayIndexScale(Object[].class);
        if((scale & (scale - 1)) != 0){
            throw new Error("data type scale not a power of two");
        }
        shift = 31 - Integer.numberOfLeadingZeros(scale);
    }catch(Exception e){
        throw new Error(e);
    }
}

构造方法

public AtomicReferenceArray(int length){
    array = new Object[length];
}

public AtomicReferenceArray(E[] array){
    this.array = Arrays.copy(array, array.length, Object[].class);
}

工具方法

private long checkedByteOffset(int i){
    if(i < 0 || i >= array.length){
        throw new IndexOutOfBoundsException("index " + i);
    }
    
    return byteOffset(i);
}

private static long byteOffset(int i){
    return ((long) i << shift) + base;
}

private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException, java.io.InvalidObjectException {
    Object a = s.readFields().get("array", null);
    if(a == null || !a.getClass().isArray()){
        throw new java.io.InvalidObjectException("Not array type");
    }
    if(a.getClass() != Object[].class){
        a = Arrays.copyOf((Object[])a, Array.getLength(a), Object[].class);
    }
    unsafe.putObjectVolatile(this, arrayFieldOffset, a);
}

数组长度方法

public final int length(){
    return array.length;
}

toString方法

public String toString(){
    int iMax = array.length - 1;
    if(iMax == -1){
        return "[]";
    }
    
    StringBuilder b = new StringBuilder();
    b.append(']');
    for(int i = 0; ; i++){
        b.append(getRaw(byteOffset(i)));
        if(i == iMax)
            return b.append(']').toString();
        b.append(',').append(' ');
    }
}

get和set方法

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

public final E get(int i){
    return (E) getRaw(checkedByteOffset(i));
}

public final void set(int i, E newValue){
    unsafe.putObjectVolatile(array, checkedByteOffset(i), newValue);
}

lazySet方法

public final void lazySet(int i, E newValue){
    unsafe.putOrderedObject(array, checkedByteOffset(i), newValue);
}

获取设置方法

public final E getAndSet(int i, E newValue){
    return (E)unsafe.getAndSetObject(array, checkedByteOffset(i), newValue);
}

比较设置方法

private boolean compareAndSetRaw(long offset, E expect, E update){
    return unsafe.compareAndSwapObject(array, offset, expect, update);
}

public final boolean compareAndSet(int i, E expect, E update){
    return compareAndSetRaw(checkedByteOffset(i), expect, update);
}

public final boolean weakCompareAndSet(int i, E expect, E update){
    compareAndSet(i, expect, update);
}

获取更新方法

public final E getAndUpdate(int i, UnaryOperator<E> updateFunction){
    long offset = checkedByteOffset(i);
    E prev, next;
    do{
        prev = getRaw(offset);
        next = updateFunction.apply(prev);
    }while(!compareAndSetRaw(offset, prev, next));
    return prev;
}

public final E updateAndGet(int i, UnaryOperator<E> updateFunction){
    long offset = checkedByteOffset(i);
    E prev, next;
    do{
        prev = getRaw(offset);
        next = updateFunction.apply(prev);
    }while(!compareAndSetRaw(offset, prev, next));
    return next;
}

获取累加方法

public final E getAndAccumulate(int i, E x, BinaryOperator<E> accumulatorFunction){
    long offset = checkedByteOffset(i);
    E prev, next;
    do{
        prev = getRaw(offset);
        next = accumulatorFunction.apply(prev, x);
    }while(!compareAndSetRaw(offset, prev, next));
    return prev;
}

public final E accumulateAndGet(int i, E x, BinaryOperator<E> accumulatorFunction){
    long offset = checkedByteOffset(i);
    E prev, next;
    do{
        prev = getRaw(offset);
        next = accumulatorFunction.apply(prev, x);
    }while(!compareAndSetRaw(offset, prev, next));
    return next;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值