Futures enable an efficient way to write parallel operations in a nonblocking IO fashion. Futures are placeholder objects for values that might not exist yet. Futures are composable, and they work with callbacks instead of traditional blocking code.

Simple Future code in Scala REPL

$ scala

Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_77).

Type in expressions for evaluation. Or try :help. scala>import concurrent.Future import concurrent.Future

scala>import concurrent.ExecutionContext.Implicits.global import concurrent.ExecutionContext.Implicits.global scala>

scala>val f: Future[String] = Future { "Hello world!" } f: scala.concurrent.Future[String] = Success(Hello world!) scala>

scala>println("Result: " + f.value.get.get)

Result: Hello world! scala>

scala>println("Result: " + f)

Result: Success(Hello world!) scala>

In order to work with futures in Scala, we have to import concurrent.Future. We also need an executor, which is a way to work with threads. Scala has a default set of execution services. You can tweak it if you like, however, for now we can just use the defaults; to do that, we just import concurrent.ExecutionContext.Implicits.global.

It's possible to retrieve the Future value. Scala has a very explicit API, which makes the developer's life easier, and also gives good samples for how we should code our own APIs. Future has a method called value, which returns Option[scala.util.Try[A]] where A is the generic type you are using for the future; for our case, it's a String A. Try is a different way to do a try... catch, and this is safer, because the caller knows beforehand that the code they are calling may fail. Try[Optional] means that Scala will try to run some code and the code may fail — even if it does not fail, you might receive None or Some. This type of system makes everybody's lives better, because you can have Some or None as the Option return. Futures are a kind of callback. For our previous sample code, the result was obtained quite quickly, however, we often use futures to call external APIs, REST services, Microservices, SOAP Webservices, or any code that takes time to run and might not get completed. Futures also work with Pattern Matcher. Let's see another sample code.

< Prev   CONTENTS   Source   Next >