当前位置:网站首页>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
边栏推荐
- Tencent resolves the address according to the IP address
- MySQL personal learning summary
- 【PIMF】OpenHarmony啃论文俱乐部—在ACM Survey闲逛是什么体验
- Regular filtering of Intranet addresses and segments
- Modify the test case name generated by DDT
- New project of OMNeT learning
- Calculation formula related to tolerance analysis
- Decimal format decimal / datetime conversion processing
- Deeply understand the relevant knowledge of 3D model (modeling, material mapping, UV, normal), and the difference between displacement mapping, bump mapping and normal mapping
- Camtasia2022软件新增功能介绍
猜你喜欢

面试百分百问到的进程,你究竟了解多少

New project of OMNeT learning

Pycham connects to the remote server and realizes remote debugging

Installation and management procedures

Detailed explanation of information abstract, digital signature, digital certificate, symmetric encryption and asymmetric encryption

On the security of key passing and digital signature

Node access to Alipay open platform sandbox to achieve payment function

MySql主从复制

Rtklib 2.4.3 source code Notes

MySQL master-slave replication
随机推荐
Easyexcel reads the geographical location data in the excel table and sorts them according to Chinese pinyin
How vscode compares the similarities and differences between two files
Flask如何在内存中缓存数据?
Idea of batch manufacturing test data, with source code
Camtasia2022软件新增功能介绍
Selenium IDE and XPath installation of chrome plug-in
Smart doc + Torna generate interface document
DDT + Excel for interface test
Introduction to how to set up LAN
vscode如何比较两个文件的异同
PyTorch:train模式与eval模式的那些坑
Detailed explanation of file operation (2)
Deepinv20 installation MariaDB
Shell脚本——Shell编程规范及变量
JMeter installation tutorial and solutions to the problems I encountered
An essay on the classical "tear down the wall in thinking"
Detailed explanation of the penetration of network security in the shooting range
Sub database and sub table & shardingsphere
Modify the test case name generated by DDT
Installing labellmg tutorial in Windows