0

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 ?

Mayur534
  • 1
  • 2
  • 1
    The [documentation for this](http://developer.android.com/reference/android/os/AsyncTask.html) seems relatively straightforward. What prevents you from simply doing it more than once? – Robert Harvey Jun 27 '14 at 19:08

2 Answers2

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();
Community
  • 1
  • 1
Soatl
  • 10,224
  • 28
  • 95
  • 153