Schedule multiple async tasks in android

I want to schedule or queue for multiple AsyncTask to run in the background. I have an AsyncTask request for an HTTP post that works in a service. At the same time, I am making another HTTP request in the AsyncTask UI thread.

UI thread takes too long to execute because one thread is already running in the service. How to solve this problem?

I need to pause the service flow, and I want to start the UI thread first, and then restart the service thread for AsyncTask again.

Thank you in advance

+6
source share
1 answer

Since AsyncTasks can share a single thread, I would try to untie them by calling:

my_task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[])null);

instead of my_task.execute((Void[])null)

as described in this answer: AsyncTask doInBackground Android SDK not working (subclass)

Another way would be to use the Executor and ThreadPoolExecutor framework.

+1
source

All Articles