gpt4 book ai didi

scala - 如何限制 Akka (2.1.2) 中的消息?

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

你们能展示一下 Akka 中限制消息的例子吗?

这是我的代码

object Program {
def main(args: Array[String]) {
val system = ActorSystem()
val actor: ActorRef = system.actorOf(Props[HelloActor].withDispatcher("akka.actor.my-thread-pool-dispatcher"))

val zzz : Function0[Unit] = () => {
println(System.currentTimeMillis())
Thread.sleep(5000)
}

var i: Int = 0
while (i < 100) {
actor ! zzz
i += 1
}

println("DONE")

// system.shutdown()
}
}

class HelloActor extends Actor {
def receive = {
case func : Function0[Unit] => func()
}
}

这是我的配置

akka {
actor {
my-thread-pool-dispatcher {
type = Dispatcher
executor = "thread-pool-executor"
thread-pool-executor {
task-queue-type = "array"
task-queue-size = 4
}
}
}
}

但是当我运行它时,它似乎是单线程的,因为我希望同时处理 4 条消息。

我在这里错过了什么?

最佳答案

我看不出问题的标题和内容之间的联系。

这是一篇关于在 Akka 中限制消息的文章:

http://letitcrash.com/post/28901663062/throttling-messages-in-akka-2

但是,您似乎对您的 Actor 一次只处理一条消息这一事实感到困惑。但这就是 Akka Actor 的工作方式。他们只有一个消息邮箱,并且在连续循环中一次只处理一条消息。

如果你想用同一个工作处理单元同时处理多个任务,我建议你看看路由器:

http://doc.akka.io/docs/akka/2.1.2/scala/routing.html

关于scala - 如何限制 Akka (2.1.2) 中的消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16007333/

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