October 2018
Intermediate to advanced
370 pages
9h 15m
English
We considered the synchronous execution of coroutines in the previous sections of this chapter. Synchronous means that tasks are executed sequentially, one after another. However, let's suggest that we need to download and display two pictures. We can run these two tasks in parallel to speed up the process of displaying these images.
Let's define the Image class and the downloadImage function as follows:
class Imagesuspend fun downloadImage(): Image { delay(Random().nextInt(10) * 1000) return Image()}
The displayImage function will look as follows:
fun displayImages(image1: Image, image2: Image) { println("$image1 ${LocalDateTime.now()}") println("$image2 ${LocalDateTime.now()}")}
The displayImage function prints objects ...
Read now
Unlock full access