Created
January 18, 2013 04:36
-
-
Save meetumaltiar/4562364 to your computer and use it in GitHub Desktop.
for expression on futures
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
import akka.dispatch.Future | |
import akka.actor.ActorSystem | |
object SumApplicationWithFutures extends App { | |
implicit val system = ActorSystem("future") | |
val startTime = System.currentTimeMillis | |
val future1 = Future(timeTakingIdentityFunction(1)) | |
val future2 = Future(timeTakingIdentityFunction(2)) | |
val future3 = Future(timeTakingIdentityFunction(3)) | |
val future = for { | |
x <- future1 | |
y <- future2 | |
z <- future3 | |
} yield (x + y + z) | |
future onSuccess { | |
case sum => | |
val elapsedTime = ((System.currentTimeMillis - startTime) / 1000.0) | |
println("Sum of 1, 2 and 3 is " + sum + " calculated in " + elapsedTime + " seconds") | |
} | |
def timeTakingIdentityFunction(number: Int) = { | |
// we sleep for 3 seconds and return number | |
Thread.sleep(3000) | |
number | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment