简体   繁体   中英

start more than one async task on android

I want to start 2 async task's, but only one get executed. The output of LogCat should be something like:

firstAsync started
secondAsync started
secondAsync ends
firstAsync ends
end

but the output of LogCat is

firstAsync started
end

it shows that the secondAsync were never been executed. Here is the java code:

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;
        }
    }

}

so, the question is, how can I start multiple async task's, hope you can help me. regards chris

AsyncTask s always execute one at a time, unless you use executeOnExecutor() :

AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework.

Starting with HONEYCOMB , tasks are executed on a single thread to avoid common application errors caused by parallel execution. If you truly want parallel execution, you can invoke executeOnExecutor(java.util.concurrent.Executor, Object[]) with THREAD_POOL_EXECUTOR .

This is interesting

In this I put a second async within the first to start on preexecute. The logcat and code shows it best:

My log cat:

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

For the following code:

@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;
        }
    }
}

on the onPostExecute() method of the first AsyncTask, start the second AsyncTask

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM