当前位置:网站首页>JUC concurrent programming 07 -- is fair lock really fair (source code analysis)
JUC concurrent programming 07 -- is fair lock really fair (source code analysis)
2022-04-23 10:04:00 【Half old 518】
Let's review the of fair lock tryAcquire
Code .
protected final boolean tryAcquire(int acquires) {
final Thread current = Thread.currentThread();
int c = getState();
if (c == 0) {
if (!hasQueuedPredecessors() && // Note that there , At the beginning, it will check whether there are nodes waiting
compareAndSetState(0, acquires)) {
setExclusiveOwnerThread(current);
return true;
}
}
else if (current == getExclusiveOwnerThread()) {
int nextc = c + acquires;
if (nextc < 0)
throw new Error("Maximum lock count exceeded");
setState(nextc);
return true;
}
return false;
}
}
Focus on the comment code , If hasQueuedPredecessors
What happens if there is a miscarriage of justice ? Is fair lock unfair . Let's study hasQueuedPredecessors
, Is there really a secret situation .
If a thread 1 Already holding the lock . This is the time 2 To get the lock , Go to the hasQueuedPredecessors()
return false, Then go back CAS
, The execution must have failed , Because now the lock is locked by the thread 1 occupy , return aquire
Method .
public final void acquire(int arg) {
if (!tryAcquire(arg) &&
acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
selfInterrupt();
}
Then walk to addWaiter
Of enq
Method . Threads 2 Enter the waiting queue .
private Node enq(final Node node) {
for (;;) {
Node t = tail;
if (t == null) {
// At this time, etc head、tail It's empty , Satisfy `t==null` Conditions .
if (compareAndSetHead(new Node())) // There are no other threads competing here , success
tail = head; // Set up the success
} else {
node.prev = t;
if (compareAndSetTail(t, node)) {
t.next = node;
return t;
}
}
}
}
Assuming that thread 2 Go to the end of the note , Now there is a thread 3 Come grab the lock . When it judges the waiting queue hasQueuedPredecessors
Returns the false, because h==t
.
public final boolean hasQueuedPredecessors() {
Node t = tail;
Node h = head;
Node s;
return h != t && // Not meeting the conditions
((s = h.next) == null || s.thread != Thread.currentThread());
}
So here comes the question , In fact, there should be threads in the waiting queue 2, Now we have come to the judgment that the waiting queue is empty , Threads 3 No, just go CAS
Do you , In case the thread 1 Just released the lock , Threads 3 Just cut in line . thus it can be seen , Fair lock is not fair .
protected final boolean tryAcquire(int acquires) {
final Thread current = Thread.currentThread();
int c = getState();
if (c == 0) {
if (!hasQueuedPredecessors() &&
compareAndSetState(0, acquires)) {
setExclusiveOwnerThread(current);
return true;
}
}
else if (current == getExclusiveOwnerThread()) {
int nextc = c + acquires;
if (nextc < 0)
throw new Error("Maximum lock count exceeded");
setState(nextc);
return true;
}
return false;
}
}
Use a chart to summarize the above process .
The key to fairness and unfairness lies in hasQueuedPredecessors
Whether there will be misjudgment .
版权声明
本文为[Half old 518]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230955171100.html
边栏推荐
- php 二维数组指定元素相等后相加否则新增
- [codeforces - 208e] blood cousins
- Zhengda international explains what the Dow Jones industrial index is?
- 杰理之有时候定位到对应地址的函数不准确怎么办?【篇】
- 1D / 1D dynamic programming learning summary
- 深度选择器
- CSP certification 202203-2 travel plan (multiple solutions)
- 防疫登记小程序
- MapReduce压缩
- 第一章 Oracle Database In-Memory 相关概念(续)(IM-1.2)
猜你喜欢
Yarn核心参数配置
2022年制冷与空调设备运行操作考试练习题及模拟考试
MapReduce计算流程详解
Construire neuf capacités de fabrication agile à l'ère métacosmique
论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》——3背景
The central control learning infrared remote control module supports network and serial port control
Exercise questions and simulation test of refrigeration and air conditioning equipment operation test in 2022
Comparative analysis of meta universe from the dimension of knowledge dissemination
深度选择器
Redis design and Implementation
随机推荐
杰理之通常影响CPU性能测试结果的因素有:【篇】
正大国际讲解道琼斯工业指数到底是什么?
Epidemic prevention registration applet
SQL调优系列文章之—SQL性能方法论
Go语言实践模式 - 函数选项模式(Functional Options Pattern)
Classic routine: DP problem of a kind of string counting
Mobius inversion
[COCI] Vje š TICA (subset DP)
Yarn资源调度器
SQL调优系列文章之—SQL调优简介
[CF 1425d] danger of mad snakes
A concise course of fast Fourier transform FFT
DBA common SQL statements (3) - cache, undo, index and wait events
Less than 100 secrets about prime numbers
杰理之AES能256bit吗【篇】
Prefix sum of integral function -- Du Jiao sieve
杰理之有时候定位到对应地址的函数不准确怎么办?【篇】
杰理之更准确地确定异常地址【篇】
中控学习型红外遥控模块支持网络和串口控制
Number theory blocking (integer division blocking)