当前位置:网站首页>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
边栏推荐
- True math problems in 1959 college entrance examination
- Talk about browser cache control
- ACL 2022 | dialogved: a pre trained implicit variable encoding decoding model for dialogue reply generation
- DDT + Excel for interface test
- SQL: How to parse Microsoft Transact-SQL Statements in C# and to match the column aliases of a view
- 如何建立 TikTok用户信任并拉动粉丝增长
- MySQL restores data through binlog file
- Loggie source code analysis source file module backbone analysis
- PyMySQL
- Change the password after installing MySQL in Linux
猜你喜欢
Nifi fast installation and file synchronization
ACL 2022 | dialogved: a pre trained implicit variable encoding decoding model for dialogue reply generation
Deepinv20 installation MariaDB
Node access to Alipay open platform sandbox to achieve payment function
如何建立 TikTok用户信任并拉动粉丝增长
无线鹅颈麦主播麦手持麦无线麦克风方案应当如何选择
CentOS MySQL multi instance deployment
Detailed explanation of information abstract, digital signature, digital certificate, symmetric encryption and asymmetric encryption
Pytorch: the pit between train mode and eval mode
Construction of promtail + Loki + grafana log monitoring system
随机推荐
Deeply understand the relevant knowledge of 3D model (modeling, material mapping, UV, normal), and the difference between displacement mapping, bump mapping and normal mapping
扫码登录的原理你真的了解吗?
Idea of batch manufacturing test data, with source code
杂文 谈谈古典的《拆掉思维里的墙》
Getting started with JDBC
◰GL-着色器处理程序封装
Deepinv20 installation MariaDB
Paging SQL
Introduction to how to set up LAN
Quick install mongodb
Shortcut keys (multiline)
Installation and management procedures
文件操作详解(2)
Grpc gateway based on Ocelot
Linux MySQL data timing dump
批量制造测试数据的思路,附源码
JSON deserialize anonymous array / object
拷贝构造函数 浅拷贝与深拷贝
文件操作《二》(5000字总结篇)
Dancenn: overview of byte self-developed 100 billion scale file metadata storage system