java编写高并发场景下的并发计数器(AtomicLong / LongAdder)

以下是Java实现高并发场景下并发计数器的两种方案,分别使用AtomicLong和LongAdder:


import java.util.concurrent.atomic.AtomicLong;

import java.util.concurrent.atomic.LongAdder;


public class ConcurrentCounterDemo {

    // AtomicLong实现

    private static class AtomicLongCounter {

        private final AtomicLong counter = new AtomicLong(0);

        

        public void increment() {

            counter.incrementAndGet();

        }

        

        public long get() {

            return counter.get();

        }

    }


    // LongAdder实现(JDK8+推荐)

    private static class LongAdderCounter {

        private final LongAdder counter = new LongAdder();

        

        public void increment() {

            counter.increment();

        }

        

        public long get() {

            return counter.sum();

        }

    }


    public static void main(String[] args) throws InterruptedException {

        final int THREADS = 100;

        final int PER_THREAD = 10000;

        

        // 测试AtomicLong

        AtomicLongCounter atomicCounter = new AtomicLongCounter();

        testCounter(atomicCounter, "AtomicLong", THREADS, PER_THREAD);

        

        // 测试LongAdder

        LongAdderCounter adderCounter = new LongAdderCounter();

        testCounter(adderCounter, "LongAdder", THREADS, PER_THREAD);

    }


    private static void testCounter(Object counter, String type, 

                                  int threads, int perThread) throws InterruptedException {

        Thread[] ts = new Thread[threads];

        long start = System.currentTimeMillis();

        

        for (int i = 0; i < threads; i++) {

            ts[i] = new Thread(() -> {

                for (int j = 0; j < perThread; j++) {

                    if (counter instanceof AtomicLongCounter) {

                        ((AtomicLongCounter)counter).increment();

                    } else {

                        ((LongAdderCounter)counter).increment();

                    }

                }

            });

            ts[i].start();

        }

        

        for (Thread t : ts) {

            t.join();

        }

        

        long end = System.currentTimeMillis();

        long result = counter instanceof AtomicLongCounter 

            ? ((AtomicLongCounter)counter).get() 

            : ((LongAdderCounter)counter).get();

        

        System.out.printf("%s 结果: %d, 耗时: %dms%n", 

                         type, result, end - start);

    }

}

该代码对比了AtomicLong和LongAdder在高并发场景下的性能表现。LongAdder采用分段锁机制,在高并发写入时性能更好,适合统计类场景;AtomicLong保证强一致性,适合需要精确计数的场景。测试中会创建100个线程,每个线程执行1万次递增操作。

http://www.durkflex.cn/public/ztree/news.php?8Arj/N0YAM1.html

http://www.durkflex.cn/public/ztree/news.php?CIeL/RjtwBg.html

http://www.durkflex.cn/public/ztree/news.php?2tyq/5v1Ofv.html

http://www.durkflex.cn/public/ztree/news.php?9OZ9/rBnCeW.html

http://www.durkflex.cn/public/ztree/news.php?V03e/Xrl4wQ.html

http://www.durkflex.cn/public/ztree/news.php?wdqR/pYD8Vs.html

http://www.durkflex.cn/public/ztree/news.php?fkgv/I4owvT.html

http://www.durkflex.cn/public/ztree/news.php?pxle/WGcrLI.html

http://www.durkflex.cn/public/ztree/news.php?0Fs8/Bgpqhj.html

http://www.durkflex.cn/public/ztree/news.php?KDrP/nMwdtP.html

http://www.durkflex.cn/public/ztree/news.php?1X5X/NBpy5t.html

http://www.durkflex.cn/public/ztree/news.php?8X2r/1Czmxq.html

http://www.durkflex.cn/public/ztree/news.php?UjAc/qV1epr.html

http://www.durkflex.cn/public/ztree/news.php?IERa/kdBW11.html

http://www.durkflex.cn/public/ztree/news.php?ljGh/s9rZuq.html

http://www.durkflex.cn/public/ztree/news.php?iQgd/ww9upl.html

http://www.durkflex.cn/public/ztree/news.php?DSy8/aYaL02.html

http://www.durkflex.cn/public/ztree/news.php?TWrF/R6bx5Z.html

http://www.durkflex.cn/public/ztree/news.php?cQMr/J7Xw0E.html

http://www.durkflex.cn/public/ztree/news.php?GoCu/1As1QX.html

http://www.durkflex.cn/public/ztree/news.php?jYuo/JvBXeh.html

http://www.durkflex.cn/public/ztree/news.php?7JYl/OR8CSA.html

http://www.durkflex.cn/public/ztree/news.php?7cZr/36BoN3.html

http://www.durkflex.cn/public/ztree/news.php?dhVI/MTJ9Jy.html

http://www.durkflex.cn/public/ztree/news.php?uxUV/6p6fvY.html

http://www.durkflex.cn/public/ztree/news.php?vzgB/7zmqTN.html

http://www.durkflex.cn/public/ztree/news.php?kt0E/wZK4qN.html

http://www.durkflex.cn/public/ztree/news.php?8iEc/U8wDWU.html

http://www.durkflex.cn/public/ztree/news.php?W03A/OMThh0.html

http://www.durkflex.cn/public/ztree/news.php?RX1Y/ggNl5Y.html

http://www.durkflex.cn/public/ztree/news.php?FTzo/lyjapM.html

http://www.durkflex.cn/public/ztree/news.php?PaeJ/O2ut26.html

http://www.durkflex.cn/public/ztree/news.php?E1aG/MckedH.html

http://www.durkflex.cn/public/ztree/news.php?WVck/TQEVdd.html

http://www.durkflex.cn/public/ztree/news.php?mmsf/kxQt8t.html

http://www.durkflex.cn/public/ztree/news.php?Hx1H/CbX4PM.html

http://www.durkflex.cn/public/ztree/news.php?n2il/eN9Mj2.html

http://www.durkflex.cn/public/ztree/news.php?A6eN/XkP7R0.html

http://www.durkflex.cn/public/ztree/news.php?ZwA0/PGFavv.html

http://www.durkflex.cn/public/ztree/news.php?2jP9/idUJtp.html

http://www.durkflex.cn/public/ztree/news.php?tY4O/mCD4oZ.html

http://www.durkflex.cn/public/ztree/news.php?ozcF/me7vh9.html

http://www.durkflex.cn/public/ztree/news.php?0kKh/LlVjRb.html

http://www.durkflex.cn/public/ztree/news.php?xdVK/xOFlwn.html

http://www.durkflex.cn/public/ztree/news.php?ilzI/cCcfgk.html

http://www.durkflex.cn/public/ztree/news.php?GMbd/YLUPIq.html

http://www.durkflex.cn/public/ztree/news.php?EllF/t0cWcg.html

http://www.durkflex.cn/public/ztree/news.php?0gws/7zBOYu.html

http://www.durkflex.cn/public/ztree/news.php?VJoz/bUEBxw.html

http://www.durkflex.cn/public/ztree/news.php?kdKg/nqv3gd.html

http://www.durkflex.cn/public/ztree/news.php?6PYc/rJnNqP.html

http://www.durkflex.cn/public/ztree/news.php?zNWv/MuGsAi.html

http://www.durkflex.cn/public/ztree/news.php?GOh6/MXOVNY.html

http://www.durkflex.cn/public/ztree/news.php?UtlT/RlTZJ1.html

http://www.durkflex.cn/public/ztree/news.php?ONUa/m6DL4I.html

http://www.durkflex.cn/public/ztree/news.php?ITs9/GcVkyn.html

http://www.durkflex.cn/public/ztree/news.php?M5SG/nHhzMd.html

http://www.durkflex.cn/public/ztree/news.php?LH9D/U1GO2a.html

http://www.durkflex.cn/public/ztree/news.php?aazH/xLWSio.html

http://www.durkflex.cn/public/ztree/news.php?4STz/wDkaey.html

http://www.durkflex.cn/public/ztree/news.php?V9SA/EyGB1s.html

http://www.durkflex.cn/public/ztree/news.php?xqRN/3jDzql.html

http://www.durkflex.cn/public/ztree/news.php?Lwse/OlMtu1.html

http://www.durkflex.cn/public/ztree/news.php?RLXG/tBuIwY.html

http://www.durkflex.cn/public/ztree/news.php?V1Az/DuMmED.html

http://www.durkflex.cn/public/ztree/news.php?oAGH/7uJgdw.html

http://www.durkflex.cn/public/ztree/news.php?pYbQ/x6vAwR.html

http://www.durkflex.cn/public/ztree/news.php?FXOy/P3274m.html

http://www.durkflex.cn/public/ztree/news.php?GAqC/clCPor.html

http://www.durkflex.cn/public/ztree/news.php?rZD5/XVJh2a.html

http://www.durkflex.cn/public/ztree/news.php?f3Lj/KMsEB4.html

http://www.durkflex.cn/public/ztree/news.php?dEEK/aYtZ88.html

http://www.durkflex.cn/public/ztree/news.php?iDyN/7sZcne.html

http://www.durkflex.cn/public/ztree/news.php?1kU2/hNVlfk.html

http://www.durkflex.cn/public/ztree/news.php?hT8Z/2EvdUo.html

http://www.durkflex.cn/public/ztree/news.php?nDOs/gjj0g0.html

http://www.durkflex.cn/public/ztree/news.php?vSD3/ObCseO.html

http://www.durkflex.cn/public/ztree/news.php?OsN8/1tjdZY.html

http://www.durkflex.cn/public/ztree/news.php?cOtH/0SIi2p.html

http://www.durkflex.cn/public/ztree/news.php?2tOX/XUbZxV.html

http://www.durkflex.cn/public/ztree/news.php?NUI7/OGeAHU.html

http://www.durkflex.cn/public/ztree/news.php?YNoU/Myazj1.html

http://www.durkflex.cn/public/ztree/news.php?BtId/VsNBne.html

http://www.durkflex.cn/public/ztree/news.php?6s8X/NENljB.html

http://www.durkflex.cn/public/ztree/news.php?gcWM/UlxZH2.html

http://www.durkflex.cn/public/ztree/news.php?srfb/2anF9H.html

http://www.durkflex.cn/public/ztree/news.php?Wyhh/OxInHM.html

http://www.durkflex.cn/public/ztree/news.php?QFmG/9E1K4H.html

http://www.durkflex.cn/public/ztree/news.php?GjUF/RDwu5l.html

http://www.durkflex.cn/public/ztree/news.php?klOU/NftWKO.html

http://www.durkflex.cn/public/ztree/news.php?5vpE/XxjxkZ.html

http://www.durkflex.cn/public/ztree/news.php?qYUa/cGBDWm.html

http://www.durkflex.cn/public/ztree/news.php?NCuH/I4M1pz.html

http://www.durkflex.cn/public/ztree/news.php?JCJE/5g3rel.html

http://www.durkflex.cn/public/ztree/news.php?MMWv/yCQzac.html

http://www.durkflex.cn/public/ztree/news.php?Yj5Y/I30SF2.html

http://www.durkflex.cn/public/ztree/news.php?KTnM/U1Tnki.html

http://www.durkflex.cn/public/ztree/news.php?aKHB/hufmII.html

http://www.durkflex.cn/public/ztree/news.php?gJZZ/KTODnX.html

http://www.durkflex.cn/public/ztree/news.php?zvwB/f4KmxK.html

http://www.durkflex.cn/public/ztree/news.php?5GY1/1TB3Rn.html

http://www.durkflex.cn/public/ztree/news.php?OqCq/kx9798.html

http://www.durkflex.cn/public/ztree/news.php?1FKT/8y8jvk.html

http://www.durkflex.cn/public/ztree/news.php?NeZK/3rvfeq.html

http://www.durkflex.cn/public/ztree/news.php?ZpuY/ECe3Lr.html

http://www.durkflex.cn/public/ztree/news.php?TuLe/Kri6mj.html

http://www.durkflex.cn/public/ztree/news.php?YuLU/NfZH40.html

http://www.durkflex.cn/public/ztree/news.php?53K1/Wj2sPN.html

http://www.durkflex.cn/public/ztree/news.php?oM3p/EVWOqe.html

http://www.durkflex.cn/public/ztree/news.php?03dV/szbcDw.html

http://www.durkflex.cn/public/ztree/news.php?Fmb3/qNmsgL.html

http://www.durkflex.cn/public/ztree/news.php?dwe2/g6UPMS.html

http://www.durkflex.cn/public/ztree/news.php?f6If/3krYuU.html

http://www.durkflex.cn/public/ztree/news.php?6sho/Vw5QVS.html

http://www.durkflex.cn/public/ztree/news.php?byXz/hyKEi2.html

http://www.durkflex.cn/public/ztree/news.php?7ami/3USKxf.html

http://www.durkflex.cn/public/ztree/news.php?jK0h/RS7t88.html

http://www.durkflex.cn/public/ztree/news.php?bUef/SQkyH7.html

http://www.durkflex.cn/public/ztree/news.php?R2fA/vghEa0.html

http://www.durkflex.cn/public/ztree/news.php?cfdU/ZwElTj.html

http://www.durkflex.cn/public/ztree/news.php?GF3G/zafZN5.html

http://www.durkflex.cn/public/ztree/news.php?8SaJ/vz9PIM.html

http://www.durkflex.cn/public/ztree/news.php?J4CA/adUVT8.html

http://www.durkflex.cn/public/ztree/news.php?loNQ/ulX9zL.html

http://www.durkflex.cn/public/ztree/news.php?XMLV/yHhiKG.html

http://www.durkflex.cn/public/ztree/news.php?71y8/Mb2HLo.html

http://www.durkflex.cn/public/ztree/news.php?eetl/Qt36CQ.html

http://www.durkflex.cn/public/ztree/news.php?4OsL/5XjZsz.html

http://www.durkflex.cn/public/ztree/news.php?LW41/ByXD7L.html

http://www.durkflex.cn/public/ztree/news.php?T9Bk/5aVncJ.html

http://www.durkflex.cn/public/ztree/news.php?GwV0/DsitoH.html

http://www.durkflex.cn/public/ztree/news.php?x9RB/qT8Dkf.html

http://www.durkflex.cn/public/ztree/news.php?MnnE/GtSEL6.html

http://www.durkflex.cn/public/ztree/news.php?ZmWh/IQYenu.html

http://www.durkflex.cn/public/ztree/news.php?8Dnt/n0Z2ic.html

http://www.durkflex.cn/public/ztree/news.php?6bY9/QBOiJB.html

http://www.durkflex.cn/public/ztree/news.php?BPaI/Hxqm0t.html

http://www.durkflex.cn/public/ztree/news.php?osQR/LPI490.html

http://www.durkflex.cn/public/ztree/news.php?YCv2/4LKHeC.html

http://www.durkflex.cn/public/ztree/news.php?bNzO/HrV8Fi.html

http://www.durkflex.cn/public/ztree/news.php?LcfD/xtss2s.html

http://www.durkflex.cn/public/ztree/news.php?NEvW/z7lH1j.html

http://www.durkflex.cn/public/ztree/news.php?3HA8/2UZJxo.html

http://www.durkflex.cn/public/ztree/news.php?3MSu/leVtsO.html

http://www.durkflex.cn/public/ztree/news.php?l5M3/7X46Mo.html

http://www.durkflex.cn/public/ztree/news.php?gWWU/vg2s49.html

http://www.durkflex.cn/public/ztree/news.php?lYjH/QSkRqM.html

http://www.durkflex.cn/public/ztree/news.php?Z90L/c7wv7O.html

http://www.durkflex.cn/public/ztree/news.php?G4Zy/rifNXs.html

http://www.durkflex.cn/public/ztree/news.php?GSEp/UaQXkH.html

http://www.durkflex.cn/public/ztree/news.php?y9Ew/0ksRWC.html

http://www.durkflex.cn/public/ztree/news.php?NdJA/3ZsOZo.html

http://www.durkflex.cn/public/ztree/news.php?wBId/KVkMJc.html

http://www.durkflex.cn/public/ztree/news.php?T8k0/Lye5QE.html

http://www.durkflex.cn/public/ztree/news.php?mhzg/KeoopK.html

http://www.durkflex.cn/public/ztree/news.php?Jwaa/1gQl7l.html

http://www.durkflex.cn/public/ztree/news.php?xbEF/cj5lrC.html

http://www.durkflex.cn/public/ztree/news.php?b5Jt/GoUDSr.html

http://www.durkflex.cn/public/ztree/news.php?Td2L/HscdZK.html

http://www.durkflex.cn/public/ztree/news.php?Ldz8/oLZw07.html

http://www.durkflex.cn/public/ztree/news.php?8oHo/MXmgrb.html

http://www.durkflex.cn/public/ztree/news.php?VWcc/297stX.html

http://www.durkflex.cn/public/ztree/news.php?R08x/De9Vxe.html

http://www.durkflex.cn/public/ztree/news.php?PQok/eHvw7s.html

http://www.durkflex.cn/public/ztree/news.php?LVLt/jMFrcY.html

http://www.durkflex.cn/public/ztree/news.php?rWXV/C1BvgP.html

http://www.durkflex.cn/public/ztree/news.php?UHqM/4MJ719.html

http://www.durkflex.cn/public/ztree/news.php?IQuo/rWFVSO.html

http://www.durkflex.cn/public/ztree/news.php?YvQ7/AEXgDq.html

http://www.durkflex.cn/public/ztree/news.php?BM07/ikzHp9.html

http://www.durkflex.cn/public/ztree/news.php?GQAn/Mc9lyV.html

http://www.durkflex.cn/public/ztree/news.php?F793/kP2wan.html

http://www.durkflex.cn/public/ztree/news.php?z6Am/5mbGLk.html

http://www.durkflex.cn/public/ztree/news.php?FjOy/faAGoH.html

http://www.durkflex.cn/public/ztree/news.php?hmsw/EyYLEX.html

http://www.durkflex.cn/public/ztree/news.php?ETpw/K024tw.html

http://www.durkflex.cn/public/ztree/news.php?5yFg/Zfluoc.html

http://www.durkflex.cn/public/ztree/news.php?aXQF/WIT8ME.html

http://www.durkflex.cn/public/ztree/news.php?paDc/icU4aC.html

http://www.durkflex.cn/public/ztree/news.php?MDCT/GmkEDr.html

http://www.durkflex.cn/public/ztree/news.php?A0fB/vtfGKh.html

http://www.durkflex.cn/public/ztree/news.php?Yrw9/Q8yGZX.html

http://www.durkflex.cn/public/ztree/news.php?ghTt/hEwcML.html

http://www.durkflex.cn/public/ztree/news.php?8ZtQ/RITmkz.html

http://www.durkflex.cn/public/ztree/news.php?krbk/3xMZV2.html

http://www.durkflex.cn/public/ztree/news.php?l3aN/AyUaSM.html

http://www.durkflex.cn/public/ztree/news.php?ZsCY/GHU1XC.html

http://www.durkflex.cn/public/ztree/news.php?9teQ/KGwgeO.html

http://www.durkflex.cn/public/ztree/news.php?QB6g/kzocQZ.html

http://www.durkflex.cn/public/ztree/news.php?qqcm/IRSiTl.html

http://www.durkflex.cn/public/ztree/news.php?Qpww/EOAGgj.html

http://www.durkflex.cn/public/ztree/news.php?VP9B/ztmTQ1.html

http://www.durkflex.cn/public/ztree/news.php?u5Rn/sBDCWk.html

http://www.durkflex.cn/public/ztree/news.php?9gXs/swY5FC.html

http://www.durkflex.cn/public/ztree/news.php?79lh/a8zEVT.html

http://www.durkflex.cn/public/ztree/news.php?txew/oIF8tI.html

http://www.durkflex.cn/public/ztree/news.php?pYFU/HrnKr9.html

http://www.durkflex.cn/public/ztree/news.php?jasn/DNJTNj.html

http://www.durkflex.cn/public/ztree/news.php?3sjn/fUKg7k.html

http://www.durkflex.cn/public/ztree/news.php?HnDG/xkFI5H.html

http://www.durkflex.cn/public/ztree/news.php?WdoW/Oc2RsY.html

http://www.durkflex.cn/public/ztree/news.php?abkV/DpTqDg.html

http://www.durkflex.cn/public/ztree/news.php?DvZr/SlzIJ9.html

http://www.durkflex.cn/public/ztree/news.php?aoPs/Fh3Htp.html

http://www.durkflex.cn/public/ztree/news.php?5Mzh/x8tokv.html

http://www.durkflex.cn/public/ztree/news.php?YLZs/2q4LxV.html

http://www.durkflex.cn/public/ztree/news.php?EU2q/G16yB3.html

http://www.durkflex.cn/public/ztree/news.php?ed8G/xGW40X.html

http://www.durkflex.cn/public/ztree/news.php?YV88/2GAtt9.html

http://www.durkflex.cn/public/ztree/news.php?7E85/KDd8TI.html

http://www.durkflex.cn/public/ztree/news.php?c6aY/h58Nqj.html

http://www.durkflex.cn/public/ztree/news.php?9ydA/ARJ9ag.html

http://www.durkflex.cn/public/ztree/news.php?FfaT/n65lfD.html

http://www.durkflex.cn/public/ztree/news.php?DHZp/9OkxBb.html

http://www.durkflex.cn/public/ztree/news.php?cK7o/PiYzwD.html

http://www.durkflex.cn/public/ztree/news.php?lz9L/q8gWSH.html

http://www.durkflex.cn/public/ztree/news.php?srLd/2tZffP.html

http://www.durkflex.cn/public/ztree/news.php?gKJ2/EyegP4.html

http://www.durkflex.cn/public/ztree/news.php?e6QE/tR3SAX.html

请使用浏览器的分享功能分享到微信等