<div dir="ltr"><div>Hi Kim,<br></div><div dir="ltr"><br></div><div dir="ltr">On Sat, Sep 7, 2024 at 10:36 AM 김민주 <<a href="mailto:miiiinju00@gmail.com" target="_blank">miiiinju00@gmail.com</a>> wrote:</div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><p>Here's a clearer outline of the scenario:</p><ul><li>Threads T1 to T10 are waiting on <code>Condition::await()</code> because the queue is full.</li><li>T11 calls <code>take()</code> and holds the lock via <code>lock.lockInterruptibly()</code>.</li><li>T12 calls <code>queue.put()</code> and enters the wait queue for <code>lock.lockInterruptibly()</code>. (As I understand, the wait queue for ReentrantLock and Condition are separate.)</li><li>T11 reduces the count and sends a signal, then releases the lock.</li><li>T1 receives the signal and moves to the lock queue. Since the ReentrantLock is in fair mode, T12 (which was already waiting) gets priority, and T1 will acquire the lock later.</li><li>T12 acquires the lock and successfully enqueues.</li></ul></div></blockquote><div>From one reading of the Javadoc, your step #5 ("T12 gets priority") is not supposed to happen that way. Instead, one of T1 through T10 should be guaranteed to acquire the lock.<br></div><div><br></div><div>Here it is again (from ReentrantLock.newCondition()):<br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">The ordering of lock reacquisition for threads returning
from waiting methods is the same as for threads initially
acquiring the lock, which is in the default case not specified,
but for <i>fair</i> locks favors those threads that have been
waiting the longest.
</blockquote></div><div><br></div><div>But part of the problem here is that this documentation is ambiguous.<br></div><div><div><br></div><div>The ambiguity is: are ALL threads trying to acquire the lock, whether on an initial attempt or after a condition wakeup, ordered for fairness together in one big pool? → In this case step #5 can't happen. Call this Interpretation A.</div></div><div><br></div><div>Or is this merely saying that threads waiting on a condition reacquire the lock based on when they started waiting on the condition, but there are no ordering guarantees between those threads and any other unrelated threads trying to acquire the lock? → In this case step #5 can happen. Call this Interpretation B.</div><div><br></div>So I think we first need to clarify which interpretation is correct here, A or B.<br><div><div><br></div><div>On that point, Victor said this:<br></div><div><br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">I've re-read ReentrantLock and AQS, and from my understanding on the
logic the Condition's place in the wait queue should be maintained,
which means that T3 shouldn't be able to "barge".</blockquote></div></div><div><br></div><div>So it sounds like Victor is confirming interpretation A. Victor do you agree?<br></div><div><br></div><div>If so, then it seems like we need to do two things:</div><div><br></div><div>1. File a Jira ticket to clarify the Javadoc, e.g. to say something like this:<br></div><div><br></div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">The ordering of lock reacquisition for threads returning
from waiting methods is the same as for threads initially
acquiring the lock, which is in the default case not specified,
but for <i>fair</i> locks favors those threads that have been
waiting the longest. <b>In the latter case, the ordering consideration includes all threads attempting to acquire the lock, regardless of whether or not they were previously blocked on the condition.</b><br></blockquote></div><div><br></div><div>2. Understand why Kim's updated test case is still failing (it must be either a bug in the test or a bug in ArrayBlockingQueue).<br></div><div><br></div><div>-Archie<br></div><br><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature">Archie L. Cobbs<br></div></div>