当前位置:网站首页>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();
}
}
边栏推荐
- AttributeError: module ‘torch._ C‘ has no attribute ‘_ cuda_ setDevice‘
- Auto. JS get all app names of mobile phones
- 【QT】自定义控件-Loading
- QSlider of QT control style series (I)
- HarmonyOS第四次培训
- Appium practice | make the test faster, more stable and more reliable (I): slice test
- Dbsync adds support for mongodb and ES
- Flask project uses flask socketio exception: typeerror: function() argument 1 must be code, not str
- Basic knowledge of road loss of 3GPP channel model
- Weebly mobile website editor mobile browsing New Era
猜你喜欢
Longest palindrome substring (dynamic programming)
Basic knowledge of road loss of 3GPP channel model
U++ 元数据说明符 学习笔记
When knative meets webassembly
Sublime tips
【二叉树】二叉树寻路
Operand of null-aware operation ‘!‘ has type ‘SchedulerBinding‘ which excludes null.
AOSP ~Binder 通信原理 (一) - 概要
Pointer and array are input in function to realize reverse order output
SQL injection - secondary injection and multi statement injection
随机推荐
背包问题(01背包,完全背包,动态规划)
拿到PMP认证带来什么改变?
Flask project uses flask socketio exception: typeerror: function() argument 1 must be code, not str
AOSP ~Binder 通信原理 (一) - 概要
Timer创建定时器
y58.第三章 Kubernetes从入门到精通 -- 持续集成与部署(三一)
Inventory host list in ansible (I wish you countless flowers and romance)
QT simple layout box model with spring
高手勿进!写给初中级程序员以及还在大学修炼的“准程序员”的成长秘籍
[Yugong series] go teaching course 005 variables in July 2022
带你遨游银河系的 10 种分布式数据库
DBSync新增对MongoDB、ES的支持
Longest non descent subsequence (LIS) (dynamic programming)
[Android kotlin collaboration] use coroutinecontext to realize the retry logic after a network request fails
U++ 游戏类 学习笔记
SQL injection cookie injection
高数中值定理总结
Y58. Chapter III kubernetes from entry to proficiency - continuous integration and deployment (Sany)
Ansible中的inventory主机清单(预祝你我有数不尽的鲜花和浪漫)
01 machine learning related regulations