复式AsyncTasks与顺序执行

复式AsyncTasks与顺序执行

本文介绍了复式AsyncTasks与顺序执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,类似的问题以前有人问,但解决这些问题不会为我工作。的情况是,我有分别得到发送通过的AsyncTask来上传到服务器对象的数组。我需要单独发送的每个人,但列表中的每个对象使用相同的AsyncTask因此调用在onPostExecute task.execute()是不是一种选择,因为这会导致它无限调用自己。我试图像这样

I know that Similar questions have been asked before but the solution to those questions will not work for me. The situation is that I have an Array of objects that are each getting sent to uploaded to the server via an asynctask. I need to send each one separately but each object in the list uses the same asynctask so calling task.execute in the onPostExecute() is not an option since that would cause it to infinitely call itself. I have tried something like so

for(each item in list){
    task.execute();
    while(task.getStatus() != android.os.AsyncTask.Status.FINISHED){
            //spin here and wait
    }
}

但这样做只是旋转永永远离开时,while循环。
任何建议都大大AP preciated

But when doing this it just spins for ever and never leaves the while loop.Any suggestions are greatly appreciated

推荐答案

从AsyncTask的文档:

From AsyncTask docs:

蜂窝开始,任务是在单个线程执行,以避免因并行执行常见的应用程序错误。

所以,如果你是不是> =蜂窝的话,说不定切换使用的ExecutorService:

so if you are not >= HONEYCOMB, then maybe switch to use ExecutorService:

ExecutorService executor = Executors.newSingleThreadExecutor();

这篇关于复式AsyncTasks与顺序执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 19:19