4

I have two total different implementations of AsyncTask, lets say AT1 and AT2.

AT1 is executed first, then AT2 is executed. AT1 waits in doInBackground until AT2 has done its task by polling this data every 500 ms. But this never happens.

So what I basically want is this: enter image description here

But what seems to happen is this: enter image description here
Except AT1 is never done, and AT2 is never started.

Is there a way I can force these two AsyncTasks to be executed on two seperate threads, or is there another solution for this?

It is not possible to first start AT2 and after that execute AT1.

EDIT
For clarification: AT1 is executed when a user opens a particular screen, and needs to download data for that screen, based on a location. AT2 is executed on Location change, and when that happens, some calculations are made that cannot be done on the UI thread.

When AT2 has never been executed, AT1 has no location to download data for, so it needs to wait for AT2 to finish. When AT2 has been executed, the location data is already there, and AT1 doesn't need to wait.

Also, this problem occurs in ICS, not in Android 2.3, like this answer suggests.

Community
  • 1
  • 1
nhaarman
  • 98,571
  • 55
  • 246
  • 278
  • "AT1 waits in doInBackground until AT2 has done its task" - so no point in running asynchronously.....? – Mitch Wheat Jun 13 '12 at 07:50
  • Both need to do network operations, and `AT1` is not always executed if `AT2` is executed. – nhaarman Jun 13 '12 at 07:52
  • However, I found this post: http://stackoverflow.com/questions/4068984/running-multiple-asynctasks-at-the-same-time-not-possible, I will investigate that. – nhaarman Jun 13 '12 at 07:52

3 Answers3

3

When I posted this question, this question appeared in the Related section. It advices to use executeOnExecutor, I've implemented this as follows:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    new SetLocationAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, location);
} else {
    new SetLocationAsyncTask().execute(location);
}
Community
  • 1
  • 1
nhaarman
  • 98,571
  • 55
  • 246
  • 278
0

What's the point of having two async tasks, if one waits for the other? And if you want to do it, why not have the first one start the second one and put the "done" stuff in the second task?

Christine
  • 5,617
  • 4
  • 38
  • 61
  • Both Asynctasks need to do network operations, so Asynctasks are needed. Furthermore, AT1 is executed on user request (open particular screen), AT2 is executed on Location change. – nhaarman Jun 13 '12 at 07:55
  • I agree with Barry, there must be a much simpler way of doing this, even if you use two asynctasks. I would not make one dependent of the other. But I'd need to know more about the app in order to say more. – Christine Jun 13 '12 at 08:09
0

Sounds like you may need to rethink your logic - if you're kicking off a thread which then needs to wait for another thread to do some work, why not just have the first thread do that work?

If you really need 2 AsyncTasks, have the first one gather whatever data is needed and don't kick off the second until after the first is finished - have a read about onPostExecute in the docs.

barry
  • 4,037
  • 6
  • 41
  • 68