gpt4 book ai didi

multithreading - ConcurrentHashMap 与 ConcurrentSkipListMap 说明

转载 作者:行者123 更新时间:2023-12-03 12:43:40 26 4
gpt4 key购买 nike

我想根据 API 文档澄清一些关于 ConcurrentHashMap 与 ConcurrentSkipListMap 的内容。

根据我的理解 ConcurrentHashMap 保证了多线程插入的线程安全性。因此,如果您有一个只能由多个线程同时填充的 map ,那么就没有问题。然而,API 继续建议它不保证锁定以进行检索,因此您可能会在这里得到误导性的结果?

相反,对于 ConcurrentSkipListMap,它声明:“插入、删除、更新和访问操作由多个线程安全地并发执行”。所以我认为这没有哈希映射所具有的上述检索问题,但显然这通常会带来性能成本?

在实践中,是否有人因为这种特殊行为而发现需要使用 ConcurrentSkipListMap,或者检索可能会给出过时的 View 通常无关紧要?

最佳答案

ConcurrentHashMap

Retrievals reflect the results of the most recently completed update operations holding upon their onset. For aggregate operations such as putAll and clear, concurrent retrievals may reflect insertion or removal of only some entries.



它对 get(key) 使用可变语义.如果 Thread1 调用 put(key1, value1)紧接着 Thread2 调用 get(key1) , Thread2 不会等待 Thread1 完成它的 put ,它们彼此不同步,Thread2 可以获得旧的关联值。但如果 put(key1, value1)在 Thread2 尝试 get(key1) 之前在 Thread1 中完成然后 Thread2 保证获得此更新( value1 )。

ConcurrentSkipListMap排序并提供

expected average log(n) time cost for the containsKey, get, put and remove operations and their variants


ConcurrentSkipListMap不是那么快,但是当您需要排序的线程安全映射时很有用。

关于multithreading - ConcurrentHashMap 与 ConcurrentSkipListMap 说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34946898/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com