Thursday, July 08, 2010

Android: AsyncTask, you are my best friend!!

1. What is AsyncTask?

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

2. Why we need AsyncTask?

For some operations that may take a lot of time, like network access or database queries, doing this kind of operations on UI thread will block the whole user interface. No event can be dispatched. From the user's perspective, the application appears hung. Even worse, if the UI thread is blocked for more than a few seconds (about 5 seconds currently) the user is presented with the infamous "application not responding" (ANR) dialog. Therefore, we need to perform these operations on background thread.

3. How to use AsyncTask?

(1) 3 types used in AsyncTask.

private class MyTask extends AsyncTask<Params, Progress, Result>


a. Params: the type of the parameters sent to the task upon execution.
b. Progress: the type of the progress units published during the background computation.
c. Result: the type of the result of the background computation.

(2) 4 steps an SyncTask will go through

a. onPreExecute() - invoked on UI thread immediately
-> Called before background computation starts
-> Do some setup (such as, display a progress dialog)
b. doInBackground(Params...) - invoked on the background thread
-> Called immediately after onPreExecute()
-> Perform computation in background thread that can take a long time.
-> Use publishProgress(Progress...) to trigger UI update progress
-> Progress will be passed to onProgressUpdate(Progress...)
c. onProgressUpdate(Progress...) - invoked on UI thread
-> Update progress
d. onPostExecute(Result) - invoked on UI thread
-> Called after the background computation finishes
-> The result will be passed from doInBackground(Params...)

(3) How to start AsyncTask?

new MyTask().execute(param1, param2, param3);

4. Rules

(1) AsyncTask instance must be created on UI thread
(2) AsyncTask.execute(...) must be invoked on UI thread
(3) Do not call onPreExecute / onPostExecute / doInBackground / onProgressUpdate manually

5. Behind AsyncTask!

(1) Thread does not exit after a task is finished?
When you create a new AsyncTask, you don't create a new thread.
You create a task that will be executed on one thread chosen from a thread pool.
If you create tons of AsyncTasks, some of these AsyncTasks will need to wait until a thread from the pool becomes available.

(2) Test on devices
Android 2.2: thread pool size = 5
Androdi 1.5: thread pool size = 1

ref:
AsyncTask
Painless threading
Asynctask threads never end!

2 comments:

  1. Interesting post, but I think you missed the following:

    1) You cannot create as AsyncTask as you want in a short period of time. The current pool size (after 1.5) is 10. When this capacity is exceeded application will get an RejectedExcecutionException.

    In Android 1.5, poolsize is always 1, because there were no parallel threads, just a single thread running all asynctaks, one at a time.

    ReplyDelete
  2. Thanks for your sharing.

    ReplyDelete