What is the best way to wait for multiple asynchronous callback functions to finish in Java before continuing. Specifically I\'m using GWT with AsyncCallback, but I think this i
Ideally, you want to do as other posters have stated and do as much as you can in a single async call. Sometimes you have to do a bunch of separate calls. Here's how:
You want to chain the async calls. When the last async completes (login), all the items are loaded.
final AsyncCallback<LoginInfo> loginCallback = new AsyncCallback<LoginInfo>() {
public void onSuccess(LoginInfo result) {
//Everything loaded
doSomethingNow();
}
};
final Runnable searchRunnable = new Runnable() {
public void run() {
loginService.login(GWT.getHostPageBaseURL(), loginCallback);
}
};
final Runnable booksRunnable = new Runnable() {
public void run() {
AjaxLoader.loadApi("search", "1", searchRunnable, null);
}
};
//Kick off the chain of events
AjaxLoader.loadApi("books", "0", booksRunnable, null);
Cheers,
--Russ
Just tossing up some ideas:
The callbacks fire some GwtEvent using the HandlerManager. The class containing the ready methods is registered with the HandlerManager as an EventHandler for the events fired by the callback methods, and holds the state (bookAPIAvailable, searchAPIAvailable, appLoaded).
When a event arrives that specific state is changed, and we check if all the states are as desired.
For an example using the GWTEvent, HandlerManager and EventHandler, see http://www.webspin.be/?p=5