October 2019
Intermediate to advanced
434 pages
11h 54m
English
Coroutines allow us to write asynchronous code in a sequential fashion without the use of callbacks. They are a part of the Kotlin language, and are now the default, Kotlin-idomatic way to write non-blocking code.
The following snippet demonstrates a basic example of using coroutines:
fun main() { GlobalScope.launch { delay(500) println("Coroutines") } println("Hello") Thread.sleep(1000)}
In this example, a new coroutine is started by calling GlobalScope.launch { }. This code will be run in the background without blocking the starting thread. This code will produce the following output:
HelloCoroutines
Because the launched coroutine doesn't block, Hello will be printed before the Coroutines line is printed within the coroutine. ...
Read now
Unlock full access