Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 6, 2023 15:39
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 dacr/ffceacf56a63bc4089e7ba7433b1773f to your computer and use it in GitHub Desktop.
Save dacr/ffceacf56a63bc4089e7ba7433b1773f to your computer and use it in GitHub Desktop.
ZIO learning - fiber based concurrency model - using fork and join / published by https://github.com/dacr/code-examples-manager #9b586f61-3106-467f-b35e-75910cf36d88/ec3dc260230ed0d054040dc6707435af333ac0d4
// summary : ZIO learning - fiber based concurrency model - using fork and join
// keywords : scala, zio, learning, pure-functional, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : 9b586f61-3106-467f-b35e-75910cf36d88
// created-on : 2021-04-02T15:36:11+02:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.2.2"
//> using dep "dev.zio::zio:2.0.13"
// ---------------------
import zio.*
object ThatApp extends ZIOAppDefault {
// using fork & join - semantically block but never block underlying threads
val logic1 = for {
_ <- Console.printLine("hello")
} yield ()
val logic2 = for {
_ <- Console.printLine("world")
} yield ()
val run = for {
a <- logic1.repeat(Schedule.recurs(5) && Schedule.spaced(50.millis)).fork
b <- logic2.repeat(Schedule.recurs(5) && Schedule.spaced(50.millis)).fork
_ <- a.join // because we don't want to exit before completion ;)
_ <- b.join // because we don't want to exit before completion ;)
} yield ()
}
// -------------------------------------------------------------
ThatApp.main(Array.empty)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment