Skip to content

Instantly share code, notes, and snippets.

@telekosmos
Last active May 3, 2022 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save telekosmos/b511d61cb6df36d8a0cf7e24cac259f2 to your computer and use it in GitHub Desktop.
Save telekosmos/b511d61cb6df36d8a0cf7e24cac259f2 to your computer and use it in GitHub Desktop.
Concurrency IO
val jobOne: IO[Int] = IO(1+1)
val jobTwo: IO[String] = IO(List("hello", "guys").mkString(" "))
// Concurrent execution (manually)
for {
j1Fiber <- jobOne.start
j2Fiber <- jobTwo.start
i <- j1Fiber.join
s <- j2Fiber.join
} yield (i, s)
// Concurrent execution (higher level)
val result2: IO[(Int, String)] = (jobOne, jobTwo).parTupled
import cats.effect._
import cats.effect.unsafe.implicits.global
import scala.concurrent.duration._
val program = for {
fiber <- IO.println(Thread.currentThread().getId).foreverM.start
_ <- IO.sleep(1.seconds)
_ <- fiber.cancel
} yield ()
program.unsafeRunSync()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment