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
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);
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. .. ..
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);
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.
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.
That's different! I think there is a point, but I hope it will be helpful.
that's all.
Recommended Posts