2

Let's say we have two HTTP Async Calls, Task 1 and Task 2. And I want to execute both of them in the same time. and once both are finished, I want to trigger a function.

enter image description here

Since we don't know which task will be finished first, how can we know that the tasks are finished and trigger the function?

  • as a side note, currently asynctasks are run on a single threaded executor, so by default they will not run in parallel. You'll have to use another executor to allow them to run simultaneously. – njzk2 Nov 14 '14 at 19:32

5 Answers5

1

If you are doing it in"parallel" the only way is setting "done" flags for each task and then at the end of each task check whether if all the other flags are "done", that way you would know that you're ready to move on;

However, this approach is a little messy and leads to spaghetti code, so, if you really don't need a "parallel" approach to your solution, you can always create an "async queue", which will execute every task one at the time asynchronously and you can keep track of the size of your queue and hence notice when it has reached the end of the pipe.

Hope it Helps!

Regards!

Martin Cazares
  • 13,637
  • 10
  • 47
  • 54
1

Create a field to keep track of how many tasks have been completed, then when each task finishes increment the value and check if the number completed matches the number required:

private int mTasksComplete = 0;
private int mTasksRequired = 2;

public void completeTask(){
    ++mTasksComplete;
    if(mTasksComplete == mTasksRequired){
        //do something
    }
}
JstnPwll
  • 8,585
  • 2
  • 33
  • 56
1

Step-1 : Create interface TaskComplete ,

public interface TaskComplete {
    public void TaskDone(String asyncClassName);
}

Step-2 : Create object of it in class where you are calling async task.

        TaskComplete taskComplete = new TaskComplete() {

        boolean firstDone = false;
        boolean secondDone = false;

        @Override
        public void TaskDone(String asyncClassName) {
            // TODO Auto-generated method stub

            if(asyncClassName.equals("FIRST_ASYNC")) {
                firstDone = true;
            } else if(asyncClassName.equals("SECOND_ASYNC")) {
                secondDone = true;
            }

            if(firstDone == true && secondDone == true) {
                // Both async completed - do your work
            }
        }
    };

Step-3 : Call these from respective onPostExecute method of async class.

    taskComplete.TaskDone("FIRST_ASYNC");
    taskComplete.TaskDone("SECOND_ASYNC");
Chitrang
  • 5,097
  • 1
  • 35
  • 58
0

you can run a function to change a flag (lets say boolean) from false to true and check both of them before run the third function.
example

bool task1Fineshed = false;
void setTask1Finished(){
    task1Fineshed=true;
    function();
}
bool task2Fineshed = false;
void setTask2Finished(){
    task2Fineshed=true;
    function();
}
void function(){
    if(task1Fineshed && task2Finished){
         //do something
    }
}
gmetax
  • 3,853
  • 2
  • 31
  • 45
0

How you can control your Trigger: you can develop an interface and implement it as your AsyncTask, on this interface you can define a callback, which will be called in onPostExecute Method of your AsyncTask as here.

How you can execute 2 or more AsyncTasks in order to execute 2 or more AsyncTask's you can take a look at these links link1 Link2

Kind Regards

Community
  • 1
  • 1
Gödel77
  • 839
  • 3
  • 15
  • 27