Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

In-Class Exrercise-6: (10 Marks) Deadlock Resolution: Our basic understanding, using synchronized keywords to synchronize critical methods should resolve all problems. Let’s imagine that we have two...

1 answer below »

In-Class Exrercise-6: (10 Marks)

Deadlock Resolution:
Our basic understanding, using synchronized keywords to synchronize critical methods should resolve all
problems. Let’s imagine that we have two accounts that can deposit, withdraw, and transfer to another
account. What happens if at the same time we want to transfer money from one account to another and
vice versa? Let’s look at an example.

DeadlockAccounts.java

public class DeadlockAccounts {
public static void main(String[] args) throws Inte
uptedException {
XXXXXXXXXXclass Account {
XXXXXXXXXXint balance = 100;
XXXXXXXXXXpublic Account(int balance) { this.balance = balance; }
XXXXXXXXXXpublic synchronized void deposit(int amount) { balance += amount; }
XXXXXXXXXXpublic synchronized boolean withdraw(int amount) {
XXXXXXXXXXif (balance >= amount) {
XXXXXXXXXXbalance -= amount;
XXXXXXXXXXreturn true;
}
XXXXXXXXXXreturn false;
}
XXXXXXXXXXpublic synchronized boolean transfer(Account destination, int amount) {
XXXXXXXXXXif (balance >= amount) {
XXXXXXXXXXbalance -= amount;
XXXXXXXXXXsynchronized(destination) {
XXXXXXXXXXdestination.balance += amount;
};
XXXXXXXXXXreturn true;
}
XXXXXXXXXXreturn false;
}
XXXXXXXXXXpublic int getBalance() { return balance; }
}
XXXXXXXXXXfinal Account bob = new Account(200000);
XXXXXXXXXXfinal Account joe = new Account(300000);
XXXXXXXXXXclass FirstTransfer extends Thread {
XXXXXXXXXXpublic void run() {
XXXXXXXXXXfor (int i = 0; i < 100000; i++) {
XXXXXXXXXXbob.transfer(joe, 2);
}
}
}
XXXXXXXXXXclass SecondTransfer extends Thread {
XXXXXXXXXXpublic void run() {
XXXXXXXXXXfor (int i = 0; i < 100000; i++) {
XXXXXXXXXXjoe.transfer(bob, 1);
}
}
}
XXXXXXXXXXFirstTransfer thread1 = new FirstTransfer();
XXXXXXXXXXSecondTransfer thread2 = new SecondTransfer();
XXXXXXXXXXthread1.start(); thread2.start();
XXXXXXXXXXthread1.join(); thread2.join();
XXXXXXXXXXSystem.out.println("Bob's balance: " + bob.getBalance());
XXXXXXXXXXSystem.out.println("Joe's balance: " + joe.getBalance());
}
}
When we run the above program on our machine it usually gets stuck. Why does this happen? If we look
closely, we can see that when we transfer money, we are entering into the transfer method that is
synchronized and locks access to all synchronized methods on the source account, and then locks
destination account which locks access to all synchronized methods on it. This scenario is illustrated in the
figure below:
Consider the following scenario:
• First thread calls transfer on Bob’s account to Joe’s account
• Second thread calls transfer on Joe’s account to Bob’s account
• Second thread decreases amount from Joe’s account
• Second thread goes to deposit amount to Bob’s account but waits for first thread to complete
transfer.
• First thread decreases amount from Bob’s account
• First thread goes to deposit amount to Joe’s account but waits for second thread to complete
transfer.
In this scenario, one thread is waiting for another thread to finish transfer and vice versa. They are stuck
with each other, and the program cannot continue. Thus, deadlock occurs. To avoid deadlock it is
necessary to lock accounts in the same order. To fix the program we’ll give each account a unique number
so that we can lock accounts in the same order when transfe
ing the money.
Write the deadlock resolved version of the above program.
Deliverable:
1. Report (2 Marks)
2. Source code (8 marks)
Sample output:
Answered Same Day Dec 02, 2021

Solution

Kondalarao answered on Dec 02 2021
102 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here