Coroutines: simplify threading on android

Coroutines: simplify threading on android

5 years ago
Anonymous $L9wC17otzH

https://medium.com/@ian.andrew.alexander/coroutines-simplifying-android-architecture-ce0cd02b9447

Android runs on Java and by default Java threads execute once, and once only. On a platform like Android where the UI is constantly updating, creating new threads for every action quickly becomes expensive. To get around this Android’s main thread uses the HandlerThread which is a subclass of the Java Thread. Android keeps this main thread alive by using a Looper which schedules Messages (Runnables) to execute.

These messages are run sequentially, which is why performing long running messages on the main thread will block subsequent messages waiting to be executed. This is why network calls, database calls, and long running operations, must all be run on an alternative thread — to avoid blocking the main thread (the UI).