我可以在一个流程应用程序中运行多少异步任务

时间:2023-01-12 18:56:26

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

我正在使用asyncTasks,用图像加载列表元素(按照android的高效加载位图教程)

In DDMS, i can see upto 5 AsyncTasks being running

在DDMS中,我可以看到多达5个异步任务正在运行

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

此外,我还添加了另一个AsyncTask,它使用MediaCodec类执行一些解码。

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

现在在DDMS中,我仍然看到5个asynctask,我的图像加载aynctask或decoding async任务执行,而不是两个。

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.

当解码正在运行时,如果我滚动列表,那么当我通过调用它的execute方法启动新的解码异步任务时,元素的图像不会被对等地更新,解码不会启动,但是如果我现在滚动列表视图,图像会被更新。

So is AsyncTask is limitted??

那么AsyncTask是有限制的吗?

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.

即使在listAdapter中,我也会在每个getView调用中启动一个AsyncTask。我希望有7个正在运行的asyncTasks(如果列表中可见的元素是7个),但是DDMS只显示5个正在运行的asyncTasks。

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

有人能解释一下我不会拼写的黑魔法是什么吗?

1 个解决方案

#1


16  

How many AsyncTasks can you run at once?

一次可以运行多少个异步任务?

In most versions of Android, the answer is 128.

在Android的大多数版本中,答案是128。

Why will you generally see exactly 5 AsyncTask threads?

为什么您通常只看到5个AsyncTask线程?

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

异步任务线程管理是相当混乱的,特别是自从第一个Android版本以来,它已经发生了很大的变化。

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).

在较新的Android版本中,默认创建5个线程,并且ThreadPoolExecutor将尝试在这5个线程上运行AsyncTasks。如果您创建了超过5个异步任务,它可以对它们进行排队或者创建新的线程(但是最多只能创建128个)。

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

请注意,在早期版本的Android中,这些限制是不同的。我相信,最初只有一个线程被创建。

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.

要回答问题的另一部分,除非您正在加载大量的图像(其中很多表示>128),否则所有的图像都应该加载,尽管可能是顺序加载的。如果前5个load而其余的没有加载,这可能表明您的doInBackground()函数没有完成,或者您正在使用的AsyncTask有其他问题。

Resources:

资源:

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

这里有一些有用的资源来学习更多关于异步任务线程管理的知识:

AsyncTask threads never die

AsyncTask线程永远不死

Android AsyncTask threads limits?

Android AsyncTask线程限制呢?

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

同时执行异步任务有限制吗?

#1


16  

How many AsyncTasks can you run at once?

一次可以运行多少个异步任务?

In most versions of Android, the answer is 128.

在Android的大多数版本中,答案是128。

Why will you generally see exactly 5 AsyncTask threads?

为什么您通常只看到5个AsyncTask线程?

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

异步任务线程管理是相当混乱的,特别是自从第一个Android版本以来,它已经发生了很大的变化。

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).

在较新的Android版本中,默认创建5个线程,并且ThreadPoolExecutor将尝试在这5个线程上运行AsyncTasks。如果您创建了超过5个异步任务,它可以对它们进行排队或者创建新的线程(但是最多只能创建128个)。

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

请注意,在早期版本的Android中,这些限制是不同的。我相信,最初只有一个线程被创建。

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.

要回答问题的另一部分,除非您正在加载大量的图像(其中很多表示>128),否则所有的图像都应该加载,尽管可能是顺序加载的。如果前5个load而其余的没有加载,这可能表明您的doInBackground()函数没有完成,或者您正在使用的AsyncTask有其他问题。

Resources:

资源:

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

这里有一些有用的资源来学习更多关于异步任务线程管理的知识:

AsyncTask threads never die

AsyncTask线程永远不死

Android AsyncTask threads limits?

Android AsyncTask线程限制呢?

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

同时执行异步任务有限制吗?