Sitemap

Android AsyncTask Internals Explained: How It Really Works

5 min readApr 14, 2019

Would also suggest going through the Handlers/Loopers working, and the basics of AsyncTasks at https://tinyurl.com/y4mbxa2w. The AsyncTasks are heavily using the ThreadPoolExecutors to do its tasks on the worker thread, and once done it passes back the result to the UI thread using Handlers.

Handling Tasks in Background and communication with UI thread:

Basic functions in Async are:
onPreExecute: Before the task begins
doInBackground: To do the task in background
onProgressUpdate: To update the progress of the task run
onPostExecute: After the task has run

But how do all these work together? How UI and worker threads communicate?

Let us start from the beginning:
Async created:

Handlers are the Android Components used to communicate between the UI thread and the background threads.
The background thread(who has UI handler reference) can post messages to it, and the messages will be received in handleMessage() on the UI thread.

--

--

Anmol Sehgal
Anmol Sehgal

Written by Anmol Sehgal

Senior Software Developer @ Amazon | ex Oracle OCI, Goldman Sachs, Expedia | Writing real-world lessons on Java, system design & production-scale architecture.

Responses (1)