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

前端 未结 3 585
深忆病人
深忆病人 2020-12-31 06:15

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         


        
相关标签:
3条回答
  • 2020-12-31 06:38

    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);
    
    0 讨论(0)
  • 2020-12-31 06:57

    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.

    0 讨论(0)
  • 2020-12-31 06:58

    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

    0 讨论(0)
提交回复
热议问题