在android上启动多个异步任务

时间:2012-12-29 21:12:03

标签: java android android-asynctask

我想启动2个异步任务,但只有一个被执行。 LogCat的输出应该是:

firstAsync started
secondAsync started
secondAsync ends
firstAsync ends
end

但LogCat的输出是

firstAsync started
end

它表明secondAsync从未被执行过。 这是java代码:

package com.example.async;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.app.Activity;

public class MainActivity extends Activity {

    public boolean stopAsync=false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new firstAsync().execute();
        new secondAsync().execute();
        Log.e("end","end");
    }

    class firstAsync extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {
            Log.e("firstAsync started","firstAsync started");
            while(!stopAsync)
            {

            }
            Log.e("firstAsync ends","firstAsync ends");
            return null;
        }
    }

    class secondAsync extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {
            Log.e("secondAsync started","secondAsync started");
            stopAsync = true;
            Log.e("secondAsync ends","secondAsync ends");
            return null;
        }
    }

}

所以,问题是,如何启动多个异步任务,希望你能帮助我。 问候 克里斯

3 个答案:

答案 0 :(得分:4)

AsyncTask总是一次执行一个,除非您使用executeOnExecutor()

  

AsyncTask旨在成为围绕ThreadHandler的帮助程序类,并不构成通用的线程框架。

     

HONEYCOMB开始,任务在单个线程上执行   避免因并行执行而导致的常见应用程序错误。如果您   真正想要并行执行,你可以调用   executeOnExecutor(java.util.concurrent.Executor, Object[])   THREAD_POOL_EXECUTOR

答案 1 :(得分:1)

这很有趣

在这个中我在第一个中放了第二个async来启动preexecute。 logcat和代码最好显示:

我的日志猫:

12-29 21:53:35.661: E/Async1: being executed
12-29 21:53:35.661: E/Async1: executing
12-29 21:53:35.661: E/Async2: being started
12-29 21:53:35.669: E/Async2: backgrounding
12-29 21:53:35.669: E/Async1: backgrounding
12-29 21:53:35.676: E/Async2: finishing
12-29 21:53:35.676: E/Async1: finishing

以下代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.e("Async1", "being executed");
    new Async1().execute();
}

public class Async1 extends AsyncTask {

    @Override
    protected void onPreExecute() {
        Log.e("Async1", "executing");
        Log.e("Async2", "being started");
        new Async2().execute();
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(Object result) {
        // TODO Auto-generated method stub
        Log.e("Async1", "finishing");
        super.onPostExecute(result);
    }

    @Override
    protected Object doInBackground(Object... arg0) {
        Log.e("Async1", "backgrounding");
        return null;
    }

    public class Async2 extends AsyncTask {

        @Override
        protected void onPostExecute(Object result) {
            Log.e("Async2", "finishing");
            super.onPostExecute(result);
        }

        @Override
        protected Object doInBackground(Object... params) {
            Log.e("Async2", "backgrounding");
            return null;
        }
    }
}

答案 2 :(得分:0)

在第一个AsyncTask的onPostExecute()方法上,启动第二个AsyncTask

protected void onPostExecute(String result) {
    if (result!=null)
    //start the Second asyncTask
    new secondAsync().execute();
}