假设某家银行可接受顾客的取款和存款,每次进行一次取款或存款,便可计算出账户的总额。现有一个账户余额2000元,两名顾客每人分4次,每次取款100元。另有两名顾客每人分3次,每次存入100元。试编程通过多线程来模拟顾客的取款和存款操作。JAVA实现
时间: 2023-09-12 18:05:39 浏览: 127
### 回答1:
以下是一个简单的Java多线程模拟银行账户取款和存款操作的代码:
```java
public class BankAccount {
private int balance; // 银行账户余额
public BankAccount(int balance) {
this.balance = balance;
}
public synchronized void deposit(int amount) {
balance += amount;
System.out.println("Deposit: " + amount + ", Balance: " + balance);
}
public synchronized void withdraw(int amount) {
if (balance < amount) {
System.out.println("Insufficient balance!");
} else {
balance -= amount;
System.out.println("Withdrawal: " + amount + ", Balance: " + balance);
}
}
public static void main(String[] args) {
BankAccount account = new BankAccount(2000);
Thread[] threads = new Thread[4];
// 模拟4个顾客每人分4次取款
for (int i = 0; i < 4; i++) {
threads[i] = new Thread(() -> {
for (int j = 0; j < 4; j++) {
account.withdraw(100);
}
});
threads[i].start();
}
// 模拟2个顾客每人分3次存款
for (int i = 0; i < 2; i++) {
threads[i] = new Thread(() -> {
for (int j = 0; j < 3; j++) {
account.deposit(100);
}
});
threads[i].start();
}
// 等待所有线程执行完毕
for (int i = 0; i < threads.length; i++) {
try {
threads[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
```
在上述代码中,我们使用了Java的synchronized关键字来确保取款和存款操作的原子性,避免多个线程同时修改银行账户余额导致数据不一致的问题。同时,我们创建了4个线程来模拟4个顾客每人分4次取款,以及2个线程来模拟2个顾客每人分3次存款。最后,我们使用join()方法等待所有线程执行完毕,确保输出结果的正确性。
### 回答2:
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class BankingSimulation {
private static int balance = 2000;
private static Lock lock = new ReentrantLock();
public static void main(String[] args) {
CustomerWithdrawer customerWithdrawer1 = new CustomerWithdrawer("顾客1", 4);
CustomerWithdrawer customerWithdrawer2 = new CustomerWithdrawer("顾客2", 4);
CustomerDepositor customerDepositor1 = new CustomerDepositor("顾客3", 3);
CustomerDepositor customerDepositor2 = new CustomerDepositor("顾客4", 3);
Thread thread1 = new Thread(customerWithdrawer1);
Thread thread2 = new Thread(customerWithdrawer2);
Thread thread3 = new Thread(customerDepositor1);
Thread thread4 = new Thread(customerDepositor2);
thread1.start();
thread2.start();
thread3.start();
thread4.start();
try {
thread1.join();
thread2.join();
thread3.join();
thread4.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("账户最终余额:" + balance);
}
static void withdraw(int amount) {
lock.lock();
try {
if (balance >= amount) {
balance -= amount;
System.out.println(Thread.currentThread().getName() + " 取款 " + amount + " 元");
} else {
System.out.println(Thread.currentThread().getName() + " 余额不足");
}
} finally {
lock.unlock();
}
}
static void deposit(int amount) {
lock.lock();
try {
balance += amount;
System.out.println(Thread.currentThread().getName() + " 存款 " + amount + " 元");
} finally {
lock.unlock();
}
}
static class CustomerWithdrawer implements Runnable {
private String name;
private int repeat;
CustomerWithdrawer(String name, int repeat) {
this.name = name;
this.repeat = repeat;
}
@Override
public void run() {
for (int i = 0; i < repeat; i++) {
withdraw(100);
}
}
}
static class CustomerDepositor implements Runnable {
private String name;
private int repeat;
CustomerDepositor(String name, int repeat) {
this.name = name;
this.repeat = repeat;
}
@Override
public void run() {
for (int i = 0; i < repeat; i++) {
deposit(100);
}
}
}
}
### 回答3:
下面是一个使用Java多线程模拟顾客取款和存款操作的示例代码:
```java
public class Bank {
private int balance = 2000; // 初始账户余额
// 同步方法,用于取款操作
public synchronized void withdraw(int amount) {
// 检查是否有足够的余额进行取款
if (balance >= amount) {
System.out.println(Thread.currentThread().getName() + "正在取款 " + amount + " 元");
balance -= amount;
System.out.println("账户余额:" + balance + " 元");
} else {
System.out.println(Thread.currentThread().getName() + "尝试取款 " + amount + " 元,余额不足");
}
}
// 同步方法,用于存款操作
public synchronized void deposit(int amount) {
System.out.println(Thread.currentThread().getName() + "正在存款 " + amount + " 元");
balance += amount;
System.out.println("账户余额:" + balance + " 元");
}
public static void main(String[] args) {
Bank bank = new Bank();
// 创建四个线程代表四个顾客
Thread customer1 = new Thread(() -> {
for (int i = 0; i < 4; i++) {
bank.withdraw(100);
}
});
Thread customer2 = new Thread(() -> {
for (int i = 0; i < 4; i++) {
bank.withdraw(100);
}
});
Thread customer3 = new Thread(() -> {
for (int i = 0; i < 3; i++) {
bank.deposit(100);
}
});
Thread customer4 = new Thread(() -> {
for (int i = 0; i < 3; i++) {
bank.deposit(100);
}
});
// 启动四个顾客线程
customer1.start();
customer2.start();
customer3.start();
customer4.start();
}
}
```
这个示例代码中,Bank类代表一个银行,其中包含两个同步方法withdraw(用于取款)和deposit(用于存款),通过synchronized关键字来确保线程安全。四个顾客分别通过多线程进行取款和存款操作。
阅读全文