in Scala 2.8 when I start actors, I can communicate via message passing. This in turn means that I can send the ultimate Exit() message or whatever I decide fits my protocol.
But how will I check if an actor has exited? I can easily imagine myself having a task where a master actor starts some worker actors and then simply waits for the answers, each time checking if this was the final answer (i.e. are any Actors still working or did they all exit?).
Of course I can let them all send back an "I'm done" message, and then count them, but this is somehow unsatisfactory.
What is best practise when testing for the completion of worker-actors?
EDIT#1
I am looking into Futures, but having trouble. Can someone explain why this code doesn't work:
package test
import scala.actors.Futures._
object FibFut extends Application{
def fib(i:Int):Int =
if(i<2)
1
else
fib(i-1)+fib(i-2)
val f = future{ fib(3) }
println(f())
}
It works if I define the function fib inside the future-body. It must be a scope thing, but I don't get any errors with the above, it simply hangs. Anyone?
EDIT#2
It seems that extending Application wasn't a nice way to go. Defining a main method made everything work. The below code is what I was looking for, so Futures get the thumbs up :)
package test
import scala.actors.Futures._
object FibFut {
def fib(i: Int): Int = if (i < 2) 1 else fib(i - 1) + fib(i - 2)
def main(args: Array[String]) {
val fibs = for (i <- 0 to 50) yield future { fib(i) }
for (future <- fibs) println(future())
}
}