11

I am using asyncTasks, to load list elements with images (Just followed android's tutorial of efficiently loading bitmaps)

In DDMS, i can see upto 5 AsyncTasks being running

Now in addition i have added another AsyncTask, which performs some decoding using MediaCodec class.

Now in DDMS, i still see 5 AsyncTasks, and my image loading aynctask or decoding async task executes, not both of them.

When decoding is running, if i scroll the list, elements' images are not updated Counterly when i launch new decoding asynctask by calling it's execute method, the decoding doesn't start, but if i scroll the list view now, the images are updated.

So is AsyncTask is limitted??

Even in listAdapter i launch an AsyncTask per getView call. I'd expecting 7 running asyncTasks (if list visible elements are 7, say), but DDMS shows only 5 asynctasks running.

Now can someone explain me what's the black magic that i can't spell?

1

1 Answer 1

22

How many AsyncTasks can you run at once?

In most versions of Android, the answer is 128.

Why will you generally see exactly 5 AsyncTask threads?

AsyncTask thread management is rather confusing, especially since it has changed a lot since the first Android release.

In newer Android versions, 5 threads are create by default, and the ThreadPoolExecutor will attempt to run the AsyncTasks on these 5 threads. If you create more than 5 AsyncTasks, it may either queue them or create new threads (but only up to 128).

Note that in earlier versions of Android, these limits were differently. I believe that, originally, there was only ever one thread created.

But all your images should load eventually...

To answer the other part of your question, unless you're loading a lot of images (where a lot means >128), all the images should load, although likely sequentially. If the first 5 load and the rest do not, that might indicate that your doInBackground() function is never finishing or there is something else wrong with the AsyncTask you are using.

Resources:

Here are some helpful resources for learning more about AsyncTask thread management:

AsyncTask threads never die

Android AsyncTask threads limits?

Is there a limit of AsyncTasks to be executed at the same time?

3
  • All my images are loading. One more doubt, Cancelled AsyncTasks,will it count to number of AsyncTasks? i.e, counted in core thread count or maximum thread count?
    – nmxprime
    Jul 1, 2014 at 10:30
  • 1
    Once a AsyncTask ends, it would not count towards the 128, I believe. However, even if you have less than 5 AsyncTasks remaining, you will likely still see 5 threads. (See the first link above.) Jul 1, 2014 at 11:06
  • Excellent Answer. Feb 16, 2018 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.