Question
For this problem we will simulate a bank. Our bank will track the balances in twenty different accounts. When the program begins, each of the
For this problem we will simulate a bank. Our bank will track the balances in twenty different accounts. When the program begins, each of the accounts contains $1000. The program will process a list of transactions which transfer money between the accounts. Once all transactions have been processed the program will go through and for each account it will print both the accounts final balance and the number of transactions (deposits and withdrawals) which occurred on that account.
Our bank program will use the main thread to read the list of banking transactions from a file. A separate set of worker threads will process the transactions and update the accounts. Javas BlockingQueue will handle most of the communication between the main thread and the worker threads.
Instead of using a GUI your program will run from the command line. When executing the program users will provide two pieces of information as parametersthe name of an external file listing all the transactions and the number of threads which will be used to process transactions. Here is an example showing a request to process the transactions in the file small.txt using four worker threads.
> java homework6/Bank small.txt 4
acct:0 bal:999 trans:1 acct:1 bal:1001 trans:1 acct:2 bal:999 trans:1 acct:3 bal:1001 trans:1 acct:4 bal:999 trans:1 acct:5 bal:1001 trans:1 acct:6 bal:999 trans:1 acct:7 bal:1001 trans:1 acct:8 bal:999 trans:1 acct:9 bal:1001 trans:1 acct:10 bal:999 trans:1 acct:11 bal:1001 trans:1
acct:12 bal:999 trans:1 acct:13 bal:1001 trans:1
acct:14 bal:999 trans:1 acct:15 bal:1001 trans:1 acct:16 bal:999 trans:1 acct:17 bal:1001 trans:1 acct:18 bal:999 trans:1 acct:19 bal:1001 trans:1
File Format: Each line in the external file represents a single transaction, and contains three numbers: the id of the account from which the money is being transferred, the id of the account to which the money is going, and the amount of money. For example the line:
17 6 104
indicates that $104 is being transferred from Account #17 to Account #6.
The test data provided includes transfers with the same from and to account numbers, so make sure your program will work correctly for these transfers. For example:
5 5 40
Count these as two transactions for the account (one transaction taking money from the account and one putting money into the account).
Termination: We need a way of communicating to the Worker threads when the main thread has finished loading all the transactions into the queue. Since the Worker threads are already getting information from the queue, one easy way to do this is to put a special value into the queue, indicating the main thread is done loading transactions. We cant use null because the BlockingQueue already uses null as a special value for its own communication purposes. Instead you can create a special transactionsomething like this:
private final Transaction nullTrans = new Transaction(-1,0,0); When your main thread is done reading in all the transactions, it places a series of nullTrans references into the queue. When the worker thread pulls a nullTrans out of the queue it knows that all the real transactions are done and it can terminate. Remember that the worker threads are actually removing these references from the queue, so youll need to add one per worker thread. Youll also need a way for the worker threads to let the main thread know when they are done. The main thread cant print out all the account balances until its sure that all transactions have not only been entered in the queue but actually been processed.
Basic Testing: Ive provided you with three files for testing purposessmall.txt, 5k.txt, and 100k.txt. You can find these files under Files -> Homework -> HW6 on Canvas. The correct output for small.txt is shown above. For both 5k.txt and 100k.txt after all the transactions have been processed all accounts should be back to their original $1000 balance level.
Details
I recommend a design with four classesBank, Account, Transaction, and Worker. Both the Account and Transactions classes are quite simple.
Account needs to store an id number, the current balance for the account, and the number of transactions that have occurred on the account. Remember that multiple worker threads may be accessing an account simultaneously and you must ensure that they cannot corrupt its data. You may also want to override the toString method to handle printing of account information.
Transaction is a simple class that stores information on each transaction (see below for more information about each transaction). If youre careful you can treat the Transaction as immutable. This means that you do not have to worry about multiple threads accessing it. Remember an immutable objects values never change, therefore its values are not subject to corruption in a concurrent environment.
The Bank class maintains a list of accounts and the BlockingQueue used to communicate between the main thread and the worker threads. The Bank is also responsible for starting up the worker threads, reading transactions from the file, and printing out all the account values when everything is done. Note: make sure you start up all the worker threads before reading the transactions from the file.
I recommend making the Worker class is an inner class of the Bank class. This way it gets easy access to the list of accounts and the queue used for communication. Workers should check the queue for transactions. If they find a transaction they should process it. If the queue is empty, they will wait for the Bank class to read in another transaction (youll get this behavior for free by using a BlockingQueue). Workers terminate when all the transactions have been processed.
Below are the starter files provided. Help would be HIGHLY appreciated.
import java.util.concurrent.*;
public class Bank {
BlockingQueue
private final Transaction nullTrans = new Transaction(-1,0,0);
public Bank() {
queue = new LinkedBlockingQueue();
}
// TODO: Add code for actually updating accounts.
// Should you make this synchronized? Why or why not?
// If not, how do you avoid concurrency issues?
public synchronized void processTransaction(Transaction t) {
System.out.println("Updating account with transaction");
}
private class Worker extends Thread {
public void run() {
try {
Transaction t;
do {
t = queue.take();
processTransaction(t);
System.out.println(this.getName() + t);
} while (t.fromAccount != -1);
} catch (InterruptedException e) {
System.out.println("interrupted");
}
System.out.println(this.getName() + "exiting");
}
}
public static void main(String[] args) {
Bank bank = new Bank();
// TODO: Replace the following with code to generate as
// many workers as needed. The number of workers is
// Given as a commandline argument.
Worker w1 = bank.new Worker();
Worker w2 = bank.new Worker();
try {
w1.start();
w2.start();
// TODO: replace the following with code for
// reading from the file and putting the transactions
// into the BlockingQueue
System.out.println("Putting 10 values from main");
for (int i = 0; i< 10; i++) {
bank.queue.put(new Transaction(
(int)(Math.random()*10),
(int)(Math.random()*10),
(int)(Math.random()*1000)));
}
bank.queue.put(bank.nullTrans);
bank.queue.put(bank.nullTrans);
System.out.println("Main finished adding all transactions");
// TODO: Add code here to wait for ALL the workers to finish
w1.join();
w2.join();
} catch (InterruptedException e) {
System.out.println("interrupted");
}
System.out.println("All threads done");
}
}
class Transaction {
int fromAccount;
int toAccount;
int amount;
public Transaction(int from, int to, int amt) {
fromAccount = from;
toAccount = to;
amount = amt;
}
public String toString() {
return "Transaction: from = " + fromAccount + ", to = " +
toAccount + " amount = " + amount;
}
}
//TODO: Create an Account class with id, num of transactions and account
balance
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started