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