
Other useful and well-named methods include: In addition to those methods, futures have methods that you’ll find on Scala collections classes, including: Other Future methodsįutures have other methods that you can use. However, as you’ll see in the Github code, there are many more println statements in the MultipleFuturesWithDebugOutput example so you can see exactly how futures work.

There are only a few println statements in this code, so you can focus on how the main parts of the application works.

currentTimeMillis () def deltaTime ( t0 : Long ) = System. About that sleep callĪ final point to note about small examples like this is that you need to have a sleep call at the end of your App:ĭef currentTime = System. The code inside onComplete doesn’t execute until after the for-expression assigns a value to result. It’s important to note that just as the JVM’s main thread didn’t stop at the for-expression, it doesn’t block here, either. That Future instance will contain an exception, so when you call result.onComplete like this, control will flow to the Failure case. For example, imagine that you call a web service, but the web service is down. Otherwise, if an exception was thrown, go to the Failure case and print the exception’s stack trace.”Īs that code implies, it’s completely possible that a Future may fail. If everything returned successfully, run the println statement shown in the Success case. You can read that code as, “Whenever result has a final value - i.e., after all of the futures return in the for-expression - come here.

In the same way that the foreach method on collections classes returns Unit and is only used for side effects, onComplete returns Unit and you only use it for side effects like printing the results, updating a GUI, updating a database, etc. OnComplete is a method that’s available on a Future, and you use it to process the future’s result as a side effect. Package futures import .global import import scala.util. To demonstrate how this works, let’s start with an example of a Future in the Scala REPL.

You typically use it when you need to call an algorithm that runs an indeterminate amount of time - such as calling a web service or executing a long-running algorithm - so you therefore want to run it off of the main thread. You can find the source code for this lesson at this URL:Ī Scala Future is used to create a temporary pocket of concurrency that you use for one-shot needs. Tip: If you’re just starting to work with futures and find the name Future to be confusing in the following examples, replace it with the name ConcurrentResult, which might be easier to understand initially. In this lesson you’ll see how to use futures, including how to run multiple futures in parallel and combine their results in a for-expression, along with other methods that are used to handle the value in a future once it returns. (As a point of comparison, Akka actors are intended to run for a long time and respond to many requests during their lifetime, but each future you create is intended to be run only once.) Def aLongRunningTask () : Future = ? val x = aLongRunningTaskīut because aLongRunningTask takes an indeterminate amount of time to return, the value in x may or may not be currently available, but it will be available at some point (in the future).Īnother important point to know about futures is that they’re intended as a one-shot, “Handle this relatively slow computation on some other thread, and call me back with a result when you’re done” construct.
