当前位置:网站首页>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
边栏推荐
- 自动化的艺术
- Borui data and F5 jointly build the full data chain DNA of financial technology from code to user
- Contraction mapping theorem
- Introduction to distributed transaction Seata
- 如何打开Win10启动文件夹?
- Detailed analysis of SQL combat of Niuke database (26-30)
- LeetCode149-直线上最多的点数-数学-哈希表
- 博睿数据携手F5共同构建金融科技从代码到用户的全数据链DNA
- 1990年1月1日是星期一,定义函数date_to_week(year,month,day),实现功能输入年月日后返回星期几,例如date_to_week(2020,11,1),返回:星期日。 提示:
- Using MATLAB programming to realize the steepest descent method to solve unconstrained optimization problems
猜你喜欢

免费在upic中设置OneDrive或Google Drive作为图床

Swift: entry of program, swift calls OC@_ silgen_ Name, OC calls swift, dynamic, string, substring

8.3 language model and data set

Vous ne connaissez pas encore les scénarios d'utilisation du modèle de chaîne de responsabilité?

LeetCode165-比较版本号-双指针-字符串

剑指 Offer II 019. 最多删除一个字符得到回文(简单)

Leetcode167 - sum of two numbers II - double pointer - bisection - array - Search

1-初识Go语言

Leetcode151 - invert words in string - String - simulation

自动化的艺术
随机推荐
How do I open the win10 startup folder?
1 - first knowledge of go language
UML学习_day2
js——實現點擊複制功能
Difference between like and regexp
Explanation and example application of the principle of logistic regression in machine learning
LeetCode167-两数之和II-双指针-二分-数组-查找
Leetcode exercise - 396 Rotation function
like和regexp差别
分布式事务Seata介绍
What is the main purpose of PCIe X1 slot?
封面和标题中的关键词怎么写?做自媒体为什么视频没有播放量
Swift protocol Association object resource name management multithreading GCD delay once
Swift - literal, literal protocol, conversion between basic data types and dictionary / array
Role of asemi rectifier module mdq100-16 in intelligent switching power supply
你還不知道責任鏈模式的使用場景嗎?
UML project example -- UML diagram description of tiktok
qt之.pro文件详解
8.2 text preprocessing
Realization of four data flow modes of grpc based on Multilingual Communication