1

I would like to send more than one request to server parallelly using AsyncTask in android

so how can i do that ?

I have seen code like

myAsync.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params)

but it is not running parallelly instead it is running serially.

please help me out.

Yella Goud
  • 157
  • 1
  • 4
  • 14
  • why executeOnExecutor but not execute()? new myAsync.execute() - call times you need Threads paralles count. And THREAD_POOL_EXECUTOR -it's only starting from API 11, wich your API Version? – Dmitry Nelepov Jun 07 '13 at 12:42
  • that code is correct, plus depends on the android version you run on – Blundell Jun 07 '13 at 12:42
  • That's the way you do it, and it works for me. Something else must be going on... – Ken Wolf Jun 07 '13 at 12:42
  • http://stackoverflow.com/questions/4068984/running-multiple-asynctasks-at-the-same-time-not-possible – wtsang02 Jun 07 '13 at 12:43
  • Refer this http://stackoverflow.com/questions/7010595/parallel-execution-of-asynctask http://stackoverflow.com/questions/4068984/running-multiple-asynctasks-at-the-same-time-not-possible – selva_pollachi Jun 07 '13 at 12:44
  • i have to send two requests one long running process and another one take little time. First i'll request a page to server which long running process then in the middle i will request to another page but it is executing once first request is completed until it is not sending request . So when send request to server that should work independently ?? – Yella Goud Jun 07 '13 at 12:47

3 Answers3

1

Hey the executeonExecutor should work perfectly.

You will need to use a thread pool Executor to execute Asynctask . Default implementation uses a serial executor running on a single thread

So create a ThreadPoolExeecutor and then use
Asynctask's executeonExecutor instead of just execute method

There has been a change in AsyncTask from Honeycomb release. Older versions had a Thread pool of 10 threads, so you could run 10 tasks in parallel. But for Honeycomb and up, default is a serial executor, which executes tasks one by one. But you can pass a ThreadPoolExecutor for execution:

if (Build.VERSION.SDK_INT >= 11) {
  //--post GB use serial executor by default --
  task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
  //--GB uses ThreadPoolExecutor by default--
  task.execute();
}
blganesh101
  • 3,647
  • 1
  • 24
  • 44
0

Create new instance of the async task and execute, then it will execute parallelly

OMAK
  • 1,031
  • 9
  • 25
0

simply... use

    new YourAssynctask().execute();

this will indeed call your Assyntask's OnpreExecute() method.

gaurav414u
  • 812
  • 13
  • 22