How many AsyncTasks i can run in an single process application

风格不统一 提交于 2019-11-27 22:12:57

问题


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:


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?



来源:https://stackoverflow.com/questions/24505981/how-many-asynctasks-i-can-run-in-an-single-process-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!