当前位置:网站首页>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
边栏推荐
- 科技的成就(二十一)
- Swift - Literal,字面量协议,基本数据类型、dictionary/array之间的转换
- Detailed analysis of SQL combat of Niuke database (26-30)
- 1n5408-asemi rectifier diode
- 每日一题-LeetCode396-旋转函数-递推
- 8.3 language model and data set
- async void 导致程序崩溃
- async关键字
- 线程同步、生命周期
- Tencent has written a few words, Ali has written them all for a month
猜你喜欢

Swift: entry of program, swift calls OC@_ silgen_ Name, OC calls swift, dynamic, string, substring

Using MATLAB programming to realize the steepest descent method to solve unconstrained optimization problems

Swift:Entry of program、Swift调用OC、@_silgen_name 、 OC 调用Swift、dynamic、String、Substring

Mds55-16-asemi rectifier module mds55-16

LeetCode151-颠倒字符串中的单词-字符串-模拟

Leetcode162 - find peak - dichotomy - array

Explanation and example application of the principle of logistic regression in machine learning

Leetcode exercise - 396 Rotation function

Thread synchronization, life cycle

MySQL报错packet out of order
随机推荐
帧同步 实现
We reference My97DatePicker to realize the use of time plug-in
在游戏世界组建一支AI团队,超参数的多智能体「大乱斗」开赛
利用 MATLAB 编程实现最速下降法求解无约束最优化问题
Share 3 tools, edit 5 works at home and earn more than 400
One of the advanced applications of I / O reuse: non blocking connect -- implemented using select (or poll)
LeetCode162-寻找峰值-二分-数组
分布式事务Seata介绍
Swift Protocol 关联对象 资源名称管理 多线程GCD 延迟 once
Pnpm installation and use
select 同时接收普通数据 和 带外数据
What is the role of the full connection layer?
Introduction to dirty reading, unrepeatable reading and phantom reading
【STC8G2K64S4】比较器介绍以及比较器掉电检测示例程序
1n5408-asemi rectifier diode
Unity_ Code mode add binding button click event
ffmpeg安装遇错:nasm/yasm not found or too old. Use --disable-x86asm for a crippled build.
8.2 text preprocessing
3、 Gradient descent solution θ
2-Go变量操作