Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Huge latency between onPreExecute and doInBackground in AsyncTask

I have an AsynkTask:

new AsyncTask<Void, Void, Void>() {

            private ProgressDialog mProgressDialog;

            @Override
            protected void onPreExecute() {
                Log.i(TAG, "Pre execute: " + System.currentTimeMillis());
                super.onPreExecute();

                mProgressDialog = ProgressDialog.show(NewWeatherActivity.this, null,
                        getResources().getString(R.string.weather_is_updating));
                mProgressDialog.setCancelable(false);

                Log.i(TAG, "Pre executed: " + System.currentTimeMillis());
            }

            @Override
            protected Void doInBackground(Void... voids) {
                Log.i(TAG, "Do in background: " + System.currentTimeMillis());
                // Some actions
                Log.i(TAG, "Done in background: " + System.currentTimeMillis());
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                Log.i(TAG, "Post execute: " + System.currentTimeMillis());
                super.onPostExecute(aVoid);

                mProgressDialog.dismiss();
                Log.i(TAG, "Post executed: " + System.currentTimeMillis());
            }
        }.execute();

Log:

I/TVLauncher/NewWeatherActivity(21691): Pre execute: 1354798705667 I/TVLauncher/NewWeatherActivity(21691): Pre executed: 1354798705713 I/TVLauncher/NewWeatherActivity(21691): Do in background: 1354798724063 I/TVLauncher/NewWeatherActivity(21691): Done in background: 1354798724083 I/TVLauncher/NewWeatherActivity(21691): Post execute: 1354798724083 I/TVLauncher/NewWeatherActivity(21691): Post executed: 1354798725046

So, latency between onPreExecute and doInBackground is about 19s. Why?

like image 508
artem Avatar asked Dec 06 '12 13:12

artem


1 Answers

You are likely calling a framework or some library that is also using an Async Task. This would manifest in a slowdown on later versions of Android.

AsyncTasks are now run sequentially. That means a new async task won't run until the last one was finished. Instead of calling execute try calling executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "");

This will likely reduce the latency to negligible levels.

like image 176
Michal Palczewski Avatar answered Nov 17 '22 13:11

Michal Palczewski