[concurrency-interest] ReentrantReadWriteLock should throw ex on writeLock().lock() if read lock held?

David Holmes dcholmes at optusnet.com.au
Tue Sep 5 19:33:02 EDT 2006

Hi Jed,

> Just a quick question regarding the acquisition policy of the
> ReentrantReadWriteLock if a reader tries to grab the write lock. What
> currently happens is that the call to writeLock().lock() blocks forever.
> Wouldn't it be better to throw some sort of exception like
> IllegalMonitorStateException (not that one specifically, but something
> like it)?. That way you would fail-fast and find the problem fairly
> quickly...

"better" is subjective. Correctly written code simply won't do this. If a
check were put in for this then correctly written code would pay the price
on the every writeLock acquisition.

You can easily write your own wrapper that would add such a check via an
assert; or place the assert directly in your code.

David Holmes

More information about the Concurrency-interest mailing list