In Java we can implements fairness by using Lock interface and ReentrantLock
class as follows :
Lock lock=new ReentrantLock(true);
Once we implement fairness then in the case of multiple threads waiting to access the lock, the one which is waiting for most duration is given access to lock.
Can anybody provide details of how JVM
keep the track of threads waiting for long time i.e how fairness is implemented by JVM
.
ReentrantLock
just use a queue? I looked at the source andReentrantLock
has 2 static classes that extendAbstractQueuedSynchronizer
, on class for fair the other for not fair.