Answered step by step
Verified Expert Solution
Question
1 Approved Answer
(a) [6 Points] Consider this pseudocode for a bank account supporting concurrent access. Assume that Lock is a valid locking class, although it is not
(a) [6 Points] Consider this pseudocode for a bank account supporting concurrent access. Assume that Lock is a valid locking class, although it is not in Java 1 class BankAccount 3 private Lock lk new Lock (O; 5 lk.acquireO 7 lk.release) private int balance-6, 4 int getBalance() int ansbalance; return ans; 10 void setBalance(int x) 11 k.acquire) 12 13 14 15 void withdraw(int amount) 16 k.acquire) 17 int b=getBalance(); 18 if(amount > b){ 19 20 21 22setBalance (bamount); 23 lk.release) 24 25J balancex; 13 k.release) lk.release(); throw new WithdrawTooLargeException); If Lock is NOT re-entrant, then the code above is broken. Below is a new version of the withdraw method your friend proposes. Explain why newWithdraw doesn't "block forever" (unlike the original code) even if locks are NOT re-entrant 1 void newwithdraw(int amount) 2 k.acquire); 3 lk.release() int b getBalance(); 5 k.acquire(); 6 if (amount > b) 7 8 lk.release(); throw new WithdrawTooLargeException); 10 k.release) 11setBalance(bamount); 12 k.acquire) 13 lk.release); (b) [7 Points] Show that newwithdraw is incorrect by giving an interleaving of two threads, both calling newWithdraw, in which a withdrawal is forgotten/lost
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered 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