本文介绍了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.

获取源代码,修改它,把它放在你自己的包中,并使用它。我使用我的,但是这是基于Android 1.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