这是scala 2.9.0.1 actor库中的一个错误

时间:2011-06-09 03:57:53

标签: scala scala-2.9

以下代码在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中的错误,还是代码有问题?

[编辑]忘了添加抛出的实际异常。

scala> $line4.$read$$iw$$iw$A1$@6be03fce: 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 _ =>
      }
    }
  }
}

1 个答案:

答案 0 :(得分:3)

这似乎是Actor框架中的一个错误。看起来你永远不应该得到RuntimeException。 See the Scala Bug Tracker了解详情。它于2011年7月7日修复,可能正在等待发布。