Created
May 13, 2015 07:52
-
-
Save hanfeisun/a266d0771402918d95fb to your computer and use it in GitHub Desktop.
HowScalaFutureWorks.scala
This file contains hidden or 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 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) |
Author
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
This is the output on my computer