Threads: Your App's Second Pair of Hands

A thread is a separate path of execution for background work, keeping your app's UI responsive. It's used for long tasks like network calls or disk I/O. The main footgun is trying to update the UI from a background thread, which will crash your app.
Why it exists
Android apps have a single main thread, also known as the UI thread, which handles all user interface updates and interactions. If you perform a long-running operation like a network request on this thread, the UI freezes. The system will eventually show an 'Application Not Responding' (ANR) dialog. Threads allow you to offload these long operations to the background, keeping the UI smooth and responsive.
The mental model
Think of your app as a coffee shop. The main thread is the cashier who takes orders and interacts with customers. A background thread is the barista who makes the coffee. If the cashier tried to make a complex latte, the line of customers would grind to a halt. Instead, the cashier passes the order to the barista and can immediately serve the next customer. The app's UI (the cashier) remains available while the background work (the barista) is being done.
How it works
At its core, you create a new Thread object and give it a piece of work to do, typically in the form of a Runnable. When you call the thread's start() method, the Android system scheduler allocates CPU time for it to run concurrently with the main thread. The key rule is that this background thread cannot directly touch the UI. To show the result, it must post a message back to the main thread's queue, which can then safely update the screen.
When to use it
Use a background thread for any operation that could take more than a few milliseconds to complete. This includes making network calls to fetch data from an API, reading or writing large files from disk, querying a database, or performing intensive calculations like processing an image.
When not to use it
While fundamental, creating and managing raw Thread objects is often discouraged in modern Android development. It's complex to handle thread lifecycle, communication, and pooling efficiently, which can lead to bugs and performance issues. Higher-level abstractions like Kotlin Coroutines or WorkManager are almost always a better choice as they manage this complexity for you.
One canonical example
An app needs to download a user's profile image from a URL. The main thread shows a placeholder and a loading spinner. It then starts a background thread to handle the network request and download the image bytes. While the download is in progress, the user can still scroll and interact with the rest of the app. Once the download is complete, the background thread signals the main thread, which then replaces the placeholder with the downloaded image.
Interview question
Which statement accurately describes the interaction between a background thread and the UI in an Android app?
- a.A background thread should use a synchronized method to safely modify UI components.
- b.A background thread must post a message to the main thread's queue to update the UI.Correct
- c.A background thread can only update UI elements after the main thread has completed all its tasks.
- d.A background thread can directly update UI elements if the update is small and fast.
Why? this is the answer
The card explicitly states that a background thread cannot directly touch the UI and must post a message back to the main thread's queue to safely update the screen. Directly updating the UI from a background thread, regardless of the update's size or speed, will cause a crash, making option D incorrect.
Just read this? Test yourself on what you have been reading.
Read the original → developer.android.com
- #android
- #concurrency
- #threads
- #background work
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.
See open roles