WeakHashMap 是否有等效的 java.util.concurrent ?
是否可以使用 Collections.synchronizedMap()
重写以下代码,同时保持并发时的正确性?
Collections.synchronizedMap(new WeakHashMap<Class, Object>());
即,是否可以使用 java.util.concurrent 中的某些内容来代替?请注意,仅仅替换为
new ConcurrentHashMap<Class, Object>(new WeakHashMap<Class, Object>()));
显然是行不通的
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Guava 的 CacheBuilder 类可让您轻松完成此操作。
请注意,这会将键相等语义更改为
==
而不是.equals()
,这在您使用Class
实例的情况下并不重要,但是一个潜在的陷阱。Guava's CacheBuilder class allows you to do this easily.
Note that this changes key equality semantics to be
==
instead of.equals()
which will not matter in your case of usingClass
instances but is a potential pitfall.我不相信有。事实上,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."
如果您的类路径中已经有 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).
Cafeine 是 Guava 缓存的流行竞争对手。
用法:
Cafeine is a popular competitor of Guava cache.
usage:
如上所述,WeakHashMap 的文档位于 https:// /docs.oracle.com/javase/7/docs/api/java/util/WeakHashMap.html 具体说:
这对我来说意味着该技术必须与垃圾收集器的行为协同工作(除非文档有错误!)
As mentioned above, the documentation for WeakHashMap at https://docs.oracle.com/javase/7/docs/api/java/util/WeakHashMap.html specifically says:
Which implies to me that this technique must work in tandem with the garbage collector's behavior (unless the documentation is buggy!)
由于垃圾收集器可以随时直接修改弱引用,绕过同步映射包装器,因此将 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.
如果您使用的是 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 ofremove
, think carefully about concurrency and read the doc thoroughly.If you are using Java 6 or below. No, you have to synchronize a WeakHashMap.