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

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

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

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

1.不加鎖情況

運(yùn)行結(jié)果:

代碼:將加鎖情況中加鎖部分進(jìn)行注釋即可

2.加鎖情況

運(yùn)行結(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()+"暫時(shí)不支持取款");
 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í)資料請(qǐng)關(guān)注專題《管理系統(tǒng)開發(fā)》。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(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 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-12-12
  • Springboot開發(fā)之利用Docker和Kubernetes部署微服務(wù)

    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)實(shí)戰(zhàn)之過早提升Premature?Promotion

    GC調(diào)優(yōu)實(shí)戰(zhàn)之過早提升Premature?Promotion

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

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

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

    SpringBoot中的自定義Starter解讀

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

    Ubuntu 15下安裝JDK1.8教程

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

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

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

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

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

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

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

最新評(píng)論

安康市| 敦化市| 台前县| 岳阳市| 扶风县| 石渠县| 西藏| 阿拉善盟| 桃园县| 区。| 岳阳县| 手机| 金堂县| 长兴县| 鞍山市| 阿图什市| 雷山县| 平阳县| 维西| 江陵县| 塘沽区| 垣曲县| 周宁县| 玉山县| 泾源县| 丘北县| 裕民县| 邹城市| 贵州省| 洱源县| 达拉特旗| 深水埗区| 左云县| 蒙自县| 柘城县| 牡丹江市| 芮城县| 崇信县| 三亚市| 金昌市| 郯城县|