I think the source code can give us more information on how it works. ReentrantLock.newCondition () returns ConditionObject in AbstractQueuedSynchronizer . Here is the source code link AQS source code .
1. Real streams are transmitted in FIFO order.
There are two queues in AbstractQueuedSynchronizer .
One to wait for a lock (just call the wait wait queue), you will see two mutable head and tail variables in the AbstractQueuedSynchronizer Description , and the equity parameter will affect the behavior of this queue. When you select a new ReentrantLock and call to acquire , AQS will call FairSync tryAcquire to check if the current thread is the first thread waiting for a lock wait queue, see hasQueuedPredecessors .
The other queue is the signal queue in the ConditionObject definition, you will see two variables firstWaiter and lastWaiter . When it waits , a node will add queues to the tail, and when the signal is called, the node will be removed from the head and added to the lock waiting queue before recquire lock.Add to the lock waiting queue does not mean that it will be woken up, but Lock.unlock () will called after a signal that waiters wake up, see unparkSuccessor .
2. The order of locking ordering for threads returning from wait methods is the same as for threads initially acquiring a lock, which is not specified by default, but for honest locks it supports those threads that were waiting for the longest one.
Awakening from the wait method does not mean locking, it will call acquireQuedued to re-lock the lock and it will be possible to park again. In my understanding, the order of the initial acquisition of the lock is the same as the order of the call is expected , as well as the order of the call to acquireQueued , which confused me , but for honest locks, the threads that were waiting for the longest are preferred. . When you wake up from waiting , in my opinion, this will be the first thread in the waiting queue for locking, When you call, getQuedued and check p == head && & & tryAcquire (arg) , lock fair or not has no effect.
Hope this helps, and let me if I am wrong.