Running multiple AsyncTasks at the same time — not possible? [android]


This allows for parallel execution on all android versions with API 4+ (Android 1.6+):

void startMyTask(AsyncTask asyncTask) {
        asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);

This is a summary of Arhimed's excellent answer.

Please make sure you use API level 11 or higher as your project build target. In Eclipse, that is Project > Properties > Android > Project Build Target. This will not break backward compatibility to lower API levels. Don't worry, you will get Lint errors if your accidentally use features introduced later than minSdkVersion. If you really want to use features introduced later than minSdkVersion, you can suppress those errors using annotations, but in that case, you need take care about compatibility yourself. This is exactly what happened in the code snippet above.


I'm trying to run two AsyncTasks at the same time. (Platform is Android 1.5, HTC Hero.) However, only the first gets executed. Here's a simple snippet to describe my problem:

public class AndroidJunk extends Activity {
 class PrinterTask extends AsyncTask<String, Void, Void> {
     protected Void doInBackground(String ... x) {
      while (true) {
       try {
       } catch (InterruptedException ie) {

    public void onCreate(Bundle savedInstanceState) {

        new PrinterTask().execute("bar bar bar");
        new PrinterTask().execute("foo foo foo");

        System.out.println("onCreate() is done.");

The output I expect is:

onCreate() is done.
bar bar bar
foo foo foo
bar bar bar
foo foo foo

And so on. However, what I get is:

onCreate() is done.
bar bar bar
bar bar bar
bar bar bar

The second AsyncTask never gets executed. If I change the order of the execute() statements, only the foo task will produce output.

Am I missing something obvious here and/or doing something stupid? Is it not possible to run two AsyncTasks at the same time?

Edit: I realized the phone in question runs Android 1.5, I updated the problem descr. accordingly. I don't have this problem with an HTC Hero running Android 2.1. Hmmm ...

Multiple asynctasks from one Activity

You can refer to this post

Running multiple AsyncTasks at the same time — not possible?

AsyncTask uses a thread pool pattern for running the stuff from doInBackground(). The issue is initially (in early Android OS versions) the pool size was just 1, meaning no parallel computations for a bunch of AsyncTasks. But later they fixed that and now the size is 5, so at most 5 AsyncTasks can run simultaneously. Unfortunately I don't remember in what version exactly they changed that.

Multiple asynctask classes in one activity

Yes , its right . U can as many times as u want to call . But a small modification can be done , instend of call 4 AsyncTask , u can one AsyncTask by making its generic in flow .

public class GetDataFromNetwork extends AsyncTask<Void,String,Object> {private  String ipAddress;
private  String webService;
private  String nameSpace;
private  String methodName;
private Context context;
private ProgressDialog progressDialog;
private SoapObject request;
private GetDataCallBack callBack;

 * Set default progress Bar properties. Internal function called from setConfig 
 * @param progressBarStyle : Progress Dialog style (spinner or horizontal)
 * @param message : Message to be displayed on progressBar
protected void setProgresDialogProperties(int progressBarStyle,
        String message) {
    progressDialog = new ProgressDialog(context);

public GetDataFromNetwork(Context context,int progressBarStyle,String message ,GetDataCallBack callBack) {
    setProgresDialogProperties(progressBarStyle, message);

 * Used for setting up the location and config of remote location/webservice 
 * @param ipAddress : IP of webservice
 * @param webService : Name of webservice
 * @param nameSpace : NameSpace of webService
 * @param methodName :Name of function of the particular webservice
public void setConfig(String ipAddress,String webService,String nameSpace,String methodName){


 * Set parameter in SoapObject
 * @param requestParamater : Map of request
public void sendData(HashMap<String, Object> requestParamater) {

protected void onPreExecute() {
protected Object doInBackground(Void... params) {
    return result;
protected void onPostExecute(Object result) {

Now u can call this class when ever u want to interact with ur server.