February 2018
Intermediate to advanced
350 pages
7h 35m
English
Let's start with a simple example without coroutines:
import kotlin.concurrent.threadfun main(args: Array<String>) { thread { Thread.sleep(1000) println("World!") } print("Hello ") Thread.sleep(2000)}
The thread function executes a block of code in a different thread. Inside the block, we are simulating an expensive I/O computation (such as accessing data from a microservice over HTTP) with Thread.sleep. Thread.sleep will block the current thread for the number of milliseconds passed as a parameter. In this example, we don't wait until the computation finishes to keep working on other things; we print another message, "Hello", while the other computation is being executed. At the end, we wait for two seconds until ...
Read now
Unlock full access