Akka scheduleOnce 回调从未调用过

Akka scheduleOnce callback never called

我有一个简单的 problem/bug,但我不知道发生了什么。我想使用 ActorSystem 调度程序来安排回调。示例代码如下:

implicit val system: ActorSystem = ActorSystem("system-test1")

system.scheduler.scheduleOnce(2 seconds)(() =>
                                         {
                                           println("Hi")
                                         })

但是,控制台没有打印任何内容,尽管我在 println 行中有一个调试器断点,但调试器并没有就此停止。谁能帮帮我?

system.scheduler.scheduleOnce(2 seconds)(() => { println("Hi") })

上面向scheduleOnce方法传递了一个采用零参数和returns Unit的函数。您正在调用的 scheduleOnce 方法的版本不将函数作为参数;它需要一个按名称调用的参数,该参数是 Unit:

final def scheduleOnce(delay: FiniteDuration)(f: => Unit)(implicit executor: ExecutionContext): Cancellable  

因此,只需传入 println("Hi"),即 returns Unit:

system.scheduler.scheduleOnce(2 seconds)(println("Hi"))