当前位置:网站首页>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
边栏推荐
- Measurement of quadrature axis and direct axis inductance of three-phase permanent magnet synchronous motor
- LeetCode_ 53 (maximum subarray and)
- RuntimeError: mean(): input dtype should be either floating point or complex dtypes. Got Long instead
- 【暑期每日一题】洛谷 P7222 [RC-04] 信息学竞赛
- 无器械健身
- 解决:拖动xib控件到代码文件中,报错setValue:forUndefinedKey:this class is not key value coding-compliant for the key
- pytorch神经网络搭建 模板
- Common interview questions ①
- 科研狗可能需要的一些工具
- Openresty rewrites the location of 302
猜你喜欢

Sorting out 49 reports of knowledge map industry conference | AI sees the future with wisdom

RuntimeError: “max_pool2d“ not implemented for ‘Long‘

Neural network - nonlinear activation

STM32扩展板 温度传感器和温湿度传感器的使用
![[hard ten treasures] - 1 [basic knowledge] classification of power supply](/img/a8/f129c9d15ca6ed99db1dacfc750ead.png)
[hard ten treasures] - 1 [basic knowledge] classification of power supply

分布式全局唯一ID解决方案详解

JVM栈和堆简介

技术分享| 融合调度中的广播功能设计

Neural network convolution layer

Pytorch(一) —— 基本语法
随机推荐
LeetCode_66(加一)
LeetCode_35(搜索插入位置)
缓冲流与转换流
One click shell to automatically deploy any version of redis
How do I sort a list of strings in dart- How can I sort a list of strings in Dart?
【硬十宝典目录】——转载自“硬件十万个为什么”(持续更新中~~)
Common UNIX Operation and maintenance commands of shell
Summary of testing experience - Testing Theory
神经网络-最大池化的使用
Pytorch(四) —— 可视化工具 Visdom
Difficulties in the development of knowledge map & the importance of building industry knowledge map
Section 27 remote access virtual private network workflow and experimental demonstration
Neural network convolution layer
Daily algorithm & interview questions, 28 days of special training in large factories - the 13th day (array)
C#读写应用程序配置文件App.exe.config,并在界面上显示
最长递增子序列及最优解、动物总重量问题
Design experience of Meizhou clinical laboratory
神经网络-卷积层
LeetCode_ 35 (search insertion position)
LeetCode_28(实现 strStr())