LongAdder/LongAccumulator/AtomicLong/AtomicInteger性能测试

该博客对比了不同并发计数方式的性能,包括synchronized、AtomicInteger、AtomicLong、LongAccumulator和LongAdder。通过开启50个线程,每个线程执行100万次操作,结果显示LongAdder在并发性能上表现最佳。

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次


                
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值