当前位置:网站首页>模拟线程通信
模拟线程通信
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();
}
}
边栏推荐
- If you ask me about R code debugging, I will tell you head, STR, help
- 3GPP信道模型路损基础知识
- 当 Knative 遇见 WebAssembly
- window定时计划任务
- 接口间调用为什么要用json、fastjson怎么赋值的、fastjson [email protected]映射关系问题
- c语言神经网络基本代码大全及其含义
- App embedded H5 --- iPhone soft keyboard blocks input text
- Test interview | how much can you answer the real test interview question of an Internet company?
- 2. Overview of securities investment funds
- Why JSON is used for calls between interfaces, how fastjson is assigned, fastjson 1.2 [email protected] Mapping relatio
猜你喜欢
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
Leetcode(46)——全排列
Function pointer and pointer function in C language
Ansible概述和模块解释(你刚走过了今天,而扑面而来的却是昨天)
torch optimizer小解析
一个酷酷的“幽灵”控制台工具
AttributeError: module ‘torch._ C‘ has no attribute ‘_ cuda_ setDevice‘
【问道】编译原理
y58.第三章 Kubernetes从入门到精通 -- 持续集成与部署(三一)
qt 简单布局 盒子模型 加弹簧
随机推荐
记录一次压测经验总结
NiO related knowledge points (I)
Ansible overview and module explanation (you just passed today, but yesterday came to your face)
Flask project uses flask socketio exception: typeerror: function() argument 1 must be code, not str
Leetcode minimum difference in student scores
sublime使用技巧
《二》标签
谈谈讲清楚这件事的重要性
PLC Analog output analog output FB analog2nda (Mitsubishi FX3U)
JS variable plus
【PHP SPL笔记】
全国气象数据/降雨量分布数据/太阳辐射数据/NPP净初级生产力数据/植被覆盖度数据
The sooner you understand the four rules of life, the more blessed you will be
How to design API interface and realize unified format return?
Error: No named parameter with the name ‘foregroundColor‘
AttributeError: module ‘torch._ C‘ has no attribute ‘_ cuda_ setDevice‘
Inventory host list in ansible (I wish you countless flowers and romance)
如何设计 API 接口,实现统一格式返回?
Development thoughts of adding new requirements in secondary development
QT控件样式系列(一)之QSlider