问题
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
回答1:
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.
回答2:
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
回答3:
UPDATE: copied from Android Developers and initiated by Yazazzello
"This class was deprecated in API level 26.0.0-alpha1. Use AsyncTask directly."
You should use this for parallel execution:
AsyncTaskCompat.executeParallel(new AsyncTask<Param, Void, Data>() {
@Override
protected Data doInBackground(Param... params) {
return downloader.getData(params[0]);
}
@Override
protected void onPostExecute(Data response) {
processData(response);
}
}, param);
来源:https://stackoverflow.com/questions/18661288/android-two-asynctasks-serially-or-parallel-execution-the-second-is-freezing