美文网首页
测试AtomicInteger,AtomicLong,LongA

测试AtomicInteger,AtomicLong,LongA

作者: 不知不怪 | 来源:发表于2023-02-05 15:45 被阅读0次
    package com.gzz.c01.e01_LongAdder;
    
    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.atomic.AtomicInteger;
    import java.util.concurrent.atomic.AtomicLong;
    import java.util.concurrent.atomic.LongAccumulator;
    import java.util.concurrent.atomic.LongAdder;
    
    import lombok.extern.slf4j.Slf4j;
    
    //参考资料 https://blog.csdn.net/weixin_43899792/article/details/124575032
    @Slf4j
    public class LongAdder_04 {
    
        static AtomicInteger atomicInteger = new AtomicInteger();
        static AtomicLong atomicLong = new AtomicLong();
        static LongAdder longAdder = new LongAdder();
        static LongAccumulator longAccumulator = new LongAccumulator((x, y) -> x + y, 0);
    
        static final int THREAD_COUNT = 50;//线程数
        static final int TIMES = 1000000;//累加次数
    
        static CountDownLatch latch_AtomicInteger = new CountDownLatch(THREAD_COUNT);
        static CountDownLatch latch_AtomicLong = new CountDownLatch(THREAD_COUNT);
        static CountDownLatch latch_LongAdder = new CountDownLatch(THREAD_COUNT);
        static CountDownLatch latch_LongAccumulator = new CountDownLatch(THREAD_COUNT);
    
        public static void main(String[] args) throws InterruptedException {
            count(() -> atomicInteger.incrementAndGet(), "AtomicInteger", latch_AtomicInteger, atomicInteger);
            count(() -> atomicLong.incrementAndGet(), "AtomicLong", latch_AtomicLong, atomicLong);
            count(() -> longAdder.increment(), "LongAdder", latch_LongAdder, longAdder);
            count(() -> longAccumulator.accumulate(1), "LongAccumulator", latch_LongAccumulator, longAccumulator);
        }
    
        static void count(Runnable runnable, String name, CountDownLatch latch, Number number) throws InterruptedException {
            long startTime = System.currentTimeMillis();
            for (int i = 1; i <= THREAD_COUNT; i++) {
                new Thread(() -> {
                    for (int j = 1; j <= TIMES; j++) {
                        runnable.run();
                    }
                    latch.countDown();
                }, String.valueOf(i)).start();
            }
            latch.await();
            long endTime = System.currentTimeMillis();
            log.info("{}:花费时间:{},数值为:{}.", name, endTime - startTime, number.longValue());
        }
    }
    
    2023-02-06 15:39:45,949[INFO]main[LongAdder_04.java:47][main]:atomicInteger:花费时间:736,数值为:50000000.
    2023-02-06 15:39:46,725[INFO]main[LongAdder_04.java:47][main]:atomicLong:花费时间:773,数值为:50000000.
    2023-02-06 15:39:46,769[INFO]main[LongAdder_04.java:47][main]:longAdder:花费时间:44,数值为:50000000.
    2023-02-06 15:39:46,802[INFO]main[LongAdder_04.java:47][main]:LongAccumulator:花费时间:33,数值为:50000000.
    

    相关文章

      网友评论

          本文标题:测试AtomicInteger,AtomicLong,LongA

          本文链接:https://www.haomeiwen.com/subject/sbxzhdtx.html