java 具有弱键和身份哈希的 ConcurrentHashMap?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15456742/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:39:58  来源:igfitidea点击:

ConcurrentHashMap with weak keys and identity hash?

javaconcurrencyweak-referencesconcurrenthashmap

提问by r.v

How do I get a ConcurrentHashMapwith weak keys and identity hashes in Java? I think Google Guava Collections can give such a thing, but can I get it from the standard library? What other options do I have?

如何ConcurrentHashMap在 Java 中使用弱密钥和身份散列?我认为 Google Guava Collections 可以提供这样的东西,但是我可以从标准库中获取吗?我还有什么其他选择?

采纳答案by Stephen C

I think Google Guava Collections can give such a thing, but can I get it from the standard library?

我认为 Google Guava Collections 可以提供这样的东西,但是我可以从标准库中获取吗?

The short answer to that is No. Java SE does not implement this particular combination.

对此的简短回答是否定的。Java SE 没有实现这种特定的组合。

  • You could instantiate a java.util.concurrent.ConcurrentHashMapwith WeakReferencekeys, and do some extra work to implement removal of map entries for broken references, but that won't give you identity hash semantics.

  • You could instantiate a java.util.IdentityHashMapwith WeakReferencekeys, and do some extra work to implement removal of map entries for broken references, but that won't give you concurrent behaviour.

  • Using a java.util.WeakHashMapwon't give you either concurrency or identity hashing.

  • You could (in theory) wrap the key class in something that overrode the natural equalsand hashcodemethods. But that is most likely to be unusable.

  • I don't think it would be possible to do this by overriding methods in either ConcurrentHashMapor IdentityHashMap.

  • 您可以java.util.concurrent.ConcurrentHashMap使用WeakReference键实例化 a ,并做一些额外的工作来实现删除损坏引用的映射条目,但这不会为您提供身份哈希语义。

  • 您可以java.util.IdentityHashMap使用WeakReference键实例化 a ,并做一些额外的工作来实现删除损坏引用的映射条目,但这不会给您带来并发行为。

  • 使用 ajava.util.WeakHashMap不会为您提供并发性或身份散列。

  • 您可以(理论上)将关键类包装在超越自然equalshashcode方法的东西中。但这很可能无法使用。

  • 我认为不可能通过覆盖ConcurrentHashMap或 中的方法来做到这一点IdentityHashMap



Maybe the only viable option would be to changethe key classes equalsand hashcodemethods to be identity based. But that won't work for "built in" key types (especially finalones) or for cases where you need value-based equals/hashcode in other parts of the application.

也许是唯一可行的办法是改变的关键类equalshashcode方法基于身份是。但这不适用于“内置”键类型(尤其是键类型final)或在应用程序的其他部分需要基于值的 equals/hashcode 的情况。

回答by r.v

The Google Guava implementation appears the easiest way to go. One may initialize the required map with new MapMaker().weakKeys().makeMap()and use just as one would use java.util.concurrent.ConcurrentHashMap. See the apidocfor more details.

Google Guava 实现似乎是最简单的方法。可以new MapMaker().weakKeys().makeMap()像使用java.util.concurrent.ConcurrentHashMap. 有关更多详细信息,请参阅apidoc

回答by Shedom Wei

if your application is under spring framework ( version is gt 3.2 ), you can consider to use org.springframework.util.ConcurrentReferenceHashMap. Below is its description:

如果您的应用程序在 spring 框架下(版本为 gt 3.2),您可以考虑使用org.springframework.util.ConcurrentReferenceHashMap. 下面是它的描述:

A ConcurrentHashMap that uses soft or weak references for both keys and values. This class can be used as an alternative to Collections.synchronizedMap(new WeakHashMap>()) in order to support better performance when accessed concurrently. This implementation follows the same design constraints as ConcurrentHashMap with the exception that null values and null keys are supported.

NOTE: The use of references means that there is no guarantee that items placed into the map will be subsequently available. The garbage collector may discard references at any time, so it may appear that an unknown thread is silently removing entries.

If not explicitly specified, this implementation will use soft entry references.

对键和值使用软引用或弱引用的 ConcurrentHashMap。此类可用作 Collections.synchronizedMap(new WeakHashMap>()) 的替代,以便在并发访问时支持更好的性能。此实现遵循与 ConcurrentHashMap 相同的设计约束,但支持空值和空键。

注意:引用的使用意味着不能保证放置到地图中的项目随后可用。垃圾收集器可能会随时丢弃引用,因此可能会出现未知线程正在悄悄删除条目。

如果未明确指定,此实现将使用软条目引用。

回答by martian

search ConcurrentWeakIdentityHashMap, you will get many examples. I wrote an implement myself, for I think the hashCode of org/ehcache/core/internal/util/ConcurrentWeakIdentityHashMap$WeakReference is so bad.

搜索 ConcurrentWeakIdentityHashMap,你会得到很多例子。我自己写了一个实现,因为我认为 org/ehcache/core/internal/util/ConcurrentWeakIdentityHashMap$WeakReference 的 hashCode 太糟糕了。

Example of ehcache3

ehcache3 示例

Example I wrote

我写的例子

Pull Rquest to fix the ehcache3 ConcurrentWeakIdentityHashMap Key hashCode

拉请求修复 ehcache3 ConcurrentWeakIdentityHashMap Key hashCode