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 ...

Get Mastering High Performance with Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.