Search code examples
scalaspring-data-jpascala-cats

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


I have a simple sequence of IO operaion with 5 second pause.

  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()

How to make timer without ExecutionContext.global, IOApp or to fix amount of threads in ExecutionContext.global?


Solution

  • Try

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

    How to configure a fine tuned thread pool for futures?