各种原子类累加效率对比:强锁<AtomicInteger<LongAdder&LongAccumulator

结论:500个线程做十万次累加,synchronized+volatile操作效率差不多是AtomicInteger的一半,LongAdder和LongAccumulate差不多少是AtomicInteger9分之一,LongAdder所以被阿里java规范推荐使用,NB!

结果展示:

 完整可运行代码:

import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.concurrent.atomic.LongAdder;


@Data
@NoArgsConstructor
@Setter
@Getter
class AcumulateEfficiencyComparsion{
   private volatile  int i=0;

    public void atomicIntegerEfficiency( AtomicInteger atomicInteger){
        atomicInteger.getAndIncrement();
    }

    public synchronized void synIncreament(){
        ++i;
    }

    public  void longAdderTets(LongAdder longAdder){
        longAdder.increment();
    }

    public  void longAccumulatorTest( LongAccumulator accumulator){
      accumulator.accumulate(1);
    }

}

public class AccumulateTest {

    public static void main(String[] args) throws InterruptedException {
        int SIZE_=500;
        Long start;
        Long end;
        AcumulateEfficiencyComparsion aec=new AcumulateEfficiencyComparsion();

        start=System.currentTimeMillis();
        CountDownLatch countDownLatch1=new CountDownLatch(SIZE_);
        for (int i = 0; i < SIZE_; i++) {
            new Thread(()->{
                for (int j = 0; j <100000 ; j++) {
                    aec.synIncreament();
                }
                countDownLatch1.countDown();
            }).start();
        }
        countDownLatch1.await();
        end=System.currentTimeMillis();
        System.out.println("synchronized+volatile强锁:" +(end-start)+"----->"+aec.getI());



        start=System.currentTimeMillis();
        CountDownLatch countDownLatch=new CountDownLatch(SIZE_);
        AtomicInteger atomicInteger=new AtomicInteger(0);
        for (int i = 0; i < SIZE_; i++) {
            new Thread(()->{
                for (int j = 0; j <100000 ; j++) {
                    aec.atomicIntegerEfficiency(atomicInteger);
                }
                countDownLatch.countDown();
            }).start();
        }
        countDownLatch.await();
        end=System.currentTimeMillis();
        System.out.println("AtomicInteger:" +(end-start)+"----->"+atomicInteger.get());


        start=System.currentTimeMillis();
        CountDownLatch countDownLatch2=new CountDownLatch(SIZE_);
        LongAdder longAdder=new LongAdder();
        for (int i = 0; i < SIZE_; i++) {
            new Thread(()->{
                for (int j = 0; j <100000 ; j++) {
                    aec.longAdderTets(longAdder);
                }
                countDownLatch2.countDown();
            }).start();
        }
        countDownLatch2.await();
        end=System.currentTimeMillis();
        System.out.println("LongAdder:" +(end-start)+"----->"+longAdder.sum());


        start=System.currentTimeMillis();
        CountDownLatch countDownLatch3=new CountDownLatch(SIZE_);
        LongAccumulator accumulator=new LongAccumulator((l,f)->l+f,0);
        for (int i = 0; i < SIZE_; i++) {
            new Thread(()->{
                for (int j = 0; j <100000 ; j++) {
                    aec.longAccumulatorTest(accumulator);
                }
                countDownLatch3.countDown();
            }).start();
        }
        countDownLatch3.await();
        end=System.currentTimeMillis();
        System.out.println("LongAccumulator:" +(end-start)+"----->"+accumulator.get());
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值