十二、高并发统计为什么用LongAdder
LongAdder,在无竞争的情况下跟AtomicLong一样,对同一个base进行操作。当出现竞争关系时则是采用化整为零的做法,用空间换时间,用一个数组cells,将一个value拆分进这个数组cells。多个线程需要同时对value进行操作时候,可以对线程id进行hash得到hash值,再根据hash值映射到这个数组cells的某个下标,再对该下标所对应的值进行自增操作。当所有线程操作完毕,将数组cells的所有值和无竞争值base都加起来作为最终结果。

源码解读:判断cell的新建和扩容


cell的创建、赋值、扩容流程

AtomicLong、LongAdder分析比较
AtomicLong
原理:CAS+自旋,incrementAndGet。
场景:低并发下的全局计算。AtomicLong能保证并发情况下计数的准确性,其内部通过CAS来解决并发安全性的问题。
缺陷:高并发后性能急剧下降,AtomicLong的自旋会成为瓶颈。
LongAdder
原理:CAS+Base+Cell数组分散,空间换时间并分散了热点数据。
场景:高并发下的全局计算。
缺陷:sum求和后还有计算线程修改结果的话,最后结果不够准确,但最终一致性。
public class Test2 { // 总线程数 public static final int SIZE_THREAD = 50; // 每个线程执行次数 public static final int _100W = 1000000; public static void main(String[] args) throws InterruptedException { ClickNumber clickNumber = new ClickNumber(); long startTime; long endTime; CountDownLatch countDownLatch1 = new CountDownLatch(SIZE_THREAD); CountDownLatch countDownLatch2 = new CountDownLatch(SIZE_THREAD); CountDownLatch countDownLatch3 = new CountDownLatch(SIZE_THREAD); // 使用add_synchronized startTime = System.currentTimeMillis(); for (int i = 1; i <= SIZE_THREAD; i++) { new Thread(() -> { try { for (int j = 1; j <= _100W; j++) { clickNumber.add_synchronized(); } } catch (Exception e) { e.printStackTrace(); } finally { countDownLatch1.countDown(); } }, String.valueOf(i)).start(); } countDownLatch1.await(); endTime = System.currentTimeMillis(); System.out.println("————————CostTime:" + (endTime - startTime) + " 毫秒" + "\t add_synchronized" + "\t" + clickNumber.number); // 使用add_atomicInteger startTime = System.currentTimeMillis(); for (int i = 1; i <= SIZE_THREAD; i++) { new Thread(() -> { try { for (int j = 1; j <= _100W; j++) { clickNumber.add_atomicInteger(); } } catch (Exception e) { e.printStackTrace(); } finally { countDownLatch2.countDown(); } }, String.valueOf(i)).start(); } countDownLatch2.await(); endTime = System.currentTimeMillis(); System.out.println("————————CostTime:" + (endTime - startTime) + " 毫秒" + "\t add_atomicInteger" + "\t" + clickNumber.number); // 使用add_longAdder startTime = System.currentTimeMillis(); for (int i = 1; i <= SIZE_THREAD; i++) { new Thread(() -> { try { for (int j = 1; j <= _100W; j++) { clickNumber.add_longAdder(); } } catch (Exception e) { e.printStackTrace(); } finally { countDownLatch3.countDown(); } }, String.valueOf(i)).start(); } countDownLatch3.await(); endTime = System.currentTimeMillis(); System.out.println("————————CostTime:" + (endTime - startTime) + " 毫秒" + "\t add_longAdder" + "\t" + clickNumber.number); } } class ClickNumber { // 计数 public int number = 0; // synchronized public synchronized void add_synchronized() { number++; } // Atomic AtomicInteger atomicInteger = new AtomicInteger(); public void add_atomicInteger() { atomicInteger.incrementAndGet(); } // LongAdder LongAdder longAdder = new LongAdder(); public void add_longAdder() { longAdder.increment(); } }
————————CostTime:1320 毫秒 add_synchronized 50000000
————————CostTime:821 毫秒 add_atomicInteger 50000000
————————CostTime:135 毫秒 add_longAdder 50000000

浙公网安备 33010602011771号