Last active
February 3, 2026 20:24
-
-
Save dacr/598ec0a4ed6005cd521aef529abef7b9 to your computer and use it in GitHub Desktop.
ZIO learning - garbage collected fibers / published by https://github.com/dacr/code-examples-manager #13f4a62f-c2ec-4405-a710-1444844f1592/62b15932e5eb6e1153643f4aef5a108801a91355
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 - garbage collected fibers | |
| // keywords : scala, zio, learning, pure-functional, @testable | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // id : 13f4a62f-c2ec-4405-a710-1444844f1592 | |
| // created-on : 2021-04-06T09:46:48+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" | |
| // --------------------- | |
| /* | |
| inspired from | |
| - [John A De Goes - ZIO: Next-Generation Effects in Scala](https://www.youtube.com/watch?v=mkSHhsJXjdc) | |
| - +23:30 | |
| */ | |
| import zio.* | |
| object Encapsulated extends ZIOAppDefault { | |
| val logic = for { | |
| queue <- Queue.bounded[Int](100) | |
| fiber <- queue.take.flatMap(v => Console.printLine(s"$v ")).forever.fork | |
| _ <- ZIO.foreach(0 to 100)(queue.offer(_)).forever | |
| } yield () | |
| def run = logic.timeout(Duration.fromMillis(5000)) // Just to avoid this script to run infinitly... | |
| } | |
| // infinitely running fiber... which will be garbage collected | |
| // (unlike threads which required to be stopped !) | |
| // ------------------------------------------------------------- | |
| Encapsulated.main(Array.empty) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment