...
Code Block | ||
---|---|---|
| ||
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.
...