Skip to content

Instantly share code, notes, and snippets.

View adamw's full-sized avatar

Adam Warski adamw

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