AtomicInteger cas乐观锁的累加方式 & cas下的ABA问题复现

3 篇文章 0 订阅
package com.autonavi.collapse.deteleable;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicStampedReference;
import java.util.stream.IntStream;

public class Test {

    //验证volatile的线程安全问题
    private static class Test1 {

        private volatile static Integer integer = 0;

        public static void main(String[] args) throws InterruptedException {
            CountDownLatch countDownLatch = new CountDownLatch(10);
            IntStream.range(0,10).forEach(i -> {
                countDownLatch.countDown();
                new Thread(() -> {
                    IntStream.range(0,100).forEach(ii ->{
                        integer++;
                    });
                }).start();
            });
            countDownLatch.await();

            System.out.println(integer);
        }

    }

    //AtomicInteger cas乐观锁的累加方式
    private static class Test2 {

        public static void main(String[] args) throws Exception {
            AtomicInteger atomicInteger = new AtomicInteger(0);
            CountDownLatch countDownLatch = new CountDownLatch(10);
            IntStream.range(0,10).forEach(i -> {
                new Thread(() -> {
                    IntStream.range(0,100).forEach(ii -> {
                        atomicInteger.getAndIncrement();
                    });
                    countDownLatch.countDown();
                }).start();
            });
            countDownLatch.await();
            System.out.println(atomicInteger);
        }

    }


    //cas下的ABA问题复现
    private static class Test3 {

        public static void main(String[] args) throws Exception {
            AtomicInteger atomicInteger = new AtomicInteger(100);
            Thread thread = new Thread(() -> {
                atomicInteger.compareAndSet(100,101);
                atomicInteger.compareAndSet(101,100);
            });
            Thread thread1 = new Thread(() -> {
                try {
                    Thread.sleep(1L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                boolean b = atomicInteger.compareAndSet(100,101);
                if (b) {
                    System.out.println("验证成功");
                } else {
                    System.out.println("验证失败");
                }
            });
            thread.start();
            thread1.start();
            thread.join();
            thread1.join();
            System.out.println(atomicInteger);
        }

    }


    //cas下的ABA问题复现
    private static class Test4 {

        public static void main(String[] args) throws Exception {
            AtomicStampedReference atomicInteger = new AtomicStampedReference(100,1);
            Thread thread = new Thread(() -> {
                atomicInteger.compareAndSet(100,101,1,2);
                atomicInteger.compareAndSet(101,100,2,3);
            });
            Thread thread1 = new Thread(() -> {
                try {
                    Thread.sleep(1L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                boolean b = atomicInteger.compareAndSet(100,101,1,2);
                if (b) {
                    System.out.println("验证成功");
                } else {
                    System.out.println("验证失败");
                }
            });
            thread.start();
            thread1.start();
            thread.join();
            thread1.join();
            System.out.println(atomicInteger.getReference());
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值