在 Android 1.6 中排队多个异步任务
我在同一代码块中运行两个异步任务,并且两个任务的 doInBackGround 部分同时运行,这导致了问题。根据下面链接的文章,在 android 1.5 中,异步任务会按执行顺序自动排队,但此功能在 Android 1.6 中已被删除。我发现的所有异步任务队列资源都提倡使用 threadPool,但这仅适用于 Honey Comb。关于处理这个问题的正确方法有什么建议吗?
I am run two Async Tasks in the same block of code and the doInBackGround portions of the two tasks are running simultaneously, which is causing problems. According to the article linked below in android 1.5 Async Tasks were automatically qeued by order of execution, but this feature was removed for Android 1.6. All of the Async Task queing resources I've found advocate using threadPool but this is only available for Honey Comb. Any suggestions on the right way to handle this problem?
Article: parallel execution of AsyncTask
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
AsyncTask.get() 将导致调用线程阻塞执行并等待它完成,你可以这样做:
这将保证第二个 AsyncTask 在第一个完成后执行,请记住 AsyncTask.get() 阻塞线程执行,所以如果你在 UI 中调用它线程,你可能会得到 ANR 异常。
AsyncTask.get() will cause the calling thread block execution and wait for it finish, you can do something like this:
This will guarantee the 2nd AsyncTask is executed after 1st one finish, bear in mind that AsyncTask.get() block thread execution, so if you call this in UI thread,you will probably get ANR exception.