当前位置:网站首页>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
边栏推荐
- Programming philosophy - automatic loading, dependency injection and control inversion
- 编程哲学——自动加载、依赖注入与控制反转
- 牛客网数据库SQL实战详细剖析(26-30)
- Select receives both normal data and out of band data
- SQL中HAVING和WHERE的区别
- epoll 的 ET,LT工作模式———实例程序
- Flink datastream type system typeinformation
- Pnpm installation and use
- Alexnet model
- Chapter 7 of JVM series -- bytecode execution engine
猜你喜欢

1n5408-asemi rectifier diode

Using MATLAB programming to realize the steepest descent method to solve unconstrained optimization problems

Set up an AI team in the game world and start the super parametric multi-agent "chaos fight"

编程哲学——自动加载、依赖注入与控制反转

Model location setting in GIS data processing -cesium

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

we引用My97DatePicker 实现时间插件使用

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

What is the role of the full connection layer?

Leetcode exercise - 396 Rotation function
随机推荐
帧同步 实现
Swift protocol Association object resource name management multithreading GCD delay once
科技的成就(二十一)
Little red book timestamp2 (2022 / 04 / 22)
Introduction to distributed transaction Seata
编程哲学——自动加载、依赖注入与控制反转
冰冰学习笔记:一步一步带你实现顺序表
Leetcode162 - find peak - dichotomy - array
Chapter 7 of JVM series -- bytecode execution engine
2-GO variable operation
1-初识Go语言
分布式事务Seata介绍
SVN详细使用教程
I/O复用的高级应用之一:非阻塞 connect———使用 select 实现(也可以用 poll 实现)
GIS数据处理-cesium中模型位置设置
Model location setting in GIS data processing -cesium
One of the advanced applications of I / O reuse: non blocking connect -- implemented using select (or poll)
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
Epolloneshot event of epoll -- instance program
async关键字