如何在没有ExecutionContext.global和IOApp的情况下在猫效果中使用计时器?

时间:2019-04-28 10:53:09

标签: scala spring-data-jpa scala-cats

我有一个简单的IO操作序列,暂停了5秒。

  implicit val timer = IO.timer(ExecutionContext.global)

  def doSth(str: String): IO[Unit] = IO(println(str))
  def greeting(): IO[Unit] =
    doSth("Before timer.") *>
      Timer[IO].sleep(5 second) *>
      doSth("After timer")

  val a = greeting().unsafeRunAsyncAndForget()

如何在不使用ExecutionContext.globalIOApp的情况下设置计时器或如何固定ExecutionContext.global中的线程数量?

1 个答案:

答案 0 :(得分:2)

尝试

implicit val timer = IO.timer(ExecutionContext.fromExecutor(Executors.newFixedThreadPool(10)))

How to configure a fine tuned thread pool for futures?