java存取款遇到的问题_java 线程同步问题之银行存取款问题

本文通过一个Java实例展示了如何处理银行存取款的线程同步问题。在示例中,创建了存款和取款线程,通过`synchronized`关键字确保对账户余额的修改是原子性的,避免并发冲突。程序会随机休眠线程以模拟不同操作的时间,并在所有线程结束后打印最终余额。
摘要由CSDN通过智能技术生成

1.[代码]java 线程同步问题之银行存取款问题

public class SaveAndFetchMoney {

/**

* 1928106_付军_计科81

*/

public static void main(String[] args) {

int num_of_save = 20; // 存款线程数

int num_of_fetch = 100; // 取款线程数

Thread[] thread = new Thread[num_of_save];

Thread[] thread1 = new Thread[num_of_fetch];

Account t = new Account("1928106", 10000);

for (int i = 0; i < num_of_save; i++) {// 创建存款线程并启动

thread[i] = new Thread(new SaveMoney(t,

(float) Math.random() * 1000));

thread[i].start();

}

for (int i = 0; i < num_of_fetch; i++) {// 创建取款线程并启动

thread1[i] = new Thread(new FetchMoney(t,

(float) Math.random() * 1000));

thread1[i].start();

}

for (int i = 0; i < num_of_save; i++) {// 等待所有存款线程结束

try {

thread[i].join();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

for (int i = 0; i < num_of_fetch; i++) {// 等待所有取款线程结束

try {

thread1[i].join();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

System.out.println("账号:" + t.accountName + " 最终余额为:" + t.balance);

}

}

class Account {

String accountName;

float balance;

Account(String accountName, int value) {

this.accountName = accountName;

this.balance = value;

}

public synchronized void add(float amount) {// 模拟存款操作

System.out.print("账号:" + this.accountName + " 的原来余额为:" + this.balance);

balance += amount;

System.out.println(" 存入" + amount + "元后,现在余额为:" + this.balance);

}

public synchronized void sub(float amount) {// 模拟取款操作

if (balance >= amount) {

System.out.print("账号:" + this.accountName + " 的原来余额为:"

+ this.balance);

balance -= amount;

System.out.println(" 取出" + amount + "元后,现在余额为:" + this.balance);

} else {

System.out.println("账号:" + this.accountName + " 的原来余额为:"

+ this.balance + "要取出的金额为:" + amount + " 余额不足!无法完成取款操作!");

}

}

}

class SaveMoney implements Runnable {

Account a;

float amount;

SaveMoney(Account a, float amount) {

this.a = a;

this.amount = amount;

}

public void run() {

try {

Thread.sleep((int) Math.random() * 2000); // 设定随机休眠时间

} catch (InterruptedException e) {

e.printStackTrace(); // 追踪异常事件发生时执行堆栈的内容

System.out.println(e.toString()); // 打印输出异常的信息的简短描述

}

a.add(amount);

}

}

class FetchMoney implements Runnable {

Account a;

float amount;

FetchMoney(Account a, float amount) {

this.a = a;

this.amount = amount;

}

public void run() {

try {

Thread.sleep((int) Math.random() * 2000); // 设定随机休眠时间

} catch (InterruptedException e) {

e.printStackTrace(); // 追踪异常事件发生时执行堆栈的内容

System.out.println(e.toString()); // 打印输出异常的信息的简短描述

}

if (a.balance >= amount) {

a.sub(amount);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值