我有一个程序可以创建一个actor,然后从默认输入中读取。 如果我写下一个特性使得基本的演员在“行动”方法上工作:
trait SocketActor extends Actor{
protected def sock:Socket
protected val in:BufferedReader=new BufferedReader(new InputStreamReader(this.sock.getInputStream()))
protected val out:PrintWriter= new PrintWriter(this.sock.getOutputStream(), true)
def act(){
println("This get to be executed")
}
如果我写下以下内容,则不会执行act方法
trait SocketActor extends Actor{
protected def sock:Socket
protected val in:ObjectInputStream=new ObjectInputStream(this.sock.getInputStream())
protected val out:ObjectOutputStream = new ObjectOutputStream (this.sock.getOutputStream())
def act(){
println("This doesn't get to be executed")
}
演员的创作可以恢复如下:
import java.net._
import java.io._
import scala.io._
import game.io._
class PlayerActor(protected val sock:Socket) extends {
} with SocketActor
object TabuClient{
def main(args:Array[String]){
try{
println("Always exected on both cases")
val port=1337
val s = new Socket(InetAddress.getByName("localhost"), port)
val a=new PlayerActor(s)
a.start()
for (line <- io.Source.stdin.getLines){
a.sendMessage(line)
}
s.close()
}
catch{
case e:Throwable=>{
e.printStackTrace()
}
}
}
两种方式都可以编译,但是第二种方法基本上在actor没有抛出异常后开始失败
答案 0 :(得分:0)
对对象输入流执行以下操作解决了它,有趣的是它不需要懒惰,如果它只是一个inputStream
trait SocketActor extends Actor{
protected def sock:Socket
protected lazy val in:ObjectInputStream=new ObjectInputStream(this.sock.getInputStream())
protected lazy val out:ObjectOutputStream = new ObjectOutputStream (this.sock.getOutputStream())
def act(){
println("This doesn't get to be executed")
}