Skip to content

Instantly share code, notes, and snippets.

@ioleo
Last active October 13, 2016 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ioleo/2438b775e76a30490f4a35360faf417a to your computer and use it in GitHub Desktop.
Save ioleo/2438b775e76a30490f4a35360faf417a to your computer and use it in GitHub Desktop.
Scala future sequence
import scala.concurrent.{Await, Future}
import scala.concurrent.duration.Duration
import scala.concurrent.ExecutionContext.Implicits.global
object Main extends App {
val seq = Future.sequence(Seq(
Future { Thread.sleep(7000); println("doing 1"); 1 },
Future { println("doing 2"); 2 },
Future { Thread.sleep(3000); println("doing 3"); 3 },
Future { println("doing 4"); 4 },
Future { println("doing 5"); 5 },
Future { Thread.sleep(5000); println("doing 6"); 6 },
Future { println("doing 7"); 7 },
Future { println("doing 8"); 8 },
Future { println("doing 9"); 9 }
))
val list = Await.result(seq, Duration.Inf)
println("-----")
println(list)
for (value <- list)
println(value)
}
/*
> run
[info] Running Main
doing 2
doing 3
doing 4
doing 5
doing 1
doing 7
doing 8
doing 9
doing 6
-----
List(1, 2, 3, 4, 5, 6, 7, 8, 9)
1
2
3
4
5
6
7
8
9
[success] Total time: 8 s, completed Oct 13, 2016 11:03:28 AM
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment