ConcurrentHashMap jdk 8 Uses TreeNodes instead of List .. Why? [closed]

半城伤御伤魂 提交于 2019-12-18 04:27:08

问题


Hi i know the workings of ConcurrentHashMap before JDK 8. I also understood the code: it was pretty modular and not very hard to understand.

The code of ConcurrentHashMap in JDK 8 has changed a lot from its previous implementations.

Because this question was classified as too broad I will now try to be very specific.

CHMv8 uses a TreeBin (a variant of RedBlackTree) for buckets rather than a linked list.

So my question what is the main advantage of using TreeBin over a linked list?

Source code here


回答1:


The main changes are to add ConcurrentHashMap specific implementations of the new Java 8 default Map methods with better concurrent implementations that rely on the internal details. These changes required lots of new inner classes which bloat the .java file

For example, some of these methods include:

compute(K key, BiFunction remappingFunction)

forEach(BiConsumer action)

merge(K key, V value, BiFunction remappingFunction)

Just to name a few.

I think this also shows why you usually shouldn't care about implementation details on how a class you don't have to maintain works. As long as the class follows the contract laid out in its javadoc, you should be agnostic of how it works since the implementation details can change in the future.



来源:https://stackoverflow.com/questions/24872732/concurrenthashmap-jdk-8-uses-treenodes-instead-of-list-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!