Call multiple AsyncTask in time not working in Android

I want to call several AsynTask . It did not work, so I help Google. I found that if I use AsyncTask.THREAD_POOL_EXECUTOR then it will work.

But it does not work.

I call it the following:

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

Please help me in solving this problem. I want to call it at a time, or is it ok if GetArea() called after GetCategory() . I'm not wrong. An application can do 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 linking Spinner on both AsynTask .
My code is:

  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); 
+6
source share
5 answers

The reason you cannot call the two .execute() AsyncTask on AsyncTask in the same project at the same time is the modification introduced by Android with Gingerbread . Prior to this version, you could call .execute() as many times as you need, since each of them ran in a separate Thread .

But with this version they are called sequentially, so if one of them is executed, the other does not start if you just call .execute() .

So basically all you have to do is check the version of the device that AsyncTask running AsyncTask and run a command depending on 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 details here .

By the way, the lines The application may be doing too much work on its main thread. not affiliated with AsyncTask . These tasks are performed in the background, and if you do the work in your doInBackground() method, this is not a problem.

It may be slightly different if you are doing some work in your onPostExecute() task, as this may be related to your main user interface, but we cannot tell you more without knowing what code you are using.

+5
source

Edited by:

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

This way you put all your workload in doInBackground and just set the adapter for listView to onPostExecute. This is the right way. And I'm sure it solves the problem.

+2
source

I can use 2 asynctasks with:

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

If it doesn't work and crash, send your logarithm. If he sends your code.

+1
source

The correct syntax

 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 ...) This is probably not what you want.

+1
source

An easy way to run multiple AsyncTask at the same time is using 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); 
+1
source

All Articles