当前位置:网站首页>When AQS wakes up the thread, I understand why it traverses from the back to the front
When AQS wakes up the thread, I understand why it traverses from the back to the front
2022-07-26 05:04:00 【On knowledge】
First, get familiar with the code , Hang and wake up
Tail traversal source code
private void unparkSuccessor(Node node) { // obtain wait state int ws = node.waitStatus; if (ws < 0) compareAndSetWaitStatus(node, ws, 0);// Will wait for status waitStatus Set to initial value 0 /** * If the subsequent node is empty , Or the status is CANCEL( Has lapsed ), Then from the tail forward traversal to find the first node in the normal blocking state * Wake up */ Node s = node.next; if (s == null || s.waitStatus > 0) { s = null; for (Node t = tail; t != null && t != node; t = t.prev) if (t.waitStatus <= 0) s = t; } if (s != null) LockSupport.unpark(s.thread);// Wake up the thread }Be careful for Logic in the loop : From the tail forward , Find the first node in the normal blocking state , Until the nodes coincide ( That is, it is equal to the current node )
High and send the logic of joining the team
Since the logic of traversal from the tail is adopted , Then it must be to solve the possible problems . And the problem is enq(…) In the methodprivate Node enq(final Node node) { for (;;) { Node t = tail; if (t == null) { // Must initialize // The queue is empty and needs to be initialized , Create an empty header node if (compareAndSetHead(new Node())) tail = head; } else { node.prev = t; //set The tail node if (compareAndSetTail(t, node)) { // The current node is set to the tail t.next = node; // Of a precursor node next Pointer to current node return t; } } } }Atomic question
In this method , Putting the current node at the end uses CAS To ensure thread safety , But notice : stay if The code in the statement block does not use any means to ensure thread safety !in other words , In high concurrency , That may be the case :
Threads A adopt CAS Get into if After the statement block , A context switch has occurred , The thread B The method is also implemented , And it's done . Then the thread C Called unparkSuccessor Method .
Suppose it is a form of traversal from beginning to end , Threads A Of next The pointer is still null! in other words , Subsequent nodes will be missed .
Diagram flow
Threads A perform CAS Set the current node as the tail :
Original thread A To execute t.next = node; take node2 Of next Set to node3, however , A context switch occurs , The time slice is handed over to the thread B, in other words , here node2 Of next still null
Threads B perform enq Logic , Final CLH The queue is shown in the figure :

A context switch occurs , The time slice is handed over to the thread C, Threads C Called unparkSuccessor Method , Suppose it is a form of traversal from beginning to end , stay node2 You will find ,next Pointer for null, It seems that there are no subsequent nodes .
A context switch occurs , The time slice is handed over to the thread A,A take node2 Of next=node3. Something strange happened : For threads C Come on , No follow-up node3 and node4, But for other threads , But these two nodes appear
ending
From the head traversal, we found the reason for this problem , Finally, let's talk about why there is no such problem when traversing from the tail ?The most fundamental reason is :
node.prev = t; Precede CAS perform , in other words , You have assigned a value to the predecessor node before setting the current node as the tail , Of course it won't happen prev=null The situation of
边栏推荐
- Redis解决库存超卖问题
- STM32 development | ad7606 parallel multi-channel data acquisition
- The integrated real-time HTAP database stonedb, how to replace MySQL and achieve nearly 100 times the improvement of analysis performance
- mysql函数汇总之日期和时间函数
- 【云原生 | 17】容器的四种网络模式
- Redis expiration deletion strategy and memory obsolescence strategy
- Alibaba cloud industrial vision intelligent engineer ACP certification - Preparation
- Two ways to create MySQL database
- 创建MySQL数据库的两种方式
- 一次线上事故,我顿悟了异步的精髓
猜你喜欢

Database startup message: ora-29702: error occurred in cluster group service

Switch to router technology: OSPF single zone configuration, OSPF multi zone and end zone

分子骨架跃迁工具-DeLinker介绍
![[cloud native | 17] four network modes of container](/img/06/01204436c27b69a5ae6fcb04642c2a.jpg)
[cloud native | 17] four network modes of container

AXI协议(4):AXI通道上的信号

推荐12个免费查找文献的学术网站,建议点赞、收藏!

手把手教你用代码实现SSO单点登录

【ACWing】2983. 玩具

域名解析过程全分析,就着文字理解更佳

Kubernetes 进阶训练营 调度器
随机推荐
ES6 modularization +commonjs
安装NCCL\mpirun\horovod\nvidia-tensorflow(3090Ti)
未来大气污染变化模拟
【pytorch】torch1.8.1安装、查看torch版本、GPU是否可用
Whether the SQL that fails to execute MySQL is counted into the slow query?
Correspondence between IEC61131 data type and C data type
The elderly who claim alimony from other children after being supported by their widowed daughter-in-law should be supported
Alibaba cloud industrial vision intelligent engineer ACP certification - Preparation
Five simple and practical daily development functions of chrome are explained in detail. Unlock quickly to improve your efficiency!
推荐12个免费查找文献的学术网站,建议点赞、收藏!
Wsl2 best practices, eliminate difficult xshell and finalshell
To study the trend of open source and gain insight into the future of the industry, stonedb community and the China Academy of communications and communications released the Research Report on the dev
时代潮流-云原生数据库的崛起
创建MySQL数据库的两种方式
嵌入式分享合集21
Add and modify the verification logic, and use -validation- to complete the group verification
Textfield and password input box that are more flexible and easy to use in compose
MySQL基础学习
汉字风格迁移篇---通过生成对抗网络学习一对多程式化汉字的转换和生成
注解@Autowired如何自动装配