It takes a long time to get results, even with Asynctask’s in parallel

Asked

Viewed 86 times

1

I send a request to the server but it takes to receive the result, I realized that the Asynctask that sends the location to the server every 1 second is getting in the way.

I realized this after I changed it to 3 seconds, where I sent a new request that took 1 to 3 seconds.

The code where Asynctask is used is below:

   final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {

        @Override
        public void run() {


                    //Atualizar GeoLocalização
                    localizacao.setUsuario(motoqueiro.getId());
                    localizacao.setLatitude(location.getLatitude());
                    localizacao.setLongitude(location.getLongitude());

                    new GeoUpdateAsynctask().execute(localizacao);

            handler.postDelayed(this, APP_REFRESH_TIME * 1000);
        }
    }, APP_REFRESH_TIME * 1000);
}

I would like to suggest some other method that you can use in this case, since I use several Asynctask in parallel.

  • If I understand your explanation correctly, the problem is on the server side, or not?.

  • No brother, the problem why the treds should be queuing up, and when I send a request to the server, I only get it when the treads finish running.

1 answer

2


You say you’re running several Asynctask parallel but not true.

As Asynctask, by default, are run in series on the same thread, even if they are different.

In order for the execution to be done in parallel you have to call the method executeOnExecutor(), passing him a Executioner.

new GeoUpdateAsynctask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, localização);
  • I speak in parallel, because elsewhere in Activity I am instantiating another class asynctask

  • When using the method execute(), even if they are different objects, the execution is placed in a queue and is therefore executed in series. To run in parallel you must use the method executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params)

  • Thanks friend. But just one question, this is the easiest method to save resource?

  • If you want to use the Asynctask class and want to run in parallel, yes.

  • Right. I can use a 4 Asynctask without any problems ?

  • In principle yes, but it will depend on what they do. In all Asynctask replace execute() for executeOnExecutor() and test. Consider putting the code you posted on a service with a dedicated thread.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.