Skip to content

Instantly share code, notes, and snippets.

@hanfeisun
Created May 13, 2015 07:52
Show Gist options
  • Save hanfeisun/a266d0771402918d95fb to your computer and use it in GitHub Desktop.
Save hanfeisun/a266d0771402918d95fb to your computer and use it in GitHub Desktop.
HowScalaFutureWorks.scala
import scala.concurrent._
import ExecutionContext.Implicits.global
import scala.concurrent.duration._
val is = 1 to 10 toList
def db = s"${Thread.currentThread}"
def f(i: Int) = 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
}
val theFuture = Future.traverse(is)(f _)
Await.result(theFuture, 100.seconds)
@hanfeisun
Copy link
Author

This is the output on my computer

Thread[ForkJoinPool-1-worker-5,5,main] ->        I'm 1 ->        I start to sleep
Thread[ForkJoinPool-1-worker-1,5,main] ->        I'm 2 ->        I start to sleep
Thread[ForkJoinPool-1-worker-7,5,main] ->        I'm 4 ->        I start to sleep
Thread[ForkJoinPool-1-worker-3,5,main] ->        I'm 3 ->        I start to sleep
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-3,5,main] ->        I'm 3 ->        I wake from sleep
Thread[ForkJoinPool-1-worker-7,5,main] ->        I'm 4 ->        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 400 =>      I start to sleep
Thread[ForkJoinPool-1-worker-1,5,main] =>        I'm 200 =>      I start to sleep
Thread[ForkJoinPool-1-worker-5,5,main] =>        I'm 100 =>      I wake from sleep
Thread[ForkJoinPool-1-worker-1,5,main] =>        I'm 200 =>      I wake from sleep
Thread[ForkJoinPool-1-worker-7,5,main] =>        I'm 400 =>      I wake from sleep
Thread[ForkJoinPool-1-worker-5,5,main] ->        I'm 6 ->        I start to sleep
Thread[ForkJoinPool-1-worker-7,5,main] ->        I'm 7 ->        I start to sleep
Thread[ForkJoinPool-1-worker-3,5,main] =>        I'm 300 =>      I wake from sleep
Thread[ForkJoinPool-1-worker-1,5,main] ->        I'm 5 ->        I start to sleep
Thread[ForkJoinPool-1-worker-3,5,main] ->        I'm 8 ->        I start to sleep
Thread[ForkJoinPool-1-worker-5,5,main] ->        I'm 6 ->        I wake from sleep
Thread[ForkJoinPool-1-worker-7,5,main] ->        I'm 7 ->        I wake from sleep
Thread[ForkJoinPool-1-worker-1,5,main] ->        I'm 5 ->        I wake from sleep
Thread[ForkJoinPool-1-worker-3,5,main] ->        I'm 8 ->        I wake from sleep
Thread[ForkJoinPool-1-worker-5,5,main] =>        I'm 600 =>      I start to sleep
Thread[ForkJoinPool-1-worker-3,5,main] =>        I'm 800 =>      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 800 =>      I wake from sleep
Thread[ForkJoinPool-1-worker-7,5,main] =>        I'm 700 =>      I wake from sleep
Thread[ForkJoinPool-1-worker-1,5,main] =>        I'm 500 =>      I wake from sleep
Thread[ForkJoinPool-1-worker-5,5,main] =>        I'm 600 =>      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 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-5,5,main] =>        I'm 1000 =>     I start to sleep
Thread[ForkJoinPool-1-worker-3,5,main] =>        I'm 900 =>      I start to sleep
Thread[ForkJoinPool-1-worker-5,5,main] =>        I'm 1000 =>     I wake from sleep
Thread[ForkJoinPool-1-worker-3,5,main] =>        I'm 900 =>      I wake from sleep

@hanfeisun
Copy link
Author

An advanced version can be viewed at https://gist.github.com/hanfeisun/9f2a486f0c7f75d67ac3

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