当前位置:网站首页>JUC learning record (2022.4.22)
JUC learning record (2022.4.22)
2022-04-23 15:00:00 【Binbin ice】
1、 spurious wakeup
Multi-threaded environment , There are multiple threads executing wait() Method , Other threads are required to execute notify() perhaps notifyAll() Ways to wake them up , If multiple threads are awakened , But only some of them are useful wake-up operations , The rest of the awakening is useless ; For threads that should not be awakened , Is false awakening .
package sync;
/** When wait stay if Intra statement time , Because the conditions have been judged before entering , So when you wake up ( Where to sleep , Where do you wake up ) You won't judge , Continue with the following statement , Both send false wake-up */
class Calculator {
private int num = 0;
public synchronized void increment() throws InterruptedException {
//if (num != 0) { // There is a false awakening
// this.wait();
//}
while (num != 0) {
// Every time I wake up, I judge again , Solve false awakening
this.wait();
}
num++;
System.out.println(Thread.currentThread().getName() + "+1, Current value :" + num);
this.notifyAll();
}
public synchronized void decrement() throws InterruptedException {
//if (num != 1) { // There is a false awakening
// this.wait();
//}
while (num != 1) {
// Every time I wake up, I judge again , Solve false awakening
this.wait();
}
num--;
System.out.println(Thread.currentThread().getName() + "-1, Current value :" + 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();
}
}
版权声明
本文为[Binbin ice]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231457175055.html
边栏推荐
- 剑指 Offer II 019. 最多删除一个字符得到回文(简单)
- 分布式事务Seata介绍
- Little red book timestamp2 (2022 / 04 / 22)
- Interviewer: let's talk about the process of class loading and the mechanism of class loading (parental delegation mechanism)
- Progress in the treatment of depression
- Share 3 tools, edit 5 works at home and earn more than 400
- 牛客网数据库SQL实战详细剖析(26-30)
- UML项目实例——抖音的UML图描述
- Programming philosophy - automatic loading, dependency injection and control inversion
- 抑郁症治疗的进展
猜你喜欢
OC to swift conditional compilation, marking, macro, log, version detection, expiration prompt
Leetcode167 - sum of two numbers II - double pointer - bisection - array - Search
[jz46 translate numbers into strings]
Arduino for esp8266串口功能简介
大文件如何快速上传?
Swift - Literal,字面量协议,基本数据类型、dictionary/array之间的转换
Detailed comparison between asemi three-phase rectifier bridge and single-phase rectifier bridge
1-初识Go语言
Don't you know the usage scenario of the responsibility chain model?
Mds55-16-asemi rectifier module mds55-16
随机推荐
Detailed analysis of SQL combat of Niuke database (26-30)
Role of asemi rectifier module mdq100-16 in intelligent switching power supply
8.5 concise implementation of cyclic neural network
Swift:Entry of program、Swift调用OC、@_silgen_name 、 OC 调用Swift、dynamic、String、Substring
select 同时接收普通数据 和 带外数据
Leetcode151 - invert words in string - String - simulation
Leetcode167 - sum of two numbers II - double pointer - bisection - array - Search
剑指 Offer II 019. 最多删除一个字符得到回文(简单)
Unity_ Code mode add binding button click event
2-Go变量操作
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
1 - first knowledge of go language
Swift - Literal,字面量协议,基本数据类型、dictionary/array之间的转换
牛客网数据库SQL实战详细剖析(26-30)
大文件如何快速上传?
Introduction to Arduino for esp8266 serial port function
【工厂模式详解】工厂方法模式
Swift Protocol 关联对象 资源名称管理 多线程GCD 延迟 once
GIS数据处理-cesium中模型位置设置
Little red book timestamp2 (2022 / 04 / 22)