Experimental archive, the code is particularly bad.
Test. java
Packageoperating.test;ImportOperating.entity.bank.Bank;ImportOperating.entity.bank.EngineeringTeam; Public classTestbank { Public Static voidMain (string[] args) {Bank Bank=NewBank (10); for(inti = 0; I! = 3; ++i) {NewThread (NewEngineeringteam (5, bank)). Start (); } }}
/
Bank. Java
PackageOperating.entity.bank;Importjava.util.ArrayList; Public classBank {/*** Number of engineering teams*/ Private Static Final intNUM = 3; /*** Number of funds currently available*/ Private intavailable; /*** Assigned Matrix*/ Private int[] allocation =New int[NUM]; /*** Demand Matrix*/ Private int[] Need =New int[NUM]; PublicBank (intavailable) { This. Available =available; } /*** Borrow money from the bank *@paramID Team ID *@paramrequest funding from the engineering team *@returnA Boolean value that returns False if the loan is successfully returned to True*/ Public synchronized BooleanBorrow (intIdintrequest) {System.out.println ("The bank has left" + available + "million. "); if(Request <=Need[id]) { if(Request <=available) { intTemp1 =available; intTemp2 =Allocation[id]; intTemp3 =Need[id]; //tempted to borrow moneyAvailable = available-request; Allocation[id]= Allocation[id] +request; Need[id]= Need[id]-request; //Security Checks if(check ()) {System.out.println ("Engineering team" + ID + ": Borrow" + Request + "million success. "); return true; } Else { //cannot borrow, undo operationAvailable =Temp1; Allocation[id]=Temp2; Need[id]=Temp3; }}} System.out.println ("Engineering team" + ID + ": Borrow" + Request + "million failed. "); return false; } /*** Security Check *@returnThe current state returns true for the security state, otherwise false is returned. */ Private BooleanCheck () {intWork =available; Boolean[] finish =New Boolean[NUM]; for(inti = 0; I! = finish.length; ++i) {Finish[i]=false; } for(inti = 0; I! = NUM; ++i) { for(intj = 0; J! = NUM; ++j) {if(!finish[j] && Need[j] <=Work ) { Work= Work +Allocation[j]; FINISH[J]=true; } } } BooleanFlag =true; for(inti = 0; I! = NUM; ++i) {flag= Flag &&Finish[i]; } returnFlag; } /*** Add Team *@paramTeam*/ Public voidAddteam (Engineeringteam team) {intID =Team.getid (); Need[id]=team.getneed (); } /*** The team pays back the money *@param Money*/ Public voidPayback (intMoney ) {Available= Available +Money ; }}
/
The engineering team that borrows money. Java
PackageOperating.entity.bank;ImportOrg.omg.CORBA.INTERNAL; Public classEngineeringteamImplementsRunnable {/*** Total number of engineering teams*/ Private Static intTotal = 0; /*** Team ID*/ Private intID; /*** Amount of funds required to complete the project*/ Private intneed; /*** Target Funds*/ Private intMax; /*** Target Bank*/ PrivateBank Bank; PublicEngineeringteam (intneed, Bank Bank) {ID= total++; This. need = max =need; This. Bank =Bank; This. Bank.addteam ( This); } Public intgetId () {returnID; } Public intgetneed () {returnneed; } /*** Keep borrowing money until it's done*/@Override Public voidrun () { while(Need > 0) {System.out.println ("Engineering team" + ID + ": Also missing" + Need + "million. "); //Random borrowing intRequest = (int) (Math.random () *need) + 1; if(Bank.borrow (ID, request)) Need-=request; Try{Thread.Sleep (500); } Catch(interruptedexception e) {e.printstacktrace (); }} bank.payback (max); System.out.println ("----Engineering team + ID +": Complete the task. ----】"); }}
Java Banker algorithm