receive from channel belonging to other actor

Exception in thread "main" java.lang.AssertionError: assertion failed: receive from channel belonging to other actor
    at scala.actors.InternalActor$class.receive(InternalActor.scala:87)
    at com.home.actor.Task.receive(ActorWordCount.scala:39)
    at com.home.actor.Task.act(ActorWordCount.scala:42)
    at com.home.actor.ActorWordCount$$anonfun$main$1.apply(ActorWordCount.scala:18)
    at com.home.actor.ActorWordCount$$anonfun$main$1.apply(ActorWordCount.scala:16)
    at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:33)
    at scala.collection.mutable.ArrayOps$ofRef.foreach(ArrayOps.scala:108)
    at com.home.actor.ActorWordCount$.main(ActorWordCount.scala:16)
    at com.home.actor.ActorWordCount.main(ActorWordCount.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

 

代码如下:

import scala.actors.{Future, Actor}
import scala.collection.mutable.ListBuffer
import scala.io.Source

/**
 * Created by li.ma on 2020/7/1.
 */
object ActorWordCount {
  def main(args: Array[String]) {
    val files = Array("D:\\temp\\1.txt", "D:\\temp\\2.txt", "D:\\temp\\3.txt")

    val futureList :ListBuffer[Future[Any]] = new ListBuffer[Future[Any]]();

    for (file <- files) {
      val task = new Task
      task.act()
      val res : Future[Any] = task !! SmTask(file)
      futureList += res
    }

    val resMap : ListBuffer[Map[String, Int]] = new ListBuffer[Map[String, Int]]
    while (futureList.size > 0) {
      val setList : ListBuffer[Future[Any]] = futureList.filter(_.isSet)
      for(item <- setList) {
        resMap += item.apply().asInstanceOf[Map[String, Int]]
        futureList.-=(item)
      }
    }
    println(resMap.flatten.groupBy(_._1).mapValues(_.foldLeft(0)(_+_._2)))
  }




}

class Task extends Actor {
  override def act(): Unit = {
    while(true) {
      receive({
        case SmTask(file) => {
          val lines : List[String] = Source.fromFile(file).getLines().toList
          val words : List[String] = lines.flatMap(_.split(" "))
          val res :Map[String, Int] = words.map((_, 1)).groupBy(_._1).mapValues(_.size)
          sender ! res
        }
      })
    }
  }
}

case class SmTask(file:String) {

}

出现这样的错误,最后定位为:

val task = new Task
task.act()

应该改成  task.start()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值