Skip to content

Instantly share code, notes, and snippets.

@hanfeisun
Created May 13, 2015 08:02
Show Gist options
  • Save hanfeisun/9f2a486f0c7f75d67ac3 to your computer and use it in GitHub Desktop.
Save hanfeisun/9f2a486f0c7f75d67ac3 to your computer and use it in GitHub Desktop.
HowScalaFutureWorksAdvance.scala
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent._
import scala.concurrent.duration._
val is = 1 to 10 toList
def db = s"${Thread.currentThread}"
def f(i: Int) = {
val a = Future {
println(db + s" -> \t I'm ${i} -> \t I start to sleep")
Thread.sleep(1 * 1000)
println(db + s" -> \t I'm ${i} -> \t I wake from sleep")
i * 100
}.map {
x =>
println(db + s" => \t I'm ${x} => \t I start to sleep")
Thread.sleep(1 * 1000)
println(db + s" => \t I'm ${x} => \t I wake from sleep")
i * 100
}
println(db + s" \t >>> \t I'm in the scope of f(${i})")
a.map {
x =>
println(db + s" ~> \t I'm ${x} ~> \t I start to sleep")
Thread.sleep(1 * 1000)
println(db + s" ~> \t I'm ${x} ~> \t I wake from sleep")
i * 100
}
println(db + s" \t >>> \t I'm in the end of f(${i})")
a
}
val theFuture = Future.traverse(is)(f _)
Await.result(theFuture, 100.seconds)
@hanfeisun
Copy link
Author

Output on my computer

Thread[ForkJoinPool-1-worker-5,5,main] ->        I'm 1 ->        I start to sleep
Thread[main,5,main]      >>>     I'm in the scope of f(1)
Thread[main,5,main]      >>>     I'm in the end of f(1)
Thread[ForkJoinPool-1-worker-1,5,main] ->        I'm 2 ->        I start to sleep
Thread[main,5,main]      >>>     I'm in the scope of f(2)
Thread[main,5,main]      >>>     I'm in the end of f(2)
Thread[ForkJoinPool-1-worker-3,5,main] ->        I'm 3 ->        I start to sleep
Thread[main,5,main]      >>>     I'm in the scope of f(3)
Thread[main,5,main]      >>>     I'm in the end of f(3)
Thread[ForkJoinPool-1-worker-7,5,main] ->        I'm 4 ->        I start to sleep
Thread[main,5,main]      >>>     I'm in the scope of f(4)
Thread[main,5,main]      >>>     I'm in the end of f(4)
Thread[main,5,main]      >>>     I'm in the scope of f(5)
Thread[main,5,main]      >>>     I'm in the end of f(5)
Thread[main,5,main]      >>>     I'm in the scope of f(6)
Thread[main,5,main]      >>>     I'm in the end of f(6)
Thread[main,5,main]      >>>     I'm in the scope of f(7)
Thread[main,5,main]      >>>     I'm in the end of f(7)
Thread[main,5,main]      >>>     I'm in the scope of f(8)
Thread[main,5,main]      >>>     I'm in the end of f(8)
Thread[main,5,main]      >>>     I'm in the scope of f(9)
Thread[main,5,main]      >>>     I'm in the end of f(9)
Thread[main,5,main]      >>>     I'm in the scope of f(10)
Thread[main,5,main]      >>>     I'm in the end of f(10)
Thread[ForkJoinPool-1-worker-5,5,main] ->        I'm 1 ->        I wake from sleep
Thread[ForkJoinPool-1-worker-5,5,main] =>        I'm 100 =>      I start to sleep
Thread[ForkJoinPool-1-worker-1,5,main] ->        I'm 2 ->        I wake from sleep
Thread[ForkJoinPool-1-worker-1,5,main] =>        I'm 200 =>      I start to sleep
Thread[ForkJoinPool-1-worker-3,5,main] ->        I'm 3 ->        I wake from sleep
Thread[ForkJoinPool-1-worker-3,5,main] =>        I'm 300 =>      I start to sleep
Thread[ForkJoinPool-1-worker-7,5,main] ->        I'm 4 ->        I wake from sleep
Thread[ForkJoinPool-1-worker-7,5,main] =>        I'm 400 =>      I start to sleep
Thread[ForkJoinPool-1-worker-5,5,main] =>        I'm 100 =>      I wake from sleep
Thread[ForkJoinPool-1-worker-5,5,main] ~>        I'm 100 ~>      I start to sleep
Thread[ForkJoinPool-1-worker-1,5,main] =>        I'm 200 =>      I wake from sleep
Thread[ForkJoinPool-1-worker-1,5,main] ~>        I'm 200 ~>      I start to sleep
Thread[ForkJoinPool-1-worker-7,5,main] =>        I'm 400 =>      I wake from sleep
Thread[ForkJoinPool-1-worker-7,5,main] ~>        I'm 400 ~>      I start to sleep
Thread[ForkJoinPool-1-worker-3,5,main] =>        I'm 300 =>      I wake from sleep
Thread[ForkJoinPool-1-worker-3,5,main] ~>        I'm 300 ~>      I start to sleep
Thread[ForkJoinPool-1-worker-1,5,main] ~>        I'm 200 ~>      I wake from sleep
Thread[ForkJoinPool-1-worker-3,5,main] ~>        I'm 300 ~>      I wake from sleep
Thread[ForkJoinPool-1-worker-3,5,main] ->        I'm 6 ->        I start to sleep
Thread[ForkJoinPool-1-worker-7,5,main] ~>        I'm 400 ~>      I wake from sleep
Thread[ForkJoinPool-1-worker-7,5,main] ->        I'm 7 ->        I start to sleep
Thread[ForkJoinPool-1-worker-1,5,main] ->        I'm 5 ->        I start to sleep
Thread[ForkJoinPool-1-worker-5,5,main] ~>        I'm 100 ~>      I wake from sleep
Thread[ForkJoinPool-1-worker-5,5,main] ->        I'm 8 ->        I start to sleep
Thread[ForkJoinPool-1-worker-1,5,main] ->        I'm 5 ->        I wake from sleep
Thread[ForkJoinPool-1-worker-3,5,main] ->        I'm 6 ->        I wake from sleep
Thread[ForkJoinPool-1-worker-5,5,main] ->        I'm 8 ->        I wake from sleep
Thread[ForkJoinPool-1-worker-7,5,main] ->        I'm 7 ->        I wake from sleep
Thread[ForkJoinPool-1-worker-5,5,main] =>        I'm 800 =>      I start to sleep
Thread[ForkJoinPool-1-worker-3,5,main] =>        I'm 600 =>      I start to sleep
Thread[ForkJoinPool-1-worker-1,5,main] =>        I'm 500 =>      I start to sleep
Thread[ForkJoinPool-1-worker-7,5,main] =>        I'm 700 =>      I start to sleep
Thread[ForkJoinPool-1-worker-3,5,main] =>        I'm 600 =>      I wake from sleep
Thread[ForkJoinPool-1-worker-7,5,main] =>        I'm 700 =>      I wake from sleep
Thread[ForkJoinPool-1-worker-5,5,main] =>        I'm 800 =>      I wake from sleep
Thread[ForkJoinPool-1-worker-7,5,main] ~>        I'm 700 ~>      I start to sleep
Thread[ForkJoinPool-1-worker-5,5,main] ~>        I'm 800 ~>      I start to sleep
Thread[ForkJoinPool-1-worker-1,5,main] =>        I'm 500 =>      I wake from sleep
Thread[ForkJoinPool-1-worker-3,5,main] ~>        I'm 600 ~>      I start to sleep
Thread[ForkJoinPool-1-worker-1,5,main] ~>        I'm 500 ~>      I start to sleep
Thread[ForkJoinPool-1-worker-7,5,main] ~>        I'm 700 ~>      I wake from sleep
Thread[ForkJoinPool-1-worker-3,5,main] ~>        I'm 600 ~>      I wake from sleep
Thread[ForkJoinPool-1-worker-3,5,main] ->        I'm 9 ->        I start to sleep
Thread[ForkJoinPool-1-worker-5,5,main] ~>        I'm 800 ~>      I wake from sleep
Thread[ForkJoinPool-1-worker-1,5,main] ~>        I'm 500 ~>      I wake from sleep
Thread[ForkJoinPool-1-worker-7,5,main] ->        I'm 10 ->       I start to sleep
Thread[ForkJoinPool-1-worker-3,5,main] ->        I'm 9 ->        I wake from sleep
Thread[ForkJoinPool-1-worker-7,5,main] ->        I'm 10 ->       I wake from sleep
Thread[ForkJoinPool-1-worker-3,5,main] =>        I'm 900 =>      I start to sleep
Thread[ForkJoinPool-1-worker-7,5,main] =>        I'm 1000 =>     I start to sleep
Thread[ForkJoinPool-1-worker-3,5,main] =>        I'm 900 =>      I wake from sleep
Thread[ForkJoinPool-1-worker-7,5,main] =>        I'm 1000 =>     I wake from sleep
Thread[ForkJoinPool-1-worker-7,5,main] ~>        I'm 1000 ~>     I start to sleep
Thread[ForkJoinPool-1-worker-5,5,main] ~>        I'm 900 ~>      I start to sleep

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment