AtomicIntegerArray源码详解

14 篇文章 0 订阅
14 篇文章 0 订阅

AtomicIntegerArray源码详解

AtomicIntegerArray主要提供原子性操作int数据类型数组元素的操作。

类定义

public class AtomicIntegerArray implements java.io.Serializable{}

属性定义

private static final long serialVersionUID = 2862133569453604235L;
private static final Unsafe unsafe = Unsafe.getUnsafe();
private static final int base = unsafe.arrayBaseOffset(int[].class);
private static final int shift;
private static int[] array;

base属性的含义是,数组中第一个元组在数组类中的偏移量。

shift属性的含义是,整型元素中,第一个为1的二进制位距离最高位的偏移量。

array属性就是底层,封装的真正的数组。

静态属性初始化

static {
    int scale = unsafe.arrayIndexScale(int[].class);
    if((scale & (scale - 1)) != 0){
        throw new Error("data type scale not a power of two");
    }
    shift = 31 - Integer.numberOfLeadingZeros(scale);
}

构造函数

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

public AtomicIntegerArray(int[] array){
    this.array = array.clone();
}

第一个构造函数,根据给定的长度创建一个AtomicIntegerArray实例,而且数组的元素都为零。

第二个构造函数,根据一个给定的数组对象创建AtomicIntegerArray对象,而且数据元素是给定数组的元素的拷贝。

工具方法

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

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

checkedByteOffset方法用于对数组元素的下标范围进行检查,如果通过检查,那么返回该下标所对应的元素,在数组类中的偏移。

获取数组长度的方法

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方法

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

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

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

get方法,获取指定下标处的元素的值。

getRaw方法,获取AtomicIntegerArray类中,指定偏移处的元素值。

set方法,设置AtomicIntegerArray中的数组中下标为i处,元素的值。

lazySet方法

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

获取设置接口

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

把数组中下标为i的元素的值设置为newValue,然后返回原来的值。

比较设置方法

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

public boolean compareAndSetRaw(long offset, int expect, int update){
    return unsafe.compareAndSwapInt(array, offset, expect, update);
}

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

获取自增方法

public final int getAndIncreament(int i){
    return getAndAdd(i, 1);
}

public final int increamentAndGet(int i){
    return getAndAdd(i, 1) + 1;
}

获取自减方法

public final int getAndDecreament(int i){
    return getAndAdd(i, -1);
}

public final int decrementAndGet(int i){
    return getAndAdd(i, -1) - 1;
}

获取增加方法

public final int getAndAdd(int i, int delta){
    return unsafe.getAndAddInt(array, checkedByteOffset(i), delta);
}

public final int addAndGet(int i, int delta){
    return getAndAdd(i, delta) + delta;
}

获取更新方法

public final int getAndUpdate(int i, IntUnaryOperator updateFunction){
    long offset = checkedByteOffset(i);
    int prev, next;
    do{
        prev = getRaw(offset);
        next = updateFunction.applyAsInt(prev);
    }while(!compareAndSetRaw(offset, prev, next));
    
    return prev;
}

public final int updateAndGet(int i, IntUnaryOperator updateFunction){
    long offset = checkedByteOffset(i);
    int prev, next;
    do{
        prev = getRaw(offset);
        next = updateFunction.applyAsInt(prev);
    }while(!compareAndSetRaw(offset, prev, next));
    return next;
}

获取累加方法

public final int getAndAccumulate(int i, int x, IntBinaryOperator accumulatorFunction){
    long offset = checkedByteOffset(i);
    int prev, next;
    do{
        prev = getRaw(offset);
        next = accumulatorFunction.applyAsInt(prev, x);
    }while(!compareAndSetRaw(offset, prev, next));
    return prev;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值