Concurrency and parallelism
Multithreading is based on the concepts of concurrency and parallelism. Concurrency refers to the ability of a task to be split into independent subtasks that can be executed out of order without affecting the final result:

Let's look at the following example:
class Baker { fun bake(): Cake { for (i in 0..1_000_000_000) { BigInteger.ONE.modPow(BigInteger.TEN, BigInteger.TEN) } return Cake() } }
The Baker
class contains the bake()
method, which invokes the modPow
function that take a significant amount of time to imitate the process of baking and returns an instance of the Cake
class:
class Cake
The Bakery
class contains the order
method, which takes the numberOfCakes
argument and returns an instance of the List<Cake>
type:
class Bakery { fun order(numberOfCakes: Int): List<Cake> { val cakes = mutableListOf<Cake>() val baker = Baker() val bakingTask = Runnable { cakes...