Question: This is an excercise in Multithreading based on the code BankTest.java from Liang (Introduction to java Programming).Found on bottom of post. Write the class
This is an excercise in Multithreading based on the code BankTest.java from Liang (Introduction to java Programming).Found on bottom of post.
Write the class " Bank" to simulate a bank with a certain amount of accounts and clients ( 10 in this example).
Each client has their own account to withdraw money to deposit to another account.
The client should only be able to withdraw from the account if the balance allows it. If the clients accountbalance does not allow withdrawal, he must wait for another client to transfer the amount to his account first.
Each client should be implemented as a thread in this program.
The clients will loop forever, and should do the following: - Choose random account to transfer money
- Choose a random amount to transfer
- If balance allows it, excecute money transfer
- If balance does not allow, wait for money t be transfered to account, before making the transfer.
- Pause for 15 milliseconds
To make sure no money is lost durig hese transactions, the class Bank should have a method Test() to check the sum of all accounts, to check the total sum has not decreased. This method should be run every 100th transfer. Do NOT use "synchronized" anywhere in the code.
Create 10 accounts, 10000 should be initial amount on each account. Bank total is the 100 000.
UML-diagram for the classes:
Class BankTest.java
public class BankTest {
private static final int NACCOUNTS = 10;
private static final int INITIAL_BALANCE = 10000;
public static void main(String[] args) {
Bank b = new Bank(NACCOUNTS, INITIAL_BALANCE);
int i;
for (i = 0; i
TransferThread t = new TransferThread(b, i,
INITIAL_BALANCE);
t.setPriority(Thread.NORM_PRIORITY + i % 2);
t.start();
}
}
}
class Bank {
private static final int NTEST = 10000;
private final int[] accounts;
private long numberOfTransactions = 0;
public Bank(int n, int initialBalance) {
accounts = new int[n];
int i;
for (i = 0; i
accounts[i] = initialBalance;
numberOfTransactions = 0;
}
public void transfer(int from, int to, int amount) {
accounts[from] -= amount;
accounts[to] += amount;
numberOfTransactions++;
if (numberOfTransactions % NTEST == 0) test();
}
public void test() {
int sum = 0;
for (int account : accounts) sum += account;
System.out.println("Transactions:" + numberOfTransactions + " Sum: " +
sum);
}
public int size() {
return accounts.length;
}
}
class TransferThread extends Thread {
private Bank bank;
private int fromAccount;
private int maxAmount;
private static final int REPS = 1000;
public TransferThread(Bank b, int from, int max) {
bank = b;
fromAccount = from;
maxAmount = max;
}
public void run() {
try {
while (!interrupted()) {
for (int i = 0; i
int toAccount = ( int ) (bank.size() * Math.random());
int amount = ( int ) (maxAmount * Math.random() / REPS);
bank.transfer(fromAccount, toAccount, amount);
sleep(1);
}
}
} catch (InterruptedException ignored) {
}
}
}
class Account {
private int balance;
private int accountNumber;
public Account(int accountNumber, int balance) {
this.accountNumber = accountNumber;
this.balance = balance;
}
void withdraw(int amount) {
balance -= amount;
}
void deposit(int amount) {
balance += amount;
}
int getAccountNumber() {
return accountNumber;
}
public int getBalance() {
return balance;
}
}
Thank you very much!
Account TO STRING FREQUENCY int int Lock long int int Account[ boolean int O e balance O i accountNumber int int Lock O i lockCondition Condition a TEST FREQUENCY 0 i lock D a transactionCount Os deviationCount D initialBalance O i accounts O a debug O s testCount ?"a Bank(int, int, boolean) m transfer(int, int, int) m s test m toStringo lock mAccount(int, int) O depositint) o getBalance withdrawlint) void void ..getAccountNumberO int int getLock) void void String int O getTransactionCounto long int i getErrorPercentage) double numberOfAccounts0 ? getDeviationCount) G AccountThreads D a bank Q a debug D accountIndex O i maxTransferAmount D a random m AccountThreads(Bank, int, int, boolean) BankTest Bank boolean int int Random boolean ACCOUNT AMOUNT int INITIAL BALANCE int void DEBUG main(StringD void
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
