Jeeten Parmar
Jeeten Parmar

Reputation: 5757

Call multiple AsyncTask at a time not working in Android

I want to call multiple AsynTask. It was not working so I google for some help. I found that If I use AsyncTask.THREAD_POOL_EXECUTOR then it will work.

But It is not working.

I am calling it as below:

new GetCategory().execute();
new GetArea().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

Please help me regarding this issue. I want to call it at a time or it is also ok if GetArea() call after GetCategory(). I am not getting error. Application may be doing too much work.
Logcat:

03-13 21:21:43.134: I/Choreographer(1222): Skipped 39 frames!  The application may be doing too much work on its main thread.
03-13 21:21:43.394: I/Choreographer(1222): Skipped 43 frames!  The application may be doing too much work on its main thread.
03-13 21:21:43.634: I/Choreographer(1222): Skipped 36 frames!  The application may be doing too much work on its main thread.
03-13 21:21:43.784: I/Choreographer(1222): Skipped 38 frames!  The application may be doing too much work on its main thread.
03-13 21:21:44.004: I/Choreographer(1222): Skipped 56 frames!  The application may be doing too much work on its main thread.
03-13 21:21:44.164: I/Choreographer(1222): Skipped 36 frames!  The application may be doing too much work on its main thread.
03-13 21:21:45.495: I/Choreographer(1222): Skipped 33 frames!  The application may be doing too much work on its main thread.
03-13 21:21:45.765: I/Choreographer(1222): Skipped 43 frames!  The application may be doing too much work on its main thread.
03-13 21:21:46.005: I/Choreographer(1222): Skipped 36 frames!  The application may be doing too much work on its main thread.
03-13 21:21:46.235: I/Choreographer(1222): Skipped 44 frames!  The application may be doing too much work on its main thread.
03-13 21:21:46.525: I/Choreographer(1222): Skipped 48 frames!  The application may be doing too much work on its main thread.
03-13 21:21:46.855: I/Choreographer(1222): Skipped 39 frames!  The application may be doing too much work on its main thread.
03-13 21:21:47.005: I/Choreographer(1222): Skipped 39 frames!  The application may be doing too much work on its main thread.
03-13 21:21:47.156: I/Choreographer(1222): Skipped 37 frames!  The application may be doing too much work on its main thread.
03-13 21:21:47.306: I/Choreographer(1222): Skipped 38 frames!  The application may be doing too much work on its main thread.

I am binding Spinner on both AsynTask.
My Code :

        Area[] areaList = gson.fromJson(result, Area[].class);

        List<Area> lList = Arrays.asList(areaList);
        Area objArea;

        areaId.add("0");
        areaName.add("Select Area");

        for (int i = 0; i < lList.size(); i++) {
            objArea = lList.get(i);
            areaName.add(objArea.getAreaName());
            areaId.add(objArea.getAreaId());
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                        getActivity(), android.R.layout.simple_dropdown_item_1line, areaName);
                ((Spinner) spnCEArea).setAdapter(adapter);

Upvotes: 6

Views: 2193

Answers (6)

Elad
Elad

Reputation: 1655

if you do want both tasks to happen now - and not sequentially - then simply make one of them run as a thread instead of an AsyncTask

Upvotes: 0

Raanan
Raanan

Reputation: 4775

The simple way to have multiple AsyncTask run one after the other is using the SERIAL_EXECUTOR http://developer.android.com/reference/android/os/AsyncTask.html#SERIAL_EXECUTOR

Basically:

new GetCategory().executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
new GetArea().executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);

Upvotes: 1

Dipendra
Dipendra

Reputation: 1557

Edited:

@Override
public void doInBackground(params.....){

        //Your other code goes here

            Area[] areaList = gson.fromJson(result, Area[].class);

            List<Area> lList = Arrays.asList(areaList);
            Area objArea;

            areaId.add("0");
            areaName.add("Select Area");

            for (int i = 0; i < lList.size(); i++) {
                objArea = lList.get(i);
                areaName.add(objArea.getAreaName());
                areaId.add(objArea.getAreaId());
            }

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                            getActivity(), android.R.layout.simple_dropdown_item_1line, areaName);

}

@Override
public void onPostExecute(Params..){


                ((Spinner) spnCEArea).setAdapter(adapter);
}

In this way, you place all your workload in doInBackground and just fitting adapter to listView in onPostExecute. This is the right way to do. And I am sure it solves the issue.

Upvotes: 2

nKn
nKn

Reputation: 13761

The reason why you can't call two .execute() methods on AsyncTask within the same project at the same time is the modification that Android introduced since Gingerbread. Up until this version, you were able to call .execute() as many times you needed as each of them were run in a separate Thread.

But as of this version, they're called sequentially so if one is executing, the other one won't start if you just call .execute().

So basically all you need to do is to check the version of the device you're running the AsyncTask on and run one or other command depending at it.

if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB_MR1)
  your_asynctask.execute(your_params);
else
  your_asynctask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, your_params);

More info here.

By the way, the The application may be doing too much work on its main thread. lines are not related to the AsyncTask. These tasks are being run in background and if you're doing the work within your doInBackground() method, it's not the issue.

A slightly different case might be if you're doing some work in your onPostExecute() task, as this might be related to your main UI, but we can't tell you more without knowing the code you're running.

Upvotes: 5

ElDuderino
ElDuderino

Reputation: 3263

The correct syntax is

Executor exec = AsyncTask.THREAD_POOL_EXECUTOR;

new GetCategory().executeOnExecutor(exec);
new GetArea().executeOnExecutor(exec);

But read the warning here http://developer.android.com/reference/android/os/AsyncTask.html#executeOnExecutor(java.util.concurrent.Executor, Params...) It's probably not what you want.

Upvotes: 1

user3274355
user3274355

Reputation: 36

I'm able to use 2 asynctasks with :

new GetCategory().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,"");
new GetArea().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,"");

If it doesn't work and crashes, post your logcat. If it does post your code.

Upvotes: 1

Related Questions