64. An Introduction to Kotlin Coroutines
The previous chapter introduced the concepts of threading on Android and explained how the user interface of an app runs on the main thread. To avoid degrading or interrupting user interface responsiveness, it is important that time consuming tasks not block the execution of the main thread. One option, as outlined in the previous chapter, is to run any such tasks on a background thread, thereby leaving the main thread to continue managing the user interface. This can be achieved either directly using thread handlers or by making use of the AsyncTask class.
Although AsyncTask and thread handlers provide a way to perform tasks on separate threads, it can be time consuming to implement, and confusing to read and maintain the associated code in an app project. This approach is also not the most efficient solution when large numbers of threads are required by an app.
Fortunately, Kotlin provides a lightweight alternative in the form of Coroutines...