Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

HELP WITH JAVA PROGRAM: ****REQUIRE : I am currently in the process of finishing

ID: 3834288 • Letter: H

Question

HELP WITH JAVA PROGRAM:

****REQUIRE: I am currently in the process of finishing a program that runs threads that deposit and withdraw money from the same bank account. I have to add a condition to the deposit method of the BankAccount class, restricting deposits to $100,000.00. The method should block until the sufficient money has been withdrawn by another thread and I want to test it wi/ a large number of deposit threads. I feel like I am finished, but when I run it i get this in the console:

___________________________________________________________________________________________

JAVA PROGRAM:

BankAccountRunner.java

public class BankAccountRunner {

   public static void main(String[] args) {

      

          BankAccount account = new BankAccount();

      

          final double AMOUNT = 10000;

      

          final int REPETITIONS = 10;

  

          final int DEPOSIT_THREADS = 10;

      

          final int WITHDRAW_THREADS = 2;

      

         

      

          for (int i = 0; i < DEPOSIT_THREADS; i++) {

      

          DepositRunnable d = new DepositRunnable(account, AMOUNT,

      

          REPETITIONS);

      

          Thread t = new Thread(d);

      

          t.start();

      

          }

      

         

      

          for (int i = 0; i < WITHDRAW_THREADS; i++) {

      

          WithdrawRunnable d = new WithdrawRunnable(account, AMOUNT,

      

          REPETITIONS * DEPOSIT_THREADS / WITHDRAW_THREADS);

      

          Thread t = new Thread(d);

      

          t.start();

      

          }

          }

      

       }

______________________________________________________________________________________

JAVA CLASS:

DepositRunner.java

public class DepositRunnable implements Runnable {

   private static final int DELAY = 1;

  

      private BankAccount account;

  

      private double amount;

  

      private int count;

  

      public DepositRunnable(BankAccount anAccount, double anAmount, int aCount) {

  

      account = anAccount;

  

      amount = anAmount;

  

      count = aCount;

      }

  

     

  

      public void run() {

  

      try {

   for (int i = 1; i <= count; i++) {

  

      account.deposit(amount);

  

      Thread.sleep(DELAY);

  

      }

  

      } catch (InterruptedException exception) {

  

      }

  

      }

  

   }

_____________________________________________________________________________________

JAVA CLASS:

WithdrawRunnable.java

public class WithdrawRunnable implements Runnable {

   private static final int DELAY = 1;

  

      private BankAccount account;

  

      private double amount;

  

      private int count;

  

     

      public WithdrawRunnable(BankAccount anAccount, double anAmount, int aCount) {

  

      account = anAccount;

  

      amount = anAmount;

  

      count = aCount;

  

      }

  

      public void run() {

  

      try {

  

      for (int i = 1; i <= count; i++) {

  

      account.withdraw(amount);

  

      Thread.sleep(DELAY);

  

      }

  

      } catch (InterruptedException exception) {

  

      }

  

      }

  

   }

Problems a Javadoc G Declaration E Console X Kterminated BankAccountRunner [Java Application] /Library/Java/JavavirtualMachines/idk 1.8.0 131.jdk/Conte Java Lang.. IL Lega LMonitorStateException at java .util.concurrent.locks.Reentrant Lock$Sync .tryRelease ReentrantLock ja at java. util.concurrent. locks. AbstractQueued Synchronizer. release AbstractQue at java util.concurrent.locks. ReentrantLock.unlockCReentrantLock.java:457) at BankAccount.deposit BankAccount. java: 55 at Deposit Runnable. run DepositRunnable.java: 28) at java lang. Thread. run Thread. java: 748) java lang.. IllegalMonitor StateException at java .util.concurrent.locks. Reentrant Lock$Sync.tryRelease ReentrantLock ja at java. util.concurrent .locks. AbstractQueuedSynchronizer. release AbstractQue at java util.concurrent locks ReentrantLock. unlock CReentrantLock. java 457) at Bank Account deposit BankAccount java: 55) at Deposit Runnable. un DepositRunnable. java: 28) at java lang. Thread. run Thread. java: 748) Exception in thread "Thread-5" java.1ang IllegalMonitor StateException at java util. concurrent .locks .Reentrant Lock$Sync .tryRelease ReentrantLock. ja at java util concurrent locks AbstractQueuedSynchronizer release AbstractQue at java util.concurrent .locks. ReentrantLock. unlock CReentrantLock.java 457) at Bank Account.deposit BankAccount. java 55) at Deposit Runnable. run DepositRunnable. java: 28) at java lang. Thread rundThread.java:748)

Explanation / Answer

bank account:

01
import java.util.concurrent.locks.Condition;
02
import java.util.concurrent.locks.Lock;
03
import java.util.concurrent.locks.ReentrantLock;
04

05
/**
06
A bank account has a balance that can be changed by
07
deposits and withdrawals.
08
*/
09
public class BankAccount {
10
public static final double MAX_BALANCE = 100000;
11

12
private double balance;
13
private Lock balanceChangeLock;
14
private Condition sufficientFundsCondition;
15
private Condition lessThanMaxBalanceCondition;
16

17
/**
18
* Constructs a bank account with a zero balance.
19
*/
20
public BankAccount() {
21
balance = 0;
22
balanceChangeLock = new ReentrantLock();
23
sufficientFundsCondition = balanceChangeLock.newCondition();
24
lessThanMaxBalanceCondition = balanceChangeLock.newCondition();
25
}
26

27
/**
28
* Deposits money into the bank account.
29
*
30
* @param amount
31
* the amount to deposit
32
*/
33
public void deposit(double amount) throws InterruptedException {
34

35
try {
36
while (balance + amount > MAX_BALANCE)
37
lessThanMaxBalanceCondition.await();
38
System.out.print("Depositing " + amount);
39
double newBalance = balance + amount;
40
System.out.println(", new balance is " + newBalance);
41
balance = newBalance;
42
sufficientFundsCondition.signalAll();
43
} finally {
44
balanceChangeLock.unlock();
45
}
46
}
47

48
/**
49
* Withdraws money from the bank account.
50
*
51
* @param amount
52
* the amount to withdraw
53
*/
54
public void withdraw(double amount) throws InterruptedException {
55

56
try {
57
while (balance < amount)
58
sufficientFundsCondition.await();
59
System.out.print("Withdrawing " + amount);
60
double newBalance = balance - amount;
61
System.out.println(", new balance is " + newBalance);
62
balance = newBalance;
63
lessThanMaxBalanceCondition.signalAll();
64
} finally {
65
balanceChangeLock.unlock();
66
}
67
}
68

69
/**
70
* Gets the current balance of the bank account.
71
*
72
* @return the current balance
73
*/
74
public double getBalance() {
75
return balance;
76
}
77

78
}


BankAccountThreadRunner
01
/**
02
* This program runs threads that deposit and withdraw money from the same bank
03
* account.
04
*/
05
public class BankAccountThreadRunner {
06
public static void main(String[] args) {
07
BankAccount account = new BankAccount();
08
final double AMOUNT = 10000;
09
final int REPETITIONS = 10;
10
final int DEPOSIT_THREADS = 10;
11
final int WITHDRAW_THREADS = 2;
12

13
for (int i = 0; i < DEPOSIT_THREADS; i++) {
14
DepositRunnable d = new DepositRunnable(account, AMOUNT,
15
REPETITIONS);
16
Thread t = new Thread(d);
17
t.start();
18
}
19

20
for (int i = 0; i < WITHDRAW_THREADS; i++) {
21
WithdrawRunnable d = new WithdrawRunnable(account, AMOUNT,
22
REPETITIONS * DEPOSIT_THREADS / WITHDRAW_THREADS);
23
Thread t = new Thread(d);
24
t.start();
25
}
26
}
27
}


DepositRunnable
01
/**
02
* A deposit runnable makes periodic deposits to a bank account.
03
*/
04
public class DepositRunnable implements Runnable {
05
private static final int DELAY = 1;
06
private BankAccount account;
07
private double amount;
08
private int count;
09

10
/**
11
* Constructs a deposit runnable.
12
*
13
* @param anAccount
14
* the account into which to deposit money
15
* @param anAmount
16
* the amount to deposit in each repetition
17
* @param aCount
18
* the number of repetitions
19
*/
20
public DepositRunnable(BankAccount anAccount, double anAmount, int aCount) {
21
account = anAccount;
22
amount = anAmount;
23
count = aCount;
24
}
25

26
public void run() {
27
try {
28
for (int i = 1; i <= count; i++) {
29
account.deposit(amount);
30
Thread.sleep(DELAY);
31
}
32
} catch (InterruptedException exception) {
33
}
34
}
35
}


WithdrawRunnable
01
/**
02
* A withdraw runnable makes periodic withdrawals from a bank account.
03
*/
04
public class WithdrawRunnable implements Runnable {
05
private static final int DELAY = 1;
06
private BankAccount account;
07
private double amount;
08
private int count;
09

10
/**
11
* Constructs a withdraw runnable.
12
*
13
* @param anAccount
14
* the account from which to withdraw money
15
* @param anAmount
16
* the amount to deposit in each repetition
17
* @param aCount
18
* the number of repetitions
19
*/
20
public WithdrawRunnable(BankAccount anAccount, double anAmount, int aCount) {
21
account = anAccount;
22
amount = anAmount;
23
count = aCount;
24
}
25

26
public void run() {
27
try {
28
for (int i = 1; i <= count; i++) {
29
account.withdraw(amount);
30
Thread.sleep(DELAY);
31
}
32
} catch (InterruptedException exception) {
33
}
34
}
35
}