[concurrency-interest] AbstractQueuedSynchronizer

Bobrowski, Maciej Maciej.Bobrowski at morganstanley.com
Thu Sep 22 07:57:14 EDT 2016

I have tried to understand the inner working of abstractQueuedSynchronizer and one thing I cannot quite explain.

When a thread decides it needs to park itself (let's say the CountDownLatch counter > 0), it does so by calling LockSuport.park. In the meantime, of course, the latch can be fully counted down, all awaiting threads unparked, while this thread is just about to park itself. Obviously that would not work, as now we have one thread parked, on a latch that is fully counted down, so no good.

This does not happen actually (I wrote a small test and verified), and it is down to the code of LockSupport.park()

public static void park(Object blocker) {
    Thread t = Thread.currentThread();
    setBlocker(t, blocker);
    unsafe.park(false, 0L);
    setBlocker(t, null);

Somehow, when the latch is fully counted down and unsafe.park is called, it immediately returns, so there is some magic behind the scenes that tells the thread not to actually park itself. Anyone could cast some light onto what is actually happening?



NOTICE: Morgan Stanley is not acting as a municipal advisor and the opinions or views contained herein are not intended to be, and do not constitute, advice within the meaning of Section 975 of the Dodd-Frank Wall Street Reform and Consumer Protection Act. If you have received this communication in error, please destroy all electronic and paper copies and notify the sender immediately. Mistransmission is not intended to waive confidentiality or privilege. Morgan Stanley reserves the right, to the extent permitted under applicable law, to monitor electronic communications. This message is subject to terms available at the following link: http://www.morganstanley.com/disclaimers  If you cannot access these links, please notify us by reply message and we will send the contents to you. By communicating with Morgan Stanley you consent to the foregoing and to the voice recording of conversations with personnel of Morgan Stanley.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20160922/31ca7a1a/attachment.html>

More information about the Concurrency-interest mailing list