File tree 1 file changed +19
-5
lines changed
src/main/scala/org/learningconcurrency/exercises/ch2
1 file changed +19
-5
lines changed Original file line number Diff line number Diff line change @@ -33,11 +33,26 @@ object Ex7 extends App {
33
33
}
34
34
}
35
35
36
- def sendAll (accounts : Set [Account ], target : Account ): Unit = {
37
- accounts.map((a) => thread {
38
- send(a,target,a.money)
39
- } ).foreach(_.join)
40
36
37
+ def sendAll (accounts : Set [Account ], target : Account ): Unit = {
38
+
39
+ def adjust () = {
40
+ target.money = accounts.foldLeft(0 )((s, a) => {
41
+ val money = a.money
42
+ a.money = 0
43
+ s + money
44
+ }
45
+ )
46
+ }
47
+
48
+ def sendAllWithSynchronize (la : List [Account ]): Unit = la match {
49
+ case h :: t => h synchronized {
50
+ sendAllWithSynchronize(t)
51
+ }
52
+ case _ => adjust()
53
+ }
54
+
55
+ sendAllWithSynchronize((target :: accounts.toList).sortBy(_.uid))
41
56
}
42
57
43
58
val accounts = (1 to 100 ).map((i) => new Account (s " Account: $i" ,i* 10 )).toSet
@@ -48,5 +63,4 @@ object Ex7 extends App {
48
63
accounts.foreach((a) => log(s " ${a.name}, money = ${a.money}" ))
49
64
log(s " ${target.name} - money = ${target.money}" )
50
65
51
-
52
66
}
You can’t perform that action at this time.
0 commit comments