当前位置:网站首页>Fair lock and unfair lock
Fair lock and unfair lock
2022-06-30 04:18:00 【No bug program yuan】
Fair lock : It means that multiple threads acquire locks according to the order in which they apply for locks , It's like waiting in line for a meal , first come , first served .
Not fair lock : The order in which multiple threads acquire locks is not the order in which they apply for locks , It is possible that the thread that applies later acquires the lock prior to the thread that applies first , In the case of high concurrency , It may cause priority inversion or blocking .
Fair lock / Not fair lock
Under contract ReentrantLock The creation of can specify the constructor of boolean Type to get a fair lock or an unfair lock , Default is unfair lock
Look look=new ReentrantLock();
//NonfairSync Just an unfair lock
public ReentrantLock() {
sync = new NonfairSync();
}
//fair Create a fair lock for true , by false Create an unfair lock
public ReentrantLock(boolean fair) {
sync = fair ? new FairSync() : new NonfairSync();
}
On the difference between the two :
Fair lock : Threads acquire a fair lock in the order in which they requested it
Fair lock , It's just fair , In a concurrent environment , Each thread will first check the waiting queue maintained by the lock when acquiring the lock , If it is empty , Or the current thread is the first one in the waiting queue , Take possession of the lock , Otherwise it will join the waiting queue , In the future, I will follow FIFO The rules of get themselves from the queue .
Not fair lock : a nonfair lock permits barging: threads requesting a lock can jump ahead of the queue of waiting threads if the lockhappens to be available when it is requested.
Not fair lock It's rude , Come up and try to own the lock , If the attempt fails , Just like fair lock . ( Grab the lock when you come up . If you can't get it, just queue up like a fair lock , First, first , If you can't get it, you'll get it first )
Not fair lock The advantages of , Throughput is greater than fair lock
Reentrant lock ( Also called recursive locking )
It refers to the lock obtained by the outer function of the same thread ﹐ The inner recursive function can still get the code of the lock , When the same thread acquires the lock in the outer method , When entering the inner layer, the method will automatically acquire the lock .
That is said , A thread can enter any block of code in which it already has a lock .
ReentrantLock/synchronized It's a typical reentry lock
The most important function of reentry lock is to avoid deadlock
lock() and unlock() As long as the pairing can work properly
边栏推荐
- JS reflect
- Es2019 key summary
- Day 12 advanced programming techniques
- 基于ROS的SLAM建图、自动导航、避障(冰达机器人)
- iMile 利用 Zadig 多云环境周部署千次,跨云跨地域持续交付全球业务
- . Net 7 JWT configuration is too convenient!
- Configure specific source IP in SLA detection of FortiGate sdwan
- Grasp grpc communication framework in simple terms
- How to analyze and solve the problem of easycvr kernel port error through process startup?
- Error in conditional filter (if) syntax in sum function in SQL Server2005
猜你喜欢

OneNote production schedule

lego_loam 代码阅读与总结

Myrpc version 3

Day 11 script and game AI

When easycvr deploys a server cluster, what is the reason why one is online and the other is offline?

DBT product initial experience

How to solve the problem of link hyperlinks when trying to link the database?

Sql语句遇到的错误,求解

Configure specific source IP in SLA detection of FortiGate sdwan

dotnet-exec 0.5.0 released
随机推荐
Daily summary of code knowledge
FortiGate configures multiple server IPS as link monitor monitoring objects on the same interface
Troubleshoot abnormal video playback problems in public network deployment based on Haikang ehomedemo tool
2021-07-14
第九天 脚本與資源管理
Maya Calendar(POJ1008)
[cloud native] AI cloud development platform - Introduction to AI model foundry (developers can experience AI training model for free)
Redis sentry, persistence, master-slave, hand tear LRU
AI落地的新范式,就“藏”在下一场软件基础设施的重大升级里
Es2016 key summary
Qt6 QML Book/Qt Quick 3D/Qt Quick 3D
SQL server2005中SUM函数中条件筛选(IF)语法报错
Basic knowledge of redis
FortiGate firewall filters the specified session and cleans it up
Knowledge - how to build rapport in sales with 3 simple skills
《机器人SLAM导航核心技术与实战》第1季:第0章_SLAM发展综述
487-3279(POJ1002)
FortiGate firewall quick initialization administrator password
Configure specific source IP in SLA detection of FortiGate sdwan
网络层详解