当前位置:网站首页>The gun startles the dragon, and the crowd "locks" Zhou Zhi
The gun startles the dragon, and the crowd "locks" Zhou Zhi
2022-07-07 10:37:00 【Solitary alum】
The gun startles the dragon , many “ lock ” Zhou Zhi
1. Why lock it ? What is the meaning of lock ?
1 Multi threaded access to shared data , Secure data
2 It will reduce the concurrency , But it can guarantee the safety of data
problem : Secsha was originally a high concurrency scenario , You locked it , Will it reduce efficiency ? To reduce the , What's the meaning of locking ?
2. Classification of locks
2.1 Fair and non-fair locks
Fair lock : seeing the name of a thing one thinks of its function , He is fair , Follow the order of first come, first come ; For example, seckill
Not fair lock : The order in which each thread acquires the lock , It is inconsistent with the order in which they apply , It is possible to catch up , Later threads , Instead, get the lock first ;
2.2 Lightweight lock and heavyweight lock
Lightweight lock : It can be handled inside the program
Heavyweight lock : The program cannot handle , It is managed by the operating system
2.3 Exclusive locks and shared locks / Read lock and write lock
- Exclusive lock , Also known as exclusive lock , Or write locks
- Concept : The lock can only be held by one thread at a time .
- JavaSE: Yes ReentrantLock and Synchronized It's all exclusive locks .
- MySql: Usage mode :SELECT * FROM table_name WHERE … FOR UPDATE, Use scenarios : Concurrent operation of commodity inventory
- Shared lock , Also known as read lock
- Concept : The lock can be held by multiple threads ;
- JavaSE: Yes ReentrantReadWriteLock, The read lock is a shared lock , Its write lock is exclusive . The shared lock of read lock can ensure that concurrent read is very efficient .
- MySql: Usage mode :SELECT * FROM table_name WHERE … LOCK IN SHARE MODE; Use scenarios : It's business A Use shared lock Got a ( Or some ) When recording , Business B
You can read these records , You can continue to add shared locks , However, these records cannot be modified or deleted
2.4 Bias lock and spin lock
Biased locking : When locking , The flag bit of the lock directly records the thread that is locked PID; The deflection lock will not release automatically , Next time when this PID When the thread continues to use this resource , There's no need to fight , Get the lock directly
spinlocks : Lightweight locks wait for resources by spinning
2.5 Class locks and object locks
Kind of lock : only one , The kind of lock is Class object
Object lock : Countless , Object locks are instance objects
2.6 Watch lock and row lock
Mainly refers to the database
Table locks : It's the whole watch that's locked
Row lock : This row of data is locked
2.7 Distributed lock -Redission Distributed lock
Why use lua Language
Because of a lot of complex business logic , Can be encapsulated in lua Sent to redis, Ensure the atomicity of the execution of this complex business logicWhy set the expiration time of the lock ?
Redis Distributed lock , Once the client does not release the lock , The server will always hold this lock , Threads in other processes cannot acquire locks , Deadlock occurs .
For example, the following two cases :
1. The network jitter process A A thread in gets the lock , And then execute finally When releasing the lock code in , From program to Redis Your network is not good , So releasing the lock failed . At this time, for redis On the server side , It doesn't know that the client has tried to release the lock , It will keep the lock to A, In this way , Threads of other processes can no longer acquire this lock .
If you set the expiration time , Even if the network of client and server is disconnected , The server is still calculating the time , When it's time, just release the lock , When the network is connected , It does not affect the acquisition of locks .
2. Server downtime process A Got the lock ,Redis Server down , So the lock didn't release . wait until Redis When we recover again ,Redis The server will also keep this lock to A, It will lock up .
If the expiration time is set , The countdown will continue after the server is restored , When the time comes, the server automatically releases the lock .What is the watchdog strategy ?
1、 client 1 The default lifetime for locking is 30 second , If you exceed 30 second , client 1 I want to keep the lock , What shall I do? ?
Redisson In the client 1 Once the lock is applied successfully , Will start a watch
dog watchdog , He's a background thread , Every time 10 Second check , If the client 1 And hold the lock key, Then it will keep extending the lock key Survival time .
2、 If you are responsible for storing this distributed lock Redission After node downtime , And when the lock is in the locked state , This lock will be locked , To avoid this happening ,Redisson Provides a watchdog to monitor the lock , Its function is to Redisson Before the instance is closed , Keep extending the validity of lock . By default , The watchdog's renewal time is 30s, You can also modify Config.lockWatchdogTimeout To specify otherwise .
2.8 synchronized The mechanism of lock escalation / The process
- jdk1.5 before ,synchronized It's the heavyweight lock , Once multithreads compete for resources , Directly to the operating system , But it's inefficient
- jdk1.5 in the future ,synchronized Lock upgrade process :
- unlocked : When it is not locked by the thread , This is an ordinary object
- Biased locking : When locking , The flag bit of the lock directly records the thread that is locked PID, The deflection lock will not release automatically , Next time when this PID When the thread continues to use this resource , There's no need to fight , Get the lock directly
- Lightweight lock : Multiple threads compete for the same lock , The lightweight lock will enter the spin state , This is also called spin lock
- Heavyweight lock : When the lightweight lock defaults to spin 10 Time , Upgrade to heavyweight lock
2.9 synchronized Is it a fair lock or an unfair lock ? Is it a lightweight lock or a heavyweight lock ?
Not fair lock stay jdk1.5 It's a heavyweight lock Leave it to the operating system to manage
stay jdk1.6 Then it becomes a lightweight lock Go inside the program No, switch to the operating system It's a reentrant lock
2.10 lock(ReententLock) Is the lock fair or unfair ? Is it a lightweight lock or a heavyweight lock ?
lock It can be a fair lock , It can also be an unfair lock , Mainly by setting fair Variable ,fair=true, Set fair lock ,fair=false, Set unfair lock
lock It's a lightweight lock
2. 11 synchronize principle
By decompiling , We see that monitor_enter( Monitor entry ) and monitor_exit( Monitor outlet ), The thread is getting monitor( The monitor ) front , Will check first monitor Whether the entry number of is 0, If it is 0 Get lock , Number of entries +1, Thread becomes monitor The owner of the , If other threads want to enter , It's going to be blocked , At this time, if you have monitor If the thread of enters again , The number of entries will continue +1, So ,monitor It can be entered repeatedly .monitor_exit Only monitor Only the owner can call , Call once ,monitor The number of entries -1, When montior=0 When , Release the lock .
边栏推荐
- 小程序跳转H5,配置业务域名经验教程
- JMeter about setting thread group and time
- Basic introduction of yarn and job submission process
- Remote meter reading, switching on and off operation command
- 【推荐系统 01】Rechub
- leetcode-304:二维区域和检索 - 矩阵不可变
- 无法打开内核设备“\\.\VMCIDev\VMX”: 操作成功完成。是否在安装 VMware Workstation 后重新引导? 模块“DevicePowerOn”启动失败。 未能启动虚拟机。
- gym安装踩坑记录
- Pre knowledge reserve of TS type gymnastics to become an excellent TS gymnastics master
- Some superficial understanding of word2vec
猜你喜欢
MySQL insert data create trigger fill UUID field value
Leetcode-560: subarray with sum K
leetcode-304:二维区域和检索 - 矩阵不可变
求方程ax^2+bx+c=0的根(C语言)
Mendeley--免费的文献管理工具,给论文自动插入参考文献
Find the greatest common divisor and the least common multiple (C language)
Prototype and prototype chain
2022年上半年5月网络工程师试题及答案
ArrayList thread insecurity and Solutions
【推荐系统 01】Rechub
随机推荐
Some properties of leetcode139 Yang Hui triangle
枪出惊龙,众“锁”周之
Five simple and practical daily development functions of chrome are explained in detail. Unlock quickly to improve your efficiency!
JMeter loop controller and CSV data file settings are used together
JS实现链式调用
Using U2 net deep network to realize -- certificate photo generation program
How much review time does it usually take to take the intermediate soft exam?
Application of OpenGL gllightfv function and related knowledge of light source
软考信息处理技术员有哪些备考资料与方法?
When do you usually get grades in the soft exam? Online pedaling?
浅谈日志中的返回格式封装格式处理,异常处理
基于HPC场景的集群任务调度系统LSF/SGE/Slurm/PBS
[recommendation system 01] rechub
字符串格式化
1321:【例6.3】删数问题(Noip1994)
IIC基本知识
[detailed explanation of Huawei machine test] tall and short people queue up
Basic introduction of yarn and job submission process
Study summary of postgraduate entrance examination in October
Trajectory planning for multi robot systems: methods and Applications Overview reading notes