当前位置:网站首页>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 ,
边栏推荐
- 3.基金的类型
- 线程同步的两个方法
- ASP. Net MVC - resource cannot be found error - asp Net MVC – Resource Cannot be found error
- 一个酷酷的“幽灵”控制台工具
- How to design API interface and realize unified format return?
- Longest common subsequence (LCS) (dynamic programming, recursive)
- Error: No named parameter with the name ‘foregroundColor‘
- y58.第三章 Kubernetes从入门到精通 -- 持续集成与部署(三一)
- 【ArcGIS教程】专题图制作-人口密度分布图——人口密度分析
- 【Android Kotlin协程】利用CoroutineContext实现网络请求失败后重试逻辑
猜你喜欢
使用知云阅读器翻译统计遗传学书籍
最长回文子串(动态规划)
Techniques d'utilisation de sublime
Basic knowledge of road loss of 3GPP channel model
Operand of null-aware operation ‘!‘ has type ‘SchedulerBinding‘ which excludes null.
Error: No named parameter with the name ‘foregroundColor‘
【Android Kotlin协程】利用CoroutineContext实现网络请求失败后重试逻辑
When knative meets webassembly
Window scheduled tasks
《四》表单
随机推荐
与利润无关的背包问题(深度优先搜索)
sublime使用技巧
U++ metadata specifier learning notes
2.证券投资基金的概述
使用知云阅读器翻译统计遗传学书籍
Dynamically generate tables
Ansible overview and module explanation (you just passed today, but yesterday came to your face)
torch optimizer小解析
磁盘监控相关命令
SQL injection HTTP header injection
局部变量的数组初始化问题
2039: [Bluebridge cup 2022 preliminaries] Li Bai's enhanced version (dynamic planning)
Liste des hôtes d'inventaire dans ansible (je vous souhaite des fleurs et de la romance sans fin)
Ansible reports an error: "MSG": "invalid/incorrect password: permission denied, please try again“
Stm32f103ze+sht30 detection of ambient temperature and humidity (IIC simulation sequence)
STM32F103实现IAP在线升级应用程序
Knapsack problem unrelated to profit (depth first search)
SQL injection - secondary injection and multi statement injection
Timer创建定时器
AttributeError: module ‘torch._ C‘ has no attribute ‘_ cuda_ setDevice‘