我可以在单个流程应用程序中运行多少个 AsyncTask [英] How many AsyncTasks i can run in an single process application
问题描述
我正在使用 asyncTasks 来加载带有图像的列表元素(只是按照 android 的高效加载位图教程)
I am using asyncTasks, to load list elements with images (Just followed android's tutorial of efficiently loading bitmaps)
在 DDMS 中,我最多可以看到 5 个 AsyncTask 正在运行
In DDMS, i can see upto 5 AsyncTasks being running
现在另外我添加了另一个 AsyncTask,它使用 MediaCodec 类执行一些解码.
Now in addition i have added another AsyncTask, which performs some decoding using MediaCodec class.
现在在 DDMS 中,我仍然看到 5 个 AsyncTask,并且我的图像加载 aynctask 或解码异步任务执行,而不是两者.
Now in DDMS, i still see 5 AsyncTasks, and my image loading aynctask or decoding async task executes, not both of them.
解码运行时,如果我滚动列表,元素的图像不会更新相反,当我通过调用它的 execute 方法启动新的解码异步任务时,解码不会开始,但是如果我现在滚动列表视图,图像会更新.
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.
所以 AsyncTask 是有限制的吗??
So is AsyncTask is limitted??
即使在 listAdapter 中,我也会为每个 getView 调用启动一个 AsyncTask.我希望有 7 个正在运行的异步任务(如果列表可见元素为 7,例如),但 DDMS 仅显示 5 个异步任务正在运行.
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?
推荐答案
一次可以运行多少个AsyncTasks
?
在大多数 Android 版本中,答案是 128.
In most versions of Android, the answer is 128.
为什么您通常会看到恰好 5 个 AsyncTask
线程?
Why will you generally see exactly 5 AsyncTask
threads?
AsyncTask
线程管理相当混乱,尤其是自第一个 Android 版本以来它发生了很大变化.
AsyncTask
thread management is rather confusing, especially since it has changed a lot since the first Android release.
在较新的 Android 版本中,默认创建 5 个线程,ThreadPoolExecutor
将尝试在这 5 个线程上运行 AsyncTask
s.如果您创建的 AsyncTask
超过 5 个,它可能会将它们排入队列或创建新线程(但最多只能创建 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).
请注意,在早期版本的 Android 中,这些限制有所不同.我相信,最初只创建了一个线程.
Note that in earlier versions of Android, these limits were differently. I believe that, originally, there was only ever one thread created.
但是您的所有图片最终都应该加载...
要回答问题的另一部分,除非您加载了大量图像(其中很多意味着 >128),否则所有图像都应该加载,尽管可能是按顺序加载的.如果前 5 个加载而其余未加载,则可能表明您的 doInBackground()
函数从未完成,或者您正在使用的 AsyncTask
存在其他问题.
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.
资源:
这里有一些有用的资源,可以帮助您了解有关 AsyncTask
线程管理的更多信息:
Here are some helpful resources for learning more about AsyncTask
thread management:
这篇关于我可以在单个流程应用程序中运行多少个 AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!