当前位置:网站首页>Two methods of thread synchronization
Two methods of thread synchronization
2022-07-07 05:14:00 【Yang Asang 815】
1: Synchronization code block
public void UpdateMoney(Double price){
System.out.println(Thread.currentThread() + " Came in ");
synchronized (this){
if(price<=money){
// Withdraw money
System.out.println(Thread.currentThread()+" To withdraw money "+price);
money-=price;
System.out.println(" The remaining balance "+money);
}else {
System.out.println(Thread.currentThread()+" I'm sorry ! Lack of balance ");
}
}
}
synchronized ( Synchronization lock object ){ Code for operating shared resources }
Be careful. : The synchronization lock object should be unique , When using instance methods , It is recommended to use shared resources , such as this,
When using static methods , Bytecode is recommended , such as : Class name .class
Method 2: Synchronization method
/***
* The way to get money
* @param price Amount to be withdrawn
*/
public synchronized void UpdateMoney(Double price){
System.out.println(Thread.currentThread() + " Came in ");
Lock lock=new ReentrantLock();
lock.lock();
if(price<=money){
// Withdraw money
System.out.println(Thread.currentThread()+" To withdraw money "+price);
money-=price;
System.out.println(" The remaining balance "+money);
}else {
System.out.println(Thread.currentThread()+" I'm sorry ! Lack of balance ");
}
lock.unlock();
}
Modifier Back add synchronized, And then call Lock Interface ReentrantLock Implementation class ,
Use lock() Method lock ,unlock() Method release lock ,
边栏推荐
- 接口间调用为什么要用json、fastjson怎么赋值的、fastjson [email protected]映射关系问题
- Leetcode longest public prefix
- Salesforce 容器化 ISV 场景下的软件供应链安全落地实践
- qt 简单布局 盒子模型 加弹簧
- JS 的 try catch finally 中 return 的执行顺序
- Appium practice | make the test faster, more stable and more reliable (I): slice test
- Monitoring cannot be started after Oracle modifies the computer name
- QSlider of QT control style series (I)
- Redis如何实现多可用区?
- Test interview | how much can you answer the real test interview question of an Internet company?
猜你喜欢
Basic knowledge of road loss of 3GPP channel model
Operand of null-aware operation ‘!‘ has type ‘SchedulerBinding‘ which excludes null.
[Android kotlin collaboration] use coroutinecontext to realize the retry logic after a network request fails
Is PMP really useful?
Salesforce 容器化 ISV 场景下的软件供应链安全落地实践
【opencv】图像形态学操作-opencv标记不同连通域的位置
SQL injection - secondary injection and multi statement injection
Flask project uses flask socketio exception: typeerror: function() argument 1 must be code, not str
Ansible overview and module explanation (you just passed today, but yesterday came to your face)
- [email protected]映射关系问题"/>
接口间调用为什么要用json、fastjson怎么赋值的、fastjson [email protected]映射关系问题
随机推荐
高手勿进!写给初中级程序员以及还在大学修炼的“准程序员”的成长秘籍
【ArcGIS教程】专题图制作-人口密度分布图——人口密度分析
AttributeError: module ‘torch._ C‘ has no attribute ‘_ cuda_ setDevice‘
How to choose an offer and what factors should be considered
window定时计划任务
Full link voltage test: the dispute between shadow database and shadow table
JS 的 try catch finally 中 return 的执行顺序
torch optimizer小解析
qt 简单布局 盒子模型 加弹簧
QSlider of QT control style series (I)
Thread和Runnable创建线程的方式对比
磁盘监控相关命令
高数中值定理总结
Longest non descent subsequence (LIS) (dynamic programming)
Operand of null-aware operation ‘!‘ has type ‘SchedulerBinding‘ which excludes null.
Array initialization of local variables
U++ metadata specifier learning notes
LinkedBlockingQueue源码分析-初始化
Redis如何实现多可用区?
Timer创建定时器