【发布时间】:2020-12-04 01:01:51
【问题描述】:
所以我有以下 Akka 调度程序设置
implicit val testActorSystem = ActorSystem(Behaviors.empty, "test-actor-system")
implicit val executionContext = testActorSystem.executionContext
val cleaner: Runnable = () => //do something
val cancellable: Cancellable = testActorSystem.scheduler.scheduleOnce(0 seconds, cleaner)
我想等到我的Runnable 完成。我怎样才能检查这个?我知道这个任务在后台转换为Futures,但在这种情况下,我无法访问这些Futures 并等待它们。那么等待任务完成的最佳做法是什么?
【问题讨论】:
-
是用于测试还是用于生产代码?
-
@IvanStanislavciuc,这有关系吗?
-
请注意,“这个任务在后台被转换为
Futures”是不正确的:不需要发生这样的转换(参见,例如LightArrayRevolverScheduler,它通过传递它来执行Runnable通过TaskHolder在ExecutionContext中执行)。 -
在您的代码的特定情况下,如果您希望在不久的将来(在
0 seconds)执行任务并获得在任务完成时完成的Future,为什么不Future { cleaner.run() }?