当前位置:网站首页>模拟线程通信
模拟线程通信
2022-07-06 23:09:00 【洋啊桑815】
1:多个线程共同操作的共享资源:User类 含有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;
}
/***
* 存钱的方法
* @param money
*/
public synchronized void updateMoney(Double money){
try {
System.out.println(Thread.currentThread()+"进来了");
Thread.sleep(2000);
if(this.money==0){
this.money+=money;
System.out.println(Thread.currentThread()+"存钱"+money);
this.notifyAll();
this.wait();
}else{
this.notifyAll();
this.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/***
* 取钱的方法的方法
* @param v
*/
public synchronized void deleteMoney(double v) {
try {
System.out.println(Thread.currentThread()+"进来了");
Thread.sleep(2000);
if(this.money>=v){
this.money-=v;
System.out.println(Thread.currentThread()+"取钱"+v);
this.notifyAll();
this.wait();
}else{
this.notifyAll();
this.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
创建两个线程:存款线程与取款线程
//取钱
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);
}
}
}
//存钱
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);
}
}
}
主方法:
public class TestDamo {
public static void main(String[] args) {
User user=new User(1000);
new GetThread(user,"小明1").start();
new GetThread(user,"小明2").start();
new SetThread(user,"小红1").start();
new SetThread(user,"小红2").start();
new SetThread(user,"小红3").start();
}
}
边栏推荐
- Can I specify a path in an attribute to map a property in my class to a child property in my JSON?
- Analysis -- MySQL statement execution process & MySQL architecture
- Pointer and array are input in function to realize reverse order output
- torch optimizer小解析
- U++4 接口 学习笔记
- Basic knowledge of road loss of 3GPP channel model
- 【PHP SPL笔记】
- Ansible reports an error: "MSG": "invalid/incorrect password: permission denied, please try again“
- A simple and beautiful regression table is produced in one line of code~
- ASP. Net MVC - resource cannot be found error - asp Net MVC – Resource Cannot be found error
猜你喜欢
Weebly mobile website editor mobile browsing New Era
SQL injection HTTP header injection
No experts! Growth secrets for junior and intermediate programmers and "quasi programmers" who are still practicing in Universities
Monitoring cannot be started after Oracle modifies the computer name
Ansible中的inventory主机清单(预祝你我有数不尽的鲜花和浪漫)
U++4 interface learning notes
带你遨游银河系的 10 种分布式数据库
torch optimizer小解析
当 Knative 遇见 WebAssembly
Salesforce 容器化 ISV 场景下的软件供应链安全落地实践
随机推荐
U++ game learning notes
3. Type of fund
Ansible中的inventory主机清单(预祝你我有数不尽的鲜花和浪漫)
01 machine learning related regulations
【Android Kotlin协程】利用CoroutineContext实现网络请求失败后重试逻辑
[hand torn STL] list
qt 简单布局 盒子模型 加弹簧
Ansible中的inventory主機清單(預祝你我有數不盡的鮮花和浪漫)
精彩速递|腾讯云数据库6月刊
Leetcode(417)——太平洋大西洋水流问题
npm ERR! 400 Bad Request - PUT xxx - “devDependencies“ dep “xx“ is not a valid dependency name
Gavin teacher's perception of transformer live class - rasa project actual combat e-commerce retail customer service intelligent business dialogue robot microservice code analysis and dialogue experim
5G VoNR+之IMS Data Channel概念
ASP. Net MVC - resource cannot be found error - asp Net MVC – Resource Cannot be found error
App embedded H5 --- iPhone soft keyboard blocks input text
Analysis -- MySQL statement execution process & MySQL architecture
R descriptive statistics and hypothesis testing
JS input and output
Basic knowledge of road loss of 3GPP channel model
vector和类拷贝构造函数