user2938723
user2938723

Reputation: 1130

How is ReentrantLock fair if the same thread keeps acquiring the lock again and again?

I am fairly new to Advanced Java and learning Reentrant locks. I understand that ReentrantLock has a fairness parameter which makes sure that the lock is given to most starving thread (unlike the case in synchronized intrinsic locks.)

However, Reentrant also means that the same thread can reacquire the lock again and again by incrementing the holdCount. If the same thread keeps acquiring lock forever how does it guarantee fairness?

Upvotes: 3

Views: 452

Answers (1)

user2023577
user2023577

Reputation: 2083

If the thread reenters the lock, it means it hasn't ever unlocked it to leave a fair chance to other threads. Fairness is upon election of the next thread when the lock is not held.

Upvotes: 5

Related Questions