循环内的 AsyncTask [英] AsyncTask inside a loop

查看:19
本文介绍了循环内的 AsyncTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我想启动几个线程一个接一个串行执行.我正在使用 Thread.join() .但是应用程序类型挂起并进入 ANR 状态.我想知道将 AsyncTask 放入循环中将一个接一个地串行执行所有任务还是并行执行?

Basically I want start a few threads which execute serially one after the another. I'm using Thread.join() for it. But the application kinds of hangs out and goes in ANR state. I want to know that putting an AsyncTask inside a loop will execute all the tasks serially one after the another or will they be executing parallelly???

for(String s : list)
{
    new asynctask(s).execute();
}

推荐答案

基本上我想启动几个线程,然后依次执行另一个.

Basically I want start a few threads which execute serially one after the another.

在阅读此声明后,我立即想到的是您应该考虑使用 IntentService 而不是每次都创建新任务.

The thing immediately comes to my mind after reading this statement is You should consider using IntentService Rather than Creating new Tasks each time.

基本上 IntentService 是

Basically IntentService is

IntentService 通过 Intent 接收请求,其中包括关于要完成的任务的一些信息.那么这个任务添加到队列中,然后所有任务完成顺序和异步.

The IntentService receives a request via the Intent, which includes some information about the task to be completed. This task is then added to a queue,Then all tasks are completed sequentially and asynchronously.

使用 IntentService 的好处是

它会保证操作至少有服务进程"优先级,不管活动发生什么.当你想顺序下载多个文件时,强烈推荐.

It will guarantee that the operation will have at least "service process" priority, regardless of what happens to the activity.It is highly recommended when you want to download multiple files sequentially.

在这里你会找到关于如何实现 IntentService 的非常好的教程.

Here you will find very good tutorial on how to implement IntentService.

http://mobile.tutsplus.com/tutorials/android/android-fundamentals-intentservice-basics/

这篇关于循环内的 AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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