Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#ccccff
private synchronized void deposit(BadLocks bl) {
  System.out.println("Depositing amount...");
  bl.balance += this.balance;
  withdraw(); // Call same instance's withdraw() method
}

Compliant Solution (2) (avoid excessive synchronization)

The deadlock can also be avoided by declaring the balance field as volatile and removing the synchronized keyword from the withdraw() method.

...