Java LongAdder:修订间差异

来自WHY42
无编辑摘要
(Riguz移动页面Java:LongAdderJava LongAdder,不留重定向)
 
(没有差异)

2023年12月19日 (二) 06:54的最新版本

LongAdder跟AtomicLong功能类似,但是在高竞争的场景下表现更优。例如结合ConcurrentHashMap来维护一个频率列表:

ConcurrentHashMap<String,LongAdder> freqs; //...
freqs.computeIfAbsent(key, k -> new LongAdder()).increment();}

The implementation extends Striped64, which is a data holder for 64-bit values. The values are held in cells, which are padded (or striped), hence the name. Each operation made upon the LongAdder will modify the collection of values present in the Striped64. When contention occurs, a new cell is created and modified, so the the old thread can finish concurrently with contending one. When you need the final value, the sums of each cell is simply added up.