如何在Android中有效地管理多个异步任务 [英] How to manage multiple Async Tasks efficiently in Android

查看:390
本文介绍了如何在Android中有效地管理多个异步任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有场景,我将不得不花费六个HTTP调用到我的服务器,以获得六个不同项目的数据。这些服务器调用不能被组合和他们的意思是这个样子。例如:如果你需要引用谷歌信息然后发送一个请求到服务器请求对谷歌的报价信息。接下来,如果你需要雅虎则启动另一个HTTP调用等。

I have scenario where I will have to make six http calls to my server to get the data for six different items. These server calls cant be combined and they are meant to be that way. Eg: If you need quote info for GOOGLE then send a request to server requesting for google's quote info. Next if you need yahoo's then you initiate another http call and so on.

这里的情况是:

  1. 现在,我的最终用户希望比较6个不同的公司。
  2. 正如我所提到的未避免的,我做的这点我用6异步任务6 HTTP调用。
  3. 当我得到的异步任务响应中的每一个,我会更新与新数据的用户界面。
  4. 它是一个糟糕的UI体验,如果我刷新UI 6倍的时间很短的时间。
  5. 在它给一个闪烁的效果,我的用户界面是不希望的。

我的问题:

  1. 如何抱过来自刷新界面,直到我得到的所有6异步任务的反应?
  2. 在我理解每个任务都是相互独立的。我应该运行一个while循环,等待,直到我得到的所有的反应?
  3. 有没有更好的方式来做到这一点,而不是一个while循环,因为如果呼叫中的任何一个没有回应,然后我会坚持等下去。

注:我猜的Andr​​oid 1.6+做并行执行异步任务

Note: I guess Android 1.6+ do execute Async tasks in parallel.

这是更多的是设计的问题,我会AP preciate有这方面的帮助。

This is more of a design question and I would appreciate any help on this.

在此先感谢

推荐答案

我发现这个解决方案更适合我的问题。 此链接描述了一对建立这种方式。 1. ExecutorService的 2. ExecutoreService和CountDownLatch

I found this solution more appropriate to my problem. This link describes a couple of ways of establishing this. 1. ExecutorService 2. ExecutoreService and CountDownLatch

<一个href="http://stackoverflow.com/questions/1250643/how-to-wait-for-all-threads-to-finish-using-executorservice">ExecutoreService与CountDownLatch

这篇关于如何在Android中有效地管理多个异步任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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