Code RoomCircular wait in nested locks
MediumPrep Room Coding #1863

Circular wait in nested locks

Code reviewConcurrencyMid–Senior~25 min

Review this Java account-transfer routine.

What is the concurrency hazard, and how is it triggered?

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.

0:00 of about 25 min
Mark a line and say what kind of problem it is.0 findings
1class Account {
2 final Object lock = new Object();
3 long balance;
4}
5 
6void transfer(Account from, Account to, long amt) {
7 synchronized (from.lock) {
8 synchronized (to.lock) { // (1)
9 from.balance -= amt;
10 to.balance += amt;
11 }
12 }
13}
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.