047_java.util.concurrent.atomic.AtomicIntegerArray

简单介绍

AtomicIntegerArray是Integer数组的线程安全原子类,和他类似的还有AtomicLongArray,AtomicReferenceArray,从实现上来说这三个都差不多,因此这里我们仅对AtomicIntegerArray进行分析。同样的,AtomicIntegerArray底层使用unsafe进行对数据的操作。

重要字段

private static final Unsafe unsafe = Unsafe.getUnsafe();
// 获取数组起始位置的偏移量
private static final int base = unsafe.arrayBaseOffset(int[].class);
private static final int shift;
private final int[] array;

static {
    // 获取数据元素的大小(size),int类型的是 4
    int scale = unsafe.arrayIndexScale(int[].class);
    if ((scale & (scale - 1)) != 0)
        throw new Error("data type scale not a power of two");
        
    // int类型(4)的前置0个数为29
    // 表达式返回位移量,用于计算下标。用于<<操作符,表示乘以2^shift  2^2=4	
    shift = 31 - Integer.numberOfLeadingZeros(scale);
}

构造函数

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

public AtomicIntegerArray(int[] array) {
    // Visibility guaranteed by final field guarantees
    this.array = array.clone();
}

AtomicIntegerArray的构造函数很简单,就是初始化了一个数组。

重要方法

unsafe.getAndAddInt

// i位置加一,返回i位置旧值
public final int getAndIncrement(int i) {
    return getAndAdd(i, 1);
}

// i位置减一,返回i位置旧值
public final int getAndDecrement(int i) {
    return getAndAdd(i, -1);
}

// i位置加delta,返回i位置旧值
public final int getAndAdd(int i, int delta) {
    return unsafe.getAndAddInt(array, checkedByteOffset(i), delta);
}

// i位置加一,返回i位置新值
public final int incrementAndGet(int i) {
    return getAndAdd(i, 1) + 1;
}

// i位置减一,返回i位置新值
public final int decrementAndGet(int i) {
    return getAndAdd(i, -1) - 1;
}

// i位置加delta,返回i位置新值
public final int addAndGet(int i, int delta) {
    return getAndAdd(i, delta) + delta;
}

unsafe.compareAndSwapInt

// 不保证有序性的原子方法
public final boolean weakCompareAndSet(int i, int expect, int update) {
    return compareAndSet(i, expect, update);
}

// i位置使用expect比较,匹配则更新为update,成功返回true
public final boolean compareAndSet(int i, int expect, int update) {
    return compareAndSetRaw(checkedByteOffset(i), expect, update);
}

// i位置使用expect比较,匹配则更新为update,成功返回true
private boolean compareAndSetRaw(long offset, int expect, int update) {
    return unsafe.compareAndSwapInt(array, offset, expect, update);
}



get/set

// 从i位置获得数据
public final int get(int i) {
    return getRaw(checkedByteOffset(i));
}

// 从offset位置获得数据
private int getRaw(long offset) {
    return unsafe.getIntVolatile(array, offset);
}

// i位置赋值newValue
public final void set(int i, int newValue) {
    unsafe.putIntVolatile(array, checkedByteOffset(i), newValue);
}

// i位置赋值newValue,仅在get的时候才真正同步到主内存 
public final void lazySet(int i, int newValue) {
    unsafe.putOrderedInt(array, checkedByteOffset(i), newValue);
}

// i位置赋值newValue
public final int getAndSet(int i, int newValue) {
    return unsafe.getAndSetInt(array, checkedByteOffset(i), newValue);
}

总结

AtomicIntegerArray是对数组的线程安全封装,提供对数组某个位置的原子操作,底层依旧使用Unsafe类进行操作。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中的`java.util.concurrent.atomic.AtomicReference`提供了一种线程安全的方式来更新对象引用。它通过使用CAS(Compare-And-Swap)算法实现了原子性的操作。 下面是一个简单的案例,展示如何使用`AtomicReference`类: ```java import java.util.concurrent.atomic.AtomicReference; public class AtomicReferenceExample { public static void main(String[] args) { // 初始化AtomicReference对象 AtomicReference<String> atomicReference = new AtomicReference<>("Hello"); // 获取当前对象引用的值 String currentValue = atomicReference.get(); System.out.println("Current value: " + currentValue); // 比较并替换 boolean updated = atomicReference.compareAndSet("Hello", "World"); System.out.println("Value updated: " + updated); // 获取更新后的值 String updatedValue = atomicReference.get(); System.out.println("Updated value: " + updatedValue); } } ``` 在上面的示例中,我们首先创建了一个`AtomicReference`对象,并初始化为字符串`"Hello"`。然后我们使用`compareAndSet`方法比较当前对象引用的值是否为`"Hello"`,如果是,则将其替换为`"World"`。最后,我们获取更新后的值,并输出到控制台。 需要注意的是,`AtomicReference`类提供了许多其他有用的方法,如`set`、`getAndSet`、`weakCompareAndSet`等,可以根据具体的需求选择使用。 总之,`AtomicReference`类是Java中一种非常有用的线程安全对象引用类,可以避免多个线程同时修改对象引用时出现的竞争条件问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值