Следующий код отлично работает в Scala 2.8, но не в 2.9.0.1 (скопируйте и вставьте в REPL). Это приведет к исключению из 2.9.0.1.
import scala.actors.Actor
import scala.actors.TIMEOUT
object A2 extends Actor {
def act = {
loop {
react {
case "hello" =>
val s = sender
reactWithin(2000){
case i:Int => s ! "hello"
case TIMEOUT => s ! "TIMEOUT"
}
case _ =>
}
}
}
}
object A1 extends Actor {
def act = {
loop {
react {
case m:String => println (A2 !? (1000, m))
case _ =>
}
}
}
}
A1.start
A2.start
A1 ! "hi"
A1 ! "hello"
Если исключение не выбрано немедленно, продолжайте делать A1 ! "hi"
или A1 ! "hello"
несколько раз.
Является ли это ошибкой в Scala 2.9.0.1, или что-то не так с кодом?
[EDIT] Забыл добавить фактическое исключение.
scala> [email protected]: caught java.lang.RuntimeException: unhandled timeout
java.lang.RuntimeException: unhandled timeout
at scala.sys.package$.error(package.scala:27)
at scala.actors.Actor$class.receiveWithin(Actor.scala:606)
at $line4.$read$$iw$$iw$A1$.receiveWithin(<console>:10)
at scala.actors.Channel.receiveWithin(Channel.scala:71)
at scala.actors.ActorCanReply$class.$bang$qmark(ActorCanReply.scala:32)
at $line3.$read$$iw$$iw$A2$.$bang$qmark(<console>:9)
at $line4.$read$$iw$$iw$A1$$anonfun$act$1$$anonfun$apply$1.apply(<console>:15)
at $line4.$read$$iw$$iw$A1$$anonfun$act$1$$anonfun$apply$1.apply(<console>:13)
at scala.actors.ReactorTask.run(ReactorTask.scala:31)
at scala.actors.Reactor$class.resumeReceiver(Reactor.scala:129)
at $line4.$read$$iw$$iw$A1$.scala$actors$ReplyReactor$$super$resumeReceiver(<console>:10)
at scala.actors.ReplyReactor$class.resumeReceiver(ReplyReactor.scala:68)
at $line4.$read$$iw$$iw$A1$.resumeReceiver(<console>:10)
at scala.actors.Actor$class.searchMailbox(Actor.scala:500)
at $line4.$read$$iw$$iw$A1$.searchMailbox(<console>:10)
at scala.actors.Reactor$$anonfun$startSearch$1$$anonfun$apply$mcV$sp$1.apply$mcV$sp(Reactor.scala:117)
at scala.actors.Reactor$$anonfun$startSearch$1$$anonfun$apply$mcV$sp$1.apply(Reactor.scala:114)
at scala.actors.Reactor$$anonfun$startSearch$1$$anonfun$apply$mcV$sp$1.apply(Reactor.scala:114)
at scala.actors.ReactorTask.run(ReactorTask.scala:33)
at scala.concurrent.forkjoin.ForkJoinPool$AdaptedRunnable.exec(ForkJoinPool.java:611)
at scala.concurrent.forkjoin.ForkJoinTask.quietlyExec(ForkJoinTask.java:422)
at scala.concurrent.forkjoin.ForkJoinWorkerThread.mainLoop(ForkJoinWorkerThread.java:340)
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:325)
EDIT2: автономная программа без REPL
package scalaapplication5
import scala.actors.Actor._
import scala.actors.Actor
import scala.actors.TIMEOUT
object Main {
A1.start
A2.start
def main(args: Array[String]): Unit = {
while (true){
A1 ! "hi"
A1 ! "hello"
Thread.sleep(1000)
}
}
}
object A2 extends Actor {
def act = loop {
react {
case "hello" =>
val s = sender
reactWithin(2000){
case i:Int => s ! "hello"
case TIMEOUT => s ! "TIMEOUT"
}
case _ =>
}
}
}
object A1 extends Actor {
def act = {
loop {
react {
case m:String => println (A2 !? (1000, m))
case _ =>
}
}
}
}