Blocking and awakening of Java concurrency Framework--aqs

Source: Internet
Author: User
Tags cas

According to the preceding thread blocking and waking section, there are three ways to implement blocking wakeup at the Java language level: Suspend and resume combinations, wait and notify combinations, park and Unpark. Where suspend and resume are discarded by Java because there is an unresolved state problem, the wait and notify also have conditions, and wait must be executed before notify, If a thread executes notify and then wait will probably cause a thread to block forever, then another solution must be proposed, that is, the park and Unpark combination, which is located under the Juc package, It should also be because of the new blocking wake-up method that was introduced when Juc was written and found that Java's existing methods did not solve the problem, because Park and Unpark used a licensing mechanism with a maximum license of 1, so Unpark and park operations did not accumulate, and Unpark could be executed before park , such as Unpark first, the park will not block behind. Java's true language level of concurrent programming should start with the concurrency expert Doug Lea-led JSR-166, a specification request submitted to JCP to add a concurrency programming tool to the Java language. By adding the Java.util.concurrent toolkit to the JDK for developers to use, developers can easily build their own synchronizer, which can only be relied on by the JVM's built-in enhancement before the concurrency process. The blocking and awakening of the ASQ framework is obviously using the park and Unpark methods of the Locksupport class, respectively, calling the park and Unpark local methods of the unsafe class. The logic is as follows: block if (attempt to acquire lock failed) {    Create node    use CAs to insert node into the tail of the queue     while (true) {    if ( Attempt to acquire the lock succeeds and node's predecessor is the head node) {Set the current node as head node     jump out of loop}else{    Use CAS mode to modify node precursor Waitstatus ID signal     if (modified successfully) {        Locksupport.park ();}  }} wakeup if (attempt to release lock succeeded) {Locksupport.unpark (The next node contains the thread);} If a thread participates in the lock competition, first try to acquire the lock, then create the node and insert the tail of the queue.Then try to acquire the lock again, if the success does not do other task processing directly return, otherwise set the node state is to run state, and finally use Locksupport Park block the current thread. When the predecessor node finishes running, it will attempt to wake the successor, using the Unpark wake of the Locksupport. In general, the Java-provided JUC concurrency Toolkit, which has a variety of problems with suspend and resume in the context of blocking and wakeup operations, must be done using the methods provided in Locksupport.

Block and wake of Java concurrency Framework--aqs

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.