当前位置:网站首页>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
边栏推荐
- LVM and disk quota
- SQL database
- 无线鹅颈麦主播麦手持麦无线麦克风方案应当如何选择
- The new MySQL table has a self increasing ID of 20 bits. The reason is
- 安装及管理程序
- ◰GL-着色器处理程序封装
- Rtklib 2.4.3 source code Notes
- Multithreaded @ async thread pool
- Use itextpdf to intercept the page to page of PDF document and divide it into pieces
- How magical is the unsafe class used by all major frameworks?
猜你喜欢

建站常用软件PhpStudy V8.1图文安装教程(Windows版)超详细

Use case execution of robot framework

Kunteng full duplex digital wireless transceiver chip kt1605 / kt1606 / kt1607 / kt1608 is suitable for interphone scheme

Sub database and sub table & shardingsphere

详解牛客----手套

oracle 中快速获取表的列名列表

DanceNN:字节自研千亿级规模文件元数据存储系统概述

Detailed explanation of the penetration of network security in the shooting range

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

NVIDIA graphics card driver error
随机推荐
Quick install mongodb
Construction of promtail + Loki + grafana log monitoring system
聊一聊浏览器缓存控制
Solution of garbled code on idea console
PHP高效读大文件处理数据
TypeError: set_figure_params() got an unexpected keyword argument ‘figsize‘
伪分布安装spark
安装及管理程序
杂文 谈谈古典的《拆掉思维里的墙》
Introduction to new functions of camtasia2022 software
关于局域网如何组建介绍
The new MySQL table has a self increasing ID of 20 bits. The reason is
NVIDIA graphics card driver error
无线鹅颈麦主播麦手持麦无线麦克风方案应当如何选择
Linux MySQL data timing dump
How much do you know about the process of the interview
JMeter installation tutorial and solutions to the problems I encountered
English | day15, 16 x sentence true research daily sentence (clause disconnection, modification)
Zhongang Mining: Fluorite Flotation Process
MySQL master-slave replication