"Java Concurrency Programming Combat"-----"J.U.C": CLH queue Lock

Source: Internet
Author: User

The CLH queue is always mentioned in the previous blogs, and in Aqs the CLH queue is a strict queue that maintains a set of threads strictly according to the FIFO. He can ensure that there is no hunger, strict first-come-first service fairness. is the CLH queue node:

The node Qnode in the CLH queue contains a field with a locked that indicates whether the node needs to acquire a lock, true to indicate that it needs to be fetched, and false to indicate that it is not required. In the CLH queue, the behavior of the Mynode is not connected by the next pointer but by the change of the node pointed to by the mypred.

Assume there are two threads (thread A, thread B). Start thread A needs to get a lock, then he creates a Qnode node and sets locked to true (indicating that a lock needs to be acquired). At the same time, get a mypred pointing to the precursor and rotate it on the locked of the precursor node until the precursor node is locked (locked is false, which we generally call spin), of course, this will tail point to itself to indicate that it is the last node of the CLH queue, as follows:

Then thread B is added to the CLH queue, and the tail domain should point to thread B.

The advantage of the CLH queue lock is that the space complexity is low (if there are n threads, l locks, each thread acquires only one lock at a time, then the required storage space is O (l+n), n threads have n mynode,l locks with l tail). The CLH variant was applied to the AQS.

Reference documents

Java Concurrent Programming Learning Note CLH Queue Lock: http://blog.csdn.net/aesop_wubo/article/details/7533186

"Java Concurrency Programming Combat"-----"J.U.C": CLH queue Lock

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.