当前位置:网站首页>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();
}
}边栏推荐
- [Android kotlin collaboration] use coroutinecontext to realize the retry logic after a network request fails
- 最长不下降子序列(LIS)(动态规划)
- 一个酷酷的“幽灵”控制台工具
- sublime使用技巧
- 2039: [Bluebridge cup 2022 preliminaries] Li Bai's enhanced version (dynamic planning)
- 局部变量的数组初始化问题
- Why do many people misunderstand technical debt
- 精彩速递|腾讯云数据库6月刊
- Comparison between thread and runnable in creating threads
- 2. Overview of securities investment funds
猜你喜欢

Y58. Chapter III kubernetes from entry to proficiency - continuous integration and deployment (Sany)

SQL injection cookie injection

带你遨游银河系的 10 种分布式数据库

window定时计划任务
![[Yugong series] go teaching course 005 variables in July 2022](/img/29/2bb30443e1e418556b5e08932f75b4.png)
[Yugong series] go teaching course 005 variables in July 2022

No experts! Growth secrets for junior and intermediate programmers and "quasi programmers" who are still practicing in Universities

Longest palindrome substring (dynamic programming)

U++4 interface learning notes

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

Mysql database (basic)
随机推荐
Weebly mobile website editor mobile browsing New Era
Error: No named parameter with the name ‘foregroundColor‘
Analysis -- MySQL statement execution process & MySQL architecture
app内嵌h5---iphone软键盘遮挡输入文字
Leetcode longest public prefix
《四》表单
磁盘监控相关命令
【二叉树】二叉树寻路
腾讯云数据库公有云市场稳居TOP 2!
Knapsack problem (01 knapsack, complete knapsack, dynamic programming)
想要选择一些部门优先使用 OKR, 应该如何选择试点部门?
If you‘re running pod install manually, make sure flutter pub get is executed first.
Linkedblockingqueue source code analysis - initialization
torch optimizer小解析
JS 的 try catch finally 中 return 的执行顺序
带你遨游银河系的 10 种分布式数据库
ThinkPHP关联预载入with
最长不下降子序列(LIS)(动态规划)
线程池的创建与使用
2039: [蓝桥杯2022初赛] 李白打酒加强版 (动态规划)