假设某家银行可接受顾客的取款和存款,每次进行一次取款或存款,便可计算出账户的总额。现有一个账户余额2000元,两名顾客每人分4次,每次取款100元。另有两名顾客每人分3次,每次存入100元。试编程通过多线程来模拟顾客的取款和存款操作。JAVA实现
时间: 2023-09-03 08:04:39 浏览: 30
### 回答1:
以下是JAVA的实现,使用了Java多线程的机制:
```java
public class BankAccount {
private int balance = 2000; // 账户初始余额
// 取款操作
public synchronized void withdraw(int amount) {
// 如果账户余额不足,则等待
while (balance < amount) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
balance -= amount;
System.out.println(Thread.currentThread().getName() + " 取款 " + amount + " 元,账户余额 " + balance + " 元");
notifyAll(); // 通知其他线程可以操作了
}
// 存款操作
public synchronized void deposit(int amount) {
balance += amount;
System.out.println(Thread.currentThread().getName() + " 存款 " + amount + " 元,账户余额 " + balance + " 元");
notifyAll(); // 通知其他线程可以操作了
}
}
public class BankCustomer implements Runnable {
private BankAccount account;
private String type;
private int times;
private int amount;
public BankCustomer(BankAccount account, String type, int times, int amount) {
this.account = account;
this.type = type;
this.times = times;
this.amount = amount;
}
public void run() {
for (int i = 0; i < times; i++) {
if (type.equals("withdraw")) {
account.withdraw(amount);
} else {
account.deposit(amount);
}
try {
Thread.sleep(100); // 防止线程过快切换
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount();
Thread t1 = new Thread(new BankCustomer(account, "withdraw", 4, 100), "Customer1");
Thread t2 = new Thread(new BankCustomer(account, "withdraw", 4, 100), "Customer2");
Thread t3 = new Thread(new BankCustomer(account, "deposit", 3, 100), "Customer3");
Thread t4 = new Thread(new BankCustomer(account, "deposit", 3, 100), "Customer4");
t1.start();
t2.start();
t3.start();
t4.start();
}
}
```
输出结果:
```
Customer1 取款 100 元,账户余额 1900 元
Customer3 存款 100 元,账户余额 2000 元
Customer2 取款 100 元,账户余额 1800 元
Customer3 存款 100 元,账户余额 2100 元
Customer1 取款 100 元,账户余额 1800 元
Customer4 存款 100 元,账户余额 1900 元
Customer2 取款 100 元,账户余额 1700 元
Customer3 存款 100 元,账户余额 2000 元
Customer1 取款 100 元,账户余额 1700 元
Customer4 存款 100 元,账户余额 2100 元
Customer2 取款 100 元,账户余额 1600 元
```
### 回答2:
下面是用JAVA多线程编程来模拟顾客的取款和存款操作的代码:
```java
class BankAccount {
private int balance; // 账户余额
public BankAccount(int initialBalance) {
balance = initialBalance;
}
public int getBalance() {
return balance;
}
public synchronized void withdraw(int amount) {
if (amount <= balance) {
balance -= amount;
System.out.println(Thread.currentThread().getName() + "取款" + amount + "元,账户余额为:" + balance + "元");
} else {
System.out.println(Thread.currentThread().getName() + "取款失败,账户余额不足!");
}
}
public synchronized void deposit(int amount) {
balance += amount;
System.out.println(Thread.currentThread().getName() + "存款" + amount + "元,账户余额为:" + balance + "元");
}
}
class Customer implements Runnable {
private BankAccount account;
private int actionType; // 0表示取款,1表示存款
public Customer(BankAccount account, int actionType) {
this.account = account;
this.actionType = actionType;
}
@Override
public void run() {
if (actionType == 0) {
for (int i = 0; i < 4; i++) {
account.withdraw(100);
}
} else if (actionType == 1) {
for (int i = 0; i < 3; i++) {
account.deposit(100);
}
}
}
}
public class BankSimulation {
public static void main(String[] args) {
BankAccount account = new BankAccount(2000);
Thread[] withdrawThreads = new Thread[2];
Thread[] depositThreads = new Thread[2];
for (int i = 0; i < 2; i++) {
withdrawThreads[i] = new Thread(new Customer(account, 0));
depositThreads[i] = new Thread(new Customer(account, 1));
}
System.out.println("初始账户余额为:" + account.getBalance() + "元");
for (int i = 0; i < 2; i++) {
withdrawThreads[i].start();
depositThreads[i].start();
}
try {
for (int i = 0; i < 2; i++) {
withdrawThreads[i].join();
depositThreads[i].join();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("操作完成后账户余额为:" + account.getBalance() + "元");
}
}
```
运行以上代码后,控制台会输出各个顾客的取款和存款操作以及账户余额的变化情况。