0

I was going through the documentation on the Internet about reentrant locks. Also read the examples everywhere about nested "synchronize"d methods and how the lock needs to be acquired only once.

But I don't understand what is "re-enter"ing here? From what I could gather, there's lock() which blocks, tryLock() which doesn't, and the locking is bypassed if thread is unavailable. And the tryLock() is what makes ReentrantLock different from the synchronized keyword.

Does it mean:

  1. same thread is allowed to re-enter the locked section if it holds the lock?
  2. does hold count keep incrementing every time same thread re-enters?
  3. if so, is it sufficient to call unlock() only once, or it needs to be called as many times thread has re-entered?

Or is there something else to re-entering that I'm missing out?

user207421
  • 305,947
  • 44
  • 307
  • 483
user1354825
  • 1,108
  • 4
  • 21
  • 54
  • possible duplicate :- https://stackoverflow.com/a/11821900/4068218 – Hari Rao Sep 30 '21 at 08:58
  • @HariRao That isn't asking the same question – Mark Rotteveel Sep 30 '21 at 08:59
  • 1. Yes. 2. Yes, because if a thread releases a lock that's already held, you don't want it to release "all of the lock". And what would be the point of the hold count if it wasn't incremented again? It could just be a boolean `isHeld`. 3. See 2. – Andy Turner Sep 30 '21 at 08:59

0 Answers0