英文:
Why in ConcurrentHashMap does JDK 8 introduce CounterCell to solve the problem of counting?
问题
关于Java 8的问题。
为什么JDK 8引入CounterCell来解决计数问题,而不是简单地使用CAS来递增baseCount,或者直接使用AtomicInteger?
理解这一点真的让我感到沮丧... 我无法弄清楚关于它的许多if-else块,以及它所表示的意义或处理方式。你能否给我一些建议,帮助我阅读和理解它?
非常感谢。
英文:
The question is about java8.
Why does JDK 8 introduce CounterCell to solve the problem of counting rather than using CAS to increment baseCount simply or using AtomicInteter directly?
And to understand it really makes me feel depressed.... i can't figure out many if-else block about what it means or handles. could you please give me some advice of reading and understanding it?
Thanks a lot.
答案1
得分: 2
因为在有竞争的环境中,LongAdder
(CounterCell
基于它) - 更快。一般来说,如果您想要理解为什么它更快 - 您需要了解AtomicInteger
和LongAdder
的操作。而且,当单个CAS
(比较并交换)操作失败时,它们处理事情的方式也不同。
英文:
Because in contended environments a LongAdder
(that CounterCell
is based on) - is faster. In general, if you want to understand how that is faster - you need to understand what AtomicInteger
and what LongAdder
do. And they handle things differently when a single CAS
fails.
专注分享java语言的经验与见解,让所有开发者获益!
评论