简体   繁体   English

演员示例不编译

[英]Actors example not compiling

I am trying to run this code to learn actors (using Scala on Eclipse) but it is telling me that values Ping and Pong are not found. 我正在尝试运行此代码来学习actor(在Eclipse上使用Scala),但它告诉我没有找到值Ping和Pong。

Any idea of what I am doing wrong? 我知道我做错了什么?

I installed akka. 我安装了akka。 Any help is appreciated. 任何帮助表示赞赏。

Thanks 谢谢


import scala.actors.Actor
import scala.actors.Actor._

class Ping(count: int, pong:Actor) extends Actor{ // type int here is not found as well
   def act(){
     var pingsLeft= count-1
     pong! Ping
      while(true){
        receive {
         case Pong =>
           if (pingsLeft % 1000 ==0)
             Console.println("Ping : pong ")
         if (pingsLeft > 0){
           pong ! Ping
           pingsleft -=1
         } else {
          Console.println("Ping : stop")
          pong ! Stop
          exit()
        }
      }
    }
  }
}

class Pong extends Actor {
  def act(){
    var pongCount =0
    while (true){
      receive {
        case Ping =>
          if(pongCount % 1000 ==0)
            Console.println("Pong : ping " + pongCount)
           sender ! Pong
           pongCount = pongCount + 1
        case Stop =>
          Console.println("Pong : stop")
          exit()
      }
    }
  }
 }

 object pingpong extends Application {
  val pong = new Pong
  val ping = new Ping(100000, pong)
  ping.start
  pong.start
}

As I stated in my comment, you should switch your example over to Akka . 正如我在评论中所说,你应该把你的例子转到Akka Here is a rough approximation of your example refactored to using Akka : 以下是使用Akka重构的示例的粗略近似值:

import akka.actor._

class Ping(count: Int, pong:ActorRef) extends Actor{ // type int here is not found as well
  pong! Ping
  var pingsLeft = count - 1

  def receive = {
    case Pong =>
      if (pingsLeft % 1000 ==0)
        Console.println("Ping : pong ")
      if (pingsLeft > 0){
        pong ! Ping
        pingsLeft -=1
      } else {
        Console.println("Ping : stop")
        pong ! Stop
        context stop self    
    }
  }
}

class Pong extends Actor {
  var pongCount =0

  def receive = {
        case Ping =>
          if(pongCount % 1000 ==0)
            Console.println("Pong : ping " + pongCount)
           sender ! Pong
           pongCount = pongCount + 1
        case Stop =>
          Console.println("Pong : stop")
          exit()    
  }
 }

case object Ping
case object Pong
case object Stop

object pingpong {
  def main(args: Array[String]) {
    val system = ActorSystem("pingpong")
    val pong = system.actorOf(Props[Pong])
    val ping = system.actorOf(Props(classOf[Ping], 100000, pong))  
  }

}

And he's a slightly refactored version, cleaning up some mutable state and also setting up the Pong instance as a child of the Ping instance so that when Ping stops, it also automatically stops the Pong instance: 并且他是一个稍微重构的版本,清理了一些可变状态,并将Pong实例设置为Ping实例的子实例,这样当Ping停止时,它也会自动停止Pong实例:

import akka.actor._

class Ping(count: Int) extends Actor{ // type int here is not found as well
  val pong = context.actorOf(Props[Pong])
  pong! Ping

  def receive = pingReceive(count - 1)

  def pingReceive(pingsLeft:Int):Receive = {
    case Pong =>
      if (pingsLeft % 1000 ==0)
        Console.println("Ping : pong ")
      if (pingsLeft > 0){
        pong ! Ping
        context.become(pingReceive(pingsLeft - 1))
      } 
      else {
        Console.println("Ping : stop")
        context stop self    
      }        
  }
}

class Pong extends Actor {
  override def postStop{
    Console.println("Pong : stop") 
  }

  def receive = pongReceive(0)

  def pongReceive(pongCount:Int):Receive = {
    case Ping =>
      if(pongCount % 1000 ==0) 
        Console.println("Pong : ping " + pongCount)

      sender ! Pong
      context.become(pongReceive(pongCount + 1))        
  }
}

case object Ping
case object Pong

object PingPong {
  def main(args: Array[String]) {
    val system = ActorSystem("pingpong")
    val ping = system.actorOf(Props(classOf[Ping], 100000))  
  }    
}

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

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