当前位置:网站首页>多线程与高并发(11)——经典面试题之实现一个容器,提供两个方法,add,size。
多线程与高并发(11)——经典面试题之实现一个容器,提供两个方法,add,size。
2022-08-10 17:13:00 【李王家的翠花】
实现一个容器,提供两个方法,add,size。写两个线程,线程 1 添加 10 个元素到容器中,线程 2 实现监控元素的个数,当个数到 5 个时,线程 2 给出提示并结束。
1、普通方法
public class Test_Container {
List list = new ArrayList<>();
private void add(Object o) {
list.add(o);
}
private Integer size() {
return list.size();
}
public static void main(String[] args) {
Test_Container container = new Test_Container();
new Thread(() -> {
System.out.println("T1 开始");
for (int i = 1; i <= 10; i++) {
container.add(new Object());
System.out.println("add + " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("T1 结束");
}, "T1").start();
new Thread(() -> {
System.out.println("T2 开始");
while(true){
if(container.size()==5){
break;
}
}
System.out.println("T2 结束");
}, "T2").start();
}
}
赶早不如赶巧,一上来我就运行出了死循环的结果…
这是为什么呢?很简单,因为线程2开始的时候,线程1都执行到7了!
还有一种情况,如下图:
T2结束的时候,T1都执行到6了。
我们加volatile关键字尝试一下。
2、volatile关键字
public class Test_ContainerWithVolatile {
volatile List list = new ArrayList<>();
private void add(Object o) {
list.add(o);
}
private Integer size() {
return list.size();
}
public static void main(String[] args) {
Test_ContainerWithVolatile container = new Test_ContainerWithVolatile();
new Thread(() -> {
System.out.println("T1 开始");
for (int i = 1; i <= 10; i++) {
container.add(new Object());
System.out.println("add + " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("T1 结束");
}, "T1").start();
new Thread(() -> {
System.out.println("T2 开始");
while(true){
if(container.size()==5){
break;
}
}
System.out.println("T2 结束");
}, "T2").start();
}
}
看上去没有死循环的问题:
但是,是因为我加了sleep的原因,如果不加sleep,还是不行。
所以,volatile关键字是不可以的,为什么不可以呢,volatile关键字禁止指令重排序,保证线程可见性,但是又保证不了原子性,更保证不了线程等待不运行。你想读到size=5的时候,他可能都变成6了。
我们用wait和notify实现尝试一下。
3、wait和notify实现
wait()方法的作用是释放锁,加入到等待队列。调用notify或者notifyAll方法不释放锁,只是让他参与锁的竞争中去。
public class Test_ContainerWithWait {
//及时可见
volatile List list = new ArrayList<>();
private void add(Object o) {
list.add(o);
}
private Integer size() {
return list.size();
}
public static void main(String[] args) {
Test_ContainerWithWait container = new Test_ContainerWithWait();
//用来上锁的对象
Object o = new Object();
new Thread(() -> {
System.out.println("T2 开始");
if (container.size() != 5) {
try {
//wait()方法必须在同步关键字修饰的方法中才能调用。
synchronized (o) {
o.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("T2 结束");
}, "T2").start();
//保证线程2先运行
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(() -> {
System.out.println("T1 开始");
for (int i = 1; i <= 10; i++) {
container.add(new Object());
System.out.println("add + " + i);
if (container.size() == 5) {
synchronized (o) {
//notify不释放锁
o.notify();
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("T1 结束");
}, "T1").start();
}
}
上面代码,执行结果如下,执行结果正常,但是,这是有问题的:
notify不释放锁,只是让对象去争这把锁,其中真正起作用的还是Thread.sleep(1000);这个,T2先去执行了。如果是以下写法:
new Thread(() -> {
synchronized (o) {
System.out.println("T1 开始");
for (int i = 1; i <= 10; i++) {
container.add(new Object());
System.out.println("add + " + i);
if (container.size() == 5) {
//notify不释放锁
o.notify();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("T1 结束");
}
}, "T1").start();
我们把锁加在代码逻辑的最外层,结果如下:
notify不释放锁,T2在5这个时候还是拿不到锁。怎么解决呢?正确写法如下;
public static void main(String[] args) {
Test_ContainerWithWait container = new Test_ContainerWithWait();
//用来上锁的对象
Object o = new Object();
new Thread(() -> {
synchronized (o) {
System.out.println("T2 开始");
if (container.size() != 5) {
try {
//wait()方法必须在同步关键字修饰的方法中才能调用。
o.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("T2 结束");
//唤醒锁让T1继续执行
o.notify();
}
}, "T2").start();
//保证线程2先运行
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(() -> {
synchronized (o) {
System.out.println("T1 开始");
for (int i = 1; i <= 10; i++) {
container.add(new Object());
System.out.println("add + " + i);
if (container.size() == 5) {
//notify不释放锁
o.notify();
try {
//通过wait释放锁
o.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("T1 结束");
}
}, "T1").start();
}
两个地方,一个是在T1线程里通过wait释放锁,一个是在T2线程里notify让T1绩效执行。结果如下:
边栏推荐
- 3 年 CRUD 从 8K 涨到 28K,谁知道这4个月我到底经历了什么?
- 【接入指南 之 直接接入】手把手教你快速上手接入HONOR Connect平台(中)
- matsuri.icu 筛选单场直播中 指定用户的弹幕
- 【云原生| Docker】 部署 Django & mysql 项目
- R语言使用oneway.test函数执行单因素方差分析(One-Way ANOVA)、使用数据集的子集数据进行单因素方差分析(subset函数筛选数据子集)
- The sword refers to OfferⅡ 045. The bottommost leftmost value of the binary tree dfs
- Talk about cloud native data platform
- v-model指令:获取和设置表单元素的值
- fastjson链分析(1.2.22-47)
- PS2手柄通讯协议解析—附资料和源码「建议收藏」
猜你喜欢
leetcode:281. 锯齿迭代器
v-show指令:切换元素的显示与隐藏
网易云信亮相LiveVideoStackCon2022,解构基于WebRTC的开源低延时播放器实践
Redis下载安装教程 (windows)
The DGIOT platform displays the whole process code analysis of OPC reporting data in real time
How to use bitwise operators in C language
bp神经网络反向传播原理,BP神经网络反向传播
1001 A+B Format (string processing)
最详解决:jupyter notebook不会自动打开浏览器问题
烟雾、空气质量、温湿度…自己徒手做个环境检测设备
随机推荐
需求骤降,成本激增,PC行业再次入冬
JNDI与RMI、LDAP
重庆新壹汽与一汽集团达成新能源项目战略合作,赋能“碳中和”创造“碳财富”
训练一个神经网络要多久,神经网络训练时间过长
promise笔记(二)
数据库注入提权总结(二)
R语言拟合ARIMA模型:使用forecast包中的auto.arima函数自动搜索最佳参数组合、模型阶数(p,d,q)、如果已知阶数则直接使用arima函数构建模型(order参数指定阶数)
BalsnCTF2021
How to realize full backup and incremental backup of MySQL database
中国芯片的营收首破万亿,优势凸显的成熟工艺产能将称霸全球
Embedded Development: Embedded Basics - Mapping Peripherals Using Arrays of Pointers
烟雾、空气质量、温湿度…自己徒手做个环境检测设备
SQL优化的魅力!从 30248s 到 0.001s
3 年 CRUD 从 8K 涨到 28K,谁知道这4个月我到底经历了什么?
R语言使用ggpubr包的ggbarplot函数可视化柱状图、设置add参数为mean_se和jitter可视化不同水平均值的柱状图并为柱状图添加误差线(se标准误差)和抖动数据点分布
深度学习培训二笔记
Basic knowledge of software engineering--requirements analysis
BalsnCTF2021
2022 CCF China Open Source Conference Notice (Fourth Round)
Polling and the principle of webSocket and socket.io