/ *
* Retrieves and removes the head of this queue, waiting if necessary
* until an element with an expired delay is available on this queue.
*
* @return the head of this queue
* @throws InterruptedException {@inheritDoc}
*/
public E take() throws InterruptedException {
final ReentrantLock lock = this.lock;
//
lock.lockInterruptibly();
try {
for (;;) {
//
E first = q.peek();
if (first == null)
//
available.await();
else {
// getDelay
long delay = first.getDelay(NANOSECONDS);
if (delay <= 0)
//
return q.poll();
first = null; // don"t retain ref while waiting
if (leader != null)
available.await();
else {
Thread thisThread = Thread.currentThread();
leader = thisThread;
try {
// x
available.awaitNanos(delay);
} finally {
// ??????????
if (leader == thisThread)
leader = null;
}
}
}
}
} finally {
// leader == null
if (leader == null && q.peek() != null)
available.signal();
lock.unlock();
}
}
What is the logic for judging leader = = thisThread where is marked with a question mark?
my understanding is that when the current thread waits for delay nanoseconds on the blocking queue of available, it is only possible that the thread inserts a new element into queue Q and sets leader to null, when the element becomes the first element of the queue after sorting. It is impossible for any other thread performing take operation to modify the value of leader, so leader is either null, or thisThread. On the basis of this understanding, the judgment of the if statement at the question mark is meaningless. Just set leader to null directly.
so I am contradictory, what on earth is the situation?