美文网首页
Atomic原子类

Atomic原子类

作者: 永远的太阳0123 | 来源:发表于2018-09-17 20:44 被阅读0次
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类型数组中的元素。
(2)AtomicLongArray:以原子方式更新long类型数组中的元素。
(3)AtomicReferenceArray:以原子方式更新引用类型数组中的元素。

3 AtomicStampedReference

AtomicStampedReference:以原子方式更新引用类型变量,同时解决了ABA问题。

4 AtomicIntegerFieldUpdater+AtomicLongFieldUpdater+AtomicReferenceFieldUpdater

(1)AtomicIntegerFieldUpdater:以原子方式更新指定类中的int类型字段,这个字段必须被volatile修饰。
(2)AtomicLongFieldUpdater:以原子方式更新指定类中的long类型字段,这个字段必须被volatile修饰。
(3)AtomicReferenceFieldUpdater:以原子方式更新指定类中的引用类型字段,这个字段必须被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
    }
    
}

相关文章

  • Atomic原子类

    1 AtomicInteger+AtomicLong+AtomicBoolean+AtomicReference ...

  • Atomic原子类

    来源:https://www.cnblogs.com/czsy/archive/2019/05/25/109219...

  • AtomicBoolean源码解析

    1.原子类 java.util.concurrent.atomic包下有很多原子类,这些原子类扩展了vola...

  • 原子类

    一、原子类纵览 类型具体类Atomic* 基本类型原子类AtomicInteger、AtomicLong、Atom...

  • Java并发之JUC原子类(5)

    1 今天我们来聊聊java.util.concurrent.atomic包下的原子类,所谓原子类就是具有原子/原子...

  • 原子类型与原子操作

    原子类型 c++11提供了原子操作类型, 模板类std::atomic。头文件#include 内存模型 在原子类...

  • JUC Atomic原子类深入

    什么是Atomic Atomic是原子性的意思,可以自动更新,用于原子增量计数器之类的应用程序。可以解决多线程环境...

  • 内存模型,顺序一致性

    对于同步,c++11添加了几个原子类型,类型如下atomic_xxx(atomic_int等),对于一般的线程之间...

  • Java中高级核心知识全面解析——Atomic(什么是Atomi

    一、Atomic 原子类介绍 Atomic翻译成中文是原子的意思。在化学上,我们知道原子是构成一般物质的最小单位,...

  • Atomic类型组

    原子类型。 在concurrent.atomic包中定义了若干原子类型,这些类型中的每个方法都是保证了原子操作的。...

网友评论

      本文标题:Atomic原子类

      本文链接:https://www.haomeiwen.com/subject/wjpdnftx.html