Threads in Android

What is Threading in Android?

Threading in Android refers to executing multiple tasks simultaneously to improve app performance and responsiveness. The threading is required because the main UI thread (a.k.a. Main Thread) handles user interactions and UI updates. If heavy tasks (e.g., network calls, database operations) run on the UI thread, the app freezes (ANR: “App Not Responding”). To avoid this, long-running operations should run in background threads.

Types of Threads in Android

Main Thread (UI Thread)
The default thread when an app starts.
Handles UI interactions (e.g., button clicks, animations, screen updates).
Should not perform long-running tasks like network requests, database access, or file operations.

Worker Threads (Background Threads)
For heavy tasks, we use background threads such as:
Java Threads (Thread class)
Handler & Looper
AsyncTask (Deprecated)
Executors
HandlerThread
RxJava & Coroutines (Advanced)

When to Use Which Threading Approach?

Approach Use Case
Thread Simple background tasks
Handler & Looper Communicating between threads
AsyncTask (Deprecated) Small tasks needing UI updates
Executors Best for managing multiple threads efficiently
HandlerThread Long-running background processing
Kotlin Coroutines Modern, simple threading for Kotlin