gpt4 book ai didi

android - 循环内的 AsyncTask

转载 作者:行者123 更新时间:2023-11-29 22:03:29 25 4
gpt4 key购买 nike

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

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

最佳答案

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

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

基本上 IntentService 是

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 的好处是

它将保证该操作至少具有“服务进程”优先级,而不管 Activity 发生什么情况。强烈建议您在要顺序下载多个文件时使用。

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

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

关于android - 循环内的 AsyncTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11389903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com