In this recipe, we are going to explore how to work effectively with the JVM Thread class in a clean way using the Kotlin standard library functions dedicated to convenient thread-running. We are going to simulate two long-running tasks and execute them concurrently in background threads.
Executing tasks in the background using threads
Getting ready
In this recipe, we are going to make use of two functions simulating long-running operations. Here is the first:
private fun `5 sec long task`() = Thread.sleep(5000)
And here is the second:
private fun `2 sec long task`() = Thread.sleep(2000)
They are both just responsible for blocking a current thread for five and two seconds, respectively, in order to simulate long-running tasks...