繁体   English   中英

Scala Actors + Console.without可能的错误

[英]Scala Actors + Console.withOut possible bug

当在actor中使用Console.withOut时,我发现了一些奇怪的行为。 对于代码:

case object I
val out = new PipedOutputStream
val pipe = new PipedInputStream(out)
def read: String = ** read from `pipe` stream
class A extends Actor{
  var b: Actor = _
  Console.withOut(out){
    b = actor { loop { self react {
          case I => println("II")
        }}}
    }
  def act = {
    loop { self react {
        case I =>
          println("I")
          b ! I
      }}
    }
  }
def main(args: Array[String]): Unit = {
  val a = new A
  a.start
  a ! I
  Thread sleep 100
  println("!!\n" + read + "!!")
  }

得到以下输出:

!!
I
II
!!

知道为什么A actor的act方法的输出也会被重定向吗? 谢谢您的回答。

更新:这是读取功能:

  @tailrec
  def read(instream: InputStream, acc: List[Char] = Nil): String =
    if(instream.available > 0) read(instream, acc :+ instream.read.toChar) else acc mkString ""
  def read: String = read(pipe)

相反,在我看来,两个withOut都没有重定向其输出,因为withOut将在调用println("II")之前很长时间就完成了执行。 由于这都是基于DynamicVariable ,因此,我不愿意对此打赌。 :-)缺少有效的代码也排除了任何测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM