ConcurrentHashMap jdk 8使用TreeNodes而不是List ..为什么? [英] ConcurrentHashMap jdk 8 Uses TreeNodes instead of List .. Why?

查看:218
本文介绍了ConcurrentHashMap jdk 8使用TreeNodes而不是List ..为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我知道 ConcurrentHashMap before jdk 8 之前的工作方式。
我也理解代码,它是相当模块化,不是很难理解。



ConcurrentHashMap jdk 8的代码已经从以前的实现中改变了很多。
由于问题分类过宽,我会尝试非常具体。



CHMv8使用TreeBin 因此我的问题是什么是使用TreeBin在LinkedList的主要优点。



http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8-b132/java/util/concurrent/ConcurrentHashMap .java

解决方案

主要变化是添加 ConcurrentHashMap 新的Java 8默认 Map 方法的具体实现,具有依赖于内部细节的更好的并发实现。这些更改需要许多新的内部类,使.java文件膨胀。



例如,其中一些方法包括:



compute(K key,BiFunction remappingFunction)



forEach(BiConsumer action)



合并(K键,V值,BiFunction remappingFunction)



我想这也说明了为什么你通常应该关心如何一个类你不必保持工作的实现细节。只要类遵循它的javadoc中规定的合同,你应该不知道它是如何工作,因为实现细节可以改变在未来。


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 jdk 8 has changed a lot from its previous implementations. Because the question was classified as too broad i would try to be very specific.

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

So my question what is the main advantage of using TreeBin over LinkedList?.

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8-b132/java/util/concurrent/ConcurrentHashMap.java

解决方案

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.

这篇关于ConcurrentHashMap jdk 8使用TreeNodes而不是List ..为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆