Java: 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 transferring the money.     //  // DeadlockAccounts.java  //  public class DeadlockAccounts {  public static void main(String[] args) throws InterruptedException {  class Account {  int balance = 100;  public Account(int balance) { this.balance = balance; }  public synchronized void deposit(int amount) { balance += amount; }  public synchronized boolean withdraw(int amount) {  if (balance >= amount) {  balance -= amount;  return true;  }  return false;  }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Java:

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 transferring the money. 

 

 // 

// DeadlockAccounts.java 

// 

public class DeadlockAccounts { 

public static void main(String[] args) throws InterruptedException { 

class Account { 

int balance = 100; 

public Account(int balance) { this.balance = balance; } 

public synchronized void deposit(int amount) { balance += amount; } 

public synchronized boolean withdraw(int amount) { 

if (balance >= amount) { 

balance -= amount; 

return true; 

return false; 

public synchronized boolean transfer(Account destination, int amount) { 

if (balance >= amount) { 

balance -= amount; 

synchronized(destination) { 

destination.balance += amount; 

}; 

return true; 

return false; 

public int getBalance() { return balance; } 

final Account bob = new Account(200000); 

final Account joe = new Account(300000); 

class FirstTransfer extends Thread { 

public void run() { 

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

bob.transfer(joe, 2); 

class SecondTransfer extends Thread { 

public void run() { 

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

joe.transfer(bob, 1); 

FirstTransfer thread1 = new FirstTransfer(); 

SecondTransfer thread2 = new SecondTransfer(); 

thread1.start(); thread2.start(); 

thread1.join(); thread2.join(); 

System.out.println("Bob's balance: " + bob.getBalance()); 

System.out.println("Joe's balance: " + joe.getBalance()); 

} }

Write the deadlock resolved version of the above program. 

Expert Solution
steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY