最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實現(xiàn)銀行存取款

 更新時間:2019年12月26日 15:10:52   作者:盈小盈*ZERO  
這篇文章主要為大家詳細介紹了Java實現(xiàn)銀行存取款,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Java銀行存取款的具體代碼,供大家參考,具體內(nèi)容如下

1.不加鎖情況

運行結(jié)果:

代碼:將加鎖情況中加鎖部分進行注釋即可

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)文章

  • Springboot常用注解及作用說明

    Springboot常用注解及作用說明

    這篇文章主要介紹了Springboot常用注解及作用說明,Springboot開發(fā)中注解是非常重要的不可或缺的,那么Springboot中有哪些常用的注解呢,今天我們就來看一下這些注解和其作用,需要的朋友可以參考下
    2023-08-08
  • java多線程之并發(fā)工具類CountDownLatch,CyclicBarrier和Semaphore

    java多線程之并發(fā)工具類CountDownLatch,CyclicBarrier和Semaphore

    這篇文章主要為大家介紹了java并發(fā)工具類CountDownLatch,CyclicBarrier和Semaphore ,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-12-12
  • Springboot開發(fā)之利用Docker和Kubernetes部署微服務(wù)

    Springboot開發(fā)之利用Docker和Kubernetes部署微服務(wù)

    這篇文章主要介紹了如何將Spring Boot開發(fā)的微服務(wù)通過Docker容器化,并使用Kubernetes進行部署和管理,幫助讀者掌握現(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?Promotion

    這篇文章主要為大家介紹了GC調(diào)優(yōu)實戰(zhàn)之過早提升Premature?Promotion
    2022-01-01
  • spring boot實現(xiàn)文件上傳

    spring boot實現(xiàn)文件上傳

    這篇文章主要為大家詳細介紹了spring boot實現(xiàn)文件上傳,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • SpringBoot中的自定義Starter解讀

    SpringBoot中的自定義Starter解讀

    這篇文章主要介紹了SpringBoot中的自定義Starter解讀,啟動器模塊其實是一個空的jar文件,里面沒有什么類、接口,僅僅是提供輔助性依賴管理,這些依賴可能用于自動裝配或者其他類庫,需要的朋友可以參考下
    2023-12-12
  • Ubuntu 15下安裝JDK1.8教程

    Ubuntu 15下安裝JDK1.8教程

    這篇文章主要為大家詳細介紹了Ubuntu 15下JDK1.8安裝教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • Spring中的@Autowired、@Qualifier和@Primary注解詳解

    Spring中的@Autowired、@Qualifier和@Primary注解詳解

    這篇文章主要介紹了Spring中的@Autowired、@Qualifier和@Primary注解詳解,@Autowired?注解,可以對類成員變量、方法和構(gòu)造函數(shù)進行標(biāo)注,完成自動裝配的工作,@Autowired?是默認根據(jù)?byType?進行自動裝配的,需要的朋友可以參考下
    2023-11-11
  • Mybatisplus創(chuàng)建Spring?Boot工程打包錯誤的解決方式

    Mybatisplus創(chuàng)建Spring?Boot工程打包錯誤的解決方式

    最近在實戰(zhàn)springboot遇到了一些坑,記錄一下,下面這篇文章主要給大家介紹了關(guān)于Mybatisplus創(chuàng)建Spring?Boot工程打包錯誤的解決方式,文中通過圖文介紹的介紹的非常詳細,需要的朋友可以參考下
    2023-03-03
  • SpringMVC訪問靜態(tài)資源的方法

    SpringMVC訪問靜態(tài)資源的方法

    本篇文章主要介紹了SpringMVC訪問靜態(tài)資源的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02

最新評論

万盛区| 雷山县| 宾川县| 芦溪县| 深州市| 离岛区| 绥德县| 定安县| 简阳市| 东乡县| 新余市| 柏乡县| 那坡县| 望城县| 仪征市| 五台县| 永寿县| 泉州市| 巴林左旗| 保靖县| 磴口县| 普定县| 集安市| 双柏县| 南通市| 双流县| 教育| 上高县| 渭源县| 逊克县| 简阳市| 金秀| 泰宁县| 中方县| 兴仁县| 萝北县| 永兴县| 遂宁市| 宜君县| 股票| 海伦市|