当前位置:网站首页>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
边栏推荐
猜你喜欢

How the FortiGate firewall rejects a port by using the local in policy policy

Basic knowledge of redis

How to use FME to create your own functional software

Named pipes for interprocess communication

With the deep integration of cloud platform, the "Xueba" objectscale in the object storage industry is coming

JS inheritance

技术分享| 融合调度中的广播功能设计

两个月拿到N个offer,什么难搞的面试官在我这里都不算事

Robot slam navigation core technology and practice Season 1: Chapter 0_ Slam development overview

lego_ Reading and summary of loam code
随机推荐
JS file block to Base64 text
Es2019 key summary
MySQL DDL change
[cloud native] AI cloud development platform - Introduction to AI model foundry (developers can experience AI training model for free)
FortiGate creates multiple corresponding DDNS dynamic domain names for multiple ADSL interfaces
Explain the underlying principles of JVM garbage collection in simple terms
errno和perror
Quick sort & merge sort
Unity when entering a string in the editor, escape the input of characters
01 backpack, dynamic planning
Memorize unfamiliar words at SSM stage and update them from time to time
Splicing strings with custom functions
. Net 7 JWT configuration is too convenient!
DBT product initial experience
Technology sharing | broadcast function design in integrated dispatching
Myrpc version 6
Grasp grpc communication framework in simple terms
进程间通信之匿名管道
管道实现进程间通信之命名管道
When easycvr deploys a server cluster, what is the reason why one is online and the other is offline?