I am currently using Guava Multimap implementation.
map = Multimaps.synchronizedSetMultimap(HashMultimap.<K, R> create());
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)
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>());
}
});
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,
- How can I create a test in multi-thread environment such that
Multimaps.newSetMultimap(new ConcurrentHashMap<>(), ConcurrentHashMap::newKeySet)
fails to work properly? Or it simply just works accidentally withConcurrentHashMap
? - If this line of code does NOT work for multiple threads environment, can anyone suggest me a way to improve multimap READ performance in multi-threads handling?
Many thanks.
ConcurrentHashMap
states: "Similarly, Iterators, Spliterators and Enumerations return elements reflecting the state of the hash table at some point at or since the creation of the iterator/enumeration. They do not throw ConcurrentModificationException." But that does not mean that the resulting Multimap is correct, some operations will not be as atomic as they should be. You could perhaps try to wrap it in a class using aReadWriteLock
or similar construct for locking.ReadWriteLock
to allow relatively efficient concurrent reads, but lock when writing, which may help if writes are rare compared to reads.