ReentrantLock allows a thread to acquire the same lock recursively, so that a lock count is incremented and decremented on successive lock/unlock. Whereas the lock count has to be decremented to zero before it is released to other threads.
Why or under what circumstances would I write code to acquire a lock recursively?
The only point I can see in having the feature is to make it convenient for us to write recursive code, where a method (which in the course of its execution acquires a lock) is called recursively.
Are there any other situations where recursive/repeated acquisition of a lock by a thread may be useful ?
Clarification of the question:
- Please ignore the lock being reentrant. Just so happens that recursivity is provided by reentrant lock.
- I am referring to the recursive feature of a lock
- Please do not answer with why use reentrant lock.
- Please do not answer with "recursivity is not the main feature of reentrant lock"
- I want to know what situations require the recursive acquisition of a lock, regardless if the lock is reentrant or not.