Created
April 17, 2015 12:10
-
-
Save samstarling/c6e4a0eaaf959d651d36 to your computer and use it in GitHub Desktop.
This file contains 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
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) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why do all of these take 3 seconds to complete?