"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 strictly FIFO-based queue that maintains a set of threads. He can ensure that there is no hunger, strict first-come-first service fairness. is the CLH queue node:

In the node qnode of the CLH queue, there is a locked field that indicates whether the node needs to acquire a lock. True indicates that a fetch is required, and false indicates that no acquisition is required. In the CLH queue, the behavior of the Mynode is affected by the change in the node that the node is not connected to by the next pointer, but by the mypred point.

If there are two threads (thread A, thread B). Start thread A requires a lock, then he creates a Qnode node and sets locked to true (indicating that a lock is required). 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), which, of course, points the tail to itself to indicate that it is the last node of the CLH queue. For example, the following:

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

The advantage of a CLH queue lock is the low space complexity, 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 A lock with L-tail). The CLH variant was applied to the AQS.

References

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

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.