0

Can i call an asyncTask multiple times with different parameters like this:

new MyAsyncTask.execute(
                     new String("x"),
                     new String(firstString)
                   );
new MyAsyncTask.execute(
                     new String("y"),
                     new String(secondString)
                   );                     

Is the above syntax correct? If yes, do the two calls run serialized by default?

xvx ph
  • 69
  • 1
  • 2
  • 12
  • Read this answer: http://stackoverflow.com/a/4072832/500105 – SuperFrog Oct 28 '15 at 18:59
  • I've already read that. But it's too messy to find my answer from. My Android version is 5.0. Now how to consider my case? Do the two calls run in serial or parallel? – xvx ph Oct 28 '15 at 19:20

1 Answers1

0

Yes you can. The default behaviour after SDK 11 is using serial executor running on a single thread.

You can modify this if you wish by using such method:

if (Build.VERSION.SDK_INT >= 11) {
    MyAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
    MyAsyncTask.execute();
}

You should read this answer if you want to understand the issue in depth: https://stackoverflow.com/a/4072832/500105

Community
  • 1
  • 1
SuperFrog
  • 7,631
  • 9
  • 51
  • 81
  • Thanks for reply. According to you, the syntax has no problem. So i posted a new question of same problem fully explaining what is happening in my code. would you please take a look at this: http://stackoverflow.com/questions/33412681/android-app-crashed-on-second-asynctask-call thanks. i know you might be busy. sorry if so. – xvx ph Oct 29 '15 at 11:17