LongAdder/LongAccumulator/AtomicLong/AtomicInteger性能测试

1.定义各类型自增方法

static class ClickNumber {
        // synchronized方法
        int number = 0;
        public synchronized void add_synchronized() {
            number++;
        }

        // atomicInteger
        AtomicInteger atomicInteger = new AtomicInteger();
        public void add_atomicInteger() {
            atomicInteger.incrementAndGet();
        }

        // atomicLong
        AtomicLong atomicLong = new AtomicLong();
        public void add_atomicLong() {
            atomicLong.incrementAndGet();
        }

        // longAccumulator
        LongAccumulator longAccumulator = new LongAccumulator(Long::sum, 0);
        public void add_longAccumulator() {
            longAccumulator.accumulate(1);
        }

        // longAdder
        LongAdder longAdder = new LongAdder();
        public void add_longAdder() {
            longAdder.increment();
        }
    }

2.编写测试方法

开启50个线程,每个线程执行100 * 10000次

/**
 * 线程数量
 */
private static final int SIZE_THREAD = 50;

/**
 * 循环次数
 */
private static final int COUNT = 10000;


 @Test
 public void test() throws InterruptedException {
        ClickNumber clickNumber = new ClickNumber();
        long startTime;
        long endTime;

        // 声明5个CountDownLatch对象,并且由主线程创建了50个线程,分别执行任务
        CountDownLatch countDownLatch1 = new CountDownLatch(SIZE_THREAD);
        CountDownLatch countDownLatch2 = new CountDownLatch(SIZE_THREAD);
        CountDownLatch countDownLatch3 = new CountDownLatch(SIZE_THREAD);
        CountDownLatch countDownLatch4 = new CountDownLatch(SIZE_THREAD);
        CountDownLatch countDownLatch5 = new CountDownLatch(SIZE_THREAD);


        /*****************************************************************************************************************/
        

        startTime = System.currentTimeMillis();
        for (int i = 1; i <= SIZE_THREAD; i++) {
            new Thread(() -> {
                try {
                    for (int j = 1; j <= 100 * COUNT; j++) {
                        clickNumber.add_synchronized();
                    }
                } catch (Exception e) {

                } finally {
                    countDownLatch1.countDown();
                }
            }, String.valueOf(i)).start();
        }

        // 主线程将在此处等待创建的50个线程执行完任务之后才继续往下执行
        countDownLatch1.await();
        endTime = System.currentTimeMillis();
        log.info("方法:add_synchronized --- 耗时:{} 毫秒,计数为:{}", endTime - startTime, clickNumber.number);

        
        /*****************************************************************************************************************/
        
        
        startTime = System.currentTimeMillis();
        for (int i = 1; i <= SIZE_THREAD; i++) {
            new Thread(() -> {
                try {
                    for (int j = 1; j <= 100 * COUNT; j++) {
                        clickNumber.add_atomicInteger();
                    }
                } catch (Exception e) {

                } finally {
                    countDownLatch2.countDown();
                }
            }, String.valueOf(i)).start();
        }

        // 主线程将在此处等待创建的50个线程执行完任务之后才继续往下执行
        countDownLatch2.await();
        endTime = System.currentTimeMillis();
        log.info("方法:add_atomicInteger --- 耗时:{} 毫秒,计数为:{}", endTime - startTime, clickNumber.atomicInteger.get());
        

        /*****************************************************************************************************************/
        

        startTime = System.currentTimeMillis();
        for (int i = 1; i <= SIZE_THREAD; i++) {
            new Thread(() -> {
                try {
                    for (int j = 1; j <= 100 * COUNT; j++) {
                        clickNumber.add_atomicLong();
                    }
                } catch (Exception e) {

                } finally {
                    countDownLatch3.countDown();
                }
            }, String.valueOf(i)).start();
        }

        // 主线程将在此处等待创建的50个线程执行完任务之后才继续往下执行
        countDownLatch3.await();
        endTime = System.currentTimeMillis();
        log.info("方法:add_atomicLong --- 耗时:{} 毫秒,计数为:{}", endTime - startTime, clickNumber.atomicLong.get());


        /*****************************************************************************************************************/
        

        startTime = System.currentTimeMillis();
        for (int i = 1; i <= SIZE_THREAD; i++) {
            new Thread(() -> {
                try {
                    for (int j = 1; j <= 100 * COUNT; j++) {
                        clickNumber.add_longAccumulator();
                    }
                } catch (Exception e) {

                } finally {
                    countDownLatch4.countDown();
                }
            }, String.valueOf(i)).start();
        }

        // 主线程将在此处等待创建的50个线程执行完任务之后才继续往下执行
        countDownLatch4.await();
        endTime = System.currentTimeMillis();
        log.info("方法:add_longAccumulator --- 耗时:{} 毫秒,计数为:{}", endTime - startTime, clickNumber.longAccumulator.longValue());


        /*****************************************************************************************************************/
        

        startTime = System.currentTimeMillis();
        for (int i = 1; i <= SIZE_THREAD; i++) {
            new Thread(() -> {
                try {
                    for (int j = 1; j <= 100 * COUNT; j++) {
                        clickNumber.add_longAdder();
                    }
                } catch (Exception e) {

                } finally {
                    countDownLatch5.countDown();
                }
            }, String.valueOf(i)).start();
        }

        // 主线程将在此处等待创建的50个线程执行完任务之后才继续往下执行
        countDownLatch5.await();
        endTime = System.currentTimeMillis();
        log.info("方法:add_longAdder --- 耗时:{} 毫秒,计数为:{}", endTime - startTime, clickNumber.longAdder.longValue());
    }

3.对比测试结果

由此可见,LongAdder牛逼。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值