本文介绍了AsyncTask、RejectedExecutionException 和 Task Limit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 AsyncTask 从远程服务器获取大量缩略图并在网格视图中显示它们.问题是,我的网格视图一次显示 20 个缩略图,因此创建 20 个 AsyncTask 并开始 20 个执行,每个缩略图一个.

I am fetching lots of thumbnails from a remote server and displaying them in a grid view, using AsyncTask. The problem is, my grid view displays 20 thumbnails at a time, so that creates 20 AsyncTasks and starts 20 executes, one per thumbnail.

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

I get RejectedExecution exception in my code. 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?

有没有办法增加这个限制?忽略这个异常是否安全?(通过有一个空的 catch(RejectedException e){} 块?)

Is there a way to increase this limit? Is it safe to just ignore this exception?(by having an empty catch(RejectedException e){} block?)

我在 Android 1.6 模拟器和我的代码中的 API 级别上运行此代码(minSDKVersion 为 3).

I am running this code on Android 1.6 emulator and the API level in my code(minSDKVersion is 3).

推荐答案

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

AsyncTask appears to presently support 10 threads and a work queue depth of 10. In theory, that would just support 20 items...if nothing else is using AsyncTask.

有没有办法增加这个限制?

抓取源代码,修改它,放到你自己的包中,然后使用那个.我用我的 AsyncTaskEx 做到了这一点,尽管那是基于 Android1.5 来源.

Grab the source code, modify it, put it in your own package, and use that one. I did this with my AsyncTaskEx, though that is based on the Android 1.5 source.

忽略这个安全吗例外?

您的工作不会排队等待执行.这是否安全"取决于您.我不知道对 AsyncTask 基础结构有任何其他影响.

Your work will not be queued for execution. Whether that is "safe" is up to you. I am not aware of any other impacts on the AsyncTask infrastructure.

这篇关于AsyncTask、RejectedExecutionException 和 Task Limit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 08:33