// you’re reading...

Java

A Caveat To Using Read-Write Locks

97a2d9f793ff528144623428b0ea345d Bookmark to Delicious

Bruce Tate, author of “Bitter Java“, referred to my post on “Implementing Read-Write Locks in Java” in his book to explain the possible performance optimization one can achieve by using read-write locks instead of applying “synchronized” (Java’s mutex). However, it should be noted that blind application of read-write locks (rwlocks) can have its own penalty.

Performance improvement obtained from use of rwlocks is based on  the assumption that “write” lock will be held infrequently and for a short duration.

Allowing reader threads to proceed in parallel. If the programmer becomes careless, and holds the write lock longer than he would had held a mutex, the performance may actually go deteriorate. This may not be intuitive at first but its something to be expected – since holding the write lock blocks reader threads as well, just like a mutex !

Discussion

No comments for “A Caveat To Using Read-Write Locks”

Post a comment