当前位置:网站首页>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 ,
边栏推荐
- 《五》表格
- ThinkPHP关联预载入with
- ClickHouse(03)ClickHouse怎么安装和部署
- STM32F103 realize IAP online upgrade application
- Dynamically generate tables
- Ansible报错:“msg“: “Invalid/incorrect password: Permission denied, please try again.“
- STM32封装ESP8266一键配置函数:实现实现AP模式和STA模式切换、服务器与客户端创建
- Knapsack problem (01 knapsack, complete knapsack, dynamic programming)
- Array initialization of local variables
- Clickhouse (03) how to install and deploy Clickhouse
猜你喜欢

Function pointer and pointer function in C language

Is it necessary to renew the PMP certificate?

Operand of null-aware operation ‘!‘ has type ‘SchedulerBinding‘ which excludes null.

【问道】编译原理

SQL injection cookie injection

If you‘re running pod install manually, make sure flutter pub get is executed first.

基于Bevy游戏引擎和FPGA的双人游戏

Leetcode(46)——全排列

How to design API interface and realize unified format return?

U++4 interface learning notes
随机推荐
Ansible中的inventory主机清单(预祝你我有数不尽的鲜花和浪漫)
Leetcode(46)——全排列
c语言神经网络基本代码大全及其含义
线程同步的两个方法
Monitoring cannot be started after Oracle modifies the computer name
SQL injection HTTP header injection
Longest non descent subsequence (LIS) (dynamic programming)
Stm32f103ze+sht30 detection of ambient temperature and humidity (IIC simulation sequence)
PMP证书有没有必要续期?
模拟线程通信
基于Bevy游戏引擎和FPGA的双人游戏
Decorator basic learning 02
与利润无关的背包问题(深度优先搜索)
sublime使用技巧
window定时计划任务
No experts! Growth secrets for junior and intermediate programmers and "quasi programmers" who are still practicing in Universities
2.证券投资基金的概述
2. Overview of securities investment funds
局部变量的数组初始化问题
IMS data channel concept of 5g vonr+