首页
统计
关于
Search
1
Sealos3.0离线部署K8s集群
1,082 阅读
2
类的加载
741 阅读
3
Spring Cloud OAuth2.0
726 阅读
4
SpringBoot自动装配原理
691 阅读
5
集合不安全问题
584 阅读
笔记
Java
多线程
注解和反射
JVM
JUC
设计模式
Mybatis
Spring
SpringMVC
SpringBoot
MyBatis-Plus
Elastic Search
微服务
Dubbo
Zookeeper
SpringCloud
Nacos
Sentinel
数据库
MySQL
Oracle
PostgreSQL
Redis
MongoDB
工作流
Activiti7
Camunda
消息队列
RabbitMQ
前端
HTML5
CSS
CSS3
JavaScript
jQuery
Vue2
Vue3
Linux
容器
Docker
Kubernetes
Python
登录
Search
标签搜索
Java
CSS
mysql
RabbitMQ
JavaScript
Redis
JVM
Mybatis-Plus
Camunda
多线程
CSS3
Python
Spring Cloud
注解和反射
Activiti
工作流
SpringBoot
Mybatis
Spring
html5
蘇阿細
累计撰写
388
篇文章
累计收到
4
条评论
首页
栏目
笔记
Java
多线程
注解和反射
JVM
JUC
设计模式
Mybatis
Spring
SpringMVC
SpringBoot
MyBatis-Plus
Elastic Search
微服务
Dubbo
Zookeeper
SpringCloud
Nacos
Sentinel
数据库
MySQL
Oracle
PostgreSQL
Redis
MongoDB
工作流
Activiti7
Camunda
消息队列
RabbitMQ
前端
HTML5
CSS
CSS3
JavaScript
jQuery
Vue2
Vue3
Linux
容器
Docker
Kubernetes
Python
页面
统计
关于
搜索到
1
篇与
的结果
2021-02-23
可重入锁
可重入锁可重入锁:打开大门的锁之后也就获得了里面两个房间的锁synchronized版可重入锁package com.sw.lock; /** * @Author suaxi * @Date 2021/2/22 23:07 * synchronized版可重入锁 */ public class Test01 { public static void main(String[] args) { Phone phone = new Phone(); new Thread(() ->{ phone.sms(); },"A").start(); new Thread(() ->{ phone.sms(); },"B").start(); } } class Phone{ public synchronized void sms(){ System.out.println(Thread.currentThread().getName()+"--->sms"); call(); } public synchronized void call(){ System.out.println(Thread.currentThread().getName()+"--->call"); } } lock版可重入锁package com.sw.lock; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * @Author suaxi * @Date 2021/2/22 23:07 * lock版可重入锁 */ public class Test02 { public static void main(String[] args) { Phone2 phone = new Phone2(); new Thread(() ->{ phone.sms(); },"A").start(); new Thread(() ->{ phone.sms(); },"B").start(); } } class Phone2{ Lock lock =new ReentrantLock(); public void sms(){ lock.lock(); //lock锁必须配对,否则会产生死锁 try { System.out.println(Thread.currentThread().getName()+"--->sms"); call(); }catch (Exception e){ e.printStackTrace(); }finally { lock.unlock(); } } public void call(){ lock.lock(); try { System.out.println(Thread.currentThread().getName()+"--->call"); }catch (Exception e){ e.printStackTrace(); }finally { lock.unlock(); } } }
2021年02月23日
144 阅读
0 评论
0 点赞