<div dir="ltr"><div dir="ltr"><p class="gmail-"><strong>Hi Archie,</strong></p><p class="gmail-">Thanks to your valuable suggestions, I was able to come up with a much more appropriate test, and I’ve learned a great deal in the process. I truly appreciate your insights! While this approach is clearly a significant improvement over the previous one, I still feel there might be concerns about the atomicity between <code>notFull.await()</code> and the signaling from outside, but I can’t deny that this new approach provides much better guarantees. Your feedback has been incredibly helpful, and I’m very grateful for your time and effort. Thank you again!</p><p class="gmail-"><br></p><p class="gmail-"><br></p><p class="gmail-"><br></p><p class="gmail-"><br></p><hr><p class="gmail-"><strong>Hi Viktor,</strong></p><p class="gmail-">Thank you for sharing your thoughts, which have given me much to consider. I have a follow-up question regarding the improvements in AQS that you mentioned. Specifically, I’d like to clarify whether these improvements ensure that, in the fair mode of <code>ReentrantLock</code>, threads waiting on a <code>Condition</code> are placed back in the queue without acquiring the lock, or if the signaling thread can now immediately acquire the lock after being signaled.</p><p class="gmail-">Initially, my concern was that a thread receiving a signal might still face starvation if another thread calls <code>put()</code> and acquires the lock before the signaled thread can do so. Here's an example scenario:</p><ol><li class="gmail-">The queue is full, and T2 is executing <code>put()</code> and is waiting in <code>Condition.await()</code>.</li><li class="gmail-">T1 calls <code>queue.take()</code>, removes an item from the queue, and is about to send a <code>signal()</code><br></li><li class="gmail-">T3 is about to call <code>put()</code> and is just before <code>lock.lockInterruptibly()</code>.</li><li class="gmail-">T1 decreases the count and sends a <code>signal()</code>, indicating that space is available in the queue.<br></li><li class="gmail-">T3 acquires the lock via <code>lock.lockInterruptibly()</code>, successfully enqueues an item because the count condition is satisfied, and releases the lock.<br></li><li class="gmail-">T2 receives the signal and wakes up, but since T3 already enqueued an item, the count has increased, and T2 must wait again in <code>await()</code>.<br></li></ol><p class="gmail-">It seems to me that this scenario could occur regardless of whether <code>ReentrantLock</code> is in fair mode or not. Has the improvement in AQS addressed this type of contention scenario to prevent such starvation issues, or is this still a possible outcome?</p><hr><p class="gmail-">Your insights into "unbounded unfairness" have also provided me with a lot of food for thought, and I’m grateful for the opportunity to reflect on these issues. </p><p class="gmail-">In your view, would the thread contention scenario I’ve described fall under the category of unbounded unfairness, or would it be considered an acceptable level of contention?</p><p class="gmail-"><br></p><p class="gmail-">Once again, thank you for all the knowledge and understanding I've gained through your feedback. I'm truly grateful for your time and expertise.</p><p class="gmail-"><br></p><p class="gmail-"><br></p><p class="gmail-">Best regards,<br>Kim Minju<br></p><p class="gmail-"><br></p></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">2024년 9월 6일 (금) 오전 4:52, Viktor Klang <<a href="mailto:viktor.klang@oracle.com">viktor.klang@oracle.com</a>>님이 작성:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="msg-5543794601931186038">




<div dir="ltr">
<div style="font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
Archie,<br>
<br>
I should've been more specific—Condition-as-implemented-by-ReentrantLock (in fair mode) provides stronger (for some definition of stronger) semantics that the Condition interface specifies.</div>
<div style="font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
Since it's related, I've recently integrated a hardening of AQS and AQLS reacquisition logic in await().</div>
<div style="font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
Given what you presented earlier about the detection of "producer parked" it's likely that the conclusion is that ABQ works as expected.</div>
<div style="font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<br>
</div>
<div id="m_-5543794601931186038Signature" style="color:inherit">
<div style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
Cheers,<br>
√</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<b><br>
</b></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<b>Viktor Klang</b></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
Software Architect, Java Platform Group<br>
Oracle</div>
</div>
<div id="m_-5543794601931186038appendonsend"></div>
<hr style="display:inline-block;width:98%">
<div id="m_-5543794601931186038divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Archie Cobbs <<a href="mailto:archie.cobbs@gmail.com" target="_blank">archie.cobbs@gmail.com</a>><br>
<b>Sent:</b> Thursday, 5 September 2024 21:23<br>
<b>To:</b> Viktor Klang <<a href="mailto:viktor.klang@oracle.com" target="_blank">viktor.klang@oracle.com</a>><br>
<b>Cc:</b> 김민주 <<a href="mailto:miiiinju00@gmail.com" target="_blank">miiiinju00@gmail.com</a>>; Daniel FUCHS <<a href="mailto:daniel.fuchs@oracle.com" target="_blank">daniel.fuchs@oracle.com</a>>; <a href="mailto:core-libs-dev@openjdk.org" target="_blank">core-libs-dev@openjdk.org</a> <<a href="mailto:core-libs-dev@openjdk.org" target="_blank">core-libs-dev@openjdk.org</a>><br>
<b>Subject:</b> Re: [External] : Re: [POTENTIAL BUG] Potential FIFO violation in BlockingQueue under high contention and suggestion for fair mode in ArrayBlockingQueue and LinkedBlockingQueue</font>
<div> </div>
</div>
<div>
<div dir="ltr">
<div dir="ltr">
<div>
<div>Apologies in advance if I'm misunderstanding anything...</div>
<div><br>
</div>
</div>
<div>On Thu, Sep 5, 2024 at 2:05 PM Viktor Klang <<a href="mailto:viktor.klang@oracle.com" target="_blank">viktor.klang@oracle.com</a>> wrote:
<br>
</div>
<blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
 Thread state polling aside, for as long as Condition::await() is allowed to spuriously wake, FIFO just cannot be "guaranteed".</blockquote>
<div> </div>
<div>What about this statement in the Javadoc for ReentrantLock.newCondition():</div>
<div><br>
</div>
<blockquote 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><br>
</div>
<div>So what you're saying is that a spurious wakeup on a Condition is not the same thing as a spurious signal() on a Condition; if it were, then the above statement would apply and FIFO ordering would be preserved.</div>
<div><br>
</div>
<div>Of course, a spurious wakeup would not find the condition being waited on satisfied unless there was a big coincidence. So an ordering violation that actually mattered should be exceedingly rare.<br>
</div>
<div><br>
</div>
<div>Anyway, this does seem to be a glitch in how things are supposed to work. That is: there can be no guaranteed ordering for Condition waiters when there can be spurious wakeups.</div>
<div><br>
</div>
<div>Maybe this corner case should be documented. Or better yet, fix the bug by requiring Condition to "filter out" spurious wakeups if preserving FIFO ordering (it should be possible).<br>
</div>
<div><br>
</div>
<div>-Archie<br>
</div>
</div>
<br>
<span>-- </span><br>
<div dir="ltr">Archie L. Cobbs<br>
</div>
</div>
</div>
</div>

</div></blockquote></div></div>