当前位置:网站首页>Lock锁
Lock锁
2022-04-23 16:53:00 【木十一的木马】
先看官方文档:
实现类有可重入锁、读锁和写锁,可重入锁最常用。
可重入锁,指的是以线程为单位,当一个线程获取对象锁之后,这个线程可以再次获取本对象上的锁,而其他的线程是不可以的。
可以看到,ReentrantLock是lock接口的一个实现类,里面实现了可重入锁和公平锁非公平锁。
公平锁:十分的公平,先来后到。
非公平锁:十分不公平,可以插队。
默认实现的是非公平锁。
它的整个逻辑就是:加锁,在try里写业务代码,在finally里解锁。
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 {
//属性
private int number = 30;
Lock lock = new ReentrantLock();
//方法
public synchronized void sale(){
//加锁
lock.lock();
try {
//写业务代码
if (number > 0){
System.out.println(Thread.currentThread().getName() + "卖出了" + (number --) + "票,剩余" + number);
}
}catch (Exception e){
e.printStackTrace();
}finally {
//解锁
lock.unlock();
}
}
}
结果:
Synchronized和Lock的区别?
(1)Synchronized 是Java的内置关键字;Lock是一个Java类。
(2)Synchronized 无法判断获取锁的状态;Lock可以判断是否获取到了锁。
(3)Synchronized 会自动释放锁;lock必须手动释放锁,如果不释放->死锁。
(4)Synchronized 线程1(获得锁,阻塞),线程2(等待,傻傻的等);lock锁不一定会等。
(5)Synchronized 可重入锁,不可以中断的,非公平锁;Lock锁,可重入锁,可以判断,自己可以设置公平还是非公平。
(6)Synchronized 适合锁少量的代码同步问题;Lock适合锁大量的同步代码。
版权声明
本文为[木十一的木马]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_49005459/article/details/124315927
边栏推荐
- Solution of garbled code on idea console
- Dlib of face recognition framework
- 100 deep learning cases | day 41 - convolutional neural network (CNN): urbansound 8K audio classification (speech recognition)
- _ Mold_ Board_
- How much do you know about the process of the interview
- vim编辑器的实时操作
- Custom implementation of Baidu image recognition (instead of aipocr)
- STM32__ 03 - beginner timer
- Server log analysis tool (identify, extract, merge, and count exception information)
- ◰GL-阴影贴图核心步骤
猜你喜欢
Bytevcharts visual chart library, I have everything you want
Derivation of Σ GL perspective projection matrix
Phpstudy V8, a commonly used software for station construction 1 graphic installation tutorial (Windows version) super detailed
Change the password after installing MySQL in Linux
JMeter installation tutorial and solutions to the problems I encountered
Nodejs installation and environment configuration
100 deep learning cases | day 41 - convolutional neural network (CNN): urbansound 8K audio classification (speech recognition)
计组 | 【七 输入/输出系统】知识点与例题
Zhongang Mining: Fluorite Flotation Process
Use case labeling mechanism of robot framework
随机推荐
How to build tiktok user trust and drive fan growth
批量制造测试数据的思路,附源码
How to choose the wireless gooseneck anchor microphone and handheld microphone scheme
Execution plan calculation for different time types
Public variables of robotframework
[pyGame games] how did angry birds, a mobile game that became popular all over the world 10 years ago, dominate the list? Classic return
Take according to the actual situation, classify and summarize once every three levels, and see the figure to know the demand
DanceNN:字节自研千亿级规模文件元数据存储系统概述
Set cell filling and ranking method according to the size of the value in the soft report
Knowledge points and examples of [seven input / output systems]
Paging the list collection
Phpstudy V8, a commonly used software for station construction 1 graphic installation tutorial (Windows version) super detailed
如何用Redis实现分布式锁?
Sub database and sub table & shardingsphere
◰ GL shader handler encapsulation
Real time operation of vim editor
ACL 2022 | dialogved: a pre trained implicit variable encoding decoding model for dialogue reply generation
Query the data from 2013 to 2021, and only query the data from 2020. The solution to this problem is carried out
Node access to Alipay open platform sandbox to achieve payment function
CentOS MySQL multi instance deployment