CHA*_*ist 5 java multithreading multimap concurrenthashmap guava
I am currently using Guava Multimap implementation.
map = Multimaps.synchronizedSetMultimap(HashMultimap.<K, R> create());
Run Code Online (Sandbox Code Playgroud)
However I found that my program performance is now bounded by the synchronization block synchronized (mutex) used in Multimaps.synchronizedSetMultimap.
Therefore I am finding if there are ANY alternatives to have a synchronized multimap, which hopefully can help improve program performance in multi-threading environment.
I don't mind if extra restrictions are imposed, like only one thread for updating (create, modify or removal), as long as I can read multimap data using multiple threads and at the same time, allow write operation. In addition, for my project usage, I would mostly read data (>99% if time) and seldom write data (< 1%), so I do not care too much on writing performance when compared to read performance).
From High-performance Concurrent MultiMap Java/Scala, someone suggest use of
Multimaps.newSetMultimap(new ConcurrentHashMap<>(), ConcurrentHashMap::newKeySet)
Run Code Online (Sandbox Code Playgroud)
and since I am using Java 7, I convert the above code as follows:
map = Multimaps.newSetMultimap(new ConcurrentHashMap<K, Collection<R>>(), new Supplier<Set<R>>() {
public Set<R> get() {
return Sets.newSetFromMap(new ConcurrentHashMap<R, Boolean>());
}
});
Run Code Online (Sandbox Code Playgroud)
which seems working really well. But again from the documentation, it states the following:
The multimap is not threadsafe when any concurrent operations update the multimap, even if map and the instances generated by factory are. Concurrent read operations will work correctly. To allow concurrent update operations, wrap the multimap with a call to synchronizedSetMultimap(com.google.common.collect.SetMultimap<K, V>).
However I have created a test code for concurrent read write, and it seems that it works (without any exceptions like ConcurrentModificationException). My current Java version is Java 7 and using Guava 14.0.1.
So my question is,
Multimaps.newSetMultimap(new ConcurrentHashMap<>(), ConcurrentHashMap::newKeySet) fails to work properly? Or it simply just works accidentally with ConcurrentHashMap?Many thanks.
你可以使用:
ConcurrentMap<K, CopyOnWriteArraySet<V>> multimap = new ConcurrentHashMap<>();
Run Code Online (Sandbox Code Playgroud)
您可以实施:
class ConcurrentArraySetMultimap<K, V> implements SetMultimap<K, V> {
Run Code Online (Sandbox Code Playgroud)
在它的上面。
如果 Javadoc 声明Multimaps.newSetMultimap()不是线程安全的,我不会试图证明它是错误的。Map我查看了实现,原因可能是附加逻辑在装饰的和之上运行Set。
| 归档时间: |
|
| 查看次数: |
162 次 |
| 最近记录: |