当前位置:网站首页>Simulate thread communication
Simulate thread communication
2022-07-07 05:14:00 【Yang Asang 815】
1: Shared resources operated by multiple threads :User class contain money deposit , And how to save and withdraw money
public class User {
private double money;
public User() {
}
public User(double money) {
this.money = money;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
/***
* A way to save money
* @param money
*/
public synchronized void updateMoney(Double money){
try {
System.out.println(Thread.currentThread()+" Came in ");
Thread.sleep(2000);
if(this.money==0){
this.money+=money;
System.out.println(Thread.currentThread()+" Deposit money "+money);
this.notifyAll();
this.wait();
}else{
this.notifyAll();
this.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/***
* The method of withdrawing money
* @param v
*/
public synchronized void deleteMoney(double v) {
try {
System.out.println(Thread.currentThread()+" Came in ");
Thread.sleep(2000);
if(this.money>=v){
this.money-=v;
System.out.println(Thread.currentThread()+" Withdraw money "+v);
this.notifyAll();
this.wait();
}else{
this.notifyAll();
this.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Create two threads : Deposit thread and withdrawal thread
// Withdraw money
public class GetThread extends Thread{
private User user;
public GetThread(User user,String name) {
super(name);
this.user = user;
}
@Override
public void run(){
while (true) {
user.deleteMoney(1000.0);
}
}
}
// Deposit money
public class SetThread extends Thread {
private User user;
public SetThread(User user,String name) {
super(name);
this.user = user;
}
@Override
public void run(){
while (true) {
user.updateMoney(10000.0);
}
}
}
Main method :
public class TestDamo {
public static void main(String[] args) {
User user=new User(1000);
new GetThread(user," Xiao Ming 1").start();
new GetThread(user," Xiao Ming 2").start();
new SetThread(user," Xiaohong 1").start();
new SetThread(user," Xiaohong 2").start();
new SetThread(user," Xiaohong 3").start();
}
}
边栏推荐
- 使用知云阅读器翻译统计遗传学书籍
- Operand of null-aware operation ‘!‘ has type ‘SchedulerBinding‘ which excludes null.
- SQL injection - secondary injection and multi statement injection
- Ansible overview and module explanation (you just passed today, but yesterday came to your face)
- STM32F103实现IAP在线升级应用程序
- Operand of null-aware operation ‘!‘ has type ‘SchedulerBinding‘ which excludes null.
- STM32F103 realize IAP online upgrade application
- U++ metadata specifier learning notes
- Complete code of C language neural network and its meaning
- 如何设计 API 接口,实现统一格式返回?
猜你喜欢
Pointer and array are input in function to realize reverse order output
Complete code of C language neural network and its meaning
Techniques d'utilisation de sublime
批量归一化(标准化)处理
Weebly移动端网站编辑器 手机浏览新时代
Salesforce 容器化 ISV 场景下的软件供应链安全落地实践
Ansible中的inventory主机清单(预祝你我有数不尽的鲜花和浪漫)
- [email protected] Mapping relatio"/>
Why JSON is used for calls between interfaces, how fastjson is assigned, fastjson 1.2 [email protected] Mapping relatio
Flask project uses flask socketio exception: typeerror: function() argument 1 must be code, not str
一文搞懂常见的网络I/O模型
随机推荐
《二》标签
Comparison between thread and runnable in creating threads
Operand of null-aware operation ‘!‘ has type ‘SchedulerBinding‘ which excludes null.
模拟线程通信
Inventory host list in ansible (I wish you countless flowers and romance)
Addressable 预下载
Time complexity & space complexity
SQL injection cookie injection
Error: No named parameter with the name ‘foregroundColor‘
Longest common subsequence (LCS) (dynamic programming, recursive)
高手勿进!写给初中级程序员以及还在大学修炼的“准程序员”的成长秘籍
DBSync新增对MongoDB、ES的支持
Basic knowledge of road loss of 3GPP channel model
Markdown编辑器
STM32封装ESP8266一键配置函数:实现实现AP模式和STA模式切换、服务器与客户端创建
Servicemesh mainly solves three pain points
Development thoughts of adding new requirements in secondary development
带你遨游银河系的 10 种分布式数据库
Flask project uses flask socketio exception: typeerror: function() argument 1 must be code, not str
U++ 游戏类 学习笔记