Skip to content

Commit 94cf9bb

Browse files
committed
fix possibility deadlock in send method
1 parent 6374389 commit 94cf9bb

File tree

1 file changed

+10
-3
lines changed
  • src/main/scala/org/learningconcurrency/exercises/ch2

1 file changed

+10
-3
lines changed

src/main/scala/org/learningconcurrency/exercises/ch2/ex7.scala

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,19 @@ object Ex7 extends App {
1818
a2.money += n
1919
}
2020

21-
a1.synchronized {
21+
if (a1.uid < a2.uid) {
22+
a1.synchronized {
23+
a2.synchronized {
24+
adjust()
25+
}
26+
}
27+
} else {
2228
a2.synchronized {
23-
adjust()
29+
a1.synchronized {
30+
adjust()
31+
}
2432
}
2533
}
26-
2734
}
2835

2936
def sendAll(accounts: Set[Account], target: Account):Unit = {

0 commit comments

Comments
 (0)