当前位置:网站首页>Lock锁(重点)
Lock锁(重点)
2022-06-22 06:10:00 【酷小亚】
传统 Synchronized
代码如下:
public class SaleTicketDemo1 {
public static void main(String[] args) {
//并发:多线程操作同一个资源类, 把资源类丢入线程
Ticket ticket = new Ticket();
//@FunctionalInterface 函数式接口 , jdk8 lambda表达式 (参数)->{ 代码 }
new Thread(() -> {
for (int i = 0; i < 60; i++) ticket.sale();}, "A").start();
new Thread(() -> {
for (int i = 0; i < 60; i++) ticket.sale();}, "B").start();
new Thread(() -> {
for (int i = 0; i < 60; i++) ticket.sale();}, "C").start();
}
}
//资源类 OOP
class Ticket {
//属性、方法
private int number = 30;
//卖票的方式
//synchronized 本质:队列,锁
public synchronized void sale() {
if (number > 0) {
System.out.println(Thread.currentThread().getName() + "卖出了" + (number--) + "票,剩余:" + number);
}
}
}
Lock 锁
公平锁:十分公平:可以先来后到
非公平锁:十分不公平:可以插队(默认)
代码如下:
public class SaleTicketDemo2 {
public static void main(String[] args) {
//并发: 多线程操作同一个资源类,把资源类丢入线程
Ticket2 ticket = new Ticket2();
//@FunctionalInterface 函数式接口 , jdk8 lambda表达式 (参数)->{ 代码 }
new Thread(() -> {
for (int i = 0; i < 40; i++) ticket.sale();
}, "A").start();
new Thread(() -> {
for (int i = 0; i < 40; i++) ticket.sale();
}, "B").start();
new Thread(() -> {
for (int i = 0; i < 40; i++) ticket.sale();
}, "C").start();
}
}
/** * Lock 三部曲 * 1、new ReentrantLock(); * 2、lock.lock(); //加锁 * 3、finally=> lock.unlock(); // 解锁 */
class Ticket2 {
//属性、方法
private int number = 30;
Lock lock = new ReentrantLock();
public void sale() {
lock.lock();// 加锁
try {
//业务代码
if (number > 0) {
System.out.println(Thread.currentThread().getName() + "卖出了" + (number--) + "票,剩余:" + number);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();// 解锁
}
}
}
Synchronzied 和 Lock 区别
1、Synchronzied 内置的Java关键字,Lock是一个java类
2、Synchronzied 无法判断获取锁的状态,Lock 可以判断是否获取到了锁
3、Synchronzied 会自动释放锁,lock 必须要手动释放锁!如果不释放锁,死锁
4、Synchronzied 线程1(获得锁)、线程2(等待,傻傻的等);Lock锁就不一定会一直等下去!
5、Synchronzied 可重入锁,不可以中断的,非公平;Lock,可重入锁,可以判断锁,非公平(可以自己设置);
6、Synchronzied 适合锁少量的代码同步问题,Lock 适合锁大量的同步代码!
边栏推荐
- Shengxin visualization (Part2) -- box diagram
- Matlab system identification
- 【云计算重点复习】
- Single cell thesis record (Part12) -- unsupervised spatial embedded deep representation of spatial transcriptomics
- 小熊派BearPi-HM Micro正式合入OpenHarmony主干
- Performance optimization best practices for reducing Game Size
- D3D学习笔记(1)—— SO 阶段介绍 AutoDraw使用条件
- MYSQL牛客刷题
- 【NAND文件系统】UBI介绍
- postgresql数据库中根据某个字段判断存在则更新(update)操作,不存在则插入(insert)
猜你喜欢

从转载阿里开源项目 Egg.js 技术文档引发的“版权纠纷”,看宽松的 MIT 许可该如何用?

经验模式分解(EMD)和希尔伯特-黄变换(HHT)

Unity development - scene asynchronous loading

400 hash table (1. sum of two numbers, 454. sum of four numbers II, 383. ransom letter)

Ptrade trading program code - from zero to firm offer 19

Single cell literature learning (Part2) -- stplus: a reference based method for the exact enhancement of St

Use of idea plug-in EASYCODE

Breakthrough in rich device platform: dayu200 based on rk3568 enters the openharmony 3.1 release trunk

单细胞论文记录(part13)--SpaGCN: Integrating gene expression, spatial location and histology to ...

【自己动手写CPU】异常相关指令的实现
随机推荐
400 hash table (1. sum of two numbers, 454. sum of four numbers II, 383. ransom letter)
clickhouse对比两台机器数据
常用CMOS模拟开关功能和原理
h = key.hashCode()) ^ (h >>> 16) 详细解读以及为什么要将hashCode值右移16位并且与原来的hashCode值进行异或操作
Single cell thesis record (Part12) -- unsupervised spatial embedded deep representation of spatial transcriptomics
汇顶科技GR551x系列开发板已支持OpenHarmony
Adaboost
drop、truncate和delete的区别
朗国科技助力OpenHarmony生态繁荣
【NAND文件系统】UBI介绍
Single cell thesis record (part13) -- spagcn: integrating gene expression, spatial location and history to
Research on automatic landing control system of carrier aircraft
Shengxin visualization (Part3) -- violin diagram
h = key. Hashcode()) ^ (H > > 16) detailed explanation and why the hashcode value should be shifted to the right by 16 bits and XOR with the original hashcode value
TCP connection details
单细胞论文记录(part6)--SpaGE: Spatial Gene Enhancement using scRNA-seq
Server PHP related web page development environment construction
leetcode每周3道(八)图之最短路
TCP连接细节问题
Ethernet communication protocol