Question: package threads; public class BankAccount { public BankAccount(LogView logView) { this.logView = logView; } public synchronized void deposit(int amount, BankAccountUser user) { int newBalance =


package threads; public class BankAccount { public BankAccount(LogView logView) { this.logView = logView; } public synchronized void deposit(int amount, BankAccountUser user) { int newBalance = balance + amount; logView.log(" " +user.getName()+ " Depositing $" +amount); balance = balance + amount; logView.log(". Balance = " + balance); checkFinished(user); assert(balance == newBalance); // should be true if this method is thread-safe user.notifyAll(); } public synchronized void withdraw(int amount, BankAccountUser user) { int newBalance = balance - amount; logView.log(" " +user.getName() + " Withdrawing $" + amount); if ( amount > balance ) { while(amount > balance){ try{ user.wait(); } catch(InterruptedException ex){ user.run(); } } //throw new RuntimeException(" ERROR: Amount (" +amount+ ") must not be greater than the balance (" +balance+ ")."); } balance = balance - amount; logView.log(". Balance = " + balance); checkFinished(user); assert(balance == newBalance); // should be true if this method is thread-safe } private void checkFinished(BankAccountUser user) { if (user.getTransactionsRemaining() == 1) { logView.log(" ****************************** " + user.getName() + " finished. ******************************"); } } public void setBalance(int balance) { this.balance = balance; } public int getBalance() { return balance; } private final LogView logView; private int balance; }
o Create a new class called BankAccountRescuer that extends BankAccountUser Create a BankAccountRescuer constructor that: Takes a name, a BankAccount, and an array of BankAccountUsers as parameters Appropriately calls super (can send a null list of transactions, but the BankAccountUser constructor will have to deal with that) and stores the user array Override the run method so that: o It loops indefinitely until all siblings have completed all their transactions Inside the loop, the thread checks if all non-finished users are waiting, and deposits $100 if they are Suggestions: . If the BankAccountRescuer constructor sends a null transaction list to the BankAccountUser constructor, the latter must guard against a null pointer error by checking for null and setting the number of transactions to zero if so . In order for the BankAccount object to be available to BankAccountRescuer, you can either: Add a getAccount method to BankAccountUser, or Store the BankAccount object in an instance field of BankAccountRescuer o For BankAccountRescuer, write private allFinished() and allWaiting() methods that loop through the user array and return the appropriate boolean values allWaiting() should only consider non-finished users Like any thread loop, BankAccountRescuer's run method should occasionally sleep o The Simulation Control class creates and starts all the sibling threads. It should be modified to also create and start the parent thread. Simulation Control uses JavaFX components, which we will learn about later. However, the modifications you need to make do not require understanding JavaFX at this time. To add the parent thread: Add an instance field parent of type BankAccountRescuer In generateUsers, just after the loop that fills the users array, set parent to a newly constructed BankAccountRescuer In the private class Runner, just after the loop that starts each of the sibling threads, start the parent thread The program should now display the desired behavior. o Create a new class called BankAccountRescuer that extends BankAccountUser Create a BankAccountRescuer constructor that: Takes a name, a BankAccount, and an array of BankAccountUsers as parameters Appropriately calls super (can send a null list of transactions, but the BankAccountUser constructor will have to deal with that) and stores the user array Override the run method so that: o It loops indefinitely until all siblings have completed all their transactions Inside the loop, the thread checks if all non-finished users are waiting, and deposits $100 if they are Suggestions: . If the BankAccountRescuer constructor sends a null transaction list to the BankAccountUser constructor, the latter must guard against a null pointer error by checking for null and setting the number of transactions to zero if so . In order for the BankAccount object to be available to BankAccountRescuer, you can either: Add a getAccount method to BankAccountUser, or Store the BankAccount object in an instance field of BankAccountRescuer o For BankAccountRescuer, write private allFinished() and allWaiting() methods that loop through the user array and return the appropriate boolean values allWaiting() should only consider non-finished users Like any thread loop, BankAccountRescuer's run method should occasionally sleep o The Simulation Control class creates and starts all the sibling threads. It should be modified to also create and start the parent thread. Simulation Control uses JavaFX components, which we will learn about later. However, the modifications you need to make do not require understanding JavaFX at this time. To add the parent thread: Add an instance field parent of type BankAccountRescuer In generateUsers, just after the loop that fills the users array, set parent to a newly constructed BankAccountRescuer In the private class Runner, just after the loop that starts each of the sibling threads, start the parent thread The program should now display the desired behavior
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
