[concurrency-interest] Locks with try/finally

Dawid Kurzyniec dawidk at mathcs.emory.edu
Thu Jun 30 21:06:01 EDT 2005


David Holmes wrote:

>Greg,
>
>Yes it has been covered :)
>
>Because if the lock() throws an exception you don't want to try to unlock()
>a lock you don't own - which will probably throw a different exception that
>replaces the first.
>
>While it should be rare for a lock() to throw it may well be possible - such
>as OutOfMemoryError. 
>
To throw in my 2c:

It gets a bit more important with tryLock(timeout) and 
lockInterruptibly(), since they are likely and expected to throw 
exceptions. And the consequences may be more serious than just exception 
masking for lock implementations other than ReentrantLock, which may 
have more sophisticated notion of ownership than per-thread, so that you 
may actually succeed in unlocking what you have not locked, and get in 
trouble much later.

Regards,
Dawid




More information about the Concurrency-interest mailing list