当前位置:网站首页>黑马程序员线程安全问题
黑马程序员线程安全问题
2022-07-19 05:16:00 【闲易生事】
1.什么是线程安全问题?
* 多个线程同时操作同一个共享资源的时候可能会出现业务安全问题,称为线程安全问题。
2.线程安全问题出现的原因?
* 存在多线程并发
* 同时访问共享资源
* 存在修改共享资源
3.模拟线程安全问题
/*
* 目标:模拟取钱时线程安全问题
* */
public class ThreadDemo {
public static void main(String[] args) {
// 1.定义一个账户类,创建一个共同账户。
Account account=new Account("ICBC-111",100000.0);
// 2.创建2个线程对象,代表小红、小明进来取钱。
new DrawThread(account,"小明").start();
new DrawThread(account,"小红").start();
}
}
—————————————————————————————————————————————————————————————————————————————————————————
/*
* 线程类,一个线程代表一个取钱用户
* */
public class DrawThread extends Thread{
private Account account;
public DrawThread(){
}
public DrawThread(Account account,String name){
super(name);
this.account=account;
}
@Override
public void run() {
account.drawMoney(100000);
}
}
—————————————————————————————————————————————————————————————————————————————————————————
/*
* 账户类,对定义对账户的操作
* */
public class Account {
private String cardId;
private double money;
public Account (){
}
public Account (String cardId,double money){
this.cardId=cardId;
this.money=money;
}
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public void drawMoney(double money){
// 1.先获取谁来取钱,线程代表人
String name=Thread.currentThread().getName();
// 2.判断余额是否足够
if (this.money>= money ) {
System.out.println(name+"来取钱了,取走了"+money);
// 3.更新余额
this.money-=money;
System.out.println(name+"取钱后剩余"+this.money);
}else {
System.out.println(name+"来取钱,余额不足!");
}
}
}
4.线程同步解决安全问题的思想是什么?
* 加锁:让多个线程实现先后依次访问共享资源,这样就解决了安全问题。
5.同步代码块
* 作用:把出线程安全问题的核心代码给上锁,每次只能一个线程开锁进入访问。
6.锁对象的规范要求
* 规范上:建议使用共享资源作为锁对象
* 对于实例方法建议使用this作为锁对象
* 对于静态方法建议使用字节码(类名.class)对象作为锁对象。
7.同步方法
* 作用:把出现线程安全问题核心方法给上锁。
* 原理:每次只能一个线程进入,执行完毕以后自动解锁,其它线程才可以进来执行。
* 实现:在核心方法上加synchronize关键字
* 同步方法底层原理:同步方法其实底层也是有隐式锁对象的,只是锁的范围是整个方法代码。如果方法是实例方法,默认用this作为锁对象。但代码要高度面向对象。如果方法是静态方法,默认用类名.class作为锁对象。
8.Lock锁
* 为了更清晰的表达如何加锁和释放锁,JDK5以后提供了一个新的锁对象Lock,更加方便、灵活。
* Lock实现提供比使用synchronized方法更加广泛的锁定操作。
* Lock是接口不能直接实例化,这里采用它的实现类ReentrantLock来构建Lock锁对象。
边栏推荐
- FPGA - detailed explanation of SPI bus (concept)
- [2022/7/17]科研整理
- Panzer_Jack 的 个人博客成立日
- Experience of installing ROS in Jetson nano (failed)
- Leetcode:13.罗马数字转整数【键值对映射】
- FPGA skimming p2: multifunctional data processor, calculate the difference between two numbers, use generate For statement simplifies the code, uses sub modules to realize the size comparison of three
- 乐山师范程序设计大赛2020-D: 后缀语言【STL】
- (data and electricity) summary of various triggers - FPGA octet (1)
- 2021-09-30
- 我的2020年线上的夏令营总结
猜你喜欢
DHCP第一次静态实验
使用google cloud部署基于flask的网站
2022 -7-18 第八小组 顾宇佳 学习笔记
Log access development with one person per day and nine people per day -- project development practice based on instruction set Internet of things operating system
Openstack uses dashboard to perform image operations, manage instances, and other operations
网络安全学习(十三)数据链路层
软件工程期末复习思维导图
详细讲解JS中的加法(+)运算,基本数据类型相加,引用数据类型相加底层的运算规则,[]+{},{}+[]
TCP three handshakes and four swings
Network Security Learning (XXIV) transport layer protocol
随机推荐
NJU南京大学数电实验课大实验:字符小游戏
2022-7-12 第八小组 顾宇佳 (Js)
2022-7-12 Group 8 guyujia (JS)
使用gem5在fft上进行测试beta版本
equals和“==”的异同
乐山师范程序设计大赛2020-E: 分石头【01背包】
静态路由复习
Understanding of arm interrupt priority
Unity实用框架(四)全局数据管理框架
经纬度及其与坐标系的转换
Qt字符串操作
FPGA - detailed explanation of SPI bus (concept)
【PTA】7-24 约分最简分式 (15 分)
代码审计之oasys系统
南京大学计算方法(数值分析)期末复习笔记
浅谈Excel文件解析
The third "intelligence Cup" National College Students' IT skills competition (solution to group B of the preliminary competition)
LeetCode:第302场周赛(总结)
Qt解决大量重复数据的操作
解决问题phpstudy导入数据库失败