当前位置:网站首页>How dare you write a resume that is proficient in concurrent programming? Why do you use a two-way linked list in AQS?
How dare you write a resume that is proficient in concurrent programming? Why do you use a two-way linked list in AQS?
2022-07-29 03:11:00 【Tom bomb architecture】
A job 4 Programmer of , The resume says proficient in concurrent programming , And also read AQS(
AbstractQueuedSynchronizer) Source code , Then the interviewer only asked such a question :“AQS Why use a two-way linked list structure ”?, Then it collapsed !
Actually AQS Everyone is no stranger , It is J.U.C A very important thread synchronizer in the package . today , Let me share with you my understanding .
in addition , I put all the videos I shared in previous periods into one 500 page PDF Supporting documents for interview question analysis , I hope this can improve the passing rate of all fans ,

How to get ? :
Scan the business card at the bottom of the article to get !
1、 Cause analysis
First , Double linked list has two pointers , A pointer points to the front node , A pointer to the successor node . therefore , Bidirectional linked lists can support constants O(1) Find the precursor node in the case of time complexity . therefore , When inserting and deleting a two-way linked list , It is simpler than a one-way linked list 、 Efficient .
From the characteristics of the two-way linked list , In my submission AQS There are three reasons for using a two-way linked list :

The first 1 One reason , Threads that do not compete for locks join the blocking queue , And the premise of blocking waiting is , The leading node of the node where the current thread is located is in normal state , This design is to avoid the problem that there are abnormal threads in the linked list, which makes it impossible to wake up subsequent threads .

therefore , Before thread blocking, you need to judge the status of the front node , If there is no pointer to the front node , You need from Head The node starts to traverse , Very low performance .

The first 2 One reason , stay Lock There's one in the interface ,lockInterruptibly() Method , This method indicates that threads blocked by locks are allowed to be interrupted .

in other words , Threads that do not compete for locks join the synchronization queue and wait , Is to allow external threads to pass interrupt() Method to trigger wakeup and interrupt . This is the time , The status of the interrupted thread will be changed to CANCELLED. And marked as CANCELLED Thread in state , There is no need to compete for locks , But it still exists in the two-way linked list .
This means that in the subsequent lock competition , You need to remove this node from the linked list , Otherwise, the thread blocked by the lock will not wake up normally . under these circumstances , If it's a one-way list , You need from Head Nodes start to traverse down one by one , Find and remove the node with abnormal state . Similarly, the efficiency is relatively low , It will also lead to competition between lock wake-up operations and traversal operations .

The first 3 One reason , To avoid the overhead of thread blocking and wakeup , So the thread just added to the linked list , First, I will try to compete for the lock by spinning .

But in fact, according to the design of fair lock , Only the next node of the head node is necessary to compete for locks , after
Continued node contention locks are of little significance . otherwise , It will cause herding , That is, a large number of threads try to compete for locks before blocking, which brings relatively large performance overhead .
therefore , To avoid this problem , Nodes added to the linked list before trying to compete for locks , You need to judge whether the front node is the head node , If it's not the head node , There is no need to trigger lock competition . So this will involve the search of the front node , If it's a one-way list , Then the implementation of this function will be very complex .
This problem , There may be 99% No one can answer . Yes AQS Without deep understanding , May not know how to answer . Understand why a technology is designed like this , The key is what kind of problems it needs to solve .
Last , I sorted all the interview questions I shared in previous periods into 1 Share 10W Document of words , I hope this can improve the passing rate of all fans

I was delayed by programming Tom, Only dry goods without water ! Your support is my biggest motivation ! Pay attention to me , The interview is no longer difficult !
Complete interview materials and answers, as well as PDF file :
Scan the business card below to receive !
↓ ↓ ↓
边栏推荐
- Navicat new database
- Plato Farm在Elephant Swap上铸造的ePLATO是什么?为何具备高溢价?
- 第09章_性能分析工具的使用
- 「PHP基础知识」输出圆周率的近似值
- 百度副总裁李硕:数字技术加持下中国劳动力成本上升是好事
- R language error: compilation failed for package '****‘
- MySQL large table joint query optimization, large transaction optimization, avoiding transaction timeout, lock wait timeout and lock table
- vasp计算任务报错:M_divide:can not subdivide 8 nodes by 6
- 数字图像处理 第10章——图像分割
- Digital image processing Chapter 10 - image segmentation
猜你喜欢

MySQL忘记密码怎么办

Linux下安装MySQL8.0的详细步骤

Engineering boy: under 20 years old, ordinary but not mediocre
![[freeswitch development practice] media bug obtains call voice flow](/img/14/9a359403606c312b30733d4a015fa5.png)
[freeswitch development practice] media bug obtains call voice flow

STC单片机驱动1.8‘TFT SPI屏幕演示示例(含资料包)

Learn more than 4000 words, understand the problem of this pointing in JS, and handwrite to realize call, apply and bind

Wechat's crazy use of glide - life cycle learning

Verilog: blocking assignment and non blocking assignment

mycat读写分离配置

SOA(面向服务架构)是什么?
随机推荐
sqlilabs less-32~less-33
What is SOA (Service Oriented Architecture)?
C语言小项目 -- 通讯录(静态版+动态版+文件版)
Hangao database best practice configuration tool Hg_ BP log collection content
MySQL large table joint query optimization, large transaction optimization, avoiding transaction timeout, lock wait timeout and lock table
C陷阱和缺陷 第3章 语义“陷阱” 3.2 非数组的指针
三子棋(玩家+电脑)
融云实时社区解决方案
mysql大表联合查询优化,大事务优化,规避事务超时,锁等待超时与锁表
Server operation management system
C陷阱与缺陷 第3章 语义“陷阱” 3.8 运算符&&、||和!
12_ UE4 advanced_ Change a more beautiful character model
codeforces每日5题(均1500)-第二十五天
Typescript learning (I)
国产ERP有没有机会击败SAP ?
shell脚本总结
Verilog's time system tasks - $time, $stime, $realtime
cuda-gdb提示:/tmp/tmpxft_***.cudafe1.stub.c: No such file or directory.
Alibaba Sentinel - 工作流程及原理解析
C traps and defects Chapter 3 semantic "traps" 3.4 avoid "couple method"