How to implement .get feature with FutureTask or BackgroundTask using android?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
If you want a .get()-style result in Android, the main rule is simple: background work can block, the UI thread cannot. FutureTask can give you a synchronous-looking get() API, but you need to structure it so the waiting happens off the main thread and the result is posted back safely.
What FutureTask.get() Really Does
FutureTask.get() blocks until the background computation finishes or fails. That behavior is fine on a worker thread, but it is dangerous on Android’s main thread because it can freeze the UI and trigger an Application Not Responding error.
A minimal Java example:
That last line is only safe if it is not executed on the UI thread.
A Safe Android Pattern with ExecutorService
On Android, a practical pattern is:
- run the work on an executor
- wait for the result on a background thread if needed
- post the final UI update to the main thread
This preserves the get() behavior without blocking the main thread.
Do Not Use get() as a UI Shortcut
A common mistake is to write code that looks simple but blocks the screen:
The problem is not FutureTask itself. The problem is where get() is called.
If all you need is “run work in the background and update the screen,” a callback-style flow is often clearer than forcing a blocking call into the design.
What “BackgroundTask” Usually Means Today
Older Android code often used AsyncTask for this kind of workflow. That pattern is now outdated for new code. In current Android apps, the common replacements are:
- '
ExecutorServiceplusHandler' - Kotlin coroutines
- WorkManager for deferrable scheduled work
If your project is Java-based and already uses executors, FutureTask is a reasonable low-level primitive. If you are working in Kotlin, coroutines usually produce a cleaner API than manually coordinating get(), threads, and handlers.
Timeouts and Cancellation Matter
A useful part of the Future API is that you do not have to wait forever. You can set a timeout or cancel the work if the screen goes away.
That is especially important on Android where activities and fragments can be destroyed while background work is still running.
Choose the Right Abstraction
Use FutureTask when you specifically want:
- a
Callable - cancellation
- timeout handling
- a
Futureresult object
Do not use it just to imitate synchronous code on the UI thread. If the architecture wants a one-shot async result, a callback or coroutine often expresses the intent more clearly.
Common Pitfalls
- Calling
get()on the main thread and freezing the UI. - Updating views directly from the worker thread instead of posting back to the main thread.
- Using old
AsyncTaskpatterns for new code when executor-based or coroutine-based designs are clearer. - Forgetting to cancel or ignore results when the activity or fragment is no longer active.
- Treating
FutureTaskas a threading strategy by itself instead of pairing it with a proper executor.
Summary
- '
FutureTask.get()blocks, so it must not run on Android’s main thread.' - A safe pattern is to execute the task on a worker and post the result back to the UI thread.
- '
FutureTaskis useful when you need timeout, cancellation, or an explicitFuture.' - For many Android apps, callbacks or coroutines are a better fit than blocking result retrieval.
- The real goal is not “use
.get()everywhere,” but “get background results without freezing the UI.”
Related reading
- How to implement lock-free skip list
- How to implement multithread safe singleton in C11 without using mutex
- How to increase debezium / kafka connect performance for initial snapshot of millions of records and enable snapshot parallely if possible?
- How to increase thread priority in pthreads?
- How to implement onBackPressed in Fragments?
- How to implement referral program in mobile Apps for both Android and iPhone
- How to initialize a Thread in Kotlin?
- How to interpret a Java thread stack?
.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.