Home > Back-end >  Multithreaded code of bank transfer, please help
Multithreaded code of bank transfer, please help

Time:09-18

Simulation implement multithreaded processing real-time bank transfer transactions, assuming that Banks had 20 account, each account has a capital of 1000 yuan, the program will achieve different amount of transfer transactions between each account, requirements are as follows:
(1) the main thread of trade information read from a file, to the more child thread processing, each thread is responsible for handling a transaction, the main thread and more child communication between threads based on producer/consumer model, is realized by using ArrayBlockingQueue
Create a file to save a large amount of trading information (at least 100), a line says a deal, format is as follows:
October 25, 2500
Said the 2500 yuan from a bank account for 10 to 25,
Tip: the main thread after the last transaction information in the Queue, should use the proper way to inform the child thread, such as the last one transaction can be set to (0, 1), the child thread cannot use null to determine whether the Queue is empty),
(2) suggest to create the following categories:
Account the Account, including Account ID, the current Balance Balance, already trading times TransanctionNum,
Bank Bank: including all accounts and keep the main thread and trading information between the child thread ArrayBlockingQueue,
(3) the child thread number can be used as a parameter passed in, or in the form of user input from the keyboard,
Run results should display all account information, according to the ascending order of accounts, the current balance, the number of transactions,
Run the example results are as follows:
Please enter the number of threads:
10
The acct: 01 bal: 251 trans: 1
The acct: 02 bal: 999 trans: 1
The acct: 03 bal: 3005 trans: 1
The acct: 04 bal: 1200 trans: 1
The acct: 05 bal: 1985 trans: 1




Below is a TXT file
1 2 425
1 6 125
5 7 161
August 15, 421
4 3 210
3, 4, 253
1 September 79
August 13, 180
October 1, 325
20 1 500
November 2, 321
5 to 16 48
June 17, 293
12 to 20 20
14 November 346
16 May 73
April 18, 222
3 the 19th 134
July 6, 74
- 1-1-1

CodePudding user response:

Package lightprocess;

Import the Java. IO. BufferedReader;
Import the Java. IO. FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
Import the Java. Util. Concurrent. ArrayBlockingQueue;
Import the Java. Util. Concurrent. BlockingQueue;
Import the Java. Util. Concurrent. TimeUnit;


The class Trans {
Int idf;
Int idt;
Int trmoney;
Public Trans (int idf, int idt, int trmoney) {
Enclosing the idf=idf;
This. Idt=idt;
Enclosing trmoney=trmoney;
}
}

The class Account {
Int ID, Balance, TransanctionNum;

Public Account (int ID, int the Balance, int TransanctionNum) {
Enclosing the Balance=the Balance;
This. ID=ID;
Enclosing TransanctionNum=TransanctionNum;
}

Public String toString () {
Return "acct:" + ID + "bal:" + + + TransanctionNum "trans" Balance;
}

}

Public class Bank implements Runnable {
BlockingQueue Tran.
The String fileName="D: \ \ \ \ middleware technology transform. TXT".
Static int [] [] shuju=new int [21] [3].

Public Bank (BlockingQueue Tran) {
This. Tran=tran;
}

Static class sonThread implements Runnable {
BlockingQueue Tran.
ArrayList The acc=new ArrayList (21);

Public sonThread (BlockingQueue Tran, ArrayList Acc) {
Enclosing the acc=acc;
This. Tran=tran;
}

Public void the run () {
Try {
A Boolean flag=true;
While (flag) {
Int money;
Trans t=tran. Take ();
Money=t.t rmoney;
If (t.i df!=1) {
Acc. Get (t.i df). The Balance=acc. Get t.i (df). The Balance - money;
Acc. Get (t.i dt). The Balance=acc. Get (t.i dt). The Balance + money;
Acc. Get (t.i df). TransanctionNum++;
Acc. Get (t.i dt). TransanctionNum++;
}
The else {
Flag=false;
}
}

{} catch InterruptedException (e)
e.printStackTrace();
Thread. CurrentThread (). Interrupt ();
}
}
}

Public void the run () {
Try {
The String fileName="D: \ \ \ \ middleware technology transform. TXT".
Int [] [] shuju=new int [20] [3].
System. The out. Println (Thread. CurrentThread (). The getName () + "is launched,");
String st=null;
Int line=0;
BufferedReader br=new BufferedReader (new FileReader (fileName));
St=br. ReadLine ();
While (st!=null) {
String s=[] st. split (" ");
for(int i=0; I<3; I++) {
Shuju [the line] [I]=Integer. The parseInt (s [I] the trim ());
}
Tran. Put (new Trans (shuju [the line] [0], shuju [the line] [1], shuju [the line] [2]));
Line++;
St=br. ReadLine ();
}
Br. The close ();
{} the catch (IOException e1)
e1.printStackTrace();
{} catch InterruptedException (e)
e.printStackTrace();
}
}

Public static void main (String [] args) throws InterruptedException {
ArrayList The account=new ArrayList (20);
For (int x=0; x<21. X++)
{
The Account ac=new Account (x, 1000, 0);
Account. Add (ac);
}
BlockingQueue Trans=new ArrayBlockingQueue (10);
Scanner sc=new Scanner(System.in);
System. The out. Print (" please enter the number of threads: ");
Int n=sc. NextInt ();
System.out.println("");

Bank Bank=new Bank (trans);
SonThread sT=new sonThread (trans, account);

New Thread (bank). The start ();
for(int i=0; INew Thread (sT, "sonThread" + (I + 1)). The start ();
}
for(int i=0; I<20; I++) {;
System. The out. Println (" acct: "+ account. Get (I). The ID +" bal: "+ account. Get (I). The Balance +" trans: "+ account. Get (I). TransanctionNum +" ");
}
}

}

CodePudding user response:

Your problem solved
  • Related