ConcurrentHashMap,在JDK8中改进的并发功能 [英] ConcurrentHashMap, which concurrent features improved in JDK8

查看:179
本文介绍了ConcurrentHashMap,在JDK8中改进的并发功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何并发专家可以在ConcurrentHashMap中解释,哪些并发功能比以前的JDK更好。

Can any concurrent expert explain in ConcurrentHashMap, which concurrent features improved comparing with which in previous JDKs

推荐答案

code> ConcurrentHashMap 已被完全重写。在Java 8之前,每个 ConcurrentHashMap 有一个并发级别,在构建时已经固定。出于兼容性原因,仍然存在构造函数接受这样的级别,虽然不是以原来的方式使用它。地图被分割成许多段,因为它的并发级别,每个都有自己的锁,因此理论上,如果它们都发生在目标不同的并发更新,可能会有并发更新

Well, the ConcurrentHashMap has been entirely rewritten. Before Java 8, each ConcurrentHashMap had a "concurrency level" which was fixed at construction time. For compatibility reasons, there is still a constructor accepting such a level though not using it in the original way. The map was split into as many segments, as its concurrency level, each of them having its own lock, so in theory, there could be up to concurrency level concurrent updates, if they all happened to target different segments, which depends on the hashing.

在Java 8中,每个哈希桶可以单独更新,因此只要没有哈希冲突,就可以有多少并发更新作为其当前容量。这符合新功能,例如 compute 保证原子更新的方法,因此,锁定至少更新的哈希桶。在最好的情况下,他们只锁定那个单一的桶。

In Java 8, each hash bucket can get updated individually, so as long as there are no hash collisions, there can be as many concurrent updates as its current capacity. This is in line with the new features like the compute methods which guaranty atomic updates, hence, locking of at least the hash bucket which gets updated. In the best case, they lock indeed only that single bucket.

此外, ConcurrentHashMap 改进应用于所有类型的哈希映射。当对于某个桶存在哈希冲突时,实现将使用该桶内的排序的映射样结构,从而降级到 O(log(n))复杂度比搜索桶时的旧实现的 O(n)复杂性。

Further, the ConcurrentHashMap benefits from the general hash improvements applied to all kind of hash maps. When there are hash collisions for a certain bucket, the implementation will resort to a sorted map like structure within that bucket, thus degrading to a O(log(n)) complexity rather than the O(n) complexity of the old implementation when searching the bucket.

这篇关于ConcurrentHashMap,在JDK8中改进的并发功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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