October 2018
Intermediate to advanced
370 pages
9h 15m
English
An instance of the Thread class represents a native thread in the corresponding operating system when a program is running. This means that each instance of the Thread consumes memory for its stack and needs time to be initialized. If you are familiar with multithreaded programming, you know that switching between the contexts of threads is a pretty expensive operation, which is why it makes no sense to invoke short-term tasks in a separate thread.
In Kotlin, a coroutine is a pure language abstraction. Coroutines refer to objects in the memory heap and switching between coroutines doesn't involve operating system kernel operations. You can use a coroutine in the same way as a thread. This means that a coroutine contains ...