当前位置:网站首页>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
边栏推荐
- 【JZ46 把数字翻译成字符串】
- 小红书 timestamp2 (2022/04/22)
- 填充每个节点的下一个右侧节点指针 II [经典层次遍历 | 视为链表 ]
- 1 - first knowledge of go language
- Practice of unified storage technology of oppo data Lake
- Unity_ Code mode add binding button click event
- Alexnet model
- LeetCode 练习——396. 旋转函数
- Epolloneshot event of epoll -- instance program
- 8.3 language model and data set
猜你喜欢
[NLP] HMM hidden Markov + Viterbi word segmentation
Thinkphp5 + data large screen display effect
8.4 realization of recurrent neural network from zero
Swift - literal, literal protocol, conversion between basic data types and dictionary / array
Vous ne connaissez pas encore les scénarios d'utilisation du modèle de chaîne de responsabilité?
Swift:Entry of program、Swift调用OC、@_silgen_name 、 OC 调用Swift、dynamic、String、Substring
Arduino for esp8266串口功能简介
Leetcode167 - sum of two numbers II - double pointer - bisection - array - Search
[stc8g2k64s4] introduction of comparator and sample program of comparator power down detection
Introduction to distributed transaction Seata
随机推荐
How to write the keywords in the cover and title? As we media, why is there no video playback
January 1, 1990 is Monday. Define the function date_ to_ Week (year, month, day), which realizes the function of returning the day of the week after inputting the year, month and day, such as date_ to
Vscode Chinese plug-in doesn't work. Problem solving
Provided by Chengdu control panel design_ It's detailed_ Introduction to the definition, compilation and quotation of single chip microcomputer program header file
Resolve the conflict between computed attribute and input blur event
nuxt项目:全局获取process.env信息
Sqlserver transaction and lock problem
填充每个节点的下一个右侧节点指针 II [经典层次遍历 | 视为链表 ]
分布式事务Seata介绍
capacitance
do(Local scope)、初始化器、内存冲突、Swift指针、inout、unsafepointer、unsafeBitCast、successor、
I/O复用的高级应用之一:非阻塞 connect———使用 select 实现(也可以用 poll 实现)
解决computed属性与input的blur事件冲突问题
Arduino for esp8266串口功能简介
UML项目实例——抖音的UML图描述
epoll 的EPOLLONESHOT 事件———实例程序
What is the main purpose of PCIe X1 slot?
Set up an AI team in the game world and start the super parametric multi-agent "chaos fight"
eolink 如何助力远程办公
LeetCode162-寻找峰值-二分-数组