Question: Account class Bank class BankSimMain class TestingThread class TransferThread class When I run my code it prints an infinity loop I'm not where the bug

Account class

Account class Bank class BankSimMain class TestingThread class TransferThread class When I

run my code it prints an infinity loop I'm not where the

Bank class

bug is. it should just print out the picture below, but it

keeps repeating. How do I fix this? 10 11 public class Account

{ private volatile int balance; private final int id; private Bank bank;

BankSimMain class

12 13 14 15 16 17 public Account(int id, int initialBalance, Bank

TestingThread class

bank) { this.id = id; this.balance = initialBalance; this.bank = bank; }

public synchronized int getBalance() { return balance; } 18 19 20 23

TransferThread class

24 25 26 27 28 29 30 31 32 public synchronized boolean

When I run my code it prints an infinity loop I'm not where the bug is. it should just print out the picture below, but it keeps repeating. How do I fix this?

withdraw(int amount) { if (amount balance)) { try { wait(); } catch

10 11 public class Account { private volatile int balance; private final int id; private Bank bank; 12 13 14 15 16 17 public Account(int id, int initialBalance, Bank bank) { this.id = id; this.balance = initialBalance; this.bank = bank; } public synchronized int getBalance() { return balance; } 18 19 20 23 24 25 26 27 28 29 30 31 32 public synchronized boolean withdraw(int amount) { if (amount balance)) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } @Override public String toString() { return String.format("Account[%d] balance %d", id, balance); } 48 49 50 51 52 of 55 } 14 public class Bank { 15 16 17 18 19 public static final int NTEST = 10; private final Account[] accounts; private long numTransactions = 0; private final int initialBalance; private final int numAccounts; Semaphore transfer Sema; private Thread testThread; private boolean open; 20 21 22 23 24 25 26 public Bank(int numAccounts, int initialBalance) { this.initialBalance = initialBalance; this.numAccounts = numAccounts; 27 28 29 30 o 31 32 33 34 accounts = new Account[numAccounts]; for (int i = 0; i

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!