1

I recently looked through SO to find the answer to the same question here, but there was not an answer directed at the question in regards to the risk of doing so. But basically I want to run another AsyncTask inside the doInBackground() method of another AsyncTask. Is this a bad approach and/or does it leave any potential side effects?

I know that running it in the onPostExecute() works and so far from past experiences I have not had any issues due to the fact that onPostExecute() runs back on the main thread which started a AsyncTask to begin with.

Community
  • 1
  • 1
JoeyL
  • 1,295
  • 7
  • 28
  • 50

2 Answers2

4

From the API docs:

•The task instance must be created on the UI thread.

doInBackground() runs on the background thread. So you cannot create and run another asynctask from doInBackground().

http://developer.android.com/reference/android/os/AsyncTask. Have a look at the topic under threading rules.

When an asynchronous task is executed, the task goes through 4 steps: (Straight from the doc)

1.onPreExecute(), invoked on the UI thread before the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.

2.doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computation must be returned by this step and will be passed back to the last step. This step can also use publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.

3.onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.

4.onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.

When first introduced, AsyncTasks were executed serially on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. Starting with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution.

If you truly want parallel execution, you can invoke executeOnExecutor(java.util.concurrent.Executor, Object[]) with THREAD_POOL_EXECUTOR.

Update 05-04-2023

AsyncTask is no more used in the android world for async stuff. Consider using Coroutines. Suggest to switch to coroutines ASAP

https://developer.android.com/kotlin/coroutines?gclid=Cj0KCQjwla-hBhD7ARIsAM9tQKuy9BV88avgYYrrwkcCHJ1zktoctbOrdrWavXT0NTDktIfPdfb8Q-4aAqXIEALw_wcB&gclsrc=aw.ds

The codelab sample is availabe at https://developer.android.com/codelabs/kotlin-coroutines#0

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Very nice and thorough response along with multiple alternatives. Thanks once again Raghunandan – JoeyL Mar 27 '13 at 11:28
  • +1 for such a comprehensive answer. I was interested to read `The AsyncTask class must be loaded on the UI thread. This is done automatically as of JELLY_BEAN.` (from the docs) - do you know what is done automatically? – David Snabel-Caunt Mar 27 '13 at 12:02
  • @ David Caunt check the link http://stackoverflow.com/questions/14978924/what-is-the-difference-between-loading-and-creating-asynctask-on-the-ui-thread. – Raghunandan Mar 27 '13 at 12:24
  • @Nate check the threading rules http://developer.android.com/reference/android/os/AsyncTask.html. Class loading and execute(Params...) must be invoked on the UI thread. I think its the same link i have posted in the answer and i merely quoted it from docs – Raghunandan Sep 01 '13 at 02:06
  • @Nate could you edit that part. i guess the phrase loading is misleading – Raghunandan Sep 01 '13 at 02:12
  • @Nate Thank you. You are right about class loading i deleted the comment that said Loaded means ... must be spawned. – Raghunandan Sep 01 '13 at 02:14
1

AsyncTask(), with the exception of the Honeycomb releases, execute serially. So, no, you cannot execute another AsyncTask() from within doInBackground() -- I guess I should say that I've never tried it, but it's highly unlikely you're going to achieve the desired affect.

I asked about AsyncTask() execution during one of the Google Office Hours. Straight from their mouths when asked, "Why did it change from serial to parallel and back to serial execution?"; "Because it broke a lot of stuff."

Bill Mote
  • 12,644
  • 7
  • 58
  • 82
  • 1
    This isn't entirely true. The execute method executes the task on the default executor, which happens to be a single thread (serial) executor since Honeycomb, as you say. But you can execute tasks on your own executor with [executeOnExecutor](https://developer.android.com/reference/android/os/AsyncTask.html#executeOnExecutor(java.util.concurrent.Executor,%20Params...)). This works well for tasks which can safely be performed in parallel. – David Snabel-Caunt Mar 27 '13 at 11:43
  • @DavidCaunt 100% true. +1 Was just trying to keep it simple :) – Bill Mote Mar 27 '13 at 11:59