Skip to content

Instantly share code, notes, and snippets.

@jsrikrishna
Last active June 13, 2021 09:25
Show Gist options
  • Save jsrikrishna/7db4b47514f7cf2ce171 to your computer and use it in GitHub Desktop.
Save jsrikrishna/7db4b47514f7cf2ce171 to your computer and use it in GitHub Desktop.
Scala Future.sequence example
/**
* @author sjalipar
*/
object myMain {
def main(args: Array[String]): Unit = {
println("test - start")
import scala.concurrent.Future
import scala.util.Success
import scala.util.Failure
import scala.concurrent.Await
import scala.concurrent.duration.DurationInt
import scala.concurrent.ExecutionContext.Implicits.global
val x1 = Future {
Thread.sleep(2000)
println("x1 is completed")
"Hello x1"
}
val x2 = Future {
Thread.sleep(4000)
println("x2 is completed")
throw new Exception("Exception occured with x2")
}
val x3 = Future {
Thread.sleep(10000)
println("x3 is completed")
"Hello x3"
}
val x = Future.sequence(List(x1, x2, x3))
x.onComplete {
case Success(res) => println("Success: " + res)
case Failure(ex) => println("Ohhh Exception: " + ex.getMessage)
}
Await.ready(x, 20.seconds)
println(s"Future val is: ${x.value}")
Thread.sleep(2000)
println("test - end")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment