我正在使用 AsyncTask 从远程服务器获取大量缩略图并在 GridView 中显示它们。问题是,我的 GridView 一次显示 20 个缩略图,因此创建 20 个 AsyncTasks 并启动 20 次执行,每个缩略图一次。

我的代码中出现 RejectedExecution 异常。我记得在某处读过,AsyncTask 一次可以在其队列中拥有的任务数量是有限的,我可能会遇到这个问题。这个栏被取消了吗?

有办法提高这个限制吗?忽略此异常是否安全?(通过使用空的 catch(RejectedException e){} block ?)

我在 Android 1.6 模拟器上运行此代码,并且代码中的 API 级别(minSDKVersion 为 3)。 [编辑:添加了 SDK 和 API 级别信息]

请您参考如下方法:

I recall reading somewhere that there is a limit to number of tasks that AsyncTask can have in its queue at a time, i might be hitting that. Was this bar lifted?

AsyncTask 目前似乎支持 10 个线程和 10 的工作队列深度。理论上,这只支持 20 个项目...如果没有其他东西使用 AsyncTask .

Is there a way to increase this limit?

获取源代码,修改它,将其放入您自己的包中,然后使用该包。我用我的 AsyncTaskEx 做到了这一点,尽管这是基于 Android 1.5 源代码。

Is it safe to just ignore this exception?

您的工作不会排队等待执行。这是否“安全”取决于您。我不知道对 AsyncTask 基础设施有任何其他影响。


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!