[concurrency-interest] when to use lock and when to use lockInterruptibly

David Holmes dholmes at dltech.com.au
Tue Nov 29 18:13:01 EST 2005


> When should a lockInterruptibly be used instead of a lock? I guess it
> depends on the lock implementation if it supports interruption. Is this
> correct? And wouldn`t it be better to always you the lockInterruptibly
> (although you have to deal with an InterruptedException).

If you want to be more responsive to interrupts *and* the acquisition of the 
lock is not critical then you might want to always lockInterruptibly. A lot 
of the time though that would force propagating the InterruptedException from 
methods that are not truly "blocking" and hence don't need to be 
interruptible.

One situation where you would not want to use interruptible locking is when 
the action you need the lock for must be performed. eg:

   void somemethod() {
     try {
         // main code
      }
      finally {
         l.lock();
         try { updateState(); } finally {
         lock.unlock(); }
      }
   }

Cheers,
David Holmes


More information about the Concurrency-interest mailing list