Scala 中的预定执行器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16625464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 05:18:08  来源:igfitidea点击:

Scheduled Executor in Scala

scalascheduled-tasks

提问by Michael

In Java I can use Scheduled Executorto schedule tasks to run after a given delay. I can use it in Scala but I wonder if there is a ScalaAPI for that.

在 Java 中,我可以使用Scheduled Executor来安排任务在给定的延迟后运行。我可以在 Scala 中使用它,但我想知道是否有ScalaAPI。

Is there any Scala API (as opposed to Scheduled Executorin Java) to schedule tasks?

是否有任何 Scala API(而不是Scheduled ExecutorJava)来安排任务?

回答by Marius Danila

Akka has something similar with schedulers:

Akka 有一些类似于调度程序的东西:

http://doc.akka.io/api/akka/2.1.4/#akka.actor.Scheduler

http://doc.akka.io/api/akka/2.1.4/#akka.actor.Scheduler

You can obtain one from the actor system:

你可以从actor系统获取一个:

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)

If you are using Akka or something based on it, like Play, that would be the way to go.

如果你使用 Akka 或基于它的东西,比如 Play,那将是要走的路。

回答by MikeB

I have been looking for a scala api for scheduled execution as well.

我也一直在寻找用于计划执行的 scala api。

Java's ScheduledExecutor:

Java 的 ScheduledExecutor:

  • uses a thread pool for running the scheduler and operating the timeouts, and so does not require a Thread per timeout
  • No akka needed
  • 使用线程池来运行调度程序和操作超时,因此不需要每个超时的线程
  • 不需要阿卡

I wrote a little scala wrapper for the single task scheduling. See the gist: https://gist.github.com/platy/8f0e634c64d9fb54559c

我为单任务调度编写了一个小 Scala 包装器。查看要点:https: //gist.github.com/platy/8f0e634c64d9fb54559c

回答by Sheng

You can use scalaz's Task,

你可以使用scalaz的任务,

import scala.concurrent.duration.{FiniteDuration, SECONDS}
import scalaz.concurrent.Task
Task.schedule(Console.println("time's up"), FiniteDuration(5, SECONDS)).runAsync { _ => }

回答by JR Utily

As an alternative, there is Monix scheduler too: https://monix.io/docs/3x/execution/scheduler.html

作为替代方案,也有 Monix 调度程序:https://monix.io/docs/3x/execution/scheduler.html

It use a Java's Scheduled Executorbehind, but it is wrapped and transparent.

它使用 Java 的Scheduled Executor背后,但它是包装和透明的。

You need to implement some Runnableto be executed, which is lighter than Akka Actor.

你需要实现一些Runnable要执行的,比 Akka 更轻Actor

For example, you can do (taken from the documentation):

例如,您可以执行以下操作(取自文档):

lazy val scheduler =
  Scheduler.singleThread(name="my-thread")

// First execution in 3 seconds, then every 5 seconds
val c = scheduler.scheduleAtFixedRate(
  3, 5, TimeUnit.SECONDS,
  new Runnable {
    def run(): Unit = {
      println("Fixed delay task")
    }
  })

// If we change our mind and want to cancel
c.cancel()