当前位置:网站首页>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 proxy
- Titanic(POJ2361)
- FortiGate firewall configuration link detection link monitor and status query
- Day 11 script and game AI
- Clients accessing the daytime service (TCP)
- FortiGate firewall quick initialization administrator password
- Smart use of bitmap to achieve 100 million level massive data statistics
- 第十二天 进阶编程技术
- 网络层详解
- 工程安全和工程质量
猜你喜欢

You know AI, database and computer system
![[fuzzy neural network prediction] water quality prediction based on fuzzy neural network, including Matlab source code](/img/88/038826ec6d16c8eb04d9ef2e01d47a.png)
[fuzzy neural network prediction] water quality prediction based on fuzzy neural network, including Matlab source code

Configure specific source IP in SLA detection of FortiGate sdwan

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

进程间通信之匿名管道

Redis sentry, persistence, master-slave, hand tear LRU

Explain the underlying principles of JVM garbage collection in simple terms

Technology sharing | broadcast function design in integrated dispatching

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

How to solve the problem of link hyperlinks when trying to link the database?
随机推荐
OneNote production schedule
An error occurs when sqlyog imports the database. Please help solve it!
Idea grey screen problem
Es2019 key summary
Default value of JS parameter
Myrpc version 4
RPC correction based on arcpy API
(03). Net Maui actual combat basic control
Redis cache avalanche, breakdown and penetration
Ora-00907: missing right parenthesis problem supplement
El upload Upload file (Manual upload, Automatic upload, upload progress)
lego_loam 代码阅读与总结
Day 11 script and game AI
Es2016 key summary
SQL追加字段
JS proxy
Splicing strings with custom functions
iMile 利用 Zadig 多云环境周部署千次,跨云跨地域持续交付全球业务
数据链路层详解
Unity when entering a string in the editor, escape the input of characters