I am a beginner in android development. I ve already tried using simple execute() to do 1 task and it worked well. But i have now come across a problem where i need to perform 3-4 tasks concurrently in background using AsyncTask. I have seen several questions on this but i dint get to see a code for doing the same. I think executeOnExecutor() is the answer to my question but i want to see an example of it. Can someone please explain me with a simple example of how to run say 2 tasks concurrently using AsyncTask ?
Asked
Active
Viewed 1,470 times
2 Answers
3
The docs for AsyncTask cover this. By default, all of the AsyncTasks happen on a single thread. To use multiple threads, you need to use a different executor. AsyncTask has a thread pool executor you can use:
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);

Coeffect
- 8,772
- 2
- 27
- 42
0
I would strongly recommend not using multiple Async tasks. I did that before and ran into a ton of issues. See this SO post. What happens is one Async Task will wait for the other to finish before starting. Try using a runnable:
Runnable myRunnable = new Runnable() {
@Override
public void run() {
//Code
}
};
new Thread(myRunnable).start();