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
| run("Eval on and back") { | |
| printThread *> printThread.on(ec1) *> printThread | |
| } | |
| /* | |
| Outputs: | |
| -- Eval on and back -- | |
| main | |
| ec1-1-716083600 |
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
| def run(name: String)(th: IO[_, _]): Unit = { | |
| println(s"-- $name --") | |
| new DefaultRuntime {}.unsafeRun(th) | |
| println() | |
| } | |
| run("Eval on") { | |
| printThread.on(ec1) | |
| } |
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
| run("async") { | |
| printThread *> a *> printThread | |
| } | |
| /* | |
| Outputs: | |
| -- async -- | |
| main | |
| ec3-1-785992331 (async) |
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 a = IO.async[Unit] { cb => | |
| ec3.submit(new Runnable { | |
| override def run(): Unit = { | |
| println(Thread.currentThread().getName + " (async)") | |
| cb(Right(())) | |
| } | |
| }) | |
| } |
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
| run("caveat 1") { | |
| val someEffect = IO.shift(ec1) *> printThread | |
| printThread *> someEffect *> printThread | |
| } | |
| /* | |
| Outputs: | |
| -- caveat 1 -- | |
| main |
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
| printThread *> printThread |
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
| run("Eval on") { | |
| printThread *> cs1.evalOn(ec2)(printThread) *> printThread | |
| } | |
| /* | |
| Outputs: | |
| -- Eval on -- | |
| main | |
| ec2-1-1945604815 |
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 cs1 = IO.contextShift(ec1) | |
| val cs2 = IO.contextShift(ec2) | |
| def run(name: String)(th: IO[_]): Unit = { | |
| println(s"-- $name --") | |
| th.unsafeRunSync() | |
| println() | |
| } | |
| run("Shift") { |
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 printThread = IO { println(Thread.currentThread().getName) } |
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 ec1 = ExecutionContext.fromExecutor(Executors.newCachedThreadPool( | |
| new NamedThreadFactory("ec1", true))) | |
| val ec2 = ExecutionContext.fromExecutor(Executors.newCachedThreadPool( | |
| new NamedThreadFactory("ec2", true))) | |
| val ec3 = Executors.newCachedThreadPool(new NamedThreadFactory("ec3", true)) |