Actual use of lockInterruptibly for a ReentrantLock

2022-09-03 01:41:35

What do you actually use for this method ? I have read the API however it's not very clear to me. Could anybody express it in other words?lockInterruptibly


答案 1

lockInterruptibly() may block if the the lock is already held by another thread and will wait until the lock is aquired. This is the same as with regular . But if another thread interrupts the waiting thread will throw .lock()lockInterruptibly()InterruptedException


答案 2

The logic is the same as for all interruptible blocking methods: it allows the thread to immediately react to the signal sent to it from another thread.interrupt

How this particular feature is used is up to the application design. For example, it can be used to kill a contingent of threads in a pool which are all waiting to aquire a lock.


推荐