java 为什么 jdk 中没有 ConcurrentLinkedHashMap 类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12300115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why there is no ConcurrentLinkedHashMap class in jdk?
提问by Geek
This question follows directly from my previous question here in SO. I think the answer to my second question is no . So I would like understand why there is no ConcurrentLinkedHashMap in java.util.concurrent package ? I mean there is a ConcurrentHashMap but no ConcurrentLinkedHashMap . Does it not make any sense at all to have such a class in Concurrent environments ? I mean what is the main technical reason here for its non availabalility ? Is there something similar in Guava/ Apache Commons ?
这个问题直接来自我在 SO 中的上一个问题。我认为我的第二个问题的答案是否定的。所以我想了解为什么 java.util.concurrent 包中没有 ConcurrentLinkedHashMap ?我的意思是有一个 ConcurrentHashMap 但没有 ConcurrentLinkedHashMap 。在并发环境中拥有这样的类是否毫无意义?我的意思是这里不可用的主要技术原因是什么?Guava/Apache Commons 中有类似的东西吗?
采纳答案by Stephen C
Why there is no ConcurrentLinkedHashMap class in jdk?
为什么 jdk 中没有 ConcurrentLinkedHashMap 类?
You would need to ask the Oracle Java guys that, but I imagine that it is a combination of:
您需要询问 Oracle Java 人员,但我想它是以下各项的组合:
- a perception that not many people would need it, and
- the inherent difficulties in implementing data structures with good performance properties in highly concurrent use cases.
- 认为没有多少人需要它,以及
- 在高度并发的用例中实现具有良好性能属性的数据结构的固有困难。
In this case, it seems to me that implementing the collection class so that iterating the key/value/entry sets is not a concurrency bottleneck would be ... um ... difficult. (And even if people havefigured a way to do it, the fact remains that designing and implementing and proving the correctness of general purpose highly concurrent data structures and algorithms is hard.)
在这种情况下,在我看来,实现集合类以便迭代键/值/条目集不是并发瓶颈将......嗯......困难。(即使人们已经找到了一种方法来做到这一点,事实仍然是设计和实现以及证明通用高并发数据结构和算法的正确性是困难的。)
回答by Arek
Looks like there is one from Google https://code.google.com/p/concurrentlinkedhashmap/
看起来谷歌有一个 https://code.google.com/p/concurrentlinkedhashmap/
Check also this post: What does it mean that ConcurrentLinkedHashMap has been integrated into Guava?
回答by UmNyobe
#define PERSONAL_OPINION
From a design point of view, it makes more sense to always have to use
从设计的角度来看,总是必须使用更有意义
Map m = Collections.synchronizedMap(new HashMap());
...
Set s = m.keySet(); // Needn't be in synchronized block
...
synchronized(m) { // Synchronizing on m, not s!
Iterator i = s.iterator(); // Must be in synchronized block
while (i.hasNext())
foo(i.next());
}
example in synchronizedMap
同步映射中的示例
Why? Because the synchronization mechanism is tied to a high abstraction (the Map
interface). But assuming I am right there can be two reasons to still have ConcurrentHashMap
:
为什么?因为同步机制与高度抽象(Map
接口)相关联。但假设我是对的,可能有两个原因仍然存在ConcurrentHashMap
:
- Either
ConcurrentHashMap
exists before this sync mechanism - Either there is a performance gain in creating a specific synchronized mechanism.
- 要么
ConcurrentHashMap
存在于此同步机制之前 - 创建特定的同步机制可以提高性能。
My point is in the ideal world of design even ConcurrentHashMap
should not exist.
我的观点是在理想的设计世界里甚至ConcurrentHashMap
不应该存在。
#end //personal opinion