美文网首页
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原子类

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