In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, Xiaobian will bring you about the use of Semaphore in concurrent programming. The article is rich in content and analyzed and described from a professional perspective. After reading this article, I hope you can gain something.
Thread to acquire lock First check whether the thread has an interrupt flag, throw an exception if there is, and try to acquire the lock if there is no, the code is as follows:
If the queue is empty, get the state of the current abstract queue, the difference between the current state and the number of resources is greater than 0 (indicating that the attempt to acquire the lock is successful), cas changes the current state to the difference and returns (the whole process spin +cas ensures that the attempt to acquire the lock is successful and modifies the atomicity of the state); If the queue has queued nodes (failure to acquire lock returns-1) or the queue has no queued nodes but the difference is less than 0 (failure to acquire lock, too many resources), the queue will be queued, and the code is as follows:
Here and exclusive lock is the difference between the parameters for shared mode, the same queue is empty to initialize the queue (spin +cas to ensure the atomicity of the initialization queue), not empty on the queue. Determine whether the previous node of the current node is the head node, if yes, try to acquire the lock again, if failed, or the previous node of the current node is not the head, change the ws of the previous node to-1, block the current thread park.
If there is a thread releasing the lock, the code is as follows:
Try to release the shared lock with the following code:
Get the state of the abstract queue, state+1 (not necessarily +1) and, cas modify the state to and, try to release the lock successfully. Wake up the subsequent node, the code is as follows:
Waking up the head node modifies ws=-1 of the head node to 0, and then wakes up. The code is as follows:
If ws=-1 = 0 for the head node, the code is as follows:
Setting the current node to head (head= current node, pre=null, thread=null of current node) means that the previous node will be kicked out of the queue (next=null of previous node is modified by one step), the next node of current node is empty, next=null of previous node is set, the previous node is dequeued, and the lock acquisition is successful;
Whereas the next node is not empty (the next node will block the previous node ws=-1 before blocking park) and release the lock for shared mode, the code is as follows:
Here we change ws=-1 of the new head node to 0 and wake up the next node, as follows:
Wake up the next thread, the next thread is awakened in the blocking loop, try to acquire the lock, fail to block the park again, succeed to continue this step, and continue to propagate, that is, wake up multiple nodes
The above is the method of using Semaphore of concurrent programming shared by Xiaobian for everyone. If you happen to have similar doubts, you may wish to refer to the above analysis for understanding. If you want to know more about it, please pay attention to the industry information channel.
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.