WeakHashMap 是否有 java.util.concurrent 等价物?

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

Is there java.util.concurrent equivalent for WeakHashMap?

javacollectionsguavajava.util.concurrentweakhashmap

提问by Nikita

Can the following piece of code be rewritten w/o using Collections.synchronizedMap()yet maintaining correctness at concurrency?

是否可以在不使用Collections.synchronizedMap()同时保持并发正确性的情况下重写以下代码?

Collections.synchronizedMap(new WeakHashMap<Class, Object>());

i.e. is there something from java.util.concurrent one can use instead? Note that merely replacing with

即是否有来自 java.util.concurrent 的东西可以替代?请注意,仅替换为

new ConcurrentHashMap<Class, Object>(new WeakHashMap<Class, Object>()));

obviously won't work

显然行不通

采纳答案by Steven Schlansker

Guava's CacheBuilderclass allows you to do this easily.

GuavaCacheBuilder类使您可以轻松地做到这一点。

CacheBuilder.newBuilder().weakKeys().build()

Note that this changes key equality semantics to be ==instead of .equals()which will not matter in your case of using Classinstances but is a potential pitfall.

请注意,这将键相等语义更改==.equals()which 在您使用Class实例的情况下无关紧要,但这是一个潜在的陷阱。

回答by objects

I don't believe there is. In fact the javadoc suggests using Collections.synchronizedMap()

我不相信有。事实上,javadoc 建议使用 Collections.synchronizedMap()

"Like most collection classes, this class is not synchronized. A synchronized WeakHashMap may be constructed using the Collections.synchronizedMap method."

“像大多数集合类一样,这个类不是同步的。可以使用 Collections.synchronizedMap 方法构造一个同步的 WeakHashMap。”

回答by Waldemar Wosiński

Cafeineis a popular competitor of Guava cache.

咖啡因是番石榴缓存的流行竞争对手。

- keys automatically wrapped in weak references
- values automatically wrapped in weak or soft references

usage:

用法:

LoadingCache<Key, Graph> graphs = Caffeine.newBuilder()
 .weakKeys()
 .weakValues()
 .build(key -> createExpensiveGraph(key));

回答by Scott Carey

If you are using Java 7 and above, this use case is solved in a thread-safe manner with ClassValuehttps://docs.oracle.com/javase/7/docs/api/java/lang/ClassValue.htmlIf you require the use of remove, think carefully about concurrency and read the doc thoroughly.

如果您使用的是 Java 7 及更高版本,则此用例将通过ClassValuehttps://docs.oracle.com/javase/7/docs/api/java/lang/ClassValue.html以线程安全的方式解决 如果您需要的使用remove,仔细考虑并发性并通读文档。

If you are using Java 6 or below. No, you have to synchronize a WeakHashMap.

如果您使用的是 Java 6 或更低版本。不,你必须同步一个 WeakHashMap。

回答by Slim Duiker

Does wrapping the WeakHashMap in a synchronized map still work correctly for what you want to do, since the garbage collector can modify the weakreferences directly at anytime, bypassing the synchronized map wrapper? I think WeakHashMap only truly works in a single threaded model.

将 WeakHashMap 包装在同步映射中是否仍然可以正常工作,因为垃圾收集器可以随时直接修改弱引用,绕过同步映射包装器?我认为 WeakHashMap 只在单线程模型中真正有效。

As mentioned above, the documentation for WeakHashMap at https://docs.oracle.com/javase/7/docs/api/java/util/WeakHashMap.htmlspecifically says:

如上所述,https: //docs.oracle.com/javase/7/docs/api/java/util/WeakHashMap.html 上的 WeakHashMap 文档特别指出:

"A synchronized WeakHashMap may be constructed using the Collections.synchronizedMap method"

“可以使用 Collections.synchronizedMap 方法构造同步的 WeakHashMap”

Which implies to me that this technique must work in tandem with the garbage collector's behavior (unless the documentation is buggy!)

这对我来说意味着这种技术必须与垃圾收集器的行为协同工作(除非文档有问题!)

回答by Gene

Does wrapping the WeakHashMap in a synchronized map still work correctly for what you want to do, since the garbage collector can modify the weakreferences directly at anytime, bypassing the synchronized map wrapper? I think WeakHashMap only truly works in a single threaded model.

将 WeakHashMap 包装在同步映射中是否仍然可以正常工作,因为垃圾收集器可以随时直接修改弱引用,绕过同步映射包装器?我认为 WeakHashMap 只在单线程模型中真正有效。