Java实现银行存取款
更新时间:2019年12月26日 15:10:52 作者:盈小盈*ZERO
这篇文章主要为大家详细介绍了Java实现银行存取款,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了Java银行存取款的具体代码,供大家参考,具体内容如下
1.不加锁情况
运行结果:

代码:将加锁情况中加锁部分进行注释即可
2.加锁情况
运行结果

缓冲区代码
package Bank;
import java.util.LinkedList;
public class BankAccount {
static double sum=1000;
private LinkedList<Object> list = new LinkedList<>();
//存款
public void deposit() {
synchronized(list)
{
System.out.print(list.size());
while(list.size()>1) {
System.out.println("暂不支持存款");
try {
// System.out.print("wait");
list.wait();
} catch (InterruptedException e) {
e.printStackTrace();
//System.out.print("wait");
}
}
list.add(new Object());
int money=300;
sum=sum+money;
System.out.println(Thread.currentThread().getName()+"存入了"+money+"元"+"现在共有存款"+sum);
list.notifyAll();
}
}
//取款
public void withdrawal() {
synchronized(list)
{
while(list.size()==0) {
// int money=50;
// sum=sum-money;
System.out.println(Thread.currentThread().getName()+"暂时不支持取款");
try {
list.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
list.remove();
int money=200;
if(sum>200)
{
sum=sum-money;
System.out.println(Thread.currentThread().getName()+"取出了"+money+"元"+"现在共有存款"+sum);
}else {
System.out.println("账户余额不足");
}
list.notify();
}
}
}
存款代码
package Bank;
public class Deposit implements Runnable {
private BankAccount bankAccount1;
public Deposit() {}
public Deposit(BankAccount bankAccount1) {
this.bankAccount1=bankAccount1;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true) {
try {
Thread.sleep(2000);
bankAccount1.deposit();
} catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}
取款代码
package Bank;
public class Withdrawal implements Runnable{
private BankAccount bankAccount;
public Withdrawal() {}
public Withdrawal(BankAccount bankAccount)
{
this.bankAccount=bankAccount;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true)
{
try {
Thread.sleep(3000);
bankAccount.withdrawal();
} catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}
主函数代码
package Bank;
public class Main {
public static void main(String[] args) {
BankAccount bankAccount1=new BankAccount();
Thread d1=new Thread(new Deposit(bankAccount1));
Thread d2=new Thread(new Deposit(bankAccount1));
Thread d3=new Thread(new Deposit(bankAccount1));
Thread w1=new Thread(new Withdrawal(bankAccount1));
Thread w2=new Thread(new Withdrawal(bankAccount1));
Thread w3=new Thread(new Withdrawal(bankAccount1));
d1.start();
d2.start();
d3.start();
w1.start();
w2.start();
w3.start();
}
}
更多学习资料请关注专题《管理系统开发》。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
java多线程之并发工具类CountDownLatch,CyclicBarrier和Semaphore
这篇文章主要为大家介绍了java并发工具类CountDownLatch,CyclicBarrier和Semaphore ,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助2021-12-12
Springboot开发之利用Docker和Kubernetes部署微服务
这篇文章主要介绍了如何将Spring Boot开发的微服务通过Docker容器化,并使用Kubernetes进行部署和管理,帮助读者掌握现代云原生应用的完整开发部署流程,有需要的可以了解下2025-03-03
GC调优实战之过早提升Premature Promotion
这篇文章主要为大家介绍了GC调优实战之过早提升Premature Promotion2022-01-01
Spring中的@Autowired、@Qualifier和@Primary注解详解
这篇文章主要介绍了Spring中的@Autowired、@Qualifier和@Primary注解详解,@Autowired 注解,可以对类成员变量、方法和构造函数进行标注,完成自动装配的工作,@Autowired 是默认根据 byType 进行自动装配的,需要的朋友可以参考下2023-11-11
Mybatisplus创建Spring Boot工程打包错误的解决方式
最近在实战springboot遇到了一些坑,记录一下,下面这篇文章主要给大家介绍了关于Mybatisplus创建Spring Boot工程打包错误的解决方式,文中通过图文介绍的介绍的非常详细,需要的朋友可以参考下2023-03-03


最新评论