-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathChapter20.scala
867 lines (712 loc) · 23.7 KB
/
Chapter20.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
import java.awt.Color
import java.awt.image.BufferedImage
import java.io.{File, IOException}
import java.util
import javax.imageio.ImageIO
import scala.actors._
import scala.collection.JavaConverters.mapAsScalaMapConverter
import scala.collection.immutable.Seq
import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, Promise}
import scala.io.Source
import scala.util.Random
import scala.util.matching.Regex
//noinspection ScalaDeprecation
object Chapter20 {
/**
* Task 1:
*
* Write a program that generates an array of `n` random numbers (where `n` is a large value,
* such as 1,000,000), and then computes the average of those numbers by distributing the work
* over multiple actors, each of which computes the sum of the values, sending the result to
* an actor that combines the results.
* If you run your program on a dual-core or quad-core processor, what is the speedup over
* a single-threaded solution?
*
* Solution:
*
* For this simple task, on my machine, single-threaded solution was two times faster then
* actors solution.
* Starting actors takes more time then calculating the average of random numbers.
*/
object RandCalc {
val MaxWorkers = 10
val NumPerWorker = 50000
def calcAverageFor(count: Int, useActors: Boolean): Double = {
if (useActors) {
val proc = new RandProcessor()
proc.start()
val futureResult = proc.process(count)
Await.result(futureResult, Duration.Inf)
}
else {
calcAverage(0 until count)
}
}
def calcAverage(range: Range): Double = {
range.map(_ => Random.nextDouble()).sum / range.size
}
}
case class RandMsgProcess(n: Int)
case class RandMsgCalc(range: Range)
case class RandMsgResult(average: Double)
class RandProcessor() extends Actor {
protected var workerCount = 0
private var processedCount = 0
private var currAverage = 0.0
private val resultPromise = Promise[Double]()
def process(count: Int): concurrent.Future[Double] = {
this ! RandMsgProcess(count)
resultPromise.future
}
def act(): Unit = {
loop {
react {
case RandMsgProcess(num) =>
startWorkers(num)
case RandMsgResult(average) =>
procResult(average)
case msg =>
throw new IllegalStateException("Unknown message: " + msg)
}
}
}
protected def getRanges(untilNum: Int, count: Int): List[Range] = {
require(untilNum >= count, "untilNum >= count")
val numPerRange = math.max(untilNum / count, untilNum % count)
var start = 0
(for (i <- 1 to count) yield {
val end = if (i == count) untilNum else start + numPerRange
val range = start until end
start = end
range
}).toList
}
protected def getWorkerCount(numCount: Int): Int = {
var workerCount = numCount / RandCalc.NumPerWorker
if (workerCount == 0) workerCount = 1
else if (workerCount > RandCalc.MaxWorkers) workerCount = RandCalc.MaxWorkers
workerCount
}
private def startWorkers(numCount: Int): Unit = {
workerCount = getWorkerCount(numCount)
for (range <- getRanges(numCount, workerCount)) {
val worker = new RandWorker()
worker.start()
worker ! RandMsgCalc(range)
}
}
protected def procResult(average: Double): Unit = {
processedCount += 1
currAverage += average
if (processedCount >= workerCount) {
resultPromise.success(currAverage / processedCount)
exit()
}
}
}
class RandWorker() extends Actor {
def act(): Unit = {
react {
case RandMsgCalc(range) =>
reply(RandMsgResult(RandCalc.calcAverage(range)))
exit()
case msg =>
throw new IllegalStateException("Unknown message: " + msg)
}
}
}
/**
* Task 2:
*
* Write a program that reads in a large image into a `BufferedImage`, using
* `javax.imageio.ImageIO.read`. Use multiple actors, each of which inverts the colors in
* a strip of the image. When all strips have been inverted, write the result.
*/
object ImageProgram {
val MaxWorkers = 10
def invert(srcFile: File, dstFile: File): Unit = {
val proc = new ImageProcessor()
proc.start()
val futureResult = proc.process(srcFile, dstFile)
Await.result(futureResult, Duration.Inf)
}
def loadImage(srcFile: File): BufferedImage = ImageIO.read(srcFile)
def saveImage(bufferedImage: BufferedImage, dstFile: File): Unit = {
ImageIO.write(bufferedImage, Utils.getFileExt(dstFile), dstFile)
}
def getStrip(image: BufferedImage, stripIndex: Int): Seq[Int] = {
val strip = new Array[Int](image.getHeight)
for (y <- 0 until image.getHeight) {
strip(y) = image.getRGB(stripIndex, y)
}
strip.toIndexedSeq
}
def setStrip(image: BufferedImage, stripIndex: Int, strip: Seq[Int]): Unit = {
for (y <- strip.indices) {
image.setRGB(stripIndex, y, strip(y))
}
}
def invertStrip(strip: Seq[Int]): Seq[Int] = {
for (rgba <- strip) yield {
var col = new Color(rgba, true)
col = new Color(255 - col.getRed, 255 - col.getGreen, 255 - col.getBlue)
col.getRGB
}
}
}
case class ImageMsgProcess(srcFile: File, dstFile: File)
case class ImageMsgInvertStrip(stripIndex: Int, strip: Seq[Int])
case class ImageMsgInvertResult(stripIndex: Int, invertedStrip: Seq[Int])
case class ImageMsgProcessEnd()
class ImageProcessor() extends Actor {
private var stripCount = 0
private var processedCount = 0
private var image: BufferedImage = null
private var resultFile: File = null
private val resultPromise = Promise[Unit]()
def process(srcFile: File, dstFile: File): concurrent.Future[Unit] = {
this ! ImageMsgProcess(srcFile, dstFile)
resultPromise.future
}
def act(): Unit = {
loop {
react {
case ImageMsgProcess(srcFile, dstFile) =>
procStart(srcFile, dstFile)
case ImageMsgInvertResult(stripIndex, invertedStrip) =>
procResult(stripIndex, invertedStrip)
case msg =>
throw new IllegalStateException("Unknown message: " + msg)
}
}
}
private def procStart(srcFile: File, dstFile: File): Unit = {
image = ImageProgram.loadImage(srcFile)
stripCount = image.getWidth
resultFile = dstFile
// start workers
val workers = for (i <- 0 until ImageProgram.MaxWorkers) yield {
val worker = new ImageWorker()
worker.start()
worker
}
// send invert strip messages to workers
for (x <- 0 until stripCount) {
workers(x % workers.length) ! ImageMsgInvertStrip(x, ImageProgram.getStrip(image, x))
}
// stop workers
for (worker <- workers) {
worker ! ImageMsgProcessEnd
}
}
private def procResult(stripIndex: Int, invertedStrip: Seq[Int]): Unit = {
processedCount += 1
ImageProgram.setStrip(image, stripIndex, invertedStrip)
if (processedCount >= stripCount) {
ImageProgram.saveImage(image, resultFile)
resultPromise.success(Unit)
exit()
}
}
}
class ImageWorker() extends Actor {
def act(): Unit = {
loop {
react {
case ImageMsgInvertStrip(stripIndex, strip) =>
reply(ImageMsgInvertResult(stripIndex, ImageProgram.invertStrip(strip)))
case ImageMsgProcessEnd =>
exit()
case msg =>
throw new IllegalStateException("Unknown message: " + msg)
}
}
}
}
/**
* Task 3:
*
* Write a program that counts how many words match a given regular expression in all files of
* all subdirectories of a given directory. Have one actor per file, one actor that traverses
* the subdirectories, and one actor to accumulate the results.
*/
object WordsCountProgram {
private val wordRegex = "text".r
def calcMatchedWords(dirPath: String, fileExtensions: String*): Int = {
val futureResult = new WordsProcessor(
WordsParams(wordRegex, dirPath, fileExtensions.toIndexedSeq),
0,
(count: Int, file, words) => count + words.length
).process()
Await.result(futureResult, Duration.Inf)
// var count = 0
// for (file <- Utils.listAllFiles(dirPath, fileExtensions: _*)) {
// for (line <- Source.fromFile(file).getLines()) {
// wordRegex.findAllIn(line).foreach(_ => count += 1)
// }
// }
//
// count
}
}
case class WordsParams(wordRegex: Regex, dirPath: String, fileExtensions: Seq[String])
case class WordsMsgProcess()
case class WordsMsgProcessEnd()
case class WordsMsgDir(dirPath: String)
case class WordsMsgFile(filePath: String)
case class WordsMsgFileResult(file: File, words: Seq[String])
class WordsProcessor[T](params: WordsParams,
resultInit: T,
resultProc: (T, File, Seq[String]) => T) extends Actor {
protected var processedCount = 0
private var fileCount = 0
private var processEnd = false
private var result: T = resultInit
private val resultPromise = Promise[T]()
def process(): concurrent.Future[T] = {
start()
this ! WordsMsgProcess()
resultPromise.future
}
def act(): Unit = {
while (true) {
receive(processMsg())
}
}
protected def processMsg(): PartialFunction[Any, Unit] = {
case msg: WordsMsgProcess =>
startDirWorker(msg)
case msg: WordsMsgFile =>
fileCount += 1
startFileWorker(msg)
case WordsMsgFileResult(file, words) =>
processedCount += 1
result = resultProc(result, file, words)
checkEnd()
case _: WordsMsgProcessEnd =>
processEnd = true
checkEnd()
case msg =>
throw new IllegalStateException("Unknown message: " + msg)
}
def checkEnd(): Unit = {
if (processedCount >= fileCount && processEnd) {
resultPromise.success(result)
exit()
}
}
def startDirWorker(msg: WordsMsgProcess): Unit = {
val dirWorker = new WordsDirWorker(params)
dirWorker.start()
dirWorker ! msg
}
def startFileWorker(msg: WordsMsgFile): WordsFileWorker = {
val fileWorker = new WordsFileWorker(params)
fileWorker.start()
fileWorker ! msg
fileWorker
}
}
class WordsDirWorker(params: WordsParams) extends Actor {
private var processor: OutputChannel[Any] = null
private var dirCount = 1 // allow process the root directory
def act(): Unit = {
while (true) {
receive {
case _: WordsMsgProcess =>
this.processor = sender
this ! WordsMsgDir(params.dirPath)
case WordsMsgDir(dirPath) =>
dirCount -= 1
processDir(dirPath)
if (dirCount == 0) {
processor ! WordsMsgProcessEnd()
exit()
}
case msg =>
throw new IllegalStateException("Unknown message: " + msg)
}
}
}
def processDir(dirPath: String): Unit = {
val dir = new File(dirPath)
require(dir.isDirectory, s"Given path should represent directory: $dirPath")
val files = dir.listFiles()
if (files == null) {
throw new IOException(s"Cannot read directory content: $dir")
}
for (file <- files) {
if (file.isDirectory) {
// process new directory in this actor
dirCount += 1
this ! WordsMsgDir(file.getPath)
}
else if (params.fileExtensions.isEmpty ||
params.fileExtensions.contains(Utils.getFileExt(file))) {
// notify processor about new file
processor ! WordsMsgFile(file.getPath)
}
}
}
}
class WordsFileWorker(params: WordsParams) extends Actor {
def act(): Unit = {
receive {
case WordsMsgFile(filePath) =>
processFile(filePath)
exit()
case msg =>
throw new IllegalStateException("Unknown message: " + msg)
}
}
def processFile(filePath: String): Unit = {
val file = new File(filePath)
require(file.isFile, s"Given path should represent file: $filePath")
val words = new ArrayBuffer[String]()
for (line <- Source.fromFile(file).getLines()) {
for (word <- params.wordRegex.findAllIn(line)) {
words += word
}
}
sendResult(file, words.toIndexedSeq)
}
def sendResult(file: File, words: Seq[String]): Unit = {
reply(WordsMsgFileResult(file, words))
}
}
/**
* Task 4:
*
* Modify the program of the preceding exercise to display all matching words.
*/
object WordsPrintProgram {
private val wordRegex = "text".r
def printMatchedWords(dirPath: String, fileExtensions: String*): String = {
val futureResult = new WordsProcessor(
WordsParams(wordRegex, dirPath, fileExtensions.toIndexedSeq),
new StringBuilder(),
(result: StringBuilder, file, words) => result ++= words.mkString ++= "\n"
).process()
Await.result(futureResult, Duration.Inf).toString()
}
}
/**
* Task 5:
*
* Modify the program of the preceding exercise to display all matching words, each with
* a list of all files containing it.
*/
object WordsPrintFilesProgram {
private val wordRegex = "text".r
def printMatchedWordsWithFiles(dirPath: String, fileExtensions: String*): String = {
val futureResult = new WordsProcessor(
WordsParams(wordRegex, dirPath, fileExtensions.toIndexedSeq),
new ArrayBuffer[(File, Seq[String])](),
(result: ArrayBuffer[(File, Seq[String])], file, words) => result += file -> words
).process()
val result: ArrayBuffer[(File, Seq[String])] = Await.result(futureResult, Duration.Inf)
// map words to files, use sorted map to get stable output
val wordToFiles = new util.TreeMap[String, List[String]]().asScala
for ((file, words) <- result; word <- words) {
val files = wordToFiles.getOrElse(word, Nil)
wordToFiles(word) = files :+ file.getPath
}
// sort the files to get stable output
for ((word, files) <- wordToFiles) {
wordToFiles(word) = files.sorted
}
// get the output
val out = new StringBuilder()
for ((word, files) <- wordToFiles) {
out ++= "found \"" ++= word ++= "\" in\n" ++= files.mkString("\n") ++= "\n\n"
}
out.toString()
}
}
/**
* Task 6:
*
* Write a program that constructs 100 actors that use a `while(true)/receive` loop,
* calling `println(Thread.currentThread)` when they receive a 'Hello message,
* and 100 actors that do the same with `loop/react`. Start them all,
* and send them all a message.
* How many threads are occupied by the first kind, and how many by the second kind?
*
* Solution:
*
* The result is impressive, the numbers are the following:
* {{{
* whileReceiveActorsCount: 100
* loopReactActorsCount: 1
* }}}
*/
object ThreadActorsProgram {
val actorsCount = 100
def calcActorThreads(whileReceiveActors: Boolean): Int = {
val futureResult = new ThreadActorsProcessor(whileReceiveActors).process()
Await.result(futureResult, Duration.Inf)
}
}
case class ThreadMsgProcess()
case class ThreadMsgHello()
case class ThreadMsgThreadId(threadId: Long)
class ThreadActorsProcessor(whileReceiveActors: Boolean) extends Actor {
private val threadIds = mutable.HashSet[Long]()
private var processedCount = 0
private val resultPromise = Promise[Int]()
def process(): concurrent.Future[Int] = {
start()
this ! ThreadMsgProcess()
resultPromise.future
}
override def act(): Unit = {
loop {
react {
case _: ThreadMsgProcess =>
startActors()
case ThreadMsgThreadId(threadId) =>
procResult(threadId)
}
}
}
def startActors(): Unit = {
for (_ <- 0 until ThreadActorsProgram.actorsCount) {
val actor =
if (whileReceiveActors) new ThreadWhileReceiveActor()
else new ThreadLoopReactActor()
actor.start()
actor ! ThreadMsgHello()
}
}
def procResult(threadId: Long): Unit = {
threadIds += threadId
processedCount += 1
if (processedCount >= ThreadActorsProgram.actorsCount) {
resultPromise.success(threadIds.size)
exit()
}
}
}
class ThreadWhileReceiveActor extends Actor {
override def act(): Unit = {
while (true) {
receive {
case _: ThreadMsgHello =>
//println(Thread.currentThread)
reply(ThreadMsgThreadId(Thread.currentThread().getId))
}
}
}
}
class ThreadLoopReactActor extends Actor {
override def act(): Unit = {
loop {
react {
case _: ThreadMsgHello =>
//println(Thread.currentThread)
reply(ThreadMsgThreadId(Thread.currentThread().getId))
}
}
}
}
/**
* Task 7:
*
* Add a supervisor to the program of exercise 3 that monitors the file reading actors
* and logs any that exit with an `IOException`. Try triggering the exception by removing
* files that have been scheduled for processing.
*/
object WordsSupervisorProgram {
private val wordRegex = "text".r
def calcMatchedWords(dirPath: String, fileExtensions: String*): Int = {
val supervisorProcessor = new WordsSupervisorProcessor(
WordsParams(wordRegex, dirPath, fileExtensions.toIndexedSeq),
0,
(count: Int, file, words) => count + words.length
)
// run process and wait for result
val result = Await.result(supervisorProcessor.process(), Duration.Inf)
// get possible errors and log them on main thread
println(supervisorProcessor.getErrors)
result
}
}
class WordsSupervisorProcessor[T](params: WordsParams,
resultInit: T,
resultProc: (T, File, Seq[String]) => T
) extends WordsProcessor[T](params, resultInit, resultProc) {
private val errors = new StringBuilder()
def getErrors: String = errors.toString()
override protected def processMsg(): PartialFunction[Any, Unit] = {
case msg: WordsMsgProcess =>
trapExit = true
super.processMsg().apply(msg)
case Exit(linked, UncaughtException(_, _, _, _, cause)) =>
processedCount += 1
errors.append(cause) ++= "\n"
checkEnd()
case Exit(linked, reason) =>
if (reason != 'normal) {
processedCount += 1
errors.append(reason) ++= "\n"
checkEnd()
}
case msg =>
super.processMsg().apply(msg)
}
override def startFileWorker(msg: WordsMsgFile): WordsFileWorker = {
val fileWorker = if (msg.filePath.endsWith(".txt"))
super.startFileWorker(WordsMsgFile(msg.filePath + "-not-exist"))
else
super.startFileWorker(msg)
link(fileWorker)
fileWorker
}
}
/**
* Task 8:
*
* Show how an actor-based program can deadlock when one sends synchronous messages.
*/
object DeadlockProgram {
val seconds = 3
def run(): Int = {
val futureResult = new DeadlockProcessor().process()
Await.result(futureResult, Duration.Inf)
}
}
case class DeadlockMsgProcess()
case class DeadlockMsgSync()
case class DeadlockMsgResult(result: Int)
class DeadlockProcessor extends Actor {
private val resultPromise = Promise[Int]()
def process(): concurrent.Future[Int] = {
start()
this ! DeadlockMsgProcess()
resultPromise.future
}
override def act(): Unit = {
loop {
react {
case _: DeadlockMsgProcess =>
val actor = new DeadlockActor(this)
actor.start()
// send synchronous message to actor
actor !? DeadlockMsgSync() match {
case DeadlockMsgResult(result) =>
resultPromise.success(result)
exit()
}
case _: DeadlockMsgSync =>
reply(DeadlockMsgResult(5))
}
}
}
}
class DeadlockActor(parent: Actor) extends Actor {
override def act(): Unit = {
loop {
react {
case _: DeadlockMsgSync =>
// ask parent actor for data synchronously and wait some time
val optionResult = parent !? (DeadlockProgram.seconds * 1000L, DeadlockMsgSync())
if (optionResult.isDefined) {
optionResult.get match {
case DeadlockMsgResult(result) =>
reply(DeadlockMsgResult(result * 10))
}
}
else {
// timeout, no data
reply(DeadlockMsgResult(-1))
}
}
}
}
}
/**
* Task 9:
*
* Produce a faulty implementation of the program in exercise 3, in which the actors update
* a shared counter. Can you demonstrate that the program acts incorrectly?
*/
object SharedCounterProgram {
private val wordRegex = "text".r
var counter = 0
def calcMatchedWords(dirPath: String, fileExtensions: String*): Int = {
val supervisorProcessor = new SharedCounterProcessor(
WordsParams(wordRegex, dirPath, fileExtensions.toIndexedSeq),
0,
(count: Int, file, words) => count + words.length
)
Await.result(supervisorProcessor.process(), Duration.Inf)
}
}
class SharedCounterProcessor[T](params: WordsParams,
resultInit: T,
resultProc: (T, File, Seq[String]) => T
) extends WordsProcessor[T](params, resultInit, resultProc) {
override def startFileWorker(msg: WordsMsgFile): WordsFileWorker = {
val fileWorker = new SharedCounterFileWorker(params)
fileWorker.start()
fileWorker ! msg
fileWorker
}
}
class SharedCounterFileWorker(params: WordsParams) extends WordsFileWorker(params) {
override def sendResult(file: File, words: Seq[String]): Unit = {
val counter = SharedCounterProgram.counter
// introduce some delay to allow other threads update the counter
Thread.sleep(200)
//NOT SAFE: updating shared counter from multiple threads !!!
SharedCounterProgram.counter = counter + words.length
super.sendResult(file: File, words: Seq[String])
}
}
/**
* Task 10:
*
* Rewrite the program of exercise 1 by using channels for communication.
*/
object ChannelCalc {
def calcAverageFor(count: Int): Double = {
val proc = new ChannelProcessor()
proc.start()
val futureResult = proc.process(count)
Await.result(futureResult, Duration.Inf)
}
}
class ChannelProcessor() extends RandProcessor {
override def act(): Unit = {
react {
case RandMsgProcess(num) =>
doProcess(num)
}
}
private def doProcess(numCount: Int): Unit = {
workerCount = getWorkerCount(numCount)
val resultChannel = new Channel[Double]
// start actors
for (range <- getRanges(numCount, workerCount)) {
Actor.actor {
val channel = new Channel[Range]
channel ! range
channel.react {
case r =>
resultChannel ! RandCalc.calcAverage(r)
}
}
}
// process results from the actors
loop {
resultChannel.react {
case average =>
procResult(average)
}
}
}
}
}