在ConcurrentHashMap中,为什么JDK 8引入CounterCell来解决计数问题?

huangapple go评论78阅读模式
英文:

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

因为在有竞争的环境中,LongAdderCounterCell基于它) - 更快。一般来说,如果您想要理解为什么它更快 - 您需要了解AtomicIntegerLongAdder的操作。而且,当单个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.

huangapple
  • 本文由 发表于 2020年4月3日 21:47:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/61013370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定