Coroutines and threads
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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access