0

I have a background thread which calls 3 asynctasks to perform tasks simultaneously. The calling thread acts as a Queue for 3 sets of these tasks.

So basically I need to call 3 asynctasks simultaneously and once they are completed I want to call the next three tasks on the queue and repeat.

However I am having trouble pausing the caller thread until the three asynctask finishes. As a result the next three tasks in the queue start running before the previous three tasks are completed.

So is there anyway to hold the caller thread until the asynctasks are completed. I know that you can user .get() in asynctask but it will not enable the three asynctasks to run simultaneously.

  • Maybe that could be done with pass a listener to your 3 asyntask and call method of your listener in the onPostExecute of your 3 asynctask, when the 3 asynctask had call this method you can launch your 3 next asynctask. And i think you should used a Service and not a thread for launch your asynctask. Hope that help. – Illuyankas Aug 03 '16 at 11:55
  • I am using the looper to queue tasks and with that I couldn't find a way for it to pause calling the next task. – Sankha Palihawadana Aug 03 '16 at 11:56
  • If i remenber well, Looper is not made for be pause. If i had to do something like that i pass by Service that get list of your task (that can be dynamicly set) and that launch 3tasks in same times then when they are finish relaunch the startComand while you had task in queue or something like that. – Illuyankas Aug 03 '16 at 12:17

2 Answers2

1

Following code is rather a pseudocode of the idea. Basically, you'll declare an interface which will check for firing next three AsyncTasks. You'll also need to maintain a counter to see if the number of received response from AsyncTask is multiplied by 3. If it is then you can trigger next three AsyncTasks.

public interface OnRunNextThree{
     void runNextThreeTasks();
}

public class MainClass extends Activity implements OnRunNextThree {

    private int asyncTasksCounter = 0;

    public void onCreate() {
        //Initiate and run first three of your DownloadFilesTask AsyncTasks

        // ...
    }

    public void runNextThreeTasks() {
        if (asyncTasksCounter % 3 == 0) {
            // you can execute next three of your DownloadFilesTask AsyncTasks now

            // ...
        } else {
            // Otherwise, since we have got response from one of our previously 
            // initiated AsyncTasks so let's update the counter value by one. 
            asyncTasksCounter++;
        }
    }

    private class DownloadFilesTask extends AsyncTask<Void, Void, Void> {

        private OnRunNextThree onRunNextThree;

        public DownloadFilesTask(OnRunNextThree onRunNextThree) {
            this.onRunNextThree = onRunNextThree;
        }


        protected Void doInBackground(Void... voids) {
            // Do whatever you need to do in background
            return null;
        }

        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            //Got the result. Great! Now trigger the interface.
            this.onRunNextThree.runNextThreeTasks();
        }
    }

}
fluffyBatman
  • 6,524
  • 3
  • 24
  • 25
0

Async tasks are meant to do things asynchronously.... so this can't be done in a straight forward way...

Even if you manage to do this, it basically defeats the whole point of asynchronous operation.

You should look for a Synchronous network operation.

Check out Volley... It is a google library specially made for network operations and it supports Synchronous operations

http://www.truiton.com/2015/02/android-volley-making-synchronous-request/

There are many other libraries available ... Retrofit is one other good library..

Kushan
  • 5,855
  • 3
  • 31
  • 45