After reading about using react in an actor, I thought reacts would share the same thread when there weren't multiple reacts pending, but it isn't the case:
import scala.actors.Actor
import scala.actors.Actor._
class SleepyReactor extends Actor {
def act() {
loop {
react {
case x => {
println("reacting to %s on thread %s".
format(x, Thread.currentThread.getName))
Thread.sleep(1000)
println("done with " + x)
}
}
}
}
}
val sleepyOne = new SleepyReactor
sleepyOne.start
sleepyOne ! "first" // runs on thread-5
// wait until completion
sleepyOne ! "second" // runs on thread-3
Can someone explain why these are running on different threads and when a new thread will be created for an actor with react?
I read somewhere react is event based, and I took that to mean that "react actors" shared a thread and if one was "reacting" the other "react actors" would be queued until the first was done. I now think I am wrong. How does this work, and how is it different than receive?
Thank you very much for any help.