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

How to work with timer in cats effect without ExecutionContext.global and IOApp?

我有一个简单的 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 中固定线程数量?

尝试

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

How to configure a fine tuned thread pool for futures?