Introduction
This article is part of the Scala knowledge bits Series.
Periodically, I will publish new exercises so you can slowly build up knowledge about Scala.
It is designed to be done in a very short amount of time and learn a little bit each day, just to create a routine.
This episode will teach you about Scala Thread Sleep.
Hope you are going to enjoy it! It is designed for anyone to learn Scala from scratch and slowly learn, one Bit at a time.
After this Bit, I would love to hear your feedback in the comments down below.
Feel free to join the Discord server as well if you would like some help and support from the rest of our community.
What are we learning today?
Today we are going to learn about Scala Thread Sleep !
This seem like a simple thing but it will allow us to introduce the idea of Thread
, which is a much more complex beast.
Thank you for iamkpr from our Discord channel for suggesting going toward asynchronous computation, threading and Future
.
Time to try on the exercise on your own and scroll down for more information when you are done or if you are stuck.
Exercise
Here is an exercise to complete today.
If I did my job well, you should be able to guess by yourself the solution based on what you previously learned and based on the clues.
But if you get stuck, scroll down to get more information.
The goal of the exercise is to replace the ???
by a piece of code so that the exercise compiles and that’s how you win! Good luck!
You can fill the exercise right in here:
Or, if it does not load, go on to Scastie (W8rybNOdRk29mQwdFVjqNg).
More information about Scala Thread Sleep
In this exercise you will learn (or have learned, if you have already solved the puzzle) about Scala Thread Sleep.
A thread
is a stream of computation. For instance, you would do:
val c = a + b val d = c * 2
Those two operations happen one at a time. There is no way for d
to be computed before c
, in this situation.
Now imagine that you want, for instance, retrieve some data somewhere from two different places. You could query those sources one at a time but most of the time it would be more efficient and faster to start requesting the data from both places so that we can collect the data from both places at once.
You might have heard of multi-threading maybe in the past. This means that the application is able to do more than one thing in parallel.
This SKB was not treating any of those topics about asynchronous operation and parallelization. However, it gaves you a taste of what it is like to have an operation that takes time. The method Thread.sleep( )
allows you to pause the current thread
for a number of milliseconds.
When you work on real life projects, time is a very important factor. And being able to leverage threads to run several things at once is extremely important. But don’t panic, we are going to slowly approach the problem, one SKB at a time.
Feel free to go back to the exercise, modify the code to try out new things and get a better intuition for Scala Thread Sleep.
Conclusion
I hope you have learned something new or had fun during this Scala Knowledge Bit.
Please ask questions or post feedback in the comments below.
Feel free to try on the next Scala Knowledege Bit.
If you are curious about the previous Scala knowledge Bits, go check it out! 🙂