当前位置:网站首页>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
边栏推荐
- [NLP] HMM hidden Markov + Viterbi word segmentation
- Introduction to dirty reading, unrepeatable reading and phantom reading
- thinkphp5+数据大屏展示效果
- Difference between like and regexp
- 如何打开Win10启动文件夹?
- 面试官:说一下类加载的过程以及类加载的机制(双亲委派机制)
- Programming philosophy - automatic loading, dependency injection and control inversion
- 8.4 realization of recurrent neural network from zero
- I/O复用的高级应用:同时处理 TCP 和 UDP 服务
- qt之.pro文件详解
猜你喜欢
Don't you know the usage scenario of the responsibility chain model?
[jz46 translate numbers into strings]
eolink 如何助力远程办公
Leetcode153 - find the minimum value in the rotation sort array - array - binary search
剑指 Offer II 019. 最多删除一个字符得到回文(简单)
Is asemi ultrafast recovery diode interchangeable with Schottky diode
Explanation and example application of the principle of logistic regression in machine learning
Swift - literal, literal protocol, conversion between basic data types and dictionary / array
【STC8G2K64S4】比较器介绍以及比较器掉电检测示例程序
你还不知道责任链模式的使用场景吗?
随机推荐
select 同时接收普通数据 和 带外数据
Swift Protocol 关联对象 资源名称管理 多线程GCD 延迟 once
GIS数据处理-cesium中模型位置设置
Little red book timestamp2 (2022 / 04 / 22)
Introduction to Arduino for esp8266 serial port function
What is the role of the full connection layer?
async void 导致程序崩溃
Sqlserver transaction and lock problem
How does eolink help telecommuting
Epoll's et, lt working mode -- example program
Explain TCP's three handshakes in detail
Select receives both normal data and out of band data
Async void caused the program to crash
[detailed explanation of factory mode] factory method mode
编程哲学——自动加载、依赖注入与控制反转
《JVM系列》 第七章 -- 字节码执行引擎
Thinkphp5 + data large screen display effect
Thread synchronization, life cycle
Do (local scope), initializer, memory conflict, swift pointer, inout, unsafepointer, unsafebitcast, success
填充每个节点的下一个右侧节点指针 II [经典层次遍历 | 视为链表 ]