A savings account object holds a nonnegative balance, and provides deposit(k) and withdraw(k) methods, where deposit(k) adds k to the balance, and withdraw(k) subtracts k, if the balance is at least k, and otherwise blocks until the balance becomes k or greater.
Implement this savings account using locks and conditions.
Now suppose there are two kinds of withdrawals: ordinary and preferred. Devise an implementation that ensures that no ordinary withdrawal occurs if there is a preferred withdrawal waiting to occur. Suppose there are two threads: one account attempting an ordinary withdrawal and another account attempting a preferred withdrawal. Both are attempting to do this at the same time. The account performing the preferred withdrawal should have priority attempting to make the withdrawal.
Now let us add a transfer() method that transfers a sum from one account to another:
void transfer(int k, Account reserve) {
lock.lock();
try {
reserve.withdraw(k);
deposit(k);
} finally {lock.unlock();}
}
4. We are given a set of 10 accounts, whose balances are unknown.
At 1:00, each of n threads tries to transfer $100 from another account
into its own account. At 2:00, a Boss thread deposits $1000 to each
account. Is every transfer method called at 1:00 certain to return?
If so, provide a test mechanism that verifies this.
If not, please explain why this is not so.