当前位置:网站首页>Lock lock
Lock lock
2022-04-23 16:57:00 【Wooden horse】
First look at the official documents :
Implementation classes have reentrant locks 、 Read lock and write lock , Reentrant locks are most commonly used .
Reentrant lock , To refer to Threads In units of , When a thread acquires an object lock , This thread can get the lock on this object again , Other threads are not allowed .
You can see ,ReentrantLock yes lock An implementation class of the interface , It implements reentrant lock, fair lock and unfair lock .
Fair lock : Very fair , first come , first served .
Not fair lock : It's very unfair , You can jump in line .
The default implementation is unfair lock .
Its whole logic is : Lock , stay try Write business code in , stay finally Inside unlock .
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/** * @author DB * @title: Test02 * @projectName Juc * @description: TODO * @date 2022/4/21 10:34 */
public class Test02 {
public static void main(String[] args) {
Ticket2 ticket2 = new Ticket2();
new Thread(() -> {
for (int i = 0; i < 40; i ++)ticket2.sale();}, "A").start();
new Thread(() -> {
for (int i = 0; i < 40; i ++)ticket2.sale();}, "B").start();
new Thread(() -> {
for (int i = 0; i < 40; i ++)ticket2.sale();}, "C").start();
}
}
class Ticket2 {
// attribute
private int number = 30;
Lock lock = new ReentrantLock();
// Method
public synchronized void sale(){
// Lock
lock.lock();
try {
// Write business code
if (number > 0){
System.out.println(Thread.currentThread().getName() + " Sold " + (number --) + " ticket , The remaining " + number);
}
}catch (Exception e){
e.printStackTrace();
}finally {
// Unlock
lock.unlock();
}
}
}
result :
Synchronized and Lock The difference between ?
(1)Synchronized yes Java Built in keywords for ;Lock It's a Java class .
(2)Synchronized Unable to determine the status of the acquired lock ;Lock It can be judged whether the lock is obtained .
(3)Synchronized Will automatically release the lock ;lock Lock must be released manually , If you don't release -> Deadlock .
(4)Synchronized Threads 1( Gets the lock , Blocking ), Threads 2( wait for , Silly wait );lock The lock doesn't have to wait .
(5)Synchronized Reentrant lock , It can't be interrupted , Not fair lock ;Lock lock , Reentrant lock , Can be judged , You can set whether it is fair or unfair .
(6)Synchronized Suitable for locking a small number of code synchronization problems ;Lock Suitable for locking a large number of synchronization code .
版权声明
本文为[Wooden horse]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231653336593.html
边栏推荐
- Detailed explanation of UWA pipeline function | visual configuration automatic test
- MySql主从复制
- File upload and download of robot framework
- Easyexcel reads the geographical location data in the excel table and sorts them according to Chinese pinyin
- Detailed explanation of the penetration of network security in the shooting range
- Node access to Alipay open platform sandbox to achieve payment function
- On the security of key passing and digital signature
- Deeply understand the relevant knowledge of 3D model (modeling, material mapping, UV, normal), and the difference between displacement mapping, bump mapping and normal mapping
- Use case execution of robot framework
- 昆腾全双工数字无线收发芯片KT1605/KT1606/KT1607/KT1608适用对讲机方案
猜你喜欢
RTKLIB 2.4.3源码笔记
Path environment variable
Detailed explanation of file operation (2)
On the security of key passing and digital signature
[pimf] openharmony paper Club - what is the experience of wandering in ACM survey
Quick install mongodb
Lock锁
org. apache. parquet. schema. InvalidSchemaException: A group type can not be empty. Parquet does not su
The new MySQL table has a self increasing ID of 20 bits. The reason is
TypeError: set_figure_params() got an unexpected keyword argument ‘figsize‘
随机推荐
Do you really understand the principle of code scanning login?
Detailed explanation of file operation (2)
Interface document yaml
Encapsulating the logging module
About background image gradient()!
织梦DEDECMS安全设置指南
Blue Bridge Cup provincial road 06 -- the second game of the 12th provincial competition
Construction of promtail + Loki + grafana log monitoring system
信息摘要、数字签名、数字证书、对称加密与非对称加密详解
Node access to Alipay open platform sandbox to achieve payment function
An essay on the classical "tear down the wall in thinking"
Kingdee Cloud Star API calling practice
How to build tiktok user trust and drive fan growth
◰GL-阴影贴图核心步骤
Use case labeling mechanism of robot framework
Installing labellmg tutorial in Windows
面试百分百问到的进程,你究竟了解多少
SQL database
UWA Pipeline 功能详解|可视化配置自动测试
英语 | Day15、16 x 句句真研每日一句(从句断开、修饰)