当前位置:网站首页>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
边栏推荐
- 8.3 language model and data set
- epoll 的 ET,LT工作模式———实例程序
- Alexnet model
- Achievements in science and Technology (21)
- Swift - Literal,字面量协议,基本数据类型、dictionary/array之间的转换
- async关键字
- 2-GO variable operation
- ffmpeg安装遇错:nasm/yasm not found or too old. Use --disable-x86asm for a crippled build.
- 科技的成就(二十一)
- Introduction to dirty reading, unrepeatable reading and phantom reading
猜你喜欢
Swift - literal, literal protocol, conversion between basic data types and dictionary / array
Leetcode162 - find peak - dichotomy - array
OC 转 Swift 条件编译、标记、宏、 Log、 版本检测、过期提示
LeetCode149-直线上最多的点数-数学-哈希表
[jz46 translate numbers into strings]
Leetcode151 - invert words in string - String - simulation
Programming philosophy - automatic loading, dependency injection and control inversion
[stc8g2k64s4] introduction of comparator and sample program of comparator power down detection
Swift: entry of program, swift calls OC@_ silgen_ Name, OC calls swift, dynamic, string, substring
利用 MATLAB 编程实现最速下降法求解无约束最优化问题
随机推荐
LeetCode162-寻找峰值-二分-数组
UML项目实例——抖音的UML图描述
Contraction mapping theorem
Model location setting in GIS data processing -cesium
Flink DataStream 类型系统 TypeInformation
I/O复用的高级应用:同时处理 TCP 和 UDP 服务
When splicing HQL, the new field does not appear in the construction method
Role of asemi rectifier module mdq100-16 in intelligent switching power supply
Swift:Entry of program、Swift调用OC、@_silgen_name 、 OC 调用Swift、dynamic、String、Substring
Programming philosophy - automatic loading, dependency injection and control inversion
Leetcode149 - maximum number of points on a line - Math - hash table
SQLSERVER事物与锁的问题
大文件如何快速上传?
Ffmpeg installation error: NASM / yasm not found or too old Use --disable-x86asm for a clipped build
LeetCode167-两数之和II-双指针-二分-数组-查找
Interviewer: let's talk about the process of class loading and the mechanism of class loading (parental delegation mechanism)
Introduction to Arduino for esp8266 serial port function
科技的成就(二十一)
8.4 realization of recurrent neural network from zero
Don't you know the usage scenario of the responsibility chain model?