What are the Scala Futures and what is the Purpose of using them?
We have stated in the introduction that Scala Futures serve the same purpose as threads in any other programming language i.e., concurrency. It means that by creating a Scala Future for any task that is expected to take a longer time than usual to execute, you can perform other tasks in parallel. This will save your computation time without blocking the normal flow of your program.
Examples of Using Scala Futures
If you wish to use the Scala Futures in Ubuntu 20.04, then the following two examples will assist you well in doing so.
Example # 1: Getting Started with Scala Futures
In this example, we only wish to share with you the general syntax of using the Scala Futures. For learning that, you need to take a look at the Scala code shown below:
For using the Scala Futures, you first need to import the two required libraries i.e., “scala.concurrent._” and “scala.concurrent.ExecutionContext.Implicits.global”. After importing these libraries, the next thing that you need to do is to create an executable class. We have created a class named “Test” with the help of the “object” keyword. Then, within this executable class, we have created a driver function “main()” inside which we have created a value named “fut”. Then, we have assigned to it a Scala Future. Inside this future, we wanted to perform two different tasks.
Because of the first task, we are basically putting our program to sleep for 1 second. Then, we wanted to calculate the result of the statement “21 + 21”. However, we would like to state over here that this second task will only be executed after the first statement will complete its execution. Then, we wanted to print the result of the “fut” variable. This result will return “not completed” if the execution of the above-mentioned Future will still be pending; otherwise, the result of the second statement will be computed. Hence, both the tasks will be completed successfully. Finally, we wanted to print a message on the terminal that will notify the user that the execution of this function is partially completed.
We will be able to run this Scala script only once it is compiled successfully which can be done with the following command:
Once this Scala script will be compiled well, we can run it with the command shown below:
The output produced by this Scala script is shown in the following image. It depicted the usage of Scala Futures in Ubuntu 20.04.
Example # 2: Executing Two Independent Tasks- One with the Scala Futures and another Normally
Now, we will take a step further by explaining to you a practical example of using Scala Futures in Ubuntu 20.04. In this example, we will be performing two different tasks. The first one will be accomplished using the Scala Futures whereas the second one will be executed normally. We have intentionally designed the first task in a way that its execution will take some time. This is exactly why we have enclosed this task within the Scala Future. However, as far as the second task is concerned, it has been designed in a way that it will be executed quickly. In this way, we will receive the output of the second task prior to receiving the output of the first task. To understand this functionality, you will have to check out the Scala script shown below:
Again, in this Scala script, we have first imported the two required libraries. Then, we have created the driver class and the driver function in the same manner as we have done in the example above. After that, we have created a value named “fut”. To this, we have assigned the Scala Future in which we have used a thread that will call the “sleep” function for a second. Then, we created a value named “x” and have assigned to it the result of the statement “2+2”. After that, we wanted to print this value on the terminal and then, print a message that the second task i.e., the calculation of “x” has been completed successfully. Then, we wanted to print the value of the “fut” variable that will depend upon the execution of our Scala Future. Finally, we wanted to print a message on the terminal to notify the user that now, the first task has also been completed.
After the compilation and execution of this Scala script, the following output appeared on the terminal. In this output, first, the result of our second task i.e., 2+2 was printed on the terminal which is “4”. Then, we were notified that the second task has been performed successfully. After that, the result of our “Future” was printed on the terminal followed by another success message.
We wanted to introduce you to the concept of Scala Futures in Ubuntu 20.04 with the help of this guide. First, we shared with you what Scala Futures are and how do they relate to the concept of threads and concurrency. After that, we established a sound understanding of Scala Futures by stating the purpose of using them. Then, we implemented two very basic examples that made use of this important concept in the Scala programming language to achieve concurrency. With the help of this guide and these examples, you can implement more complex examples in which you will be able to execute multiple tasks concurrently with the help of Scala Futures.