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
显然不行
6 个解决方案
#1
34
Guava's CacheBuilder class allows you to do this easily.
Guava的CacheBuilder类允许您轻松完成此操作。
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实例的情况下无关紧要,但这是一个潜在的陷阱。
#2
19
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。”
#3
0
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.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!)
这对我来说意味着这种技术必须与垃圾收集器的行为协同工作(除非文档有问题!)
#4
0
Cafeine is a popular competitor of Guava cache.
Cafeine是Guava缓存的热门竞争对手。
- 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));
#5
0
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. No, you have to synchronize a WeakHashMap.
如果您使用的是Java 6或更低版本。不,您必须同步WeakHashMap。
#6
-1
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只能在单线程模型中运行。
#1
34
Guava's CacheBuilder class allows you to do this easily.
Guava的CacheBuilder类允许您轻松完成此操作。
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实例的情况下无关紧要,但这是一个潜在的陷阱。
#2
19
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。”
#3
0
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.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!)
这对我来说意味着这种技术必须与垃圾收集器的行为协同工作(除非文档有问题!)
#4
0
Cafeine is a popular competitor of Guava cache.
Cafeine是Guava缓存的热门竞争对手。
- 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));
#5
0
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. No, you have to synchronize a WeakHashMap.
如果您使用的是Java 6或更低版本。不,您必须同步WeakHashMap。
#6
-1
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只能在单线程模型中运行。