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?
Since AsyncTask uses a thread pool executor with max number of worker threads (128) and the delayed tasks queue has fixed size 10. If you try to execute more than 138 AsyncTasks the app will crash with java.util.concurrent.RejectedExecutionException.
In newer Android versions, 5 threads are create by default, and the ThreadPoolExecutorwill 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).
In newer Android versions, 5 threads are create by default, and the ThreadPoolExecutor will attempt to run the AsyncTask s on these 5 threads. If you create more than 5 AsyncTask s, it may either queue them or create new threads (but only up to 128).
Here are these four methods of AsyncTasks. 1. onPreExecute () – It invoked on the main UI thread before the task is executed. This method is mainly used to setup the task for instance by showing a ProgressBar or ProgressDialog in the UI (user interface).
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 AsyncTask
s on these 5 threads. If you create more than 5 AsyncTask
s, 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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With