Threading Example in Android
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Android threading exists to keep the main thread responsive. Any long-running work such as network calls, decoding large files, or heavy computation should happen off the UI thread, while UI updates must return to the main thread in a controlled way.
The Main Rule: Do Not Block the UI Thread
Android renders views, processes input, and dispatches lifecycle callbacks on the main thread. If you perform slow work there, the app feels frozen and may trigger an ANR. The fix is not "use threads everywhere", but "move only the expensive work off the main thread and marshal results back safely".
Basic Example with Thread and runOnUiThread
The simplest demonstration uses a plain Java thread for background work and then switches back to the activity thread for UI changes.
This works for small examples and makes the threading boundary obvious: background work in the thread, UI mutation on the main thread.
Prefer Executors for Repeated Work
Creating raw threads repeatedly is not ideal for real applications. Executors let you reuse worker threads and centralize scheduling.
This pattern scales better and avoids spawning an unbounded number of threads.
Handlers and the Main Looper
If you want explicit control over posting work back to the main thread, use a Handler tied to Looper.getMainLooper().
This makes the main-thread hop visible even outside an Activity context.
Lifecycle-Safe Thinking
Threading bugs in Android are often lifecycle bugs in disguise. A background task may finish after the activity is paused, stopped, or destroyed. If the result blindly updates the old screen, you can crash or show stale state.
One simple defensive habit is to separate work execution from UI rendering. Let the worker produce plain data, and let the activity decide whether it is still in a valid state to display it.
This is not a substitute for architecture components, but it makes the lifecycle risk visible. In larger apps, ViewModel plus observable state is usually a better boundary than pushing thread results directly into views.
Modern Recommendation: Coroutines or WorkManager
If you are writing Kotlin, coroutines are usually a better abstraction than manually wiring threads and handlers. If the task must survive app restarts or run deferrable background work, WorkManager is the better tool.
Still, understanding the low-level example matters because many Android APIs and legacy codebases are built on the same main-thread rule.
Common Pitfalls
- Doing network or file I O directly in a click handler or lifecycle callback.
- Updating a
Viewfrom a background thread and hittingCalledFromWrongThreadException. - Creating many raw threads instead of reusing an executor.
- Forgetting to shut down thread pools owned by short-lived components.
- Using complex threading when the real need is coroutine scope management or
WorkManager.
Summary
- The UI thread must stay free for rendering and input.
- Slow work belongs on a background thread or executor.
- UI updates must return to the main thread using
runOnUiThread, aHandler, or a modern equivalent. - Executors are more maintainable than repeatedly creating raw threads.
- Coroutines and
WorkManagerare usually better for modern Android, but the threading fundamentals stay the same.
Related reading
- Threading in a PyQt application Use Qt threads or Python threads?
- Threading in Python
- ''threading'' object has no attribute ''Thread''
- Threading pool similar to the multiprocessing Pool?
- To Do list before publishing Android app to market
- to drawRect or not to drawRect when should one use drawRect/Core Graphics vs subviews/images and why?
- Threading pool similar to the multiprocessing Pool?
- Threading vs Parallelism, how do they differ?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.