In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "what is the CLH queue lock of java". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
What is it
CLH queue locks are spin locks that provide fairness in first-come-first-served service.
What are the characteristics?
CLH is implemented based on linked lists. The thread is just constantly spinning, constantly polling the status of the precursor node, and if the precursor node releases the lock, the thread ends spinning.
For scenarios with short lock time, it has higher performance than blocking threads, and can maintain a certain degree of fairness.
And with spin times to do in-depth optimization, so that the use of a wider range of scenarios, better performance.
What's the use?
For specific application scenarios, the use of this lock can improve performance, which is a direction of lock optimization.
Analysis of realization principle
The core idea is not to block threads and replace them with loops.
A LCH reference implementation
Public class ClhSpinLock implements Lock {/ * precursor node each thread * / private final ThreadLocal prev = ThreadLocal.withInitial (Node::new); / * * current node * / private final ThreadLocal currentThreadNode = ThreadLocal.withInitial (Node::new); / * point to the node at the end of the queue *
* it is worth noting that the default value of this node is false, that is, if the precursor node is the default node, it will not play the role of a lock * that is, the first thread will return immediately after the lock () operation comes in.
* and all threads share this tail reference * list promotion depends on the shared tail * / private final AtomicReference tail = new AtomicReference (new Node ()); public ClhSpinLock () {} / * 1. The initial state tail points to a node (head) node * +-+ * | head | |: = Node |-- > |: = Node | * |: = Prev |-| |: = Prev |-> +-* +-+ | head | * +-+ * 3. Look for the prev-node of the current node and start spinning * / @ Override public void lock () {/ / noteworthy details / / if the first thread comes in / / then pred.locked is false, so this thread calls lock and returns directly / / after the second thread comes in, its pred is the first line. Threads after / / of a program and so on final Node currentThreadNode = this.currentThreadNode.get () CurrentThreadNode.locked = true; / / all threads push final Node prev = this.tail.getAndSet (currentThreadNode) through the constant transformation of this tail; / / this setting that only satisfies each thread's own prev is actually a this.prev.set (prev) that can be omitted. / / spin while (prev.locked) {} @ Override public void unlock () {final Node node = this.currentThreadNode.get (); node.locked = false; this.currentThreadNode.set (this.prev.get ());} @ Override public void lockInterruptibly () throws InterruptedException {} @ Override public boolean tryLock () {return false } @ Override public boolean tryLock (long time, TimeUnit unit) throws InterruptedException {return false;} @ Override public Condition newCondition () {return null;} private static class Node {private volatile boolean locked;}
Related test code
Public class ClhSpinLockTest {private static int count = 0; private static void testLock (Lock lock) {try {lock.lock (); System.out.println ("task startup thread name:" + Thread.currentThread (). GetName () + "task start statistics:" + count + "task execution time:" + System.currentTimeMillis ()); for (task I = 0; I
< 100; i++) { count++; } System.out.println("任务启动线程名称:" + Thread.currentThread().getName() + " 任务结束时统计数:" + count); } finally { lock.unlock(); } } public static void main(String[] args) { int workerThreadNums = 5; // 这个锁是需要每个相关线程都需要持有的 final ClhSpinLock clhSpinLock = new ClhSpinLock(); // 这个是一个栅栏 目的是等到所有线程都执行完 看结果 final CyclicBarrier cyclicBarrier = new CyclicBarrier(workerThreadNums, () ->System.out.println ("final result:" + count); for (int I = 0; I
< workerThreadNums; i++) { new Thread(() ->{testLock (clhSpinLock); try {cyclicBarrier.await ();} catch (Exception e) {e.printStackTrace ();}}) .start ();} advantages and disadvantages
Low space complexity
Under different CPU architectures, the performance is different.
This is the end of the content of "what is the CLH queue lock for java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.