1

I have someclass which do large network operations and it do take some time to complete,and hence i put it in AsyncTask .I have to do this process 'n' times so in the main thread using a for loop i called this asynctask n times.will it throw any error since there is an interrupt in completing the for loop.?

// inside main thread 
for(i=0;i<n;i++)
{
 new asynctask().execute(new someclass());
}

2 Answers2

2

Running mutliple AsyncTask is not recommended, but if it is few times, then it will work but all async task will run serially not in parallel. But if you want async tasks to run parallelly then you can call it's executeOnExecutor(..) method where you have to pass THREAD_POOL_EXECUTOR as parameter. You can search on google you can find many links. Here is an example for your help.

Community
  • 1
  • 1
Android Killer
  • 18,174
  • 13
  • 67
  • 90
-1

don't call AsyncTask n times just put your for loop in onPostExecute() and do any task up to n times

    private class AsyncTaskRunner extends AsyncTask<String, String, String> {



      @Override
      protected String doInBackground(String... params) {

       } catch (Exception e) {
        e.printStackTrace();
       }
       return resp;
      }


      @Override
      protected void onPostExecute(String result) {
       // execution of result of Long time consuming operation

   for(i=0;i<n;i++)
    {
    //do your operation here
    }

      }


      @Override
      protected void onPreExecute() {
       // Things to be done before execution of long running operation. For
       // example showing ProgessDialog
      }


      @Override
      protected void onProgressUpdate(String... text) {

       // Things to be done while execution of long running operation is in
       // progress. For example updating ProgessDialog



      }
     }
    }
Jignesh Jain
  • 1,518
  • 12
  • 28