Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to run multiple AsyncTask in same time?

Tags:

android

I've two activities in my application. In my Activity A I'm using one AsyncTask and second Activity B also using another one AsyncTask. In my Activity A I've upload some data to server and in my Activity B I'm trying to download some other data from server. Both these are running in AsyncTask. My problem is when I trying to download data from server in Activity B onPreExecute() method was called but doInBackground() method was not called it is waiting up to the first Activity A's doInBackground() action finished. Why it is happened? Is it possible to run more than one background actions at a same time..

In Activity A

ImageButton submit_button = (ImageButton) findViewById(R.id.submit_button);

submit_button.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View record_button) 
        {
                       new Save_data().execute();
        }
       });
class Save_data extends AsyncTask<String, Integer, Integer> 
 {
    protected void onPreExecute() 
    {

}
    protected Integer doInBackground(String... arg0) 
{
     //uploading data here
    }

 }

In my Activity B

ImageButton get_button = (ImageButton) findViewById(R.id.get_button);
    get_button.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View record_button) 
            {
                           new download_process().execute();
            }
           });
    class download_process extends AsyncTask<String, integer, integer>
     {
        protected void onPreExecute() 
        {
       Log.e("pre-execute","has been called");//This Log works well
    }
         protected Integer doInBackground(String... arg0) 
    {
         //downloading data here
        }   
     }
like image 751
Amsheer Avatar asked Aug 21 '13 12:08

Amsheer


People also ask

Can we run multiple async task at a time?

So async let provides a built-in way to run multiple operations concurrently when we have a known, finite set of tasks to perform.

How many times an instance of AsyncTask can be executed?

AsyncTask instances can only be used one time.

What is the replacement of AsyncTask?

AsyncTask is used to perform time talking operations in android, but it's marked as deprecated from android 11. There are many alternatives are available for replacing an AsyncTask, one of the replacements is ExecutorService.

Why did AsyncTask get deprecated?

Why Android AsyncTask is Deprecated? Here is the official reason it is deprecated. AsyncTask was intended to enable proper and easy use of the UI thread. However, the most common use case was for integrating into UI, and that would cause Context leaks, missed callbacks, or crashes on configuration changes.


2 Answers

Yes, it is, but since Honeycomb, there's a change in the way AsyncTask is handled on Android. From HC+ they are executed sequentially, instead of being fired in parallel, as it used to be. Solution for this is to run AsyncTask using THREAD_POOL_EXECUTOR executor explicitely. You can create helper class for that:

public class AsyncTaskTools {
    public static <P, T extends AsyncTask<P, ?, ?>> void execute(T task) {
        execute(task, (P[]) null);
    }
    
    @SuppressLint("NewApi")
    public static <P, T extends AsyncTask<P, ?, ?>> void execute(T task, P... params) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
        } else {
            task.execute(params);
        }
    }
}

and then you can simply call:

AsyncTaskTools.execute( new MyAsyncTask() );

or with params (however I rather suggest passign params via task constructor):

AsyncTaskTools.execute( new MyAsyncTask(), <your params here> );
like image 43
Marcin Orlowski Avatar answered Oct 02 '22 02:10

Marcin Orlowski


use executor as follows

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

See this

like image 184
Arun C Avatar answered Oct 02 '22 02:10

Arun C