Last active
May 3, 2022 11:27
-
-
Save telekosmos/b511d61cb6df36d8a0cf7e24cac259f2 to your computer and use it in GitHub Desktop.
Concurrency IO
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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