当前位置:网站首页>JUC学习记录(2022.4.22)
JUC学习记录(2022.4.22)
2022-04-23 14:57:00 【彬彬ice】
1、虚假唤醒
多线程环境下,有多个线程执行了wait()方法,需要其他线程执行notify()或者notifyAll()方法去唤醒它们,假如多个线程都被唤醒了,但是只有其中一部分是有用的唤醒操作,其余的唤醒都是无用功;对于不应该被唤醒的线程而言,便是虚假唤醒。
package sync;
/** 当wait在if语句内时,因为进入前已经判断过条件,所以唤醒时(在哪睡,在哪醒)就不会再判断,继续执行下面语句,既发送虚假唤醒 */
class Calculator {
private int num = 0;
public synchronized void increment() throws InterruptedException {
//if (num != 0) { //存在虚假唤醒
// this.wait();
//}
while (num != 0) {
//每次醒都重新判断,解决虚假唤醒
this.wait();
}
num++;
System.out.println(Thread.currentThread().getName() + "+1,当前值:" + num);
this.notifyAll();
}
public synchronized void decrement() throws InterruptedException {
//if (num != 1) { //存在虚假唤醒
// this.wait();
//}
while (num != 1) {
//每次醒都重新判断,解决虚假唤醒
this.wait();
}
num--;
System.out.println(Thread.currentThread().getName() + "-1,当前值:" + num);
this.notifyAll();
}
}
public class Compute {
public static void main(String[] args) {
Calculator calculator = new Calculator();
new Thread(() -> {
try {
for (int i = 0; i < 10; i++) {
calculator.increment();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "AA").start();
new Thread(() -> {
try {
for (int i = 0; i < 10; i++) {
calculator.decrement();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "BB").start();
new Thread(() -> {
try {
for (int i = 0; i < 10; i++) {
calculator.increment();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "CC").start();
new Thread(() -> {
try {
for (int i = 0; i < 10; i++) {
calculator.decrement();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "DD").start();
}
}
版权声明
本文为[彬彬ice]所创,转载请带上原文链接,感谢
https://blog.csdn.net/xiaosong2001/article/details/124349053
边栏推荐
- PCIe X1 插槽的主要用途是什么?
- The difference between having and where in SQL
- One of the advanced applications of I / O reuse: non blocking connect -- implemented using select (or poll)
- Frame synchronization implementation
- JS -- realize click Copy function
- 解决computed属性与input的blur事件冲突问题
- 【JZ46 把数字翻译成字符串】
- 填充每个节点的下一个右侧节点指针 II [经典层次遍历 | 视为链表 ]
- Tencent has written a few words, Ali has written them all for a month
- Swift Protocol 关联对象 资源名称管理 多线程GCD 延迟 once
猜你喜欢
Thread synchronization, life cycle
Don't you know the usage scenario of the responsibility chain model?
1n5408-asemi rectifier diode
eolink 如何助力遠程辦公
Svn detailed use tutorial
Programming philosophy - automatic loading, dependency injection and control inversion
LeetCode167-两数之和II-双指针-二分-数组-查找
we引用My97DatePicker 实现时间插件使用
[NLP] HMM hidden Markov + Viterbi word segmentation
MySQL error packet out of order
随机推荐
async关键字
Comment eolink facilite le télétravail
封面和标题中的关键词怎么写?做自媒体为什么视频没有播放量
Progress in the treatment of depression
【无标题】
Alexnet model
你還不知道責任鏈模式的使用場景嗎?
Is asemi ultrafast recovery diode interchangeable with Schottky diode
剑指 Offer II 019. 最多删除一个字符得到回文(简单)
LeetCode153-寻找旋转排序数组中的最小值-数组-二分查找
压缩映射定理
How do I open the win10 startup folder?
epoll 的 ET,LT工作模式———实例程序
冰冰学习笔记:一步一步带你实现顺序表
Detailed comparison between asemi three-phase rectifier bridge and single-phase rectifier bridge
Swift - Literal,字面量协议,基本数据类型、dictionary/array之间的转换
Introduction to Arduino for esp8266 serial port function
win10 任务栏通知区图标不见了
Swift Protocol 关联对象 资源名称管理 多线程GCD 延迟 once
脏读、不可重复读和幻读介绍