Last active
May 6, 2023 15:39
-
-
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
This file contains 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
// 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