/
Main.scala
54 lines (39 loc) · 1.2 KB
/
Main.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package demo
import cats.effect.IO
import java.util.concurrent.Executors
import scala.concurrent.ExecutionContext
object Main {
implicit def extendIO(io: IO.type): IOExtensions.type = IOExtensions
val blocking = ExecutionContext.fromExecutor(
Executors.newFixedThreadPool(1, NamedGroupThreadFactory("blocking"))
)
val compute = ExecutionContext.fromExecutor(
Executors.newFixedThreadPool(1, NamedGroupThreadFactory("compute"))
)
val threadName = IO(Thread.currentThread().getName)
def println(str: String) = IO(scala.Predef.println(str))
val thirdPartyCall = IO.async[Unit] { cb =>
new Thread(
{ () =>
// third party library stuff on some thread we don't manage
cb(Right(()))
}
).start
}
def main(args: Array[String]): Unit = pureMain(args.toList).unsafeRunSync()
def pureMain(args: List[String]): IO[Unit] = for {
_ <- IO.shift(compute)
a <- threadName
_ <- println(s"a: $a")
_ <- IO.shift(blocking)
b <- threadName
_ <- IO.shift(compute)
_ <- println(s"b: $b")
c <- threadName
_ <- println(s"c: $c")
_ <- thirdPartyCall
_ <- IO.shift(compute)
d <- threadName
_ <- println(s"d: $d")
} yield ()
}