Java LongAdder:修订间差异
建立內容為「 Category:Concurrency」的新頁面 |
小 Riguz移动页面Java:LongAdder至Java LongAdder,不留重定向 |
||
(未显示同一用户的2个中间版本) | |||
第1行: | 第1行: | ||
LongAdder跟AtomicLong功能类似,但是在高竞争的场景下表现更优。例如结合ConcurrentHashMap来维护一个频率列表: | |||
<syntaxhighlight lang="java"> | |||
ConcurrentHashMap<String,LongAdder> freqs; //... | |||
freqs.computeIfAbsent(key, k -> new LongAdder()).increment();} | |||
</syntaxhighlight> | |||
<q> | |||
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. | |||
</q> | |||
[[Category:Concurrency]] | [[Category:Concurrency]] |
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.