当前位置:网站首页>Suppose a bank's ATM machine, which allows users to deposit and withdraw money. Now there is 200 yuan in an account, and both user a and user B have the right to deposit and withdraw money from this a
Suppose a bank's ATM machine, which allows users to deposit and withdraw money. Now there is 200 yuan in an account, and both user a and user B have the right to deposit and withdraw money from this a
2022-07-05 06:19:00 【aigo-2021】
Suppose a bank ATM machine , It allows users to deposit and withdraw . Now there is a deposit in an account 200 element , user A And the user B Both have the right to deposit and withdraw money from this account . user A Will deposit 100 element , And users B Remove 50 element , Then the final deposit in the account should be 250 element . The actual operation process is as follows :
(1) to A Deposit operation :
1) Get the deposit amount of the account 200, Time consuming 2s.
2) Increase the account amount 100, Time consuming negligible
3) The newly generated account result 300 Back to ATM On the server of the machine , Time consuming 2s(2) Proceed again B Withdrawal operation :
4) The amount of deposit in the account after being increased 300, Time consuming 2s.
5) Judge whether the withdrawal amount is less than the account balance , if , Reduce the account amount 50, Otherwise, an exception will be thrown , Time consuming negligible .
6) The newly generated account result 250 Back to ATM On the server of the machine , Time consuming 2s.
Please according to the above requirements , take A Operation and B The operations of are represented by threads , Write a Java The program realizes this function .
class ATM {
private float account;
public ATM(float money) {
this.account = money;
}
public synchronized float getAccount(){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return account;
}
public synchronized void deposit(float money){
account+=money;
}
public synchronized void drawMoney(float money) throws Exception {
if (account < money) {
throw new Exception(" Lack of balance ");
}
account -= money;
}
}
class UserA extends Thread{
private ATM atm;
private float money;
public UserA(ATM a,float m){
atm=a;
money=m;
}
public void run(){
System.out.println(" The amount deposited in the account is :"+atm.getAccount()+" element ");
System.out.println(" user A Save "+money+" element " );
try{
atm.deposit(money);
}
catch(Exception e){ }
System.out.println(" The balance is :"+atm.getAccount()+" element ");
}
}
class UserB extends Thread{
private ATM atm;
private float money;
public UserB(ATM a,float m){
atm=a;
money=m;
}
public void run(){
System.out.println(" The amount deposited in the account is :"+atm.getAccount()+" element ");
System.out.println(" user B Take out "+money+" element " );
try{
atm.drawMoney(money);
System.out.println(" The balance is :"+atm.getAccount()+" element ");
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
}
class TestATM{
public static void main(String[] args) {
ATM a=new ATM(200);
UserA userA=new UserA(a,100);
UserB userB=new UserB(a,50);
userA.start();
try {
userA.join();// The current thread is blocked , Until the execution of another thread is completed , Unblock current thread
} catch (InterruptedException e) {
userB.start();
}
System.out.println("==================");
userB.start();
try {
userB.join();
} catch (InterruptedException e) {
userA.start();
}
}
}Running results :

边栏推荐
- MatrixDB v4.5.0 重磅发布,全新推出 MARS2 存储引擎!
- Navicat连接Oracle数据库报错ORA-28547或ORA-03135
- 传统数据库逐渐“难适应”,云原生数据库脱颖而出
- 高斯消元 AcWing 884. 高斯消元解异或线性方程组
- How to understand the definition of sequence limit?
- Appium foundation - use the first demo of appium
- [BMZCTF-pwn] ectf-2014 seddit
- Daily question 1189 Maximum number of "balloons"
- Records of some tools 2022
- Introduction to LVS [unfinished (semi-finished products)]
猜你喜欢

QQ computer version cancels escape character input expression

Is it impossible for lamda to wake up?

MySQL advanced part 1: index

阿里新成员「瓴羊」正式亮相,由阿里副总裁朋新宇带队,集结多个核心部门技术团队

可变电阻器概述——结构、工作和不同应用

Simple selection sort of selection sort

求组合数 AcWing 888. 求组合数 IV

SPI details

MySQL advanced part 2: optimizing SQL steps

Appium自动化测试基础 — Appium测试环境搭建总结
随机推荐
【Rust 笔记】16-输入与输出(上)
Erreur de connexion Navicat à la base de données Oracle Ora - 28547 ou Ora - 03135
redis发布订阅命令行实现
Leetcode divide and conquer / dichotomy
Sword finger offer II 058: schedule
Appium基础 — 使用Appium的第一个Demo
Leetcode-1200: minimum absolute difference
Leetcode dynamic programming
[2021]GIRAFFE: Representing Scenes as Compositional Generative Neural Feature Fields
A reason that is easy to be ignored when the printer is offline
博弈论 AcWing 893. 集合-Nim游戏
快速使用Amazon MemoryDB并构建你专属的Redis内存数据库
Chapter 6 relational database theory
Leetcode-9: palindromes
How to set the drop-down arrow in the spinner- How to set dropdown arrow in spinner?
Filter the numbers and pick out even numbers from several numbers
Error ora-28547 or ora-03135 when Navicat connects to Oracle Database
博弈论 AcWing 892. 台阶-Nim游戏
11-gorm-v2-02-create data
实时时钟 (RTC)