当前位置:网站首页>Thread safety issues
Thread safety issues
2022-07-01 04:48:00 【Yangasang 815】
Thread safety issues refer to : When multiple threads operate a shared resource at the same time , Business security issues may arise ,
It usually happens when you modify the same shared resource with your colleagues .
Let me give you an example of this code :
The first is this shared account class People, It contains updateMoney() Method , Used to modify the amount stored inside Money,
public class People {
private double money;
public People() {
}
public People(double money) {
this.money = money;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public void updateMoney(double price){
System.out.println(" The current thread has connected to the account , The account balance :"+money);
System.out.println(Thread.currentThread().getName()+" Withdraw money :"+price);
money-=price;
System.out.println(" The account balance :"+money);
}
}
The second is its main class ;
public class Test1 {
public static void main(String[] args) throws Exception {
People people=new People(1000.0);// Put... In your account 1000 element
// Create two threads
new MyThread(people,"1 Number ").start();
new MyThread(people,"2 Number ").start();
}
}
class MyThread extends Thread{
private People people;
public MyThread(People people,String name) {
super(name);
this.people=people;
}
@Override
public void run(){
// call People Methods , Take it out of it 1000 element ;
people.updateMoney(1000.0);
}
}Of course , There's a problem , The two threads created execute simultaneously , They will simultaneously move from People Remove from 1000 element , And originally People There is only 1000 element ,name Will result in the following results ;
The current thread has connected to the account , The account balance :1000.0
The current thread has connected to the account , The account balance :1000.0
2 No. to withdraw money :1000.0
1 No. to withdraw money :1000.0
The account balance :0.0
The account balance :-1000.0
This causes thread safety problems
边栏推荐
- Shell之一键自动部署Redis任意版本
- Difference between cookie and session
- [daily question in summer] letter delivery by p1629 postman in Luogu (to be continued...)
- Query long transaction
- Pytorch(三) —— 函数优化
- FileInputStream
- The longest increasing subsequence and its optimal solution, total animal weight problem
- 【硬十宝典目录】——转载自“硬件十万个为什么”(持续更新中~~)
- RuntimeError: mean(): input dtype should be either floating point or complex dtypes.Got Long instead
- Daily algorithm & interview questions, 28 days of special training in large factories - the 13th day (array)
猜你喜欢

分布式架构系统拆分原则、需求、微服务拆分步骤

【硬十宝典】——1.【基础知识】电源的分类

LeetCode316-去除重复字母-栈-贪心-字符串

This sideline workload is small, 10-15k, free unlimited massage
![[summer daily question] Luogu p5886 Hello, 2020!](/img/ac/4be05f80aab7fb766674e6e2d16fbc.png)
[summer daily question] Luogu p5886 Hello, 2020!

pytorch中常用数据集的使用方法

Section 27 remote access virtual private network workflow and experimental demonstration

STM32 光敏电阻传感器&两路AD采集

神经网络-卷积层

手动实现一个简单的栈
随机推荐
技术分享| 融合调度中的广播功能设计
Pytoch (IV) -- visual tool visdom
Take a cold bath
Pytoch (I) -- basic grammar
【硬十宝典】——2.【基础知识】开关电源各种拓扑结构的特点
C read / write application configuration file app exe. Config and display it on the interface
Pytorch(三) —— 函数优化
PR 2021 quick start tutorial, learn about the and functions of the timeline panel
Talk about testdeploy
[difficult] sqlserver2008r2, can you recover only some files when recovering the database?
CF1638E. Colorful operations Kodori tree + differential tree array
STM32扩展板 温度传感器和温湿度传感器的使用
Pytoch (III) -- function optimization
LeetCode316-去除重复字母-栈-贪心-字符串
Leecode record 1351 negative numbers in statistical ordered matrix
Pytoch (II) -- activation function, loss function and its gradient
LeetCode_ 66 (plus one)
[daily question in summer] Luogu p1568 race
LeetCode_ 35 (search insertion position)
About the transmission pipeline of stage in spark