The word "sequence" means a series of actions one after the other.
object Test {
def main(args: Array[String]) {
def producer() = {
val list = Seq(
future { println("startFirst"); Thread.sleep(3000); println("stopFirst") },
future { println("startSecond"); Thread.sleep(1000); println("stopSecond") }
)
Future.sequence(list)
}
Await.result(producer, Duration.Inf)
}
}
Therefore I expect this program to print:
startFirst
stopFirst
startSecond
stopSecond
or even:
startSecond
stopSecond
startFirst
stopFirst
but not (as it happens):
startFirst
startSecond
stopSecond
stopFirst
Why this method is not called Future.parallel()
?
And what should I use to guarantee that all futures in a Seq
of futures are triggered serially (as opposed to in parallel) ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…