ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LongAdder和AtomicLong比较

2021-05-03 11:01:23  阅读:302  来源: 互联网

标签:LongAdder Thread int 线程 AtomicLong threadNum 比较


LongAdder和AtomicLong比较

public class LongAdderMain {


    public static void main(String[] args) throws InterruptedException {
        atomicAlongVsLongAdderTest(1, 10000000);
        System.out.println("-----------------------------------");
        atomicAlongVsLongAdderTest(10, 10000000);
        System.out.println("-----------------------------------");
        atomicAlongVsLongAdderTest(50, 10000000);

    }

    private static void atomicAlongVsLongAdderTest(int threadNum, int times)
    											 throws InterruptedException {

        long start = System.currentTimeMillis();
        long result = longAdderRun(threadNum, times);
        System.out.println("LongAdder  线程数: " + threadNum + " 结果: " 
        		+ result + " 耗时: " + (System.currentTimeMillis() - start));

        long start1 = System.currentTimeMillis();
        long result1 = atomicLongRun(threadNum, times);
        System.out.println("AtomicLong 线程数: " + threadNum + " 结果: " 
        + result1 + " 耗时: " + (System.currentTimeMillis() - start1));

    }

    private static Long atomicLongRun(int threadNum, int times) 
    									throws InterruptedException {

        AtomicLong counter = new AtomicLong();
        List<Thread> list = new ArrayList<>();
        for (int i = 0; i < threadNum; i++) {
            Thread t = new Thread(() -> {
                int j = 0;
                while (j < times) {
                    counter.incrementAndGet();
                    j++;
                }

            });
            list.add(t);
        }


        for (Thread thread : list) {
            thread.start();
        }

        for (Thread thread : list) {
            thread.join();
        }
        return counter.get();
    }

    private static long longAdderRun(int threadNum, int times)
    								 throws InterruptedException {
        LongAdder counter = new LongAdder();
        List<Thread> list = new ArrayList<>();
        for (int i = 0; i < threadNum; i++) {
            Thread t = new Thread(() -> {
                int j = 0;
                while (j < times) {
                    counter.increment();
                    //如果每次都取值,longAdder效率不如atomicLong高
//                    counter.longValue();
                    j++;
                }

            });
            list.add(t);
        }


        for (Thread thread : list) {
            thread.start();
        }

        for (Thread thread : list) {
            thread.join();
        }
        return counter.longValue();
    }


}

结果:

LongAdder  线程数: 1 结果: 10000000 耗时: 125 ms
AtomicLong 线程数: 1 结果: 10000000 耗时: 108 ms
-----------------------------------
LongAdder  线程数: 10 结果: 100000000 耗时: 137 ms
AtomicLong 线程数: 10 结果: 100000000 耗时: 2153 ms
-----------------------------------
LongAdder  线程数: 50 结果: 500000000 耗时: 338 ms
AtomicLong 线程数: 50 结果: 500000000 耗时: 10134 ms

可以发现, LongAdder的效率还是很高的

LongAdder

实现方式, 相比较AtomicLong, LongAdder 实现不仅仅使用了CAS, 而且也使用了 类似ConcurrentHashMap 分段锁一样的机制:

  • Base + Cell[] 数组
  • 如果没有线程竞争,result = Base
  • 如果存在线程竞争, result = Base+ Cells.value

在这里插入图片描述

标签:LongAdder,Thread,int,线程,AtomicLong,threadNum,比较
来源: https://blog.csdn.net/u013887008/article/details/116373968

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有