0

I am developing an android app which needs to call one asynctask function multiple times one after another in one activity something like this:

  • .. some code
  • new task().execute();
  • .. other code
  • new task().execute();
  • .. other code
  • new task().execute();

When I'm executing this type of code all tasks are running approximately in parallel, but I need this tasks to run one after another. How can I do this without calling next task in onPostExecute()

Adrian Tandrau
  • 13
  • 1
  • 2
  • 7
  • Could you use a single AsyncTask that has a loop in it? onProgressUpdate() could be used to run that other code. – MikeHelland Jan 19 '14 at 22:38
  • actually that "other code" means some updates in database, this thing will happen 10 times for other tables after each execute of asynctask, and each "other code" run is based on previous execution of asynctask ... so this is not working :( – Adrian Tandrau Jan 19 '14 at 22:50
  • Those updates to the databases should probably take place in an AsyncTask too instead of the UI thread. You could take the code out of the task.doInBackground and put it into its own class. Then change the AsyncTask.doInBackground() to call the task, update the database, call the next task, update the database, ect – MikeHelland Jan 19 '14 at 22:55
  • nope, that updates need to run o main activity, the steps of this executions are as follows. First on main activity I'm getting some data from android database and send it over http request to a online server (this process is in doInBackground), and onPostExecute I get the response from the online server and make some updates. After this first steps are done, in the main activity I want to select other data, and again same asynctask process. I can't get all data first and than run asynctask only once, because I need some data to be updated before some other selects. – Adrian Tandrau Jan 19 '14 at 23:03

2 Answers2

5

One solution would be to create an AsyncTask object in your AsyncTask class like:

class Task extends AsyncTask {
AsyncTask next;
public void setNext(AsyncTask next){
   this.next=next;
}

//in last line of post execute
   if(next!=null){
       next.execute();
   }
}

now your code would be:

Task t=new Task();
Task t1=new Task();
Task t2=new Task();
t.setNext(t1);
t1.setNext(t2);
t.execute();

2nd way would be to create your own threadpool like:

 class ThreadPool implements Runnable {
    ConcurrentLinkedQueue<AsyncTask> tasks = new ConcurrentLinkedQueue<AsyncTask>();
    Activity activity;

    public ThreadPool(Activity activity) {
        this.activity = activity;
    }

    boolean stop = false;

    public void stop() {
        stop = true;
    }

    public void execute(AsyncTask task) {
        tasks.add(task);
    }

    @Override
    public void run() {
        while (!stop) {
            if (tasks.size() != 0) {

                final AsyncTask task = tasks.remove();
                activity.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        task.execute();
                    }
                });

            }
        }
    }
}

and your code would be:

ThreadPool pool=new ThreadPool(this);
pool.start();    
.. some code
pool.execute(new task());
.. other code
pool.execute(new task());
.. other code
pool.execute(new task());
vipul mittal
  • 17,343
  • 3
  • 41
  • 44
0

Making AsyncTask object each time will cost you more memory and performance because you each time need to create new object and use it for one time and creating new one and so on , there is way help you in schedule your tasks using Handler and here example about how to you can implement it

http://binarybuffer.com/2012/07/executing-scheduled-periodic-tasks-in-android

mohammed momn
  • 3,230
  • 1
  • 20
  • 16