2

Is android async task slow or am i doing something wrong?

Here is what i do

Log.e("Filler", "before");
new DownloadListContent().execute("string");
Log.e("Filler", "after");

and DownloadListContent()..

class DownloadListContent extends AsyncTask<Object, Object, Object> {
    protected Void doInBackground(Object... urls) {             
    Log.e("Filler", "Am in doInBackground");

....
}

And here is the logCat.

03-15 23:18:**47**.598: E/Filler(17150): before
03-15 23:18:**47**.598: E/Filler(17150): after
03-15 23:18:**59**.789: E/Filler(17150): Am in doInBackground

That is 12 seconds before do in background occurs. Why is that happening?

In the mean time i have other instances of other classes of AsyncTask do some other network jobs. Does AsyncTask affects one an other?

I really cannot figure that one out!

UPDATE

Thank you for the comments. seems that async has a hard limit on how many threads will run at the same time. This is a killer if you have to download a bunch of images at the same time with your data.

Following CommonsWare's method here i can seperate the type of asyncTask so that one type (images) don't block other type (list data).

Community
  • 1
  • 1
weakwire
  • 9,284
  • 8
  • 53
  • 78
  • is that on an actual phone or on the emulator? The emulator is slow, depending on your computers hardware. – Bill Gary Mar 15 '12 at 23:30
  • no i'm testing on an HTC Desire.12 seconds are soooo slow!Is there a hard limit on how many async tasks are allowed to run at the same time? – weakwire Mar 15 '12 at 23:31
  • 2
    Show all your code for the class. Remember there is only so much resource to got round such as CPU, memory or even bandwidth (which can be varied depending on the location or if on wifi). You may find that this task is waiting for the others to finish to then get access to do network related things, however this is a guess not fact as we need more code. – Graham Smith Mar 15 '12 at 23:45
  • Thank you for the comments. it seams that Async task has a limited queue – weakwire Mar 15 '12 at 23:53

1 Answers1

5

Right, it does have limited pool of threads. On Honeycomb, the pool size is one, so you can't have two tasks at the same time. You can supply your own executor though, to have multiple tasks run in parallel. That might not improve performance though, so you should maybe think of restructuring your code a bit:

http://developer.android.com/reference/android/os/AsyncTask.html#executeOnExecutor(java.util.concurrent.Executor, Params...)

Jethro
  • 928
  • 1
  • 10
  • 20
Nikolay Elenkov
  • 52,576
  • 10
  • 84
  • 84