------------------------------------------------------
Its application scenarios can be described as follows:
For example, if the company's senior management wants to hold a meeting with 10 people, the meeting must wait until all the 10 people arrive,
If someone hasn't arrived yet after a certain period of time, maybe they don't want to wait. They can leave, and others may continue to wait...
Its usage can be seen in the API...
------------------------------------------------------
1. The countdownlatch instantiation function needs to initialize the initial value of the AQS resource, that is, 10 people in the scenario
2. In await () mode, if the resource waiting for AQS is 0, that is, the person waiting for AQS in the scenario will be notified by phone about what to do (interrupted)
3. Await (time) recording + persons waiting for AQS Resources in the interruptible mode is 0, that is, those waiting for a while in the scenario
4. Countdown () reduces AQS resources by 1, that is, one person in the scenario goes to the meeting room.
------------------------------------------------------
1. countdownlatch is also based on the AQS architecture and is a shared mode.
------------------------------------------
Class sync extends abstractqueuedsynchronizer {
// Implementation of tryacquireshared
Public int tryacquireshared (INT acquires ){
Return getstate () = 0? 1:-1;
}
// Implementation of tryreleaseshared
Public Boolean tryreleaseshared (INT releases ){
For (;;){
Int c = getstate ();
If (C = 0) return false;
Int nextc = C-1;
If (compareandsetstate (C, nextc) return nextc = 0;
}
}
}
------------------------------------------
From the implementation of sync, we can see that for tryacquireshared, we can only check whether the resource of AQS is 0. If it is zero, we will return a number greater than zero,
Otherwise, the return value is less than zero. For tryreleaseshared implementation, if the resource of AQS is reduced by 1 and then changed to 0
You can release the synchronization thread...
2. Await ()-AQS. acquiresharedinterruptibly (1). In this way, enter tryacquireshared, sleep, and wake up the cycle...
3. Await (time)-AQS. tryacquiresharednanos (1, time), enter tryacquireshared, sleep for a limited time, and wake up the cycle...
4. Countdown ()-AQS. releaseshared (1). If tryreleaseshared returns true, wake up head-> next-> thread
Remove all threads from the control of the synchronizator
-------------------------------------------------------
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service