Question: JAVA CONCURRENCY: threads Consider the following partial code for banking with de- Problem 1 (7 Polnts) Notice that the code uses a lock object and
Consider the following partial code for banking with de- Problem 1 (7 Polnts) Notice that the code uses a lock object and a condition object (lines (1) and (2)). Suppose posit and withdraw functions. public class DepositRunnable implements Runnable that two threads Pl and P2 are concurrently (almost at the same time) accessing one account with different functions such that Pl is performing deposit and P2 performing withdrawal. The following statements explain each step- by-step of the development of the code. Fill in the blanks. t // try to deposit public void run) f try ( account.deposit(amount); h on) ( catch (InterruptedException exception) C) private BankAccount account; private double amount 1. (4 points) Explain why the lock is needed. In other words, what happens if the lock was not used? ecuting lines sequently public class WithdravRunnable implements RunnableI t I/ try to withdrav the lock is not used, for example, thread P1 ex and thread P2 executing and con- (such as an un- public void run) may try account.vithdrav(amount); h catch (InterruptedException exception)C) private BankAccount account private double anount expected overdraft) may occur. 2. (3 points) Explain why the condition object is needed. In other words, what happens Iif only the lock is used (without the condition object)? public class BankAccount ( private double balance private Lock balanceChangeLock; 1 (I) private Condition sufficientFundsCondition; 1/(2) The given code without the condition object can lead Consider the follow- ing execution scenario: Upon acquiring the lock P2 enters the try block. Suppose the condition (balance
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
