1

I have two asyctask that are called at the same time, but I need both two asynctask results to process the next step.

I have one solution: Given two member variable to check the return state of two async task:

boolean b1 = false;
boolean b2= false;
Result r1 = null;
Result r2 = null;

callback1(
   done(Result r){
        b1 = true;
        r1 = r;
        asyncTwoFunction(b1,b2)
}
)

callback2(
   done(Result r){
        b2 = true;
        r2 = r;
        asyncTwoFunction(b1,b2)
}
)

asyncTwoFunction(b1,b2){
    if(b1 && b2){
         doSomeThing(r1,r2);
         b1 = false;
         b2 = false;
    }
}

Are there some better way to do this? Thanks

Shi
  • 510
  • 1
  • 5
  • 16
  • asyncTwoFunction, is that supposed to run asynchronously or is that meant to run in the Main thread? If main thread then you got the proper solution. – zapl Sep 19 '15 at 17:51
  • You can use [CountDownLatch](http://developer.android.com/reference/java/util/concurrent/CountDownLatch.html) in case you have possibility to await in concurrent thread. But it don't give you a callback. – Stepan Tsymbal Sep 19 '15 at 18:00
  • To use callbacks you can use Futures and [add your own interface](http://stackoverflow.com/questions/826212/java-executors-how-to-be-notified-without-blocking-when-a-task-completes) to get a callback. But that is too much for only two tasks in one place. So your solutions looks quite reasonable. – Stepan Tsymbal Sep 19 '15 at 18:05
  • @zapl both tasks are run in background. Is my solution correct in this case? – Shi Sep 19 '15 at 18:19
  • @StepanTsymbal I am looking for a simple solution. I thought of FutureTask or EventBus. But They are too complicate only for this simple purpose. The only thing I don't like is to use member variable. – Shi Sep 19 '15 at 18:22
  • @watchforever It is a part of java.util package and works pretty straight-forward. FutureTask is more flexible, but a bit more complicated. So you have to choose what suit you best. I think you don't have to worry about that too much if that case appears only once in your application. – Stepan Tsymbal Sep 19 '15 at 18:38

2 Answers2

2

OnPostexecute of AsyncTask will be called on main thread once AsyncTask finishes its job. Since the final callback will be on main thread it is easy to monitor the state of each asynctask by calling get status on each asynctask. get status()

siva
  • 1,850
  • 13
  • 14
0

You can run two AsyncTasks and coordinate them, but the thread executor works in serial mode by default and will run them one after the other anyway. So why write code that jumps through hoops to sync the tasks and cause more bugs?

I would just have a single AsyncTask that does the work of both tasks in one background operation.

kris larson
  • 30,387
  • 5
  • 62
  • 74
  • For example, I am using parse as the backend, The first asynctask to get a list of all users; the second asynctask to get a list of all my friends. Then put all users in a list and mark the friends. For the performance, I would like to do these two asynctask toghether. Then show them in the Activity. – Shi Sep 19 '15 at 18:28
  • So why not a single AsyncTask that gets both a list of users and a list of friends? Unless you start messing with ThreadPoolExecutor, running two AsyncTasks isn't going to be any faster than a single AsyncTask. – kris larson Sep 19 '15 at 18:39
  • 1
    I really doubt that two different asynctask runs in serial manner. – siva Sep 19 '15 at 18:49
  • "get a list of all users" and "get one user's friend list" are two api calls (They are both designed to be Async calls). How can I do them in a single AsyncTask? The following is the function to get the user's friend: mFriendsRelation.getQuery().findInBackground(new FindCallback() { @Override public void done(List friends, ParseException e) { } }); – Shi Sep 19 '15 at 18:57