当前位置:网站首页>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
边栏推荐
- Es2018 key summary
- Unity échappe à l'entrée de caractères lors de l'entrée de chaînes dans l'éditeur
- Quick sort & merge sort
- Myrpc version 3
- Knowledge - how to build rapport in sales with 3 simple skills
- Day 10 data saving and loading
- 第十一天 脚本与游戏AI
- Modifier of JS regular expression
- Blue Bridge Cup: magic cube rotation [Vocational group]
- Green new power and "zero" burden of computing power -- JASMINER X4 series is popular
猜你喜欢

Slam mapping, automatic navigation and obstacle avoidance based on ROS (bingda robot)

Named pipes for interprocess communication
![[Thesis reading | deep reading] role2vec:role based graph embeddings](/img/69/c94700fbbbda20df4e54803c703b48.png)
[Thesis reading | deep reading] role2vec:role based graph embeddings

The new paradigm of AI landing is "hidden" in the next major upgrade of software infrastructure

Implementation of aut, a self-developed transport layer protocol for sound network -- dev for dev column

Myrpc version 4

Smart use of bitmap to achieve 100 million level massive data statistics

Solve the problem of Navicat connecting to the database

FortiGate firewall configuration log uploading regularly

Myrpc version 1
随机推荐
[fuzzy neural network prediction] water quality prediction based on fuzzy neural network, including Matlab source code
. Net 7 JWT configuration is too convenient!
Qt Creator 8 Beta2发布
487-3279(POJ1002)
7-3 打怪升级 单源最短路
Detailed explanation of data link layer
lego_loam 代码阅读与总结
技术分享| 融合调度中的广播功能设计
iMile 利用 Zadig 多云环境周部署千次,跨云跨地域持续交付全球业务
Titanic(POJ2361)
JS file block to Base64 text
I get n offers in two months. I don't have any difficult interviewers here
Use ideal to connect to the database. The results show some warnings. How to deal with this part
Ora-00907: missing right parenthesis problem supplement
Grasp grpc communication framework in simple terms
[cloud native] AI cloud development platform - Introduction to AI model foundry (developers can experience AI training model for free)
Share an example of a simple MapReduce method using a virtual machine
Implementation steps of dynamic proxy
JS generator
Error in conditional filter (if) syntax in sum function in SQL Server2005