当前位置:网站首页>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
边栏推荐
- 杰理之通常影响CPU性能测试结果的因素有:【篇】
- 101. Symmetric Tree
- Odoo server setup notes
- Prefix sum of integral function -- Du Jiao sieve
- Chapter I Oracle database in memory related concepts (Continued) (im-1.2)
- Chapter 3 enable and adjust the size of IM column storage (im-3.1)
- 杰理之更准确地确定异常地址【篇】
- 2022 mobile crane driver test question bank simulation test platform operation
- DBA常用SQL语句(6)- 日常管理
- Windows安装redis并将redis设置成服务开机自启
猜你喜欢
随机推荐
杰理之栈溢出 stackoverflow 怎么办?【篇】
通过流式数据集成实现数据价值(5)- 流分析
Sim Api User Guide(5)
Rain produces hundreds of valleys, and all things grow
ansible playbook语法和格式 自动化云计算
Operation of 2022 tea artist (primary) test question simulation test platform
【无标题】
failureForwardUrl与failureUrl
Nvidia最新三维重建技术Instant-ngp初探
理解作用域
Chapter 2 Oracle database in memory architecture (I) (im-2.1)
Common DBA SQL statements (4) - Top SQL
构建元宇宙时代敏捷制造的九种能力
Yarn资源调度器
Educational Codeforces Round 81 (Rated for Div. 2)
構建元宇宙時代敏捷制造的九種能力
2022茶艺师(初级)考试试题模拟考试平台操作
[CF 1425d] danger of mad snakes
[untitled]
[COCI] Vje š TICA (subset DP)