Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Okay everyone, i have come up with a solution using the getState function of the actor class. In the solution I used an idea from this thread: <a href="https://stackoverflow.com/questions/2721337/best-method-to-peek-into-a-scala-actors-mailbox">Best method to peek into a Scala Actor&#39;s Mailbox</a> in which reactWithin(0) is used. I ran into trouble when using react and loop where the program would simply block on big calculations. This was solved by replacing loop with while(true) and reactWithin(int) with receiveWithin(int).</p> <p>My solution looks as follows (beware, bigass code-lump):</p> <pre><code>package test import scala.actors._ import scala.actors.Actor.State._ case class Computation(index: Int, a: () ⇒ Int) case class Result(i: String) object Main { def main(args: Array[String]) { val m = new Master m.start } } class Master extends Actor { val N = 40 var numberOfAnswers = 0 def fib(x: Int): Int = if (x &lt; 2) 1 else fib(x - 1) + fib(x - 2) val computers = for (i ← 0 to N) yield new Computer def act { for (i ← 0 until computers.size) { computers(i).start computers(i) ! Computation(i, () =&gt; fib(i)) } println("done Initializing actors") while (true) { receiveWithin(1000) { case Result(i) =&gt; val numberDone = computers.map(_.getState == Terminated).filter(_ == true).length println(i) numberOfAnswers += 1 case TIMEOUT =&gt; val allDone = computers.map(_.getState == Terminated).reduceRight(_ &amp;&amp; _) println("All workers done?:" + allDone) println("# of answers:" + numberOfAnswers) if (allDone) exit() } } } } class Computer extends Actor { def act { loop { react { case Computation(i, f) ⇒ sender ! Result("#" + i + " Res:" + f()) exit() } } } } </code></pre> <p>The program calculates the fibonacci numbers (in the worst possible way). The idea is simply to test utilization of multiple threads for big workloads. The following line checks whether some actor has yet to terminate:</p> <pre><code>computers.map(_.getState == Terminated).reduceRight(_ &amp;&amp; _) </code></pre> <p>where computers is of the type IndexedSeq[Computer]. The trick is that using the TIMEOUT message, I can periodically check if all the work is done and act accordingly (in this case, exit when there are no more active workers). I take advantage of the fact that each worker sends the results before they exit. In that way I know that I will always receive the results and handle them before they will be shown as Terminated.</p> <p>Can someone comment on the fact that the program "locks up" (stops receiving messages) when I use react and loop instead of while(true) and receive?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload