Does AsyncTask work at the same time?

Does AsyncTask work at the same time or does the first method appear for the first time ?,

For example, I have 3 AsyncTasks that have the same interface class and the same listener functions. Perform 3 asynchronous tasks simultaneously. What response from AsyncTasks will be shown in the listener function?

Doubts:

1. Is AsyncTasks running in parallel, or is it running in the first way first? 2. If AsyncTasks works in parallel, how to handle the same listener function for all AsyncTasks?

Nb : Doubt 2 is that the first answer is obtained when several requests are executed simultaneously without using AsyncTask. (Answer by Web Api).

Thanks in advance.

+4
source share
2 answers

For multiple query you can use ThreadPoolExecutor

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    new callApi().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, location);
} else {
    new callApi().execute(location);
}

Thread Pool Template

AsyncTask uses a thread pool template to run stuff from doInBackground ()

A thread pool template where the number of threads is created to perform a number of tasks. This is basically a container in which several threads enter the queue to perform various tasks.

Example:

public class MultipleAsyncTask extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        runMultipleAsyncTask(); // Start Async Task
    }
    private void runMultipleAsyncTask() // Run Multiple Async Task
    {
        FirstAsyncTask asyncTask = new FirstAsyncTask(); // First
        if(AppUtil.isCurrentVersionHoneycombAndAbove()) // Above Api Level 13
        {
            asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        }
        else // Below Api Level 13
        {
            asyncTask.execute();
        }
        SecondAsyncTask asyncTask2 = new SecondAsyncTask(); // Second
        if(AppUtil.isCurrentVersionHoneycombAndAbove())// Above Api Level 13
        {
            asyncTask2.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        }
        else // Below Api Level 13
        {
            asyncTask2.execute();
        }
    }
    //Start First Async Task:
    private class FirstAsyncTask extends AsyncTask<Void, Void, Void>
    {
        @Override
        protected void onPreExecute()
        {
            Log.i("AsyncTask" ,"FirstOnPreExecute()");
        }
        @Override
        protected Void doInBackground(Void... params)
        {
            for(int index = 0; index < 50; index++)
            {
                Log.i("AsyncTask" ,"FirstAsyncTask");
                try
                {
                    Thread.sleep(100);
                }
                catch (InterruptedException exception)
                {
                    exception.printStackTrace();
                }
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void result)
        {
            Log.d("AsyncTask" ,"FirstonPostExecute()");
        }
    }
    //Start Second Async Task:
    private class SecondAsyncTask extends AsyncTask<Void, Void, Void>
    {
        @Override
        protected void onPreExecute()
        {
            Log.i("AsyncTask" ,"SecondOnPreExecute()");
        }
        @Override
        protected Void doInBackground(Void... params)
        {
            for(int index = 0; index < 50; index++)
            {
                Log.d("AsyncTask" ,"SecondAsyncTask");
                try
                {
                    Thread.sleep(100);
                }
                catch (InterruptedException exception)
                {
                    exception.printStackTrace();
                }
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void result)
        {
            Log.d("AsyncTask" ,"SecondOnPostExecute()");
        }
    }
}

Output:

FirstOnPreExecute()
SecondOnPreExecute()
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstonPostExecute()
SecondOnPostExecute()

The same asyncTask for various functions, such as api requests:

boolean flag;
    @Override
    protected String doInBackground (String... params) {
        flag= params[0].endsWith("/repos");
        //other statements
    }

Now in your onPostExecute:

if(flag){
    //parse one way
} else {
    //parse another way
}
+4
source

, , -, , onPreExecute() AsyncTask doInBackground() AsyncTask .

,

new FirstAsyncTask().execute();

new SecondAsyncTask().execute();

onPreExecute() FirstAsyncTask() doInBackground() FirstAsyncTask(), , SecondAsyncTask() onPreExecute, doInBackground() SecondAsyncTask(). doInBackground() fisrt async .

+3

Source: https://habr.com/ru/post/1689765/


All Articles