Android AsyncTask 的替代方案
我有一个 UI 小部件,需要将地址映射到相应的联系人姓名并以给定的格式显示它们。 我使用 AsyncTask 执行查询以在 doInbackground() 中查找地址,并在 onPostExecute() 中在小部件上显示名称格式。 当地址有限制时,这可以很好地工作。 但是,当要映射的地址数量增加......大约 150 时,我得到 RejectedExecutionException。 据我了解,这是因为 maxPoolsize 为 128,而 AsyncTask 的队列限制为 10。 我尝试使队列无界...但是在小部件上添加名称变得非常慢。这是因为无界队列 maxPoolSize 被忽略,并且一次我只能有 corePoolSize 线程,即 5。
所以,我想知道除了 AsyncTask 之外是否还有其他替代方案可以用于此用例? 克隆 AsyncTask &增加核心/最大池大小似乎不是一个好的行为,因为这意味着许多并发线程(无效的资源使用)&调度开销。 目前地址没有限制,因此很容易就会超过 500 个。 此类情况我该如何处理?
I have a UI widget where I need to map addresses to corresponding contact names and display them in a given format.
I use AsyncTask to perform the query to lookup the address in doInbackground() and display the name format on the widget in onPostExecute().
This works fine when there is a limit on the addresses.
But when number of addresses to be mapped increase..in the order of 150, I get RejectedExecutionException.
I understand that this is because maxPoolsize is 128 and queue is bounded to 10 for AsyncTask.
I tried making the queue unbounded...but adding of names on the widget becomes very slow. That is because with unbounded queue maxPoolSize is ignored and at a time I can have only corePoolSize threads i.e 5.
So, I wanted to know if there is any alternative other than AsyncTask that I can use for this use-case??
Cloning AsyncTask & increasing core/max pool sizes does not seem to be good behavior as that would mean many simultaneous threads(ineffective resource usage) & scheduling overhead.
There is currently no limit on the addresses, and hence it could easily goto more than 500.
How should I handle such cases ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种方法可能是将这大约 500 个地址的映射划分为大约 80 个较小的部分,然后为每个部分使用单独的 AsyncTask。
我建议的另一种选择是使用 Service 来完成这项工作你。
One approach could be to divide the mapping of this approximately 500 addresses in smaller parts of around 80 and then use a separate AsyncTask for every part.
Another alternative I would recommend is to use a Service that does this work for you.