13

I wrote an AsyncTask and most of the time there is no delay between its constructor been called and its doInBackground been called (0 ms delay). But whenever contacts syncing is happening at the background, I often experience 1-3 seconds delay between my AsyncTasks's constructor and doInBackground. This delay is unacceptable in my circumstances. I understand that AsyncTask is a background thread and this problem can be solved by using Thread and setting its priority higher. But what I want to found out is, how do I know what's causing my AsyncTask's doInBackground from being called? I used adb shell top -m 10 and the process usage seems quite normal when this issue happened.

Any help is appreciated.

thanks

Leo Chen
  • 969
  • 1
  • 11
  • 25
  • IMO, commonly it will execute immediately, if there is another thread is running as well, will make it laggy so better check your threads and sync them well. – Tom Sep 13 '12 at 11:45

3 Answers3

39

I also face this issue for long period, but now it is solved. Use the code below

new AsyncTaskName().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

instead the code

new AsyncTaskName().execute();

it would solve the problem of running doInbackground late.

Milan Shukla
  • 1,602
  • 18
  • 16
  • 1
    this worked perfectly for me. please note this was added in api 11 and the warning from: `http://developer.android.com/reference/android/os/AsyncTask.html#executeOnExecutorjava.util.concurrent.Executor, Params...)` – Jayrox Jun 25 '13 at 15:07
  • 1
    It has to be noted that executeOnExecutor cannot be used with API version earlier as 11. See thread http://stackoverflow.com/questions/9119627/android-sdk-asynctask-doinbackground-not-running-subclass/23678665#23678665 – L. G. May 15 '14 at 12:49
  • This works for me. I'll just add that we can do it for all API versions with AsyncTaskCompat.executeParallel() from support-v4 lib. It handles 2 cases: from API 11+ we need to manually select the THREAD_POOL_EXECUTOR, but before API 11 - all tasks were run in parallel. – pratt Aug 22 '16 at 13:43
  • isn't this just parallel running? It comes with warnings – miracle-doh Apr 25 '17 at 20:04
  • awesome! no more delay before `doInBackground` starts – user25 May 26 '18 at 21:26
2

We generally don't bother about task scheduling by the jvm. And infact we need not to bother also.

If something needs to be done as quick as possible in your application do it in the constructor itself or use onPre of Asynctask (Remember it execute on UI thread).

But i agree there is something fishy in the doInBackgroud calling in Android AsyncTask i itself had witnessed the doInbackground didn't get called after onPre. You can google this also. many people had faced it. I moved to use Traditional Thread.

I wrote my own Asynctask using Traditional thread at core and to mimic onPre and onPost i used Handler. You can go for that option also.

Rohit Sharma
  • 13,787
  • 8
  • 57
  • 72
1

It's important to distinguish between creating and executing the task. (An ASyncTask has a separate execute() method, as well as a constructor, as you have noticed.)

Creating a thread is potentially quite expensive, so you might find that creating the task in advance, and then only executing it at the correct time, gives better results.

If the background operation is likely to be repeated often, you may also find that an IntentService, which handles requests one-at-a-time in a background thread, is more suitable.

Graham Borland
  • 60,055
  • 21
  • 138
  • 179