Code Room
Code reviewHard
Question
Review this Java account-transfer method.
What a strong answer looks like
Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix — specific and kind.
Learn the concepts
class Account { private long balance; final Object lock = new Object(); long balance() { synchronized (lock) { return balance; } } void credit(long a) { synchronized (lock) { balance += a; } } void debit(long a) { synchronized (lock) { balance -= a; } }} void transfer(Account from, Account to, long amount) { synchronized (from.lock) { synchronized (to.lock) { from.debit(amount); to.credit(amount); } }}Run or narrate your approach, then ask the coach.