同时执行的AsyncTasks有限制吗? [英] Is there a limit of AsyncTasks to be executed at the same time?

查看:24
本文介绍了同时执行的AsyncTasks有限制吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该有一个简单的答案,但我找不到任何答案.由于我仍然是一个机器人假人,所以我来这里问你们.

我一直在制作这个在启动时执行 10 个 AsyncTask 的项目.每个任务都包含 3 个 URL,这些 URL 在那里收集数据,但在应用中没有做任何重要的事情(目前).

我还有 10 个文本视图,用于跟踪 AsyncTasks 的进度.

当一个任务开始时,相应的文本视图被置于开始"当任务正在进行时,它会将其适当的文本视图设置为正在下载"任务完成后,它会将其适当的文本视图设置为已完成"

这是我观察到的,并开始质疑 AsyncTask.当我启动应用程序时,我注意到有 5 个文本视图被切换到正在下载"标记,所以我看到 5 个 AsyncTasks 正在按他们应该的方式工作.完成后,它会启动一个新的 AsyncTask.然而他们永远不会超过 5 的限制.

是什么导致了 5 个 AsynchTasks 同时运行的限制?我是否在某个找不到的文件中导致了这个问题?这是android 2.3.3的限制吗?也许是我用来模拟应用的设备的限制?

谁能帮我详细说明一下?

解决方案

是的,有一个限制.AsyncTaskThreadPoolExecutor 核心池大小为 5,但最大池大小为 128(从 1.6 - 4.0.3),所以我真的认为您应该看到所有 10 个同时运行.但是你不能改变它.如果您真的想做一些不同的事情(除非您有非常具体的原因,否则我不会推荐它),您将不得不使用更大的池大小进行一些自定义操作,或者只是手动启动一堆线程.

更新:

以下是文档的说明:

<块引用>

如果有超过corePoolSize但少于maximumPoolSize的线程在运行,只有当队列已满时才会创建一个新线程.

这就是为什么.您的队列未满,因此它只是将其保持在核心池大小.

This should have an easy answer, yet I couldn't find any. And since I'm still an android dummy I came here to ask you people.

I've been making this project that executes 10 AsyncTasks on the startup. Each task contains 3 URLs that collect data there and do nothing important in the app (yet).

I also have 10 textviews which I use to keep track of the progress of the AsyncTasks.

When a task starts the appropriate textview is put on "Start" When a task is progressing it sets its appropriate textview to "Downloading" When a task is finished it sets its appropriate textview to "Finished"

This is what I observed and came to question about the AsyncTask. When I start the app I notice 5 of the textviews being switched to the "Downloading" marker, so I see 5 AsyncTasks doing their job as they should. When done it starts up a new AsyncTask. Yet they never reach over that limit of 5.

What causes this limit of 5 AsynchTasks running at the same time? Did I cause this in some file which I cannot find? Is this a limit of android 2.3.3? Maybe a limit of the device I'm using to sim the app?

Can anyone elaborate for me?

解决方案

Yes, there's a limit. AsyncTask is backed by a ThreadPoolExecutor with a core pool size of 5, but a maximum pool size of 128 (from 1.6 - 4.0.3), so really I would think you should see all 10 of yours run at once. You can't change it though. If you really want to do something different (and I wouldn't recommend it unless you have a very specific reason), you'll have to do something custom with a larger pool size or just spin up a bunch of threads manually.

Update:

Here's what the docs say:

If there are more than corePoolSize but less than maximumPoolSize threads running, a new thread will be created only if the queue is full.

So that's why. Your queue isn't full, so it just keeps it at the core pool size.

这篇关于同时执行的AsyncTasks有限制吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆