java:多线程实现银行ATM机,要求有存款机,取款机和存取款一体机三种类型的机器;每一种机器至少有两个用户使用。
时间: 2024-05-03 07:19:03 浏览: 190
好的,下面是一个简单的 Java 多线程实现银行 ATM 机的示例代码:
```java
// 存款机类
class DepositMachine implements Runnable {
private Bank bank;
private String name;
public DepositMachine(Bank bank, String name) {
this.bank = bank;
this.name = name;
}
public void run() {
while (true) {
int amount = (int) (Math.random() * 1000);
bank.deposit(name, amount);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
// 取款机类
class WithdrawMachine implements Runnable {
private Bank bank;
private String name;
public WithdrawMachine(Bank bank, String name) {
this.bank = bank;
this.name = name;
}
public void run() {
while (true) {
int amount = (int) (Math.random() * 1000);
bank.withdraw(name, amount);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
// 存取款一体机类
class ATM implements Runnable {
private Bank bank;
private String name;
public ATM(Bank bank, String name) {
this.bank = bank;
this.name = name;
}
public void run() {
while (true) {
int amount = (int) (Math.random() * 1000);
if (Math.random() < 0.5) {
bank.deposit(name, amount);
} else {
bank.withdraw(name, amount);
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
// 银行类
class Bank {
private Map<String, Integer> accounts;
public Bank() {
accounts = new HashMap<String, Integer>();
accounts.put("Alice", 1000);
accounts.put("Bob", 2000);
accounts.put("Charlie", 3000);
}
public synchronized void deposit(String name, int amount) {
int balance = accounts.get(name);
balance += amount;
accounts.put(name, balance);
System.out.println(name + " deposited " + amount + ", balance is " + balance);
}
public synchronized void withdraw(String name, int amount) {
int balance = accounts.get(name);
if (balance < amount) {
System.out.println(name + " tried to withdraw " + amount + ", but balance is not enough");
return;
}
balance -= amount;
accounts.put(name, balance);
System.out.println(name + " withdrew " + amount + ", balance is " + balance);
}
}
// 测试类
public class BankTest {
public static void main(String[] args) {
Bank bank = new Bank();
Thread[] threads = new Thread[6];
threads[0] = new Thread(new DepositMachine(bank, "Alice"));
threads[1] = new Thread(new DepositMachine(bank, "Bob"));
threads[2] = new Thread(new WithdrawMachine(bank, "Alice"));
threads[3] = new Thread(new WithdrawMachine(bank, "Bob"));
threads[4] = new Thread(new ATM(bank, "Charlie"));
threads[5] = new Thread(new ATM(bank, "Charlie"));
for (int i = 0; i < 6; i++) {
threads[i].start();
}
}
}
```
上面的代码定义了三个类:`DepositMachine` 表示存款机,`WithdrawMachine` 表示取款机,`ATM` 表示存取款一体机;还有一个 `Bank` 类表示银行。在 `DepositMachine`、`WithdrawMachine` 和 `ATM` 类中,都实现了 `Runnable` 接口,并且在 `run` 方法中模拟了用户进行存款、取款或存取款操作。在 `Bank` 类中,使用了一个 `Map` 来存储每个用户的账户余额,并且实现了 `deposit` 和 `withdraw` 方法来处理存款和取款操作,同时使用了 `synchronized` 关键字来保证线程安全。
在测试类 `BankTest` 中,创建了 6 个线程,其中前两个线程分别表示 Alice 和 Bob 使用存款机进行存款,第三个和第四个线程表示 Alice 和 Bob 使用取款机进行取款,最后两个线程表示 Charlie 使用存取款一体机进行存取款操作。运行程序后,可以看到多个线程同时对银行账户进行操作,但是由于使用了 `synchronized` 关键字,所以不会出现线程安全问题。
阅读全文