October 2019
Intermediate to advanced
434 pages
11h 54m
English
Coroutines work well together to achieve non-blocking code. At some point, however, it's likely that the code will need to interact with traditional, sequential blocking code. We can illustrate this by revisiting out working example:
fun main() { GlobalScope.launch { delay(500) println("Coroutines") } println("Hello") Thread.sleep(1000)}
In the preceding code, we are launching our coroutine from our standard main() function. However, the main() function isn't aware of any coroutines, so we have to call Thread.sleep() to prevent the termination of that function before our coroutine finishes. This mirrors the simple examples given at the beginning of this chapter using Thread and a short delay. However, for a non-trivial ...
Read now
Unlock full access