Last active
May 25, 2024 10:20
-
-
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/f5b2d5dd9a0ac9ca3e9a24636e000866e51ef42d
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
// 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.4.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