Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Actual use of lockInterruptibly for a ReentrantLock

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

like image 297
Rollerball Avatar asked Jul 23 '13 13:07

Rollerball


People also ask

What is the use of reentrant lock?

As the name says, ReentrantLock allows threads to enter into the lock on a resource more than once. When the thread first enters into the lock, a hold count is set to one. Before unlocking the thread can re-enter into lock again and every time hold count is incremented by one.

What is lockInterruptibly?

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 lock() . But if another thread interrupts the waiting thread lockInterruptibly() will throw InterruptedException .

What is the difference between lock and ReentrantLock?

Lock is an interface. It defines a set of methods that all locks should have. ReentrantLock is a concrete class that implements the Lock interface. It implements all the methods defined in Lock , plus much more.

Is ReentrantLock fair?

ReentrantLock API doc says: The constructor for this class accepts an optional fairness parameter. When set true, under contention, locks favor granting access to the longest-waiting thread. Note however, that fairness of locks does not guarantee fairness of thread scheduling.


1 Answers

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 lock(). But if another thread interrupts the waiting thread lockInterruptibly() will throw InterruptedException.

like image 109
Evgeniy Dorofeev Avatar answered Oct 05 '22 23:10

Evgeniy Dorofeev