[JAVA] Android --Is the order of serial processing and parallel processing of AsyncTask guaranteed? ??

In Android development, about serial processing and parallel processing with AsyncTask, I did not know what kind of movement it would be if it worked almost at the same time, and the order is actually guaranteed, so let's write a light code and check it I've done it, so I'd like to keep it as a memo.

[Reference URL] developer - AsyncTask Android-Serial processing of AsyncTask? Parallel processing? How is the asynchronous processing of AsyncTask realized

[Prototype 1] Task 1: Serial processing, Task 2: Parallel processing

task1:execute() task2:executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)

//1st async task
AsyncTask task1 = new AsyncTask() {

    @Override
    protected Object doInBackground(Object[] objects) {
        Log.v("task1", "start!!!");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Object o) {
        super.onPostExecute(o);
        Log.v("task1", "done!!!");
    }
}
.execute(); 

//Second async task
AsyncTask task2 = new AsyncTask() {

    @Override
    protected Object doInBackground(Object[] objects) {
        Log.v("task2", "start!!!");
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Object o) {
        super.onPostExecute(o);
        Log.v("task2", "done!!!");
    }
}
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

Execution result

I ran it several times to see if it was in order.

【First time】
5970-5995/com.example.testcode V/task2: start!!!
5970-5994/com.example.testcode V/task1: start!!!

5970-5970/com.example.testcode V/task2: done!!!
5970-5970/com.example.testcode V/task1: done!!!


【Second time】
7138-7157/com.example.testcode V/task2: start!!!
7138-7156/com.example.testcode V/task1: start!!!

7138-7138/com.example.testcode V/task2: done!!!
7138-7138/com.example.testcode V/task1: done!!!

【Third time】
7383-7403/com.example.testcode V/task1: start!!!
7383-7404/com.example.testcode V/task2: start!!!

7383-7383/com.example.testcode V/task2: done!!!
7383-7383/com.example.testcode V/task1: done!!!

The second start is reversed. .. ..

[Prototype 2] Task 1: Parallel processing, Task 2: In case of serial processing

task1:executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR) task2:execute()

//1st async task
AsyncTask task1 = new AsyncTask() {

    @Override
    protected Object doInBackground(Object[] objects) {
        Log.v("task1", "start!!!");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Object o) {
        super.onPostExecute(o);
        Log.v("task1", "done!!!");
    }
}
.execute(); 

//Second async task
AsyncTask task2 = new AsyncTask() {

    @Override
    protected Object doInBackground(Object[] objects) {
        Log.v("task2", "start!!!");
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Object o) {
        super.onPostExecute(o);
        Log.v("task2", "done!!!");
    }
}
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

Execution result

I also tried it several times to see if it was in order.

【First time】
7267-7273/com.example.testcode V/task2: start!!!
7267-7272/com.example.testcode V/task1: start!!!

7267-7267/com.example.testcode V/task2: done!!!
7267-7267/com.example.testcode V/task1: done!!!

【Second time】
7643-7662/com.example.testcode V/task1: start!!!
7643-7663/com.example.testcode V/task2: start!!!

7643-7643/com.example.testcode V/task2: done!!!
7643-7643/com.example.testcode V/task1: done!!!

【Third time】
7871-7891/com.example.testcode V/task1: start!!!
7871-7892/com.example.testcode V/task2: start!!!

7871-7871/com.example.testcode V/task2: done!!!
7871-7871/com.example.testcode V/task1: done!!!

The execution result here is similar to that of Prototype 1.

Summary

It seems that the order of processing is not guaranteed when serial processing and parallel processing are executed almost at the same time. If you want to guarantee the order surely, it is better to do both in series processing.

Finally

That's different! I think there is a point, but I hope it will be helpful.

that's all.

Recommended Posts

Android --Is the order of serial processing and parallel processing of AsyncTask guaranteed? ??
Order of processing in the program
Basic basis of Android asynchronous processing "AsyncTask"
The order of Java method modifiers is fixed
The comparison of enums is ==, and equals is good [Java]
About the problem of deadlock in parallel processing in gem'sprockets' 4.0
Why the width of the full screen element is 100% and the height is 100vh
[Java] Get the dates of the past Monday and Sunday in order
The design concept of Java's Date and Time API is interesting
Spring validation was important in the order of Form and BindingResult
Get the acceleration and bearing of the world coordinate system on Android
'% 02d' What is the percentage of% 2?
Explanation of the order of rails routes
This and that of the JDK
The content of the return value of executeBatch is different between 11g and 12c
Is it possible to put the library (aar) in the Android library (aar) and use it?