Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android two AsyncTasks serially or parallel execution? - The second is freezing but the result is ok

I run two AsyncTask tasks in my Android application which are from the same class but with different parameters. For example:

new myAsynckTask(a,b,c).execute();
new myAssyncTask(a,d,e).execute();

Do they execute in parallel or in a serial order? I ask this because when the first one starts, shows the progress of execution and when finishes I see the second one which needs more time to finish but I can't see the progress(I'm able to see the rectangle but the progress bar is not showing 20%..and so on). It's like freezing but the result is ok.

What I want to do is to run them in serial order and be able to see the progress in the two of them. I run the app on Android Jelly Bean 4.2.2 API Level 17

like image 429
George Melidis Avatar asked Sep 06 '13 15:09

George Melidis


People also ask

Do Async execute make threads in parallel or serial?

If you are running on Android 1.5, they will be executed serially. Otherwise, they will be executed in parallel.

Can we run multiple async task at a time?

So async let provides a built-in way to run multiple operations concurrently when we have a known, finite set of tasks to perform.

How many times an instance of async task can be executed?

AsyncTask instances can only be used one time.


2 Answers

Do they execute in parallel or in a serial order?

If your android:targetSdkVersion is 13 or higher, and you are running on an Android 3.2 or higher device, they will be executed serially.

If you are running on Android 1.5, they will be executed serially.

Otherwise, they will be executed in parallel.

You can opt into parallel execution by replacing execute() with executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR).

For more, see the "Order of Execution" section of the AsyncTask JavaDocs.

like image 100
CommonsWare Avatar answered Oct 08 '22 20:10

CommonsWare


The answer to your question is: it totally depends on what version of Android you're running this on, and is a huge problem I've faced in several applications.

You should check out this link if you want to see how to run them correctly

like image 34
Cruceo Avatar answered Oct 08 '22 21:10

Cruceo