-6ren">
gpt4 book ai didi

Scala Actor 异常 "react on channel belonging to other actor"

转载 作者:行者123 更新时间:2023-12-01 10:29:43 26 4
gpt4 key购买 nike

鉴于以下代码:

class A extends Actor {
def act() {
loop {
reactWithin(1000) {
case _ => println("A Message")
}
}
}
}


class B extends A {
val test = Actor.actor {
loop {
reactWithin(1000) {
case "B" => println("B Message")
}
}
}
}

创建 B 的实例 val t = new B()抛出以下异常:
scala.actors.Actor$$anon$1@452bb7e0: caught java.lang.AssertionError: assertion failed: react on channel belonging to other actor
java.lang.AssertionError: assertion failed: react on channel belonging to other actor
at scala.actors.ReplyReactor$class.reactWithin(ReplyReactor.scala:123)
at A.scala$actors$Actor$$super$reactWithin(Tester.scala:11)
at scala.actors.Actor$class.reactWithin(Actor.scala:613)
at A.reactWithin(Tester.scala:11)
at B$$anonfun$1$$anonfun$apply$mcV$sp$1.apply(Tester.scala:24)
at B$$anonfun$1$$anonfun$apply$mcV$sp$1.apply(Tester.scala:24)
at scala.actors.Reactor$class.seq(Reactor.scala:280)
at A.seq(Tester.scala:11)
at scala.actors.Reactor$$anon$3.andThen(Reactor.scala:258)
at scala.actors.Combinators$class.loop(Combinators.scala:26)
at A.loop(Tester.scala:11)
at B$$anonfun$1.apply$mcV$sp(Tester.scala:23)
at scala.actors.Actor$$anon$1.act(Actor.scala:135)
at scala.actors.Reactor$$anonfun$dostart$1.apply(Reactor.scala:222)
at scala.actors.Reactor$$anonfun$dostart$1.apply(Reactor.scala:222)
at scala.actors.ReactorTask.run(ReactorTask.scala:36)
at scala.concurrent.forkjoin.ForkJoinPool$AdaptedRunnable.exec(ForkJoinPool.java:611)
at scala.concurrent.forkjoin.ForkJoinTask.quietlyExec(ForkJoinTask.java:422)
at scala.concurrent.forkjoin.ForkJoinWorkerThread.mainLoop(ForkJoinWorkerThread.java:340)
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:325)

有人可以请教我这是为什么吗? :)

最佳答案

在 Actor.actor 块中,您必须使用“self”变量来引用诸如 react 和 reactWithin 之类的内容,例如:self.reactWithin(1000)。这是为了将它们与已经从父类继承的方法区分开来。

关于Scala Actor 异常 "react on channel belonging to other actor",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3624254/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com