Java 并发编程之 AtomicIntergerArray AtomicLongArray AtomicReferenceArray

Java juc About 1,266 words

说明

原子并发数组,保证在多线程下对每个索引位置上的元素的原子性。

AtomicIntergerArray

Integer类型数组。

public class AtomicIntegerArrayDemo {

    public static void main(String[] args) {
        AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(10);
        atomicIntegerArray.set(0, 100);
        atomicIntegerArray.compareAndSet(0, 100, 2021);
        System.out.println(atomicIntegerArray.get(0));

    }

}

AtomicLongArray

Long类型数组。

public class AtomicLongArrayDemo {

    public static void main(String[] args) {
        AtomicLongArray atomicLongArray = new AtomicLongArray(10);
        atomicLongArray.set(0, 100);
        atomicLongArray.compareAndSet(0, 100, 2021);
        System.out.println(atomicLongArray.get(0));
    }

}

AtomicReferenceArray

引用类型数组。

public class AtomicReferenceArrayDemo {

    public static void main(String[] args) {
        AtomicReferenceArray<Object> objectAtomicReferenceArray = new AtomicReferenceArray<>(10);
        System.out.println(objectAtomicReferenceArray.get(0));
        objectAtomicReferenceArray.set(0, new Object());
        System.out.println(objectAtomicReferenceArray.get(0));
        objectAtomicReferenceArray.compareAndSet(1, null, new Object());
        System.out.println(objectAtomicReferenceArray.get(1));
    }

}
Views: 1,075 · Posted: 2021-09-24

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh