Fair mode
When constructed as fair, threads contend for entry using an approximately
arrival-order policy. When the currently held
lock is released either the longest-waiting single writer thread will
be assigned the write lock, or if there is a group of reader threads
waiting longer than all waiting writer threads, that group will be
assigned the read lock.
A thread that tries to acquire a fair read
lock (non-reentrantly) will block if either the write lock is held, or
there is a waiting writer thread. The thread will not acquire the read
lock until after the oldest currently waiting writer thread has
acquired and released the write lock. Of course, if a waiting writer
abandons its wait, leaving one or more reader threads as the longest
waiters in the queue with the write lock free, then those readers will
be assigned the read lock.
A thread that tries to acquire a fair write lock (non-reentrantly)
will block unless both the read lock and write lock are free (which
implies there are no waiting threads). (Note that the non-blocking
ReentrantReadWriteLock.ReadLock.tryLock() and
ReentrantReadWriteLock.WriteLock.tryLock() methods do not honor this
fair setting and will acquire the lock if it is possible, regardless
of waiting threads.)