Atomic原子類

1 AtomicInteger+AtomicLong+AtomicBoolean+AtomicReference

(1)AtomicInteger:以原子方式更新int類型變量。
(2)AtomicLong:以原子方式更新loog類型變量。
(3)AtomicBoolean:以原子方式更新boolean類型變量。
(4)AtomicReference:以原子方式更新引用類型變量。

public class AtomicIntegerDemo {

    private AtomicInteger counter = new AtomicInteger();
    private CountDownLatch countDownLatch = new CountDownLatch(10);

    public AtomicInteger getCounter() {
        return counter;
    }

    public CountDownLatch getCountDownLatch() {
        return countDownLatch;
    }

    public void add() {
        for (int i = 0; i < 100; i++) {
            counter.incrementAndGet();
        }
        countDownLatch.countDown();
    }

}
public class AtomicIntegerTest {

    public static void main(String[] args) {
        AtomicIntegerDemo demo = new AtomicIntegerDemo();
        List<Thread> threads = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            threads.add(new Thread(() -> {
                demo.add();
            }));
        }
        for (Thread thread : threads) {
            thread.start();
        }
        try {
            demo.getCountDownLatch().await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 輸出1000
        System.out.println(demo.getCounter().get());
    }

}
2 AtomicIntegerArray+AtomicLongArray+AtomicReferenceArray

(1)AtomicIntegerArray:以原子方式更新int類型數(shù)組中的元素。
(2)AtomicLongArray:以原子方式更新long類型數(shù)組中的元素。
(3)AtomicReferenceArray:以原子方式更新引用類型數(shù)組中的元素。

3 AtomicStampedReference

AtomicStampedReference:以原子方式更新引用類型變量,同時(shí)解決了ABA問(wèn)題。

4 AtomicIntegerFieldUpdater+AtomicLongFieldUpdater+AtomicReferenceFieldUpdater

(1)AtomicIntegerFieldUpdater:以原子方式更新指定類中的int類型字段,這個(gè)字段必須被volatile修飾。
(2)AtomicLongFieldUpdater:以原子方式更新指定類中的long類型字段,這個(gè)字段必須被volatile修飾。
(3)AtomicReferenceFieldUpdater:以原子方式更新指定類中的引用類型字段,這個(gè)字段必須被volatile修飾。
示例一:

public class Data {
    public volatile int value1 = 10;
    protected volatile int value2 = 20;
    volatile int value3 = 30;
}
public class AtomicIntegerFieldUpdaterDemo1 {

    public static void main(String[] args) {
        Data data = new Data();
        System.out.println(data.value1);// 輸出10
        System.out.println(data.value2);// 輸出20
        System.out.println(data.value3);// 輸出30
        AtomicIntegerFieldUpdater<Data> updater1 = AtomicIntegerFieldUpdater.newUpdater(Data.class, "value1");
        updater1.compareAndSet(data, 10, 100);// 成功
        updater1.compareAndSet(data, 10, 200);// 失敗
        System.out.println(data.value1);// 輸出100
        AtomicIntegerFieldUpdater<Data> updater2 = AtomicIntegerFieldUpdater.newUpdater(Data.class, "value2");
        System.out.println(updater2.incrementAndGet(data));// 輸出21
        AtomicIntegerFieldUpdater<Data> updater3 = AtomicIntegerFieldUpdater.newUpdater(Data.class, "value3");
        System.out.println(updater3.decrementAndGet(data));// 輸出29
    }

}

示例二:

public class AtomicIntegerFieldUpdaterDemo2 {
    
    public volatile int value1 = 1;
    protected volatile int value2 = 2;
    volatile int value3 = 3;
    private volatile int value4 = 4;

    public static void main(String[] args) {
        AtomicIntegerFieldUpdaterDemo2 demo = new AtomicIntegerFieldUpdaterDemo2();
        System.out.println(demo.value1);// 輸出1
        System.out.println(demo.value2);// 輸出2
        System.out.println(demo.value3);// 輸出3
        System.out.println(demo.value4);// 輸出4
        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterDemo2> updater1 = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterDemo2.class, "value1");
        updater1.compareAndSet(demo, 1, 10);// 成功
        updater1.compareAndSet(demo, 1, 20);// 失敗
        System.out.println(demo.value1);// 輸出10
        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterDemo2> updater2 = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterDemo2.class, "value2");
        System.out.println(updater2.incrementAndGet(demo));// 輸出3
        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterDemo2> updater3 = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterDemo2.class, "value3");
        System.out.println(updater3.decrementAndGet(demo));// 輸出2
        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterDemo2> updater4 = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterDemo2.class, "value4");
        System.out.println(updater4.getAndSet(demo, 40));// 輸出4
        System.out.println(demo.value4);// 輸出40
    }
    
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容