AtomicLong(CAS)和LongAdder 并发时效率

java.util.concurrent下提供了很多多线程的类,可以满足我们在多线程/高并发下的各种业务
下面简单比较下AtomicLong和LongAdder 并发时效率
这两个类位于java.util.concurrent.atomic

上代码:

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;

/**
 * 创建时间: 2018/12/21 16:09
 * 描述:
 */
public class ListThreadTest {
    private static int j=1;

    public static void main(String[] args){
       //测试10次 每次开启100个线程,知道100个线程结束后计算耗时
        for(int i=0;i<10;i++){
            LongAdder longAdder=new LongAdder();
            longAdder.add(1L);
            CountDownLatch countDownLatch=new CountDownLatch(100);
            Long start1=System.currentTimeMillis();
            for(int j=0;j<100;j++){
                Thread thread=new Thread(new TestLongAdder(countDownLatch,longAdder));
                thread.start();
            }
            try {
                countDownLatch.await();
                System.out.println("LongAdder 耗时="+(System.currentTimeMillis()-start1));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("LongAdder结束,AtomicLong开始===================");
        for(int i=0;i<10;i++){
            AtomicLong atomicLong=new AtomicLong(1L);
            CountDownLatch countDownLatch=new CountDownLatch(100);
            Long start2=System.currentTimeMillis();
            for(int j=0;j<100;j++){
                Thread thread=new Thread(new TestAtomicLong(countDownLatch,atomicLong));
                thread.start();
            }
            try {
                countDownLatch.await();
                System.out.println("atomicLong 耗时="+(System.currentTimeMillis()-start2));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    static class  TestChildren implements Runnable{
       public CountDownLatch countDownLatch;
       private List list;
        public TestChildren(List list,CountDownLatch countDownLatch){
            this.countDownLatch=countDownLatch;
            this.list=list;
        }
        @Override
        public void run() {
            for(int i=0;i<1000;i++){
                list.add("1");
            }
            this.countDownLatch.countDown();
        }
    }

    static class TestAtomicLong implements Runnable{
        public CountDownLatch countDownLatch;
        private  AtomicLong atomicLong;

        public TestAtomicLong(CountDownLatch countDownLatch, AtomicLong atomicLong) {
            this.countDownLatch = countDownLatch;
            this.atomicLong = atomicLong;
        }
        @Override
        public void run() {
            for(int i=0;i<100000;i++){
                atomicLong.incrementAndGet();
            }
            this.countDownLatch.countDown();
        }
    }
    static class TestLongAdder implements Runnable{
        public CountDownLatch countDownLatch;
        private  LongAdder longAdder;

        public TestLongAdder(CountDownLatch countDownLatch, LongAdder longAdder) {
            this.countDownLatch = countDownLatch;
            this.longAdder = longAdder;
        }

        @Override
        public void run() {
            for(int i=0;i<100000;i++){
                longAdder.increment();
            }
            this.countDownLatch.countDown();
        }
    }
}

结果:

LongAdder 耗时=91
LongAdder 耗时=79
LongAdder 耗时=86
LongAdder 耗时=102
LongAdder 耗时=87
LongAdder 耗时=81
LongAdder 耗时=136
LongAdder 耗时=61
LongAdder 耗时=90
LongAdder 耗时=117
LongAdder结束,AtomicLong开始===================
atomicLong 耗时=170
atomicLong 耗时=184
atomicLong 耗时=139
atomicLong 耗时=133
atomicLong 耗时=146
atomicLong 耗时=196
atomicLong 耗时=292
atomicLong 耗时=279
atomicLong 耗时=219
atomicLong 耗时=213

执行过很多次 每次都是atomicLong耗时长,所以效率相对来讲低

 

觉得文章有帮助的话就赞赏下吧!

 

微信:

AtomicLong(CAS)和LongAdder 并发时效率_第1张图片

支付宝:

AtomicLong(CAS)和LongAdder 并发时效率_第2张图片

 

你可能感兴趣的:(线程)