Skip to content

Instantly share code, notes, and snippets.

@samstarling
Created April 17, 2015 12:10
Show Gist options
  • Save samstarling/c6e4a0eaaf959d651d36 to your computer and use it in GitHub Desktop.
Save samstarling/c6e4a0eaaf959d651d36 to your computer and use it in GitHub Desktop.
package com.samstarling.futures
import com.twitter.util.{Future, Await}
import com.samstarling.util.time
object ConcurrentComposition extends App {
def slowTask(time: Int): Future[String] = Future {
Thread.sleep(time)
"Hello"
}
time("collect", {
Await.result(Future.collect(List(slowTask(1000), slowTask(2000))))
})
time("join", {
Await.result(Future.join(slowTask(1000), slowTask(2000)))
})
time("for", {
for {
r1 <- slowTask(1000)
r2 <- slowTask(2000)
} yield (r1, r2)
})
}
@samstarling
Copy link
Author

Why do all of these take 3 seconds to complete?

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