我有一个简单的问题/错误,但我无法弄清楚什么是幸福的。我想使用ActorSystem scheuler来安排回调。示例代码如下:
implicit val system: ActorSystem = ActorSystem("system-test1")
system.scheduler.scheduleOnce(2 seconds)(() =>
{
println("Hi")
})
但是,没有任何内容打印到控制台,虽然我在println行中有调试器断点,但调试器并没有停在那里。任何人都可以帮助我吗?
答案 0 :(得分:1)
system.scheduler.scheduleOnce(2 seconds)(() => { println("Hi") })
上面向scheduleOnce
方法传递一个函数,该函数接受零参数并返回Unit
。您调用的scheduleOnce
方法的版本不会将函数作为参数;它需要一个名为call {name}的参数Unit
:
final def scheduleOnce(delay: FiniteDuration)(f: => Unit)(implicit executor: ExecutionContext): Cancellable
因此,只需传入println("Hi")
,即Unit
:
system.scheduler.scheduleOnce(2 seconds)(println("Hi"))