当前位置:网站首页>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 :

边栏推荐
- Shutter web hardware keyboard monitoring
- Gauss Cancellation acwing 884. Solution d'un système d'équations Xor linéaires par élimination gaussienne
- Matrixdb V4.5.0 was launched with a new mars2 storage engine!
- MySQL怎么运行的系列(八)14张图说明白MySQL事务原子性和undo日志原理
- MIT-6874-Deep Learning in the Life Sciences Week 7
- Leetcode backtracking method
- Sum of three terms (construction)
- 数据可视化图表总结(一)
- 博弈论 AcWing 891. Nim游戏
- SQLMAP使用教程(一)
猜你喜欢

1.13 - RISC/CISC

开源存储这么香,为何我们还要坚持自研?

MySQL advanced part 2: SQL optimization

RGB LED infinite mirror controlled by Arduino

Network security skills competition in Secondary Vocational Schools -- a tutorial article on middleware penetration testing in Guangxi regional competition

Simple selection sort of selection sort

LeetCode 0107. Sequence traversal of binary tree II - another method

Leetcode-6108: decrypt messages

2021apmcm post game Summary - edge detection

Sqlmap tutorial (II) practical skills I
随机推荐
Leetcode-9: palindromes
区间问题 AcWing 906. 区间分组
Sqlmap tutorial (II) practical skills I
栈 AcWing 3302. 表达式求值
【LeetCode】Easy | 20. Valid parentheses
1040 Longest Symmetric String
[rust notes] 15 string and text (Part 1)
[leetcode] day95 effective Sudoku & matrix zeroing
【Rust 笔记】17-并发(下)
Record the process of configuring nccl and horovod in these two days (original)
Real time clock (RTC)
Presentation of attribute value of an item
Leetcode-6108: decrypt messages
Error ora-28547 or ora-03135 when Navicat connects to Oracle Database
[rust notes] 17 concurrent (Part 2)
In depth analysis of for (VaR I = 0; I < 5; i++) {settimeout (() => console.log (I), 1000)}
LeetCode-54
Simple selection sort of selection sort
Groupbykey() and reducebykey() and combinebykey() in spark
Gaussian elimination acwing 884 Gauss elimination for solving XOR linear equations