当前位置:网站首页>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

 Insert picture description here

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

 Insert picture description here

  • 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 logic

  • Why 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

 Insert picture description here

  1. jdk1.5 before ,synchronized It's the heavyweight lock , Once multithreads compete for resources , Directly to the operating system , But it's inefficient
  2. 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

 Insert picture description here

2. 11 synchronize principle

 Insert picture description here
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 .

原网站

版权声明
本文为[Solitary alum]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070828542751.html