2

我想启动 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;
        }
    }

}

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

4

3 回答 3

4

AsyncTasks 总是一次执行一个,除非你使用executeOnExecutor()

AsyncTask 被设计为一个辅助类ThreadHandler并且不构成通用线程框架。

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

于 2012-12-29T21:25:07.453 回答
1

这是有趣的

在此,我在第一个异步中放置了第二个异步以开始预执行。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;
        }
    }
}
于 2012-12-29T22:01:05.873 回答
0

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

protected void onPostExecute(String result) {
    if (result!=null)
    //start the Second asyncTask
    new secondAsync().execute();
}
于 2012-12-29T21:17:26.747 回答