• 56 说一下 atomic 的原理?


    说一下 atomic 的原理?

    答:

    JDK Atomic开头的类,是通过 CAS 原理解决并发情况下原子性问题。

    CAS 包含 3 个参数,CAS(V, E, N)。V 表示需要更新的变量,E 表示变量当前期望值,N 表示更新为的值。只有当变量 V 的值等于 E 时,变量 V 的值才会被更新为 N。如果变量 V 的值不等于 E ,说明变量 V 的值已经被更新过,当前线程什么也不做,返回更新失败。

    当多个线程同时使用 CAS 更新一个变量时,只有一个线程可以更新成功,其他都失败。失败的线程不会被挂起,可以继续重试 CAS,也可以放弃操作。

    CAS 操作的原子性是通过 CPU 单条指令完成而保障的。JDK 中是通过 Unsafe 类中的 API 完成的。

    在并发量很高的情况,会有大量 CAS 更新失败,所以需要慎用。
     

    原文链接: https://www.cnblogs.com/ConstXiong/p/12020540.html (本文)
    下面内容太多,以后再来看吧
    原文链接: https://blog.csdn.net/wuzhiwei549/article/details/82621947
    原文链接: https://www.jb51.net/article/129690.htm
    原文链接: https://www.jianshu.com/p/3d68e2b9cb12
    原文链接: https://cloud.tencent.com/developer/article/1476540
    原文链接: http://www.manongjc.com/article/57064.html
     

    未使用原子类,测试代码

    package constxiong.interview;
     
    /**
     * JDK 原子类测试
     * @author ConstXiong
     * @date 2019-06-11 11:22:01
     */
    public class TestAtomic {
     
        private int count = 0;
        
        public int getAndIncrement() {
            return count++;
        }
        
    //    private AtomicInteger count = new AtomicInteger(0);
    //    
    //    public int getAndIncrement() {
    //        return count.getAndIncrement();
    //    }
        
        public static void main(String[] args) {
            final TestAtomic test = new TestAtomic();
            for (int i = 0; i <3; i++) {
                new Thread(){
                    @Override
                    public void run() {
                        for (int j = 0; j <10; j++) {
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            System.out.println(Thread.currentThread().getName() + " 获取递增值:" + test.getAndIncrement());
                        }
                    }
                }.start();
            }
        }   
    }
    

     

    打印结果中,包含重复值

    Thread-0 获取递增值:1
    Thread-2 获取递增值:2
    Thread-1 获取递增值:0
    Thread-0 获取递增值:3
    Thread-2 获取递增值:3
    Thread-1 获取递增值:3
    Thread-2 获取递增值:4
    Thread-0 获取递增值:5
    Thread-1 获取递增值:5
    Thread-1 获取递增值:6
    Thread-2 获取递增值:8
    Thread-0 获取递增值:7
    Thread-1 获取递增值:9
    Thread-0 获取递增值:10
    Thread-2 获取递增值:10
    Thread-0 获取递增值:11
    Thread-2 获取递增值:13
    Thread-1 获取递增值:12
    Thread-1 获取递增值:14
    Thread-0 获取递增值:14
    Thread-2 获取递增值:14
    Thread-1 获取递增值:15
    Thread-2 获取递增值:15
    Thread-0 获取递增值:16
    Thread-1 获取递增值:17
    Thread-0 获取递增值:19
    Thread-2 获取递增值:18
    Thread-0 获取递增值:20
    Thread-1 获取递增值:21
    Thread-2 获取递增值:22
     

    测试代码修改为原子类

    package constxiong.interview;
     
    import java.util.concurrent.atomic.AtomicInteger;
     
    /**
     * JDK 原子类测试
     * @author ConstXiong
     * @date 2019-06-11 11:22:01
     */
    public class TestAtomic {
     
    //    private int count = 0;
    //    
    //    public int getAndIncrement() {
    //        return count++;
    //    }
        
        private AtomicInteger count = new AtomicInteger(0);
        
        public int getAndIncrement() {
            return count.getAndIncrement();
        }
        
        public static void main(String[] args) {
            final TestAtomic test = new TestAtomic();
            for (int i = 0; i <3; i++) {
                new Thread(){
                    @Override
                    public void run() {
                        for (int j = 0; j <10; j++) {
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            System.out.println(Thread.currentThread().getName() + " 获取递增值:" + test.getAndIncrement());
                        }
                    }
                }.start();
            }
        }
        
        
    }
    

     

    打印结果中,不包含重复值

    Thread-0 获取递增值:1
    Thread-2 获取递增值:2
    Thread-1 获取递增值:0
    Thread-0 获取递增值:3
    Thread-1 获取递增值:4
    Thread-2 获取递增值:5
    Thread-0 获取递增值:6
    Thread-1 获取递增值:7
    Thread-2 获取递增值:8
    Thread-0 获取递增值:9
    Thread-2 获取递增值:10
    Thread-1 获取递增值:11
    Thread-0 获取递增值:12
    Thread-1 获取递增值:13
    Thread-2 获取递增值:14
    Thread-0 获取递增值:15
    Thread-1 获取递增值:16
    Thread-2 获取递增值:17
    Thread-0 获取递增值:18
    Thread-1 获取递增值:19
    Thread-2 获取递增值:20
    Thread-0 获取递增值:21
    Thread-2 获取递增值:23
    Thread-1 获取递增值:22
    Thread-0 获取递增值:24
    Thread-1 获取递增值:25
    Thread-2 获取递增值:26
    Thread-0 获取递增值:27
    Thread-2 获取递增值:28
    Thread-1 获取递增值:29

  • 相关阅读:
    青浦图书志
    师徒俩就这样比划着 讲着该怎么去使用Reactor Programming
    Stephane Maldini and Violeta Georgieva at SpringOne Platform 2019
    reactor 大会
    这样使用Mono.zip就可以合并多个值进行下一步运算
    You are totally right 和 You are absolutely right 区别
    reacctor 1
    developer.ibm.com
    zip~~~~~~~~~~~
    044 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 06 使用do-while循环实现猜字游戏
  • 原文地址:https://www.cnblogs.com/ynzj123/p/12902480.html
Copyright © 2020-2023  润新知