2

while trying to make a data structure (class) thread-safe with Java's ReentrantReadWriteLock() I stumbled across those 2 methods.

lock() - Non-Interruptible Mode: we acquire lock in a non-interruptible mode which means even if the thread waiting in queue is interrupted or unparked, it is going to simply retry acquiring the lock. This goes on, resulting in blocking and unblocking of the thread till it acquires the lock.

lockInterruptibly() - Interruptible Mode: in this case if the thread in waiting is interrupted by some other thread it will result in InterruptedException thus there won’t be any retry.

Now my questions are:

  1. When I want my threads to be interruptable do I have to you use lockInterruptibly()?
  2. Why would it make sense to use lockInterruptibly() (advantages), besides the fact that one can handle an InterruptedException in a certain way?
  3. Is it beneficial to make my Threads interruptible, in terms of performance etc.?

Would appreciate your help :)

[main source: https://www.javarticles.com/2016/06/java-reentrantlock-interruption-example.html]

0 Answers0