0

When I run multiple AsyncTasks in my view using AsyncTask.THREAD_POOL_EXECUTOR flag I found that no more than two AsyncTasks running concurrently. Actually when two AsyncTasks complete their work next two get started. I also try this AsyncTask test project in github. I got a same result, looking at logcat there where no more two AsyncTasks running concurrently.

How can I run more than two asynctasks concurrently?

Misagh Emamverdi
  • 3,654
  • 5
  • 33
  • 57

1 Answers1

2

The documentation says:

When first introduced, AsyncTasks were executed serially on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. Starting with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution.

So that means that they are executed on a single thread, meaning, by default, you can only have one AsyncTask active at a time.

But, fear not, the documentation further states:

If you truly want parallel execution, you can invoke executeOnExecutor(java.util.concurrent.Executor, Object[]) with THREAD_POOL_EXECUTOR.

So... new MyTask().executeOnExecutor( AsyncTask.THREAD_POOL_EXECUTOR );

will do the trick for you.

323go
  • 14,143
  • 6
  • 33
  • 41