Scala 中的预定执行器 [英] Scheduled Executor in Scala
本文介绍了Scala 中的预定执行器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Java 中,我可以使用 ScheduledThreadPoolExecutor 来安排在给定延迟后运行的任务.我可以在 Scala 中使用它,但我想知道是否有 Scala API.
In Java I can use Scheduled Executor to schedule tasks to run after a given delay. I can use it in Scala but I wonder if there is a Scala API for that.
是否有任何 Scala API(相对于 Java 中的 Scheduled Executor
)来安排任务?
Is there any Scala API (as opposed to Scheduled Executor
in Java) to schedule tasks?
推荐答案
Akka 有一些类似调度器的东西:
Akka has something similar with schedulers:
http://doc.akka.io/api/akka/2.1.4/#akka.actor.Scheduler
你可以从actor系统中获取:
You can obtain one from the actor system:
val actorSystem = ActorSystem()
val scheduler = actorSystem.scheduler
val task = new Runnable { def run() { log.info("Hello") } }
implicit val executor = actorSystem.dispatcher
scheduler.schedule(
initialDelay = Duration(5, TimeUnit.SECONDS),
interval = Duration(10, TimeUnit.SECONDS),
runnable = task)
如果你正在使用 Akka 或基于它的东西,比如 Play,那就是要走的路.
If you are using Akka or something based on it, like Play, that would be the way to go.
这篇关于Scala 中的预定执行器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文