Java實現(xiàn)銀行存取款
本文實例為大家分享了Java銀行存取款的具體代碼,供大家參考,具體內(nèi)容如下
1.不加鎖情況
運行結(jié)果:

代碼:將加鎖情況中加鎖部分進(jìn)行注釋即可
2.加鎖情況
運行結(jié)果

緩沖區(qū)代碼
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+"元"+"現(xiàn)在共有存款"+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+"元"+"現(xiàn)在共有存款"+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();
}
}
}
}
主函數(shù)代碼
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();
}
}
更多學(xué)習(xí)資料請關(guān)注專題《管理系統(tǒng)開發(fā)》。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java多線程之并發(fā)工具類CountDownLatch,CyclicBarrier和Semaphore
這篇文章主要為大家介紹了java并發(fā)工具類CountDownLatch,CyclicBarrier和Semaphore ,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2021-12-12
Springboot開發(fā)之利用Docker和Kubernetes部署微服務(wù)
這篇文章主要介紹了如何將Spring Boot開發(fā)的微服務(wù)通過Docker容器化,并使用Kubernetes進(jìn)行部署和管理,幫助讀者掌握現(xiàn)代云原生應(yīng)用的完整開發(fā)部署流程,有需要的可以了解下2025-03-03
GC調(diào)優(yōu)實戰(zhàn)之過早提升Premature?Promotion
這篇文章主要為大家介紹了GC調(diào)優(yōu)實戰(zhàn)之過早提升Premature?Promotion2022-01-01
Spring中的@Autowired、@Qualifier和@Primary注解詳解
這篇文章主要介紹了Spring中的@Autowired、@Qualifier和@Primary注解詳解,@Autowired?注解,可以對類成員變量、方法和構(gòu)造函數(shù)進(jìn)行標(biāo)注,完成自動裝配的工作,@Autowired?是默認(rèn)根據(jù)?byType?進(jìn)行自動裝配的,需要的朋友可以參考下2023-11-11
Mybatisplus創(chuàng)建Spring?Boot工程打包錯誤的解決方式
最近在實戰(zhàn)springboot遇到了一些坑,記錄一下,下面這篇文章主要給大家介紹了關(guān)于Mybatisplus創(chuàng)建Spring?Boot工程打包錯誤的解決方式,文中通過圖文介紹的介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03

