[concurrency-interest] AbstractQueuedSynchronizer

Bobrowski, Maciej Maciej.Bobrowski at morganstanley.com
Thu Sep 22 08:37:18 EDT 2016


Thanks. So two questions:


1.       What does the set/unset blocker do?

2.       What is the token you are referring to? How does it relate to a latch, which is an entirely different object? In this case, there is no unpark called for that thread as it is not yet part of the wait queue AFAIK

From: David Holmes [mailto:davidcholmes at aapt.net.au]
Sent: 22 September 2016 13:25
To: Bobrowski, Maciej (IST); concurrency-interest at cs.oswego.edu
Subject: RE: [concurrency-interest] AbstractQueuedSynchronizer

Park() has a token associated with it, so if unpark() is called before the park() the park() will return immediately and consume the token.

David

From: Concurrency-interest [mailto:concurrency-interest-bounces at cs.oswego.edu] On Behalf Of Bobrowski, Maciej
Sent: Thursday, September 22, 2016 9:57 PM
To: concurrency-interest at cs.oswego.edu<mailto:concurrency-interest at cs.oswego.edu>
Subject: [concurrency-interest] AbstractQueuedSynchronizer

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?

Thanks,
Maciej


________________________________

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.


________________________________

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/3bb19f47/attachment.html>


More information about the Concurrency-interest mailing list