简体   繁体   English

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

[英]Is there java.util.concurrent equivalent for WeakHashMap?

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>());

ie is there something from java.util.concurrent one can use instead? 即是否可以使用java.util.concurrent中的东西? Note that merely replacing with 请注意,仅替换为

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

obviously won't work 显然不行

Guava 's CacheBuilder class 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 Class instances but is a potential pitfall. 请注意,这会将键等式语义更改为==而不是.equals() ,这在您使用Class实例的情况下无关紧要,但这是一个潜在的陷阱。

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。”

Cafeine is a popular competitor of Guava cache. Cafeine是番石榴缓存的热门竞争对手。

- 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));

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? 将WeakHashMap包装在同步映射中是否仍能正常运行您想要执行的操作,因为垃圾收集器可以随时直接修改弱引用,绕过同步映射包装器? I think WeakHashMap only truly works in a single threaded model. 我认为WeakHashMap只能在单线程模型中运行。

As mentioned above, the documentation for WeakHashMap at https://docs.oracle.com/javase/7/docs/api/java/util/WeakHashMap.html specifically says: 如上所述,WeakHashMap的文档位于https://docs.oracle.com/javase/7/docs/api/java/util/WeakHashMap.html,具体说:

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

If you are using Java 7 and above, this use case is solved in a thread-safe manner with ClassValue https://docs.oracle.com/javase/7/docs/api/java/lang/ClassValue.html If you require the use of remove , think carefully about concurrency and read the doc thoroughly. 如果您使用的是Java 7及更高版本,则使用ClassValue以线程安全的方式解决此用例https://docs.oracle.com/javase/7/docs/api/java/lang/ClassValue.html如果您需要使用remove ,仔细考虑并发性并彻底阅读doc。

If you are using Java 6 or below. 如果您使用的是Java 6或更低版本。 No, you have to synchronize a WeakHashMap. 不,您必须同步WeakHashMap。

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? 将WeakHashMap包装在同步映射中是否仍能正常运行您想要执行的操作,因为垃圾收集器可以随时直接修改弱引用,绕过同步映射包装器? I think WeakHashMap only truly works in a single threaded model. 我认为WeakHashMap只能在单线程模型中运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM