当前位置:网站首页>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
边栏推荐
- Quick install mongodb
- 自定义my_strcpy与库strcpy【模拟实现字符串相关函数】
- MySQL modify master database
- 众昂矿业:萤石浮选工艺
- Decimal format decimal / datetime conversion processing
- SQL database
- About background image gradient()!
- 昆腾全双工数字无线收发芯片KT1605/KT1606/KT1607/KT1608适用对讲机方案
- ByteVCharts可视化图表库,你想要的我都有
- Use case execution of robot framework
猜你喜欢
PyTorch:train模式与eval模式的那些坑
New project of OMNeT learning
SQL database
TypeError: set_figure_params() got an unexpected keyword argument ‘figsize‘
扫码登录的原理你真的了解吗?
Modify the test case name generated by DDT
ACL 2022 | dialogved: a pre trained implicit variable encoding decoding model for dialogue reply generation
Real time operation of vim editor
Use itextpdf to intercept the page to page of PDF document and divide it into pieces
Path environment variable
随机推荐
Introduction to how to set up LAN
oracle 中快速获取表的列名列表
Kingdee Cloud Star API calling practice
VLAN高级技术,VLAN聚合,超级Super VLAN ,Sub VLAN
Detailed explanation of the penetration of network security in the shooting range
Interface document yaml
无线鹅颈麦主播麦手持麦无线麦克风方案应当如何选择
Change the password after installing MySQL in Linux
英语 | Day15、16 x 句句真研每日一句(从句断开、修饰)
PHP高效读大文件处理数据
Derivation of Σ GL perspective projection matrix
Pycham connects to the remote server and realizes remote debugging
Project framework of robot framework
深入了解3D模型相关知识(建模、材质贴图、UV、法线),置换贴图、凹凸贴图与法线贴图的区别
Zhimeng dedecms security setup Guide
An essay on the classical "tear down the wall in thinking"
New project of OMNeT learning
PostgreSQL column storage and row storage
1959年高考数学真题
自定义my_strcpy与库strcpy【模拟实现字符串相关函数】