0
當演員中使用Console.withOut
時,我發現了一些奇怪的行爲。下面的代碼:Scala演員+ Console.withOut可能的bug
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
演員的act
方法輸出也重定向?謝謝您的回答。
UPDATE: 這裏讀功能:
@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)
你還可以發佈你從`pipe`讀取的代碼嗎? – axel22 2011-02-15 09:00:40