public void transferMoney(Account fromAccount, Account toAccount, Amount amount) throws InsufficientFundsException { synchronized (fromAccount) { synchronized (toAccount) { if (fromAccount.getBalance().compareTo(amount) < 0) throw new InsufficientFundsException(); else { fromAccount.debit(amount); toAccount.credit(amount); } } } } Description: If from account A to account B to transfer x money, and from account B to account A - y, then if the circumstances fail, transaction 1 will take account monitor A, transaction 2 will take account monitor B. Result is a deadlock.
I just can not understand: How does the second trad get to the second synchronized, if the first synchronized has already been blocked by the first trad? I reviewed a bunch of lectures and reread synchronized.