I have some storage similar to a map. I have been using synchronized(this)
for get
and put
methods. Since this storage is mostly used for reading, I thought of using ReentrantReadWriteLock
for better performance - to lock only on put
.
I expected better performance on lookups (since storage content was not changed). However, my JMH test shows the opposite:
.benchmarkClassMap3ClassLookup thrpt 20 460.152 7.417 ops/ms
.benchmarkClassMapClassLookup thrpt 20 1196.085 23.635 ops/ms
The first line is for the code using ReentrantReadWriteLock
. The second line is for the original code, with synchronized
. EDIT: test was running with 2 threads.
Did anyone else benchmarked ReentrantReadWriteLock
? Shouldn't be the results different? Or do the positive result only show up in the multi-threaded environment?
Related: this.