当前位置:网站首页>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 插槽的主要用途是什么?
- 成都控制板设计提供_算是详细了_单片机程序头文件的定义、编写及引用介绍
- 1990年1月1日是星期一,定义函数date_to_week(year,month,day),实现功能输入年月日后返回星期几,例如date_to_week(2020,11,1),返回:星期日。 提示:
- Contraction mapping theorem
- raised exception class EAccexxViolation with ‘Access violation at address 45EFD5 in module 出错
- Do (local scope), initializer, memory conflict, swift pointer, inout, unsafepointer, unsafebitcast, success
- Share 3 tools, edit 5 works at home and earn more than 400
- go基础 反射
- [detailed explanation of factory mode] factory method mode
- Daily question - leetcode396 - rotation function - recursion
猜你喜欢
Leetcode165 compare version number double pointer string
Leetcode151 - invert words in string - String - simulation
[stc8g2k64s4] introduction of comparator and sample program of comparator power down detection
Programming philosophy - automatic loading, dependency injection and control inversion
Using MATLAB programming to realize the steepest descent method to solve unconstrained optimization problems
Provided by Chengdu control panel design_ It's detailed_ Introduction to the definition, compilation and quotation of single chip microcomputer program header file
LeetCode 练习——396. 旋转函数
Thread synchronization, life cycle
分享 20 个不容错过的 ES6 的技巧
Swift: entry of program, swift calls OC@_ silgen_ Name, OC calls swift, dynamic, string, substring
随机推荐
[jz46 translate numbers into strings]
LeetCode167-两数之和II-双指针-二分-数组-查找
8.4 realization of recurrent neural network from zero
How to write the keywords in the cover and title? As we media, why is there no video playback
win10 任务栏通知区图标不见了
[NLP] HMM hidden Markov + Viterbi word segmentation
Model location setting in GIS data processing -cesium
JS -- realize click Copy function
【JZ46 把数字翻译成字符串】
Advanced application of I / O multiplexing: Processing TCP and UDP services at the same time
Redis master-slave synchronization
成都控制板设计提供_算是详细了_单片机程序头文件的定义、编写及引用介绍
epoll 的EPOLLONESHOT 事件———实例程序
OC 转 Swift 条件编译、标记、宏、 Log、 版本检测、过期提示
The art of automation
js——实现点击复制功能
Swift - literal, literal protocol, conversion between basic data types and dictionary / array
Sqlserver transaction and lock problem
When splicing HQL, the new field does not appear in the construction method
剑指 Offer II 019. 最多删除一个字符得到回文(简单)