一段时间后,这个 Actor 填满了堆栈。可能的解决方案 ?

object Puller extends Actor {

 def act() = {
  receiveWithin(2000) {
    case Stop => println("stoping puller")
        exit()
    case Noop => println("nothing happens")
        act()
    case TIMEOUT => doPull
        act()
  }
}

  def doPull() = // stuff...
}

我很不高兴找到在Scala编程此代码。

最佳答案

您的 act 不是尾递归的。您可以按如下方式修改它:

  @tailrec // in the presence of this annotation, the compiler will complain, if the code is not tail-recursive
  def act() = {
    receiveWithin(2000) {
      case Stop => println("stoping puller"); exit()
      case Noop => println("nothing happens")
      case TIMEOUT => doPull
    }
    act()
  }

关于scala - 使用 Scala Actor 和 receiveWithin 的 Stackoverflow 异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6121541/

10-13 04:15