[concurrency-interest] Performance of ReentrantReadWriteLock
David M. Lloyd
david.lloyd at redhat.com
Thu Nov 19 00:01:41 EST 2009
At a guess, I'd hazard that two things are taking effect.
1) *Lock are significantly faster than synchronization in your environment,
across the board. I believe this was the case for most or all Sun Java 5
JVM releases on Intel, though I could be mistaken there.
2) Map operations are so short that there's no significant contention for
the lock anyway. If you were holding the lock across, say, 50 to 100
operations maybe there'd be a bigger difference.
From my experience, a RWLock is only a win when the operations are
relatively long-lasting *and* it's a mostly-read situation. But often when
you're in the almost-all-reads situation, using a copy-on-write methodology
can be even better (no lock on the read path at all, plus in many cases you
can easily achieve cool snapshot semantics too).
On 11/18/2009 10:46 PM, Norman Elton wrote:
> I've done some very basic testing of a ReentrantReadWriteLock vs.
> synchronized. I fired up five threads, each of which is accessing a
> shared Map<Integer,Integer>. At first, I did 99% read operations, then
> 100% read operations. Initial testing, with no locking or
> synchronization, proved about 8M operations per second per thread.
> Then I tested synchronizing the get() and put() methods. Performance
> dropped to about 700K operations per second. Synchronization obviously
> has a large overhead.
> Strange thing is, the ReentrantReadWriteLock performed just about as
> well. Even in a 100% read environment, where I would think threads
> would never have to block for one another.
> Am I missing something here? Shouldn't I be seeing significantly
> better numbers for the ReentrantReadWriteLock? Presumably, it's
> allowing multiple threads to hit the hash at the same time. I would
> expect numbers somewhere between the synchronizing hash and the
> completely untouched HashMap.
> Thoughts? Thanks!
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
More information about the Concurrency-interest