Home > Article >

Article: An Introductory Look at the Scala Future

A Scala Future represents a value that may or may not currently be available, but will be available at some point, or an exception if that value could not be made available.

Published

scala_future
Author: Mensah Alkebu-Lan

Table of Contents #

Introduction #

A Scala Future represents a value that may or may not currently be available, but will be available at some point, or an exception if that value could not be made available.

In a single-threaded scenario, short-running tasks may have to wait for long-running tasks to complete. This is one of the things that makes multi-threaded programming more attractive in some cases.

To get a better idea of how concurrency works in Scala, we provide the following simple example.

Start the Scala REPL #

First, start the Scala REPL by typing the scala command in the command line:

malkebu-lan@xps-13-9310-uequations-3536:~$ scala
Welcome to Scala 2.11.12 (OpenJDK 64-Bit Server VM, Java 11.0.12).
Type in expressions for evaluation. Or try :help.

scala>

From here, let’s enter the required import statements for the exercise. One way of entering multiple lines into the REPL is by using the :paste command:

scala> :paste
// Entering paste mode (ctrl-D to finish)

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.{Failure, Success}

// Exiting paste mode, now interpreting.

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.{Failure, Success}

scala>

It may be worth mentioning something about the ExecutionContext.Implicits.global import statement. It is the default global execution context. You can think of an execution context as being a thread pool.

We can also use :paste for multi-line methods, or we can use an open bracket to let the REPL know we intend to enter code until we close the bracket:

scala> def longRunningAlgorithm() = {
| Thread.sleep(10000)
| 42
| }
longRunningAlgorithm: ()Int

scala>
Now the function is defined, let’s see what happens when we enter this ten-second method into a Future. The simplest way to create a new future is to the Future.apply method. An example is given below:

scala> val eventualInt = Future(longRunningAlgorithm())
eventualInt: scala.concurrent.Future[Int] = Future(<not completed>)

‘Not completed’ means the process is still running. As you may guess, if you wait ten seconds and enter the name of the variable, you should see the method call has returned a value:

scala> eventualInt
res0: scala.concurrent.Future[Int] = Future(Success(42))

Another Example #

Let’s come up with a slightly more interesting example. Let’s quit the REPL by entering ‘:q.’:

scala> :q
malkebu-lan@xps-13-9310-uequations-3536:~$

Let’s make a simple text file:

malkebu-lan@xps-13-9310-uequations-3536:~$ touch myText.txt
malkebu-lan@xps-13-9310-uequations-3536:~$ echo "M8wJ myKeyword E4Uq" > myText.txt

Now, let’s enter back into the REPL and create a method that can do a computation on the contents of this file. First, we’ll paste in the three required import statements as before then create the following method:

scala> val firstOccurrence: Future[Int] = Future {
| val source = scala.io.Source.fromFile("myText.txt")
| source.toSeq.indexOfSlice("myKeyword")
| }
firstOccurrence: scala.concurrent.Future[Int] = Future(<not completed>)

It shouldn’t take long for this method call to complete. Enter in the name of the variable to see if the method has returned a value:

scala> firstOccurrence
res0: scala.concurrent.Future[Int] = Future(Success(5))

References #

  1. “Concurrency | Scala 3 — Book.” Scala Documentation, https://docs.scala-lang.org/scala3/book/concurrency.html. Accessed 22 August 2022.
  2. Castorina, Diego. “Concurrency and Fault Tolerance Made Easy: An Akka Tutorial with Examples.” Toptal, https://www.toptal.com/scala/concurrency-and-fault-tolerance-made-easy-an-intro-to-akka. Accessed 22 August 2022.
  3. Drygala, Lukasz. “A Guide to Scala Futures.” Baeldung, 29 December 2021, https://www.baeldung.com/scala/futures. Accessed 22 August 2022.
  4. Alexander, Alvin. “Simple concurrency with Scala Futures (Futures tutorial) | alvinalexander.com.” Alvin Alexander, 22 October 2020, https://alvinalexander.com/scala/concurrency-with-scala-futures-tutorials-examples/. Accessed 22 August 2022.
  5. “Futures and Promises.” Scala Documentation, https://docs.scala-lang.org/overviews/core/futures.html. Accessed 22 August 2022.