Java实现银行存取款的方法

  介绍

这篇文章给大家分享的是有关Java实现银行存取款的方法的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

具体内容如下

<强> 1。不加锁情况

运行结果:

癑ava实现银行存取款的方法"

代码:将加锁情况中加锁部分进行注释即可

<强> 2。加锁情况

运行结果

癑ava实现银行存取款的方法"

<强>缓冲区代码

package 银行;   ,   import  java.util.LinkedList;   ,   public  class  BankAccount  {   ,static  double 金额=1000;   ,   ,private  LinkedList list =, new  LinkedList<在();   ,//存款   ,public  void 存款(),{   ,同步(列表)   ,{   ,System.out.print (list.size ());   ,而(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 对象());   int 才能;钱=300;   ,=+钱总和;   ,System.out.println (Thread.currentThread () . getname() +“存入了“+钱+“元”+“现在共有存款“+总和);   ,,list.notifyAll ();   ,}   ,}   ,//取款   ,public  void 撤军(),{   ,同步(列表)   ,{   ,而(list.size ()==0), {//,int 钱=50;//,和=sum-money;   ,System.out.println (Thread.currentThread () . getname() +“暂时不支持取款“);   ,try  {   list.wait才能();   ,}catch  (InterruptedException  e), {   e.printStackTrace才能();   ,}   ,}   ,list.remove ();   ,int 钱=200;   ,如果(sum> 200)   ,{   ,金额=sum-money;   ,System.out.println (Thread.currentThread () . getname() +“取出了“+钱+“元”+“现在共有存款“+总和);   ,}else  {   ,System.out.println(“账户余额不足“);   ,}   ,list.notify ();   ,   ,}   ,}   ,   }

存款代码

package 银行;   ,   public  class  Deposit  implements  Runnable  {   ,private  BankAccount  bankAccount1;   ,public 存款(),{}   ,   ,public 存款(BankAccount  bankAccount1), {   ,this.bankAccount1=bankAccount1;   ,}   ,   ,@Override   ,public  void  run (), {   ,//TODO  Auto-generated  method 存根   ,而(真正的){   ,try  {   ,thread . sleep (2000);   ,bankAccount1.deposit ();   ,}catch  (InterruptedException  e), {   ,//TODO: handle 例外   ,e.printStackTrace ();   ,}   ,}   ,}   }

取款代码

package 银行;   ,   public  class  Withdrawal  implements  Runnable {   ,   ,private  BankAccount  bankAccount;   ,   ,public 撤军(),{}   ,   ,public 撤军(BankAccount  bankAccount)   ,{   ,this.bankAccount=bankAccount;   ,}   ,   ,@Override   ,public  void  run (), {   ,//TODO  Auto-generated  method 存根   ,而(真正的)   ,{   ,try  {   ,thread . sleep (3000);   ,bankAccount.withdrawal ();   ,   ,}catch  (InterruptedException  e), {   ,//TODO: handle 例外   ,e.printStackTrace ();   ,}   ,}   ,}   }

主函数代码

package 银行;   ,   public  class  Main  {   ,public  static  void  main (String [], args), {   ,BankAccount  bankAccount1=new  BankAccount ();   ,   d1, Thread =new 线程(new 存款(bankAccount1));   d2, Thread =new 线程(new 存款(bankAccount1));   d3, Thread =new 线程(new 存款(bankAccount1));   ,   w1, Thread =new 线程(new 撤军(bankAccount1));   w2, Thread =new 线程(new 撤军(bankAccount1));   w3, Thread =new 线程(new 撤军(bankAccount1));   ,   ,d1.start ();   ,d2.start ();   ,d3.start ();   ,w1.start ();   ,w2.start ();   ,w3.start ();   ,   ,}   }

感谢各位的阅读!关于“Java实现银行存取款的方法”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

Java实现银行存取款的方法

Copyright © 2020-2023 feiqueyun.cn. All Rights Reserved. 肥雀云_南京肥雀信息技术有限公司版权所有 南京肥雀信息技术有限公司 苏ICP备16063723号-5