当前位置:网站首页>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
边栏推荐
- epoll 的EPOLLONESHOT 事件———实例程序
- 【STC8G2K64S4】比较器介绍以及比较器掉电检测示例程序
- Epoll's et, lt working mode -- example program
- Unity_ Code mode add binding button click event
- 成都控制板设计提供_算是详细了_单片机程序头文件的定义、编写及引用介绍
- LeetCode149-直线上最多的点数-数学-哈希表
- Swift Protocol 关联对象 资源名称管理 多线程GCD 延迟 once
- 科技的成就(二十一)
- like和regexp差别
- Don't you know the usage scenario of the responsibility chain model?
猜你喜欢
UML学习_day2
Leetcode151 - invert words in string - String - simulation
【无标题】
线程同步、生命周期
每日一题-LeetCode396-旋转函数-递推
《JVM系列》 第七章 -- 字节码执行引擎
8.2 text preprocessing
冰冰学习笔记:一步一步带你实现顺序表
Arduino for esp8266串口功能简介
Interviewer: let's talk about the process of class loading and the mechanism of class loading (parental delegation mechanism)
随机推荐
LeetCode167-两数之和II-双指针-二分-数组-查找
We reference My97DatePicker to realize the use of time plug-in
I/O复用的高级应用之一:非阻塞 connect———使用 select 实现(也可以用 poll 实现)
LeetCode 练习——396. 旋转函数
What is the role of the full connection layer?
Advanced application of I / O multiplexing: Processing TCP and UDP services at the same time
Leetcode153 - find the minimum value in the rotation sort array - array - binary search
Swift:Entry of program、Swift调用OC、@_silgen_name 、 OC 调用Swift、dynamic、String、Substring
async关键字
封面和标题中的关键词怎么写?做自媒体为什么视频没有播放量
we引用My97DatePicker 实现时间插件使用
Chapter 7 of JVM series -- bytecode execution engine
线程同步、生命周期
Brute force of DVWA low -- > High
Practice of unified storage technology of oppo data Lake
Epoll's et, lt working mode -- example program
LeetCode165-比较版本号-双指针-字符串
nuxt项目:全局获取process.env信息
Swift - Literal,字面量协议,基本数据类型、dictionary/array之间的转换
The difference between having and where in SQL