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