Last active
November 10, 2017 08:16
-
-
Save petitviolet/dc04d140afc233a107dbe94e003c6ec5 to your computer and use it in GitHub Desktop.
Run scalaz Task in sequencial and parallel
This file contains 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
import scalaz._ | |
import scalaz.concurrent.Task | |
object TaskPrac extends App { | |
private def task(i: Int) = Task { | |
println(s"sleeping: $i") | |
Thread.sleep(i * 100) | |
println(s"wakeup: $i") | |
i | |
} | |
{ | |
println("============") | |
(for { | |
i <- task(1) | |
j <- task(2) | |
k <- task(3) | |
} yield {i + j + k }).unsafePerformAsync(println) | |
Thread.sleep(1000) | |
} | |
{ | |
println("============") | |
val k = task(3) | |
val i = task(1) | |
val j = task(2) | |
(for { | |
_i <- i | |
_k <- k | |
_j <- j | |
} yield {_i + _j + _k }).unsafePerformAsync(println) | |
Thread.sleep(1000) | |
} | |
{ | |
println("============") | |
val k = task(3) | |
val i = task(1) | |
val j = task(2) | |
Task.gatherUnordered(k :: j :: i :: Nil).unsafePerformAsync(println) | |
Thread.sleep(1000) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
実行するとこうなる