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

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