当前位置:网站首页>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 .
边栏推荐
- 那些易混淆的概念(三):function和class
- 施努卡:机器人视觉抓取工作原理 机器视觉抓取
- P1031 [noip2002 improvement group] average Solitaire
- [second on] [jeecgboot] modify paging parameters
- Multisim--软件相关使用技巧
- 【推薦系統 01】Rechub
- Socket communication principle and Practice
- [daiy5] jz77 print binary tree in zigzag order
- [dai6] mirror image of JZ27 binary tree
- Adb 实用命令(网络包、日志、调优相关)
猜你喜欢
The variables or functions declared in the header file cannot be recognized after importing other people's projects and adding the header file
String formatting
BUUCTF---Reverse---reverse1
1323: [example 6.5] activity selection
1323:【例6.5】活动选择
使用U2-Net深层网络实现——证件照生成程序
Use the fetch statement to obtain the repetition of the last row of cursor data
成为优秀的TS体操高手 之 TS 类型体操前置知识储备
Serial communication relay Modbus communication host computer debugging software tool project development case
Using U2 net deep network to realize -- certificate photo generation program
随机推荐
枪出惊龙,众“锁”周之
Some superficial understanding of word2vec
P2788 math 1 - addition and subtraction
软考一般什么时候出成绩呢?在线蹬?
[dai6] mirror image of JZ27 binary tree
Application of OpenGL gllightfv function and related knowledge of light source
Socket communication principle and Practice
【作业】2022.7.6 写一个自己的cal函数
浅谈日志中的返回格式封装格式处理,异常处理
555 circuit details
【PyTorch 07】 动手学深度学习——chapter_preliminaries/ndarray 习题动手版
Jump to the mobile terminal page or PC terminal page according to the device information
【机器学习 03】拉格朗日乘子法
软考中级,软件设计师考试那些内容,考试大纲什么的?
Talking about the return format in the log, encapsulation format handling, exception handling
【推荐系统 02】DeepFM、YoutubeDNN、DSSM、MMOE
leetcode-303:区域和检索 - 数组不可变
Experience sharing of software designers preparing for exams
深入分析ERC-4907协议的主要内容,思考此协议对NFT市场流动性意义!
Use the fetch statement to obtain the repetition of the last row of cursor data