当前位置:网站首页>AQS learning notes
AQS learning notes
2022-06-22 08:26:00 【ambition_ forever】
1、 The first day
ReentrantLock
AQS
Program = thought + Code
thought = Solutions to problems
Banks do business
1、 It depends on how many people handle it , Just go to the counter , Line up more often
2、 There is waiting in the queue
3、 If the president's brother-in-law comes to do business , There's definitely no need to queue up , Go straight to business .
Unfair
4、 It's too late , Rearrange
if (failed)
cancelAcquire(node);
5、 A simple operation
A&&B
A||B
6、 Six operations
Grab the lock
1、 Look at the lock flag state = 0 By default, no thread holds the lock state = 1 A thread holds a lock
2、state = 0 No thread holds the lock , Locking requires the thread to modify the lock flag bit , It needs a mechanism that only one thread can modify the success lock flag at the same time =CAS
3、state >= 1 The wired stack has a lock , Is the current thread that grabs the lock the thread that owns the lock .
yes , Reentrant state+1
No , Lock snatch failed
4、 Optimize : See if there is anyone in the waiting area , If someone , The lock must be occupied .
If it is judged that someone is waiting hasQueuedPredecessors
Fair lock 、 There are differences in unfair locks .
The only difference between fair locks and unfair locks is that there is a critical area ,
The thread that previously held the lock has executed the external task , Lock released , At this time, a thread happens to come ( Not fair lock ).
It has the meaning of existence in reality .
Fair lock
if (c == 0) {
if (!hasQueuedPredecessors() &&
compareAndSetState(0, acquires)) {
setExclusiveOwnerThread(current);
return true;
}
Not fair lock
if (c == 0) {
if (compareAndSetState(0, acquires)) {
setExclusiveOwnerThread(current);
return true;
}
}
Node node
CLH Algorithm , One way queue
AQS The bidirectional queue
Release the lock
1、 Reentry problem
2、 The lock status bit returns to 0
3、 Wake up the waiting thread in the queue
The team
If you don't grab the lock , And not reentrant , Then you need to join the team .
!tryAcquire(arg) Locking failed
acquireQueued(addWaiter(Node.EXCLUSIVE), arg) Need to be queued , Turn the present into Node, Then set the alarm clock to block .
public final void acquire(int arg) {
if (!tryAcquire(arg) &&
acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
selfInterrupt();
}
set alarm waitStatus
AQS queue
The first node in the queue , The node corresponding to the thread that holds the lock .
Queue entry , There are no nodes in the queue ,
There is no such thing as ,AQS The queue generates two nodes
Possessive ,AQS Tail insertion .
Outgoing queue
Out of the team
The task of leaving the queue is left to the awakened thread .
Blocking
After entering the queue , What do you do
The second node in the queue grabs the lock again , Because the first node is the node that holds the lock . Try to see before blocking .
Set the alarm clock
Blocking
Spin twice
Of a precursor node waitStatus It is amended as follows -1, then park 了 .
Wake up the
2、 the second day
To be continued ....
边栏推荐
- C语言实现往MySQL插入和读取图片
- Mt4-mql4 language EA automatic transaction programming introduction to proficiency
- Example of QT qtableview
- Mt4/mql4 getting started to mastering EA tutorial lesson 3 - common functions of MQL language (III) - common functions of K-line value taking
- How to create an index
- Submit values of various inputs of the form
- 0 basic self-study STM32 (wildfire) -- what is a register?
- Introduction to bee's main functions and features
- DTD constraints
- Android kotlin Camera2预览功能实现
猜你喜欢

How to troubleshoot OOM

swagger中的枚举、自定义类型和swaggerignore

Some mathematical operation functions in LabVIEW

What actions might cause thread context switching?

The challenge of image based voice processing in real-time audio and video -- RTC dev Meetup

Mt4/mql4 getting started to mastering EA tutorial lesson 8 - common functions of MQL language (VIII) - common time function

Analyzing the role of cognitive theory in maker teacher training

Chapter VIII web project testing (the end of this chapter)

The solution to the problem of the first screen picture loading flicker

Prompt installremove of the Service denied when installing MySQL service
随机推荐
On Fresnel phenomenon
Mysq index optimization and how to avoid deadlock
Interpreting the technology group in maker Education
Type of sub database and sub table
Mt4/mql4 getting started to mastering EA tutorial lesson 3 - common functions of MQL language (III) - common functions of K-line value taking
复杂科学在创客教学研究中的应用
C language implements inserting and reading pictures into MySQL
DTD约束
Matrix operation
Mysql+orcle (SQL implements recursive query of all data of child nodes)
开展有效的创客教育课程与活动
Postgresql源码(56)可扩展类型分析ExpandedObject/ExpandedRecord
Chapter VIII web project testing (the end of this chapter)
I spring and autumn web Penetration Test Engineer (elementary) learning notes (Chapter 1)
One hot and embedding
Bee read write separation Usage Summary
The necessity of steam education culture inheritance
中断中为何不能使用信号量,中断上下文为何不能睡眠
QT error prompt 1:invalid use of incomplete type '***‘
Mt4/mql4 getting started to proficient in foreign exchange EA automatic trading tutorial - common functions of MQL language