Java implementation of bank deposit and withdrawal

Time:2021-2-21

This example for you to share the Java bank deposit and withdrawal of the specific code, for your reference, the specific content is as follows

1. No locking

Results of operation:

Code: comment the locking part in the locking situation

2. Locking

Running results

Buffer code

package Bank;
 
import java.util.LinkedList;
 
public class BankAccount {
 static double sum=1000;
 
 private LinkedList<Object> list = new LinkedList<>();
 //Deposit
 public void deposit() {
 synchronized(list)
 {
 System.out.print(list.size());
 while(list.size()>1) {
 
 System.out.println ("deposit not supported for the time being");
 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() + "deposited" + money "+" Yuan "+" current joint deposit "+ sum)";
   list.notifyAll();
 }
 }
 //Withdrawal
 public void withdrawal() {
 synchronized(list)
 {
 while(list.size()==0) {
// int money=50;
// sum=sum-money;
 System.out.println ( Thread.currentThread (). Getname() + "withdrawal is not supported at the moment");
 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() + "taken out" + money + "Yuan" + "current joint deposit" + sum) ";
 }else {
 System.out.println ("insufficient account balance");
 }
 list.notify();
 
 }
 }
 
}

Deposit code


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();
 }
 }
 }
}

Withdrawal code


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();
 }
 }
 }
}

Main function code


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();
 
 }
}

For more learning materials, please pay attention to the topic management system development.

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.

Recommended Today

Background management system menu management module

1 menu management page design 1.1 business design Menu management, also known as resource management, is the external manifestation of system resources. This module is mainly to add, modify, query and delete the menu. CREATE TABLE `sys_menus` ( `id` int(11) NOT NULL AUTO_INCREMENT, `Name ` varchar (50) default null comment ‘resource name’, `URL ` varchar […]