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?
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)
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