Akka演员无限循环

时间:2015-11-01 22:30:59

标签: scala akka

我正在尝试编写一个简单的矩阵乘法程序,使用Scala和Akka actor进行并发处理。我甚至没有编写10%的代码,而且我遇到了麻烦。我创造了两个演员 - 大师和工人。我正在尝试在它们之间进行通信,但它会遇到无限循环。任何建议都非常感谢。正如您所看到的,下面的代码什么都不做,它会在主服务器上打印2个10X10矩阵,然后调用该工作程序。但是工人的workDone消息永远不会回到主人那里。我也怀疑这必须做一些我正在收到的警告:

变量模式后的

模式无法匹配(在主服务器的内部接收“masterSend”)

import akka.actor.{ActorRef, Actor, ActorSystem, Props}

import scala.Array._
import scala.util.Random

case object masterSend
case object workSend
case object workDone


object MatrixMultiply {

  val usage = """
               Usage: MainStart <matrix-dimension> <high-value>
              """

  def main(args: Array[String]) {

    if (args.length != 2) {
      println(usage)
      System.exit(1)
    }
    val Dim = args(0).toInt
    val Max = args(1).toInt

    val system = ActorSystem("ComputeSystem")
    val worker = system.actorOf(Props[Worker], name = "worker")
    val master = system.actorOf(Props(new Master(Dim, Max, worker)), name = "master")
    master ! masterSend

  }

  class Master(Dim: Int, Max: Int, worker : ActorRef) extends Actor {
    def receive = {
      case masterSend =>

        val r = new Random(34636)
        val matrixA = ofDim[Int](Dim,Dim)
        val matrixB = ofDim[Int](Dim,Dim)

        println("Matrix A: ")
        for (i <- 0 to Dim - 1) {
          for (j <- 0 to Dim - 1) {
            matrixA(i)(j) = r.nextInt(Max)
            print(matrixA(i)(j) + " ")
          }
          println()
        }

        r.setSeed(23535)
        println("Matrix B: ")
        for (i <- 0 to Dim - 1) {
          for (j <- 0 to Dim - 1) {
            matrixB(i)(j) = r.nextInt(Max)
            print(matrixB(i)(j) + " ")
          }
          println()
        }

        worker ! workSend

      case workDone =>
        println("Work was done!!")
        context.system.shutdown()

    }
  }

  class Worker extends Actor {

    def receive = {
      case workSend =>
        println("Work Done")
        sender ! workDone

    }
  }

}

1 个答案:

答案 0 :(得分:3)

问题在于您创建的对象上的模式匹配。它匹配得恰到好处。不要打扰自己的物体。例如使用字符串:

object A {
  val masterSend = "masterSend"
  val workSend = "workSend"
  val workDone = "workDone"
}
object MatrixMultiply {



  val usage = """
               Usage: MainStart <matrix-dimension> <high-value>
              """

  def main(args: Array[String]) {
    val Dim = 3
    val Max = 2
    val system = ActorSystem("ComputeSystem")
    val worker = system.actorOf(Props[Worker], name = "worker")
    val master = system.actorOf(Props(new Master(Dim, Max, worker)), name = "master")
    master ! A.masterSend

  }

  class Master(Dim: Int, Max: Int, worker : ActorRef) extends Actor {
    def receive = {
      case A.masterSend =>
        println("Master sent")
        worker ! A.workSend
      case A.workDone =>
        println("Work was done!!")
        context.system.shutdown()
    }
  }

  class Worker extends Actor {
    def receive = {
      case A.workSend =>
        println("Work Done")
        sender ! A.workDone

    }
  }

}

您已使用小写字母命名对象。 object messageSend 但模式匹配认为它不是一个对象,而是一个新的变量。

case messageSend => messageSend - 是一个变量 你可以在这里写任何东西case magicBall =>也会编译。