The Async Challenge
Create and Execute your own AsyncTask.
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.
A common thing which an
AsyncTask achieve is that it runs a task on a
background thread and whose result is published on the
The three input types used by an AsyncTask are the following :
Params: the type of the parameters sent to the task upon execution
Progress: the type of the progress units published during the background. computation
Result: the type of the result of the background computation
We know the 4 major steps for an
AsyncTask to be successfully implemented are :
onPreExecute: invoked on the
UI Thread, before the execution of any task
doInBackground(Params..): invoked on the
background threadimmediately after
onProgressUpdate(Progress…): invoked on the
UI Threadto show the progress of the executing task
onPostExecute(Result…): invoked on the
UI Threadto publish the results immediately after the
So , the implementation for
AsyncTask Contract is below :
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
onPostExecute() invoked on the
UI Thread, I am using
Handler (which allows you to communicate back with
UI thread from the
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() function takes a lambda function
task as a parameter, responsible for starting the Thread Execution.
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.
Thread.stop()is deprecated because it was inherently unsafe.
Now, you’re good to schedule your tasks with the
AsyncTask inside an Activity :
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…
Contribute to pooja-srivs/CustomAsyncTask development by creating an account on GitHub.
These are the list of submissions from the dev community :
Submitted AsyncTask challenge, using Handler Thread and also improved the API for error handling and his submission is below
Hola Amigos! 🙌, welcome to my coding playground! go on explore around 👩💻 Do let me know if you find something…
He also does great Kotlin and Android articles at
AndroidBites | Big-Brain-Kotlin | Chetan Gupta
Design & Develop modern android applications using native technologies, boost productivity using Kotlin & Java in sync…
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