当前位置:网站首页>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
边栏推荐
- VIM easy to use tutorial
- 分布式-总结列表
- Neural network - nonlinear activation
- Pytoch (III) -- function optimization
- 科研狗可能需要的一些工具
- Solve the problem that the external chain file of Qiankun sub application cannot be obtained
- LeetCode_ 58 (length of last word)
- VIM简易使用教程
- [une question par jour pendant l'été] course luogu p1568
- Shell analysis server log command collection
猜你喜欢

解决:拖动xib控件到代码文件中,报错setValue:forUndefinedKey:this class is not key value coding-compliant for the key
![[hard ten treasures] - 2 [basic knowledge] characteristics of various topological structures of switching power supply](/img/c2/6dfb9f477306edb46ff2a6a6ca32dd.png)
[hard ten treasures] - 2 [basic knowledge] characteristics of various topological structures of switching power supply

手动实现一个简单的栈

One click shell to automatically deploy any version of redis

Dual contractual learning: text classification via label aware data augmentation reading notes
![[hard ten treasures] - 1 [basic knowledge] classification of power supply](/img/a8/f129c9d15ca6ed99db1dacfc750ead.png)
[hard ten treasures] - 1 [basic knowledge] classification of power supply

STM32 photoresistor sensor & two channel AD acquisition

Pytest automated testing - compare robotframework framework

数据加载及预处理

神经网络-最大池化的使用
随机推荐
Measurement of quadrature axis and direct axis inductance of three-phase permanent magnet synchronous motor
[difficult] sqlserver2008r2, can you recover only some files when recovering the database?
分布式数据库数据一致性的原理、与技术实现方案
FileOutPutStream
【暑期每日一题】洛谷 P5886 Hello, 2020!
Pytoch (II) -- activation function, loss function and its gradient
[2020 overview] overview of link prediction based on knowledge map embedding
Pytorch(三) —— 函数优化
Technology sharing | broadcast function design in integrated dispatching
Collect the annual summary of laws, regulations, policies and plans related to trusted computing of large market points (national, ministerial, provincial and municipal)
LM small programmable controller software (based on CoDeSys) note 19: errors do not match the profile of the target
Common interview questions ①
Summary of acl2021 information extraction related papers
C#读写应用程序配置文件App.exe.config,并在界面上显示
C - detailed explanation of operators and summary of use cases
[pat (basic level) practice] - [simple simulation] 1064 friends
STM32 photoresistor sensor & two channel AD acquisition
Openresty rewrites the location of 302
About the transmission pipeline of stage in spark
Pytorch(四) —— 可视化工具 Visdom