当前位置:网站首页>What is the principle of synchronized lock escalation in multithreading?
What is the principle of synchronized lock escalation in multithreading?
2022-07-27 02:25:00 【Current affairs reader hjj】
List of articles
In a multithreaded synchronized What is the principle of lock upgrade ?
synchronized Lock upgrade principle
There is a... In the object header of the lock object threadid Field , On the first visit threadid It's empty ,jvm Let it hold biased lock , And will threadid Set as its thread id, It will be judged when entering again threadid Is it with its thread id Agreement , If consistent, you can use this object directly , If it's not consistent , Upgrade biased lock to lightweight lock , Get the lock by spinning the loop a certain number of times , After a certain number of execution , If the object to be used has not been acquired normally , The lock will be upgraded from lightweight to heavyweight , This process constitutes synchronized Lock upgrade .
Purpose of lock upgrade
Lock upgrade is to reduce the performance consumption of locks . stay Java 6 Optimization after synchronized How to implement , We use the way of upgrading biased lock to lightweight lock and then to heavyweight lock , Thus, the performance consumption of the lock is reduced .
Four necessary conditions for deadlock
1) mutual exclusion : A resource can only be accessed by one process at a time .
2) Request and hold : When a process is blocked by a request for resources , Keep the acquired resources .
3) Inalienable : Resources obtained by the process , Before use , No forcible deprivation of .
4) Loop waiting for : A circular waiting resource relationship is formed between several processes .
Understand the cause of deadlock , In particular, there are four necessary conditions for deadlock , It can be avoided as much as possible 、 Prevention and
Deadlock Relieving . therefore , In system design 、 Pay attention to how to keep these four necessary conditions from holding , How to make sure
A reasonable allocation algorithm of fixed resources , Avoid processes permanently occupying system resources . Besides , Also prevent the process from waiting
Occupy resources in case of . therefore , Reasonable planning should be made for the allocation of resources .
About thread context switching
Context switch
In multithreading programming, the number of general threads is greater than CPU The number of cores , And one CPU The core can only be used by one thread at any time , In order for these threads to be executed effectively ,CPU The strategy is to allocate time slices for each thread and rotate them . When one thread runs out of time slices, it will be ready again for other threads to use , This process is a context switch .
Generally speaking, it is : The current task is finished CPU Time slice will save its state before switching to another task , In order to switch this task next time , You can reload the status of this task . The process from save to reload is a context switch .

Because of the limited funds , The purchased ECS is 1 nucleus 2G Of , The core is 1 That is, it can only be used by one thread at any time , Running multiple threads will cause context switching , Therefore, the effect of parallelism cannot be achieved , Running multiple threads with a single core may lead to thread context switching, which may lead to the execution efficiency not as high as that of a single thread , Therefore, it is not recommended to use multithreading in projects .
My learning forum
HandsomeForum: use Java Prepared Learning Forum , Build our own circle !(http://huangjunjie.vip:66)
The article links ( In a multithreaded synchronized What is the principle of lock upgrade ?):http://huangjunjie.vip:66/question/read/nt2pn5mojclyvag1vv
The article links ( About thread context switching ):http://huangjunjie.vip:66/question/read/qs7ho1rcy8jqhapi9m
边栏推荐
- About unsafe problems such as fopen and strError encountered in vs2022 or advanced version running environment
- NAT网络地址转化实验
- Can bus communication application
- Golang - sync包的使用 (WaitGroup, Once, Mutex, RWMutex, Cond, Pool, Map)
- Codeforces Round #809 (Div. 2), problem: (C) Qpwoeirut And The City
- 多点双向重发布和路由策略-拓扑实验
- C language - assignment operator, compound assignment operator, self increasing and self decreasing operator, comma operator, conditional operator, goto statement, comment
- TreeSet集合存储元素的问题
- Republishing and routing strategy of OSPF
- HCIP-第五天-OSPF扩展配置实验
猜你喜欢

【洋哥带你玩转线性表(三)——双向链表】

MGRE, PPP, HDLC comprehensive experiment

Hcip day 1

离开页面的提示

【C语言】阶乘实现

HCIA Basics (1)

Experiment exercise of two-layer packaging technology (HDLC, ppp--pap\chap, GRE)

Nb-iot networking communication
npm报错, Error: EPERM: operation not permitted, mkdir

Codeforces Round #809 (Div. 2), problem: (C) Qpwoeirut And The City
随机推荐
Nb-iot access to cloud platform
Nb-iot networking communication
Hcip OSPF comprehensive experiment
MySQL课程2.表的各种查询
ESP8266Wi-Fi数据通讯
【C语言】阶乘实现
C语言——二维数组、指针
【降维打击,带你深度学习CPU(上)】
Introduction to STM32 lesson 1
Can bus communication application
C language - characters and strings, arithmetic operators, type conversions
ESP8266Wi-Fi接入云平台
Wechat applet: user wechat login process (attached: flow chart + source code)
Codeforces Round #796 (Div. 2), problem: (1688C) Manipulating History
HCIP-第五天-OSPF扩展配置实验
数字芯片的面积优化:第三届“华为杯”研究生创芯大赛数字方向上机题1详解
NB-IOT接入云平台
Full company mGRE and star topology mGRE
oSPF基础实验配置
RS-485总线通信应用