July 2018
Intermediate to advanced
266 pages
7h 11m
English
Concurrency needs to be thought about and designed for, and because of that, it's important to make it explicit in terms of when a computation should run concurrently. Suspendable computations will run sequentially by default. Since they don't block the thread when suspended, there's no direct drawback:
fun main(args: Array<String>) = runBlocking { val time = measureTimeMillis { val name = getName() val lastName = getLastName() println("Hello, $name $lastName") } println("Execution took $time ms")}suspend fun getName(): String { delay(1000) return "Susan"}suspend fun getLastName(): String { delay(1000) return "Calvin"}
In this code, main() executes the suspendable computations getName() and getLastName() in the current thread, ...
Read now
Unlock full access