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

Jed Wesley-Smith jed at atlassian.com
Tue Sep 5 20:30:05 EDT 2006


Hi Giuliano,

tryLock() does the job, and I don't have a problem using it. I don't 
bring this up as a particular problem I have (apart from the first time 
I naïvely used it :-) , but as a general discussion point. If some code 
does call writeLock().lock() while holding a read lock it will block 
forever - which seems to me undesirable.

Giuliano Mega wrote:
> Hi Jed,
>
> I always thought that Lock.tryLock was supposed to play this role
> (except that it returns false instead of throwing an exception). Am I
> missing something from your problem that prevents you from using
> tryLock?
>
> Best,
>
> On 9/5/06, Jed Wesley-Smith <jed at atlassian.com> wrote:
>   
>> 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...
>>
>> Or in other words, is there a specific reason you would want to call
>> something in such a way that the only return possible is an
>> InterruptedException if you are lucky enough that another thread notices
>> your plight and interrupts you :-)
>>     
-- 
cheers,
- jed.




More information about the Concurrency-interest mailing list