The Async Challenge

Create and Execute your own AsyncTask.

Pooja Srivastava
Mar 2 · 3 min read

For over decades, AysncTask has been one of the most widely used solutions for writing concurrent code in Android and now even when it’s deprecated, it is good to know how you can reproduce the same functionality on your own.

So, in this article I have tried to implement my own AsyncTask , before going through the article — just try to implement it on your own and consider submitting the link of your Github.

Let’s continue…

A common thing which an AsyncTask achieve is that it runs a task on a background thread and whose result is published on the UI thread.

The three input types used by an AsyncTask are the following :
1. Params : the type of the parameters sent to the task upon execution
2. Progress : the type of the progress units published during the background. computation
3. Result : the type of the result of the background computation

We know the 4 major steps for an AsyncTask to be successfully implemented are :

  1. onPreExecute : invoked on the UI Thread, before the execution of any task
Fig. 1.1 Internal working of an AsyncTask

So , the implementation for AsyncTask Contract is below :

The AsyncTaskResolver class has got these 4 steps implemented as an abstract functions within the class which later will be triggered from the execute() method which is basically the triggering point of the asynchronous operation.

And to let these methods onPreExecute(), onProgressUpdate() and onPostExecute() invoked on the UI Thread, I am using Handler (which allows you to communicate back with UI thread from the background thread).

The class for spawning a new thread for the background execution would look like :

To execute the task in the background, I am spawning a background thread using the ThreadSpawner class that has the two main functions — execute() and cancel().

The execute() function takes a lambda function task as a parameter, responsible for starting the Thread Execution.

Inside execute() method, we are creating a new thread which on start triggers the lambda. The execute() is returning the current instance of the ThreadSpawner class which is useful for achieving the chaining of functions.

To cleanly shut down the Thread, we have the cancel() method inside the ThreadSpawner class, which calls the Thread.interrupt() to stop the thread.

Note : Thread.stop() is deprecated because it was inherently unsafe.

Now, you’re good to schedule your tasks with the AsyncTask inside an Activity :

Trigger the AsyncTask using execute() functions and terminate the task using the cancel() function :

I know there are hundred better ways to design the API , but this was the minimum I came up with, link to the code is attached below, if you have suggestions and improvements please do open a PR on repository. Contributions and Suggestions are always welcome…

Community Submissions

These are the list of submissions from the dev community :

Chetan Gupta

Submitted AsyncTask challenge, using Handler Thread and also improved the API for error handling and his submission is below

He also does great Kotlin and Android articles at


Submitted the challenge and his solution covers :
- Can be created and executed everywhere. Not just on the main, UI thread
- Optionally be auto-cancelled via the lifecycle
- Has only one value to handle: the result. For parameters and progress, you can add your own implementation

Geek Culture

Proud to geek out. Follow to join our +1.5M monthly readers.