当前位置:网站首页>Three queues CXQ, entrylist, waitset personal understanding analysis
Three queues CXQ, entrylist, waitset personal understanding analysis
2022-06-09 05:41:00 【Cat cat God】
Catalog
1.synchronized,wait(),notify() Methods, and so on ?
2.synchronized When relocking , How about the lock snatching process ?
5. General figure ( Personal painting )
1.synchronized,wait(),notify() Methods, and so on ?
In fact, it is manipulation cxq,entrylist,waitset These three queues are realized . So it's necessary to sort it out
entrylist yes Double linked list
2.synchronized When relocking , How about the lock snatching process ?
As we all know, a heavyweight lock means that each thread passes through cas To modify ObjectMonitor in ower Field , The successful modification means that the lock has been snatched , If you fail, you will cas Join in cxq in ,cxq It's a stack structure .

3.wait() Method will cause the current thread to go to waitset In line ,waitset yes Circular double linked list
4.notify() The method is actually moving waitset The threads in either go to cxq Either entrylist in , The wake-up mechanism will be triggered when the synchronization method ends , according to Qmode Different types wake up with different rules .
5. General figure ( Personal painting )

notif() according to Policy Do different things
Policy==0 : Put in entrylist The head of the queue
Policy==1 : Put in entrylist At the end of the queue
Policy==2 : Judge entrylist Is it empty , If it is empty, put entrylist in , Otherwise in the cxq Queue head position ( Default
Strategy )
Policy==3 : Judge cxq Is it empty , If it is empty , Put it directly into the head , Otherwise in the cxq At the end of the queue
When the synchronized code block is executed , What does wakeup look like ? according to Qmode Make a selection
according to QMode Policy wakes up :
QMode=2, take cxq The head node wakes up directly
QMode=3, If cxq Non empty , hold cxq The queue is placed in entrylist Tail of ( Sequential heel cxq Agreement )
QMode=4, If cxq Non empty , hold cxq The queue is placed in entrylist The head of ( Sequential heel cxq contrary )
QMode=0, Do nothing , Keep going down (QMode The default is 0) The default is 0
Qmode=0 The logic of judgment is to judge first entrylist Is it empty , If it's not empty , Then take out the first wake-up , Such as
If the result is empty, then start from cxq Get the first wakeup inside
6. Write code to verify
package com.imooc.controller.jdk;
import java.util.concurrent.TimeUnit;
/**
* @description:
* @author: yk
* @time: 2022/5/19 15:11
*/
public class synchonizedTest {
private static final Object object=new Object();
public static void main(String[] args) throws InterruptedException {
synchonizedTest synchonizedTest=new synchonizedTest();
synchonizedTest.methoda();
}
private void methoda() throws InterruptedException {
new Thread(()->{
synchronized (object){
System.out.println(Thread.currentThread().getName()+"start");
try {
methodb();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"end");
}
},"A").start();
}
private void methodb() throws InterruptedException {
new Thread(()->{
synchronized (object) {
System.out.println(Thread.currentThread().getName()+"start");
methodc();
try {
TimeUnit.MICROSECONDS.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
object.notify();
System.out.println(Thread.currentThread().getName()+"end");
}
},"B").start();
}
private void methodc(){
new Thread(()->{
System.out.println(Thread.currentThread().getName()+"start");
synchronized (object) {
System.out.println(Thread.currentThread().getName()+"end");
}
},"C").start();
}
}

A end Forever Cend front , in other words A Forever than C Get the lock first
a start-up b after wait,b start-up c after ,b Sleep 200ms( here c We have reached the entrance of the lock ), then b Wake up the a
here a,c Grab the lock together
result :a Forever c Grab the lock first
Personal summary : You can see that they are all by default jdk, To wake up a thread is to wake it up first waitset Thread in Wake up again cxq Medium
边栏推荐
- debian11安装nfs server后固定端口号以便设置防火墙
- Analysis of semaphore source code of AQS
- 使用MAT进行内存问题定位
- Linked list
- Connecting pyqt5 and SQL Server Databases
- Palindrome linked list leetcode
- Esmascript 6.0 advanced
- Embedded audio and video solutions webrtc vs metartc
- MySQL add field or create table SQL statement
- Number that appears only once -leetcode
猜你喜欢

Embedded audio and video solutions webrtc vs metartc

输入两个正整数m和n,求其最大公约数和最小公倍数。

Deque of STL

Wamp environment setup (apache+mysql+php)

IP address division and subnet

Heqibao's trip to Chongqing ~

Stack

Alibaba cloud AI training camp - SQL basics 4: set operation - addition and subtraction of tables, join, etc

Yolov5-6.0系列 | yolov5的模型网络构建

Swagger basic use quick start
随机推荐
线程 interrupted 详细解析
Source code analysis of cyclicbarrier in AQS
Detailed explanation of groupby function
array
DTD的概念及编写
Alibaba cloud AI training camp - machine learning 2:xgboost
SSL證書包含了哪些信息?
Alibaba cloud AI training camp -sql basics 5: window functions, etc
【IT】福昕pdf保持工具選擇
Tricks | [trick6] learning rate adjustment strategy of yolov5 (one cycle policy, cosine annealing, etc.)
MRNA factory| quantitative detection of LNP encapsulated RNA content by ribogreen
Wechat applet wx Getlocation location error information summary
Alibaba cloud AI training camp - SQL basics 3: complex query methods - views, subqueries, functions, etc
Yolov5-6.0系列 | yolov5的模型网络构建
Source code analysis of reentrantreadwritelock of AQS
Add failed when BigDecimal is 0.00
Ecmascript6.0 Basics
redis 分布式锁的几种实现方式
Common interview questions
[C language] a quick pass operator