WeakHashMap 是否有等效的 java.util.concurrent ?

发布于 2024-08-22 03:46:33 字数 356 浏览 7 评论 0 原文

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

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

即,是否可以使用 java.util.concurrent 中的某些内容来代替?请注意,仅仅替换为

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

显然是行不通的

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

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

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

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

obviously won't work

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

遮云壑 2024-08-29 03:46:33

GuavaCacheBuilder 类可让您轻松完成此操作。

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

请注意,这会将键相等语义更改为 == 而不是 .equals() ,这在您使用 Class 实例的情况下并不重要,但是一个潜在的陷阱。

Guava's CacheBuilder class allows you to do this easily.

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.

过气美图社 2024-08-29 03:46:33

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

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

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

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

寄离 2024-08-29 03:46:33

如果您的类路径中已经有 Spring Framework,那么一个选项是 ConcurrentReferenceHashMap :

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/util/ConcurrentReferenceHashMap。 html

您可以选择使用 引用(对于键和值)。

If you happen to have the Spring Framework in your classpath already, then one option is ConcurrentReferenceHashMap:

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/util/ConcurrentReferenceHashMap.html

You can choose between using weak or soft references (for both the keys and values).

恋竹姑娘 2024-08-29 03:46:33

Cafeine 是 Guava 缓存的流行竞争对手。

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

用法:

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

Cafeine is 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));
薄荷港 2024-08-29 03:46:33

将 WeakHashMap 包装在同步映射中是否仍然有效
正确地完成你想做的事情,因为垃圾收集器可以
随时直接修改弱引用,绕过
同步地图包装器?我认为 WeakHashMap 只真正适用于
单线程模型。

如上所述,WeakHashMap 的文档位于 https:// /docs.oracle.com/javase/7/docs/api/java/util/WeakHashMap.html 具体说:

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

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

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.

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

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

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

晚风撩人 2024-08-29 03:46:33

由于垃圾收集器可以随时直接修改弱引用,绕过同步映射包装器,因此将 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? I think WeakHashMap only truly works in a single threaded model.

软糖 2024-08-29 03:46:33

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

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

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文